Friday, 30 August 2013

Making a raspberry pi temperature sensor (Step 9 - Your product is finished)

You have now succesfully completed making a temperature sensor with your raspberry pi.
If you wish to turn the raspberry pi off and restart it without connecting to a monitor, you must plug in a keyboard to the raspberry pi and plug the pi back into the socket. Then you wait 2 minutes before pressing Alt+F4 and the F5. This is assuming you have followed all of the previous steps.

Enjoy your temperature sensor.

Making a raspberry temperature sensor (step 8- lights and e-mail)

On sen.se make 2 more feeds. Set these up so that they e-mail you whenever data is recieved, this is quite easy on sen.se. Put led's into the gpio's 23, 24 and 25 preferably red, yellow and green (we are making a traffic light system). Then edit your code from the previous step and put in the new feed id's into feed_id 2 and 3.

import httplib
import json as simplejson
from random import randint
import time
import os
import glob
import wiringpi2 as wiringpi

wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(23, 1) #red
wiringpi.pinMode(24, 1) #yellow
wiringpi.pinMode(25, 1) #green
wiringpi.digitalWrite(23, 0)
wiringpi.digitalWrite(24, 0)
wiringpi.digitalWrite(25, 0)

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

run_number = 0

SENSE_API_KEY =
FEED_ID1 = #actual graph
FEED_ID2 = #min
FEED_ID3 = #max

def read_temp_raw():
     f = open(device_file, 'r')
     lines = f.readlines()
     f.close()
     return lines

def read_temp(): 
      lines = read_temp_raw()
      while lines[0].strip()[-3:]!= 'YES':
           time.sleep(1)
           lines = read_temp_raw()
       equals_pos = lines[1].find('t=')
        if equals_pos != -1:
              temp_string = lines[1][equals_pos+2:]
              tempC = float(temp_string)/1000.0
              return tempC

def send_to_opensense(data):
     try:
         datalist = [{"feed_id" :FEED_ID1, "value" :data['C']},]
         headers = {"sense_key": SENSE_API_KEY,"content-type":"appliaction/json"}
         conn = httplib.HTTPConnection("api.sen.se")
         conn.request("POST", "/events/", simplejson.dumps(datalist),headers)
         response = conn.getresponse()
         conn.close()
     except:
         pass

ef send_to_opensense2(data): #do not edit please
     try:
           datalist = [{"feed_id" :FEED_ID2, "value" :data['C']},]
           headers = {"sense_key": SENSE_API_KEY,"content-type":"appliaction/json"}
           conn = httplib.HTTPConnection("api.sen.se")
           conn.request("POST", "/events/", simplejson.dumps(datalist),headers)
           response = conn.getresponse()
           conn.close()
     except:
           pass

def send_to_opensense3(data):
      try:
            datalist = [{"feed_id" :FEED_ID3, "value" :data['C']},]
             headers = {"sense_key": SENSE_API_KEY,"content-type":"appliaction/json"}
             conn = httplib.HTTPConnection("api.sen.se")
             conn.request("POST", "/events/", simplejson.dumps(datalist),headers)
             response = conn.getresponse()
             conn.close()
      except:
             pass

while(True):
       try:
            run_number = run_number + 1
            tempC = read_temp()
            print "RasPI(2) Ambient Run:",run_number," tempC:",tempC
            data = {'C' : tempC}
            send_to_opensense(data)
        if 26.5 <= tempC and 29.5 >= tempC: # good range of 26.5-29.5 change for if range wants to change
               wiringpi.digitalWrite(25, 1)
               wiringpi.digitalWrite(24, 0)
               wiringpi.digitalWrite(23, 0)
        if 26 <= tempC and 26.5 > tempC: # okay range of 26 - 26.5 change if range wants to change
               wiringpi.digitalWrite(25, 0)
               wiringpi.digitalWrite(24, 1)
               wiringpi.digitalWrite(23, 0)
        if 26 > tempC: # bad range less than 26 change if range needs to change
               wiringpi.digitalWrite(25, 0)
               wiringpi.digitalWrite(24, 0)
               wiringpi.digitalWrite(23, 1)
               send_to_opensense2(data)
         if 29.5 < tempC and 30 >= tempC: # okay range of 29.5 - 30 change if range wants to change
               wiringpi.digitalWrite(25, 0)
               wiringpi.digitalWrite(24, 1)
               wiringpi.digitalWrite(23, 0)
         if 30 < tempC: # bad range more than 30 change if range needs to change
               wiringpi.digitalWrite(25, 0)
               wiringpi.digitalWrite(24, 0)
               wiringpi.digitalWrite(23, 1)
               send_to_opensense3(data) #
               time.sleep(10) #frequency of reading in seconds, change if required
# if ranges are changed, please make sure that ranges are continuous and do not edit the inequality signs
        except:
               pass

Getting the Pi to Auto-Start (Step 7)

To get the Pi to go straight to the desktop on start-up:
  • In the LX terminal type sudo raspi-config and a menu should appear where you can select to change the configuration for startup, so that when you start the pi it will skip the login step and the startx step and go straight to your desktop.
To get a python programme to run automatically:
  • In the /home/pi/.config directory create a folder called 'autostart'.
  • In Leafpad create a file called 'mypythonprogram.desktop' and write the following code and save it to the directory /home/pi/desktop/ :
                        [Desktop Entry]
                        Encoding=UTF-8
                        Type=Application
                        Name="The name of your program"
                        Comment=
                        Exec=sudo python "The place where the program is saved for
                                                         instance /home/pi/mypython.py"
                        StartupNotify=false
                        Terminal=false
                        Hidden=false
  • Then cut and paste the file into the autostart folder you created.
  • Test it to see if it works by typing sudo reboot into the LX terminal. 
  •  
     

Making a raspberry pi temperature sensor (step 6- coding the sensor)

Here is the sensor code. All you have to do is enter the API key and the feed ID. You will have to type this all out though it will not run if copied into python

import httplib
import json as simplejson
from random import randint
import time
import os
import glob

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

run_number = 0

SENSE_API_KEY =  "your API key here" #note quotation marks
FEED_ID1 =  your feed ID here #note no quotation marks

def read_temp_raw():
     f = open(device_file, 'r')
     lines = f.readlines()
     f.close()
      return lines

def read_temp(): 
         lines = read_temp_raw()
        while lines[0].strip()[-3:]!= 'YES':
        time.sleep(1)
        lines = read_temp_raw()
         equals_pos = lines[1].find('t=')
        if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        tempC = float(temp_string)/1000.0
        return tempC

def send_to_opensense(data):
    try:
          datalist = [{"feed_id" :FEED_ID1, "value" :data['C']},]
          headers = {"sense_key": SENSE_API_KEY,"content-type":"appliaction/json"}
          conn = httplib.HTTPConnection("api.sen.se")
          conn.request("POST", "/events/", simplejson.dumps(datalist),headers)
          response = conn.getresponse()
         conn.close()
    except:
        pass


while(True):
      try:
           run_number = run_number + 1
           tempC = read_temp()
           print "RasPI(2) Ambient Run:",run_number," tempC:",tempC
           data = {'C' : tempC}
          send_to_opensense(data)
          time.sleep(10)
     except:
          pass

{code mostly from whiskeytangohotel}

Making a raspberrry pi temperature sensor (step 5- Set up the DS18b20)

This is probably one of the most difficult steps in the whole process.

This adafruit guide is probably the best tutorial for setting up the sensor.
http://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing/overview

The guide uses some parts that aren't necessary like the pi cobbler but life is easier if you have them.

Our ds18b20 came from amazon and had different wire colours to a normal ds18b20 sensor.
It came with red, white and a shielded wire these are:
red - VCC
white - Data
Shielded - Ground

Make sure the 3rd pin on the 5V side is used for ground, the 3.3V pin is used for power and the 4th pi on the 3.3V side is used for the data.

Making a raspberry pi temperature sensor (step 4- sen.se)

You need somewhere for the data your pi is going to collect to go. You can obviously just display it on a screen but so long as you have an internet connection it is easier to display it on the web.

Although there are lots of different places you could post your data to, sen.se is probably the best. It is specifically designed for arduino and raspberry pi users and so it's very handy. There are loads of different things you can do with displays and feeds and so you can set it up however you want.

All you have to do is go on the website and apply for an invitation the link is below:

http://open.sen.se/

Flashing lights


import wiringpi2 as wiringpi
from time import sleep
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(17, 1)
wiringpi.pinMode(27, 1)
wiringpi.pinMode(22, 1)
wiringpi.pinMode(24, 1)
wiringpi.pinMode(25, 1)
wiringpi.digitalWrite(17, 0) #yellow
wiringpi.digitalWrite(27, 0) #green
wiringpi.digitalWrite(22, 0) #bottum red
wiringpi.digitalWrite(24, 0) #mid red
wiringpi.digitalWrite(25, 0) #top red
sleep(3)
while 2 == 2:
    wiringpi.digitalWrite(22, 1)
    wiringpi.digitalWrite(24, 1)
    wiringpi.digitalWrite(25, 1)
    wiringpi.digitalWrite(17, 0)
    wiringpi.digitalWrite(27, 1)
    sleep(2)
    wiringpi.digitalWrite(27, 0)
    wiringpi.digitalWrite(17, 1)
    sleep(2)
    wiringpi.digitalWrite(22, 0)
    wiringpi.digitalWrite(24, 0)
    wiringpi.digitalWrite(25, 0)
    wiringpi.digitalWrite(17, 0)
    wiringpi.digitalWrite(27, 1)
    sleep(2)
    wiringpi.digitalWrite(27, 0)
    wiringpi.digitalWrite(17, 1)
    sleep(2)