Transmit Temperature with Raspberry Pi - LEKULE

Breaking

19 Aug 2015

Transmit Temperature with Raspberry Pi

Requirements
  • Raspberry Pi
    • Used in article: Model B Revision 1.0 with Raspbian (Debian GNU/Linux 7.6 (wheezy))
  • I2C temperature sensor
  • Method to connect Raspberry Pi to internet
    • Use in article: Raspberry Pi wired directly to router
  • Google account to create and access sheets

Setting up the I2C

Installing drivers

  1. Open a terminal on the Pi or use SSH
  2. Type the following on the terminal to install support tools:
    sudo apt-get install python-smbus
    sudo apt-get install i2c-tools
  3. Use raspi-config to enable the i2c drivers:
    sudo raspi-config
    1. Select "Advanced Options"
    2. Select "I2C"
    3. Select "Yes"
    4. Select "Yes"
  4. Reboot the Pi

Connecting the wires

Raspberry Pi TMP102 Board
3v3 Power VCC
Ground GND
SDA SDA
SCL SCL
Ground ADD0

Testing the connection

  1. Open a terminal on the Pi or use SSH
  2. Type the following:
    sudo i2cdetect -y 0
    1. ​Note, if you get an error message "Error: Could not open file `/dev/i2c-0' or `/dev/i2c/0': No such file or directory", follow these steps:
      1. Add the following lines to /etc/modules:
        i2c_bcm2708
        i2c_dev
      2. Reboot the pi
      3. Alternatively load the modules at runtime by typing the following:
        sudo modprobe i2c_bcm2708
        sudo modprobe i2c_dev
  3. ​​You should see the temp sensor at address 0x48.
  4. Run the following python script and verify the temperature reported is the same as in the room using the command:
    sudo python TMP102_read_temp.py
    Hint: Use SFTP or a USB flash drive to copy the script to a folder on the pi

                    #!/usr/bin/python
import smbus
#0 = /dev/i2c-0
#1 = /dev/i2c-1
I2C_BUS = 0
bus = smbus.SMBus(I2C_BUS)
    
#7 bit address (will be left shifted to add the read write bit)
DEVICE_ADDRESS = 0x48      

#Read the temp register
temp_reg_12bit = bus.read_word_data(DEVICE_ADDRESS , 0 )
temp_low = (temp_reg_12bit & 0xff00) >> 8
temp_high = (temp_reg_12bit & 0x00ff)
#convert to temp from page 6 of datasheet
temp  = ((( temp_high * 256 ) + temp_low) >> 4 )
#handle negative temps
if temp > 0x7FF:
 temp = temp-4096;
temp_C = float(temp) * 0.0625
temp_F = temp_C * 9/5+32
print "Temp = %3.1f C -- %3.1f F" % (temp_C,temp_F)
                  

 

Saving data to Google

Create a sheet

  1. If you don't have a Google account, create one.
  2. Create a new sheet and create a header in column A for date and column B for temp.
  3. Delete all the extra rows except for the header. The Pi will be appending new rows to the sheet, so you don't want the data to be far down the sheet.
    Example used in this demo: https://docs.google.com/spreadsheets/d/1DRIfCrX7HUyIeMmd2c0A6k1pNcZ7zQXFaNhOE0Rc3PM/edit?usp=sharing

Set up authentication

  1. Obtain OAuth2 credentials by following the steps outlined by Google below. Follow steps 1-4, you only need the json file.
  2. Search the json file for "client_id". Save the text following "client_id", it will be used in the python script.
    "client_email": "284377770079-0o2pssk1b0qjjddi6rvag4h7i7rsl1on@developer.gserviceaccount.com",
    *note: the text in the file you download will be different
  3. Copy the json file to a folder on the Raspberry Pi.
  4. In the sheet created in the previous section, click File>Share. Select "Can Edit" and paste the "client-email" above into the email line.
  5. On the Pi, install the necessary software by typing the following on the command line:
    sudo apt-get install python-pip
    sudo pip install gspread oauth2client
    sudo apt-get install python-openssl

Testing the connection

  1. Run the following script on the Pi from the same directory as the json file from the earlier step. The script uses the time from the Pi for the first column, so make sure the time is setup correctly using raspi-config.
    sudo python TMP102_google_sheet.py

                    import sys
import time
import datetime
import gspread
import oauth2client.client
import json
import smbus

#Change the following settings based on your setup
#0 = /dev/i2c-0
#1 = /dev/i2c-1
I2C_BUS = 0
DEVICE_ADDRESS = 0x48    

#json filename for credentials
JSON_FILENAME       = 'Temp Logger-68e32d47588c.json'

# Google sheet to save to
GSHEET_NAME = 'temp_logging_demo'

"""
Write TMP102 data to google sheets
"""
#load credentials from json and open the spreadsheet for writing
json_key = json.load(open(JSON_FILENAME))
creds = oauth2client.client.SignedJwtAssertionCredentials(json_key['client_email'], 
  json_key['private_key'],
  ['https://spreadsheets.google.com/feeds'])
client_inst = gspread.authorize(creds)
gsheet = client_inst.open(GSHEET_NAME).sheet1

#initialize the i2c bus
bus = smbus.SMBus(I2C_BUS)

#Read the temp register
temp_reg_12bit = bus.read_word_data(DEVICE_ADDRESS , 0 )
temp_low = (temp_reg_12bit & 0xff00) >> 8
temp_high = (temp_reg_12bit & 0x00ff)
#convert to temp from page 6 of datasheet
temp  = ((( temp_high * 256 ) + temp_low) >> 4 )
#handle negative temps
if temp > 0x7FF:
 temp = temp-4096;
temp_C = float(temp) * 0.0625
temp_F = temp_C * 9/5+32

curr_time = datetime.datetime.now()
print "Writing new row to %s: %s - %3.1f"  % (GSHEET_NAME,curr_time,temp_F)

#write a new row to the spreadsheet with the current time and temperature
gsheet.append_row((curr_time, temp_F))
                  

 

Automatically run

  1. You could modify the script to loop, or you could use a cron job so the I2C bus is released when not in use. Here is how to setup a cron job.
  2. Add how often you want the script to run using crontab:
    crontab -e
  3. Add the following line to run every 10mn:
    */10 * * * * cd /path/to/script && python /path/to/script >> /path/to/log 2>&1

​Graphing on a webpage

  1. Get a share link from the google sheets by clicking File>Share>Get Shareable Link
  2. Download the .zip file below
  3. Paste the link into the code below where "https://docs.google.com/spreadsheets/d/1DRIfCrX7HUyIeMmd2c0A6k1pNcZ7zQXFaNhOE0Rc3PM" is.
  4. Open the webpage, and it should look like the following. You could also paste the javascript into an existing webpage.

 


Raspberry Pi Temp Example12:00 AM4:00 AM8:00 AM12:00 PM4:00 PM8:00 PM7778798081Date and TimeTemp (F)
DateTemp
7/30/2015 22:00:0579.5875
7/30/2015 22:10:0480.15
7/30/2015 22:20:0480.2625
7/30/2015 22:30:0479.25
7/30/2015 22:40:0579.475
7/30/2015 22:50:0478.8
7/30/2015 23:00:0479.3625
7/30/2015 23:10:0579.25
7/30/2015 23:20:0579.1375
7/30/2015 23:30:0479.475
7/30/2015 23:40:0478.575
7/30/2015 23:50:0579.1375
7/31/2015 0:00:0579.475
7/31/2015 0:10:0478.8
7/31/2015 0:20:0579.3625
7/31/2015 0:30:0579.475
7/31/2015 0:40:0578.9125
7/31/2015 0:50:0579.25
7/31/2015 1:00:0578.8
7/31/2015 1:10:0579.025
7/31/2015 1:20:0579.3625
7/31/2015 1:30:0478.35
7/31/2015 1:40:0479.025
7/31/2015 1:50:0479.1375
7/31/2015 2:00:0478.2375
7/31/2015 2:10:0479.025
7/31/2015 2:20:0479.25
7/31/2015 2:30:0478.2375
7/31/2015 2:40:0579.025
7/31/2015 2:50:0579.1375
7/31/2015 3:00:0578.2375
7/31/2015 3:10:0578.9125
7/31/2015 3:20:0479.1375
7/31/2015 3:30:0478.575
7/31/2015 3:40:0478.6875
7/31/2015 3:50:0578.9125
7/31/2015 4:00:0479.1375
7/31/2015 4:10:0478.35
7/31/2015 4:20:0578.9125
7/31/2015 4:30:0579.025
7/31/2015 4:40:0578.8
7/31/2015 4:50:0578.575
7/31/2015 5:00:0579.025
7/31/2015 5:10:0579.025
7/31/2015 5:20:0578.2375
7/31/2015 5:30:0478.8
7/31/2015 5:40:0478.9125
7/31/2015 5:50:0479.1375
7/31/2015 6:00:0477.9
7/31/2015 6:10:0578.8
7/31/2015 6:20:0579.025
7/31/2015 6:30:0579.25
7/31/2015 6:40:0578.4625
7/31/2015 6:50:0579.25
7/31/2015 7:00:1079.25
7/31/2015 7:10:1678.6875
7/31/2015 7:20:0579.25
7/31/2015 7:40:2078.9125
7/31/2015 7:50:0679.1375
7/31/2015 8:00:0578.2375
7/31/2015 8:10:1578.9125
7/31/2015 8:20:0579.1375
7/31/2015 8:40:0579.025
7/31/2015 8:50:0578.575
7/31/2015 9:00:1078.6875
7/31/2015 9:10:1078.9125
7/31/2015 9:20:1078.125
7/31/2015 9:40:0479.25
7/31/2015 9:50:0578.35
7/31/2015 10:10:0579.025
7/31/2015 10:30:0579.025
7/31/2015 10:40:0578.6875
7/31/2015 10:50:0579.025
7/31/2015 11:00:0579.3625
7/31/2015 11:10:0478.575
7/31/2015 11:20:0579.1375
7/31/2015 11:30:0478.9125
7/31/2015 11:40:0979.025
7/31/2015 11:50:0579.25
7/31/2015 12:00:0578.8
7/31/2015 12:10:0579.1375
7/31/2015 12:20:0578.4625
7/31/2015 12:30:0579.3625
7/31/2015 12:40:0578.6875
7/31/2015 12:50:0479.3625
7/31/2015 13:00:0479.025
7/31/2015 13:10:0479.3625
7/31/2015 13:20:0578.9125
7/31/2015 13:30:0579.3625
7/31/2015 13:40:0579.025
7/31/2015 13:50:0579.3625
7/31/2015 14:00:0578.575
7/31/2015 14:10:0479.475
7/31/2015 14:20:0578.9125
7/31/2015 14:30:0579.1375
7/31/2015 14:40:0579.25
7/31/2015 14:50:0578.575
7/31/2015 15:00:0579.3625
7/31/2015 15:10:0578.575
7/31/2015 15:20:0579.25
7/31/2015 15:30:0579.1375
7/31/2015 15:40:0578.6875
7/31/2015 15:50:0579.3625
7/31/2015 16:00:0578.9125
7/31/2015 16:10:0579.3625
7/31/2015 16:20:0579.1375
7/31/2015 16:30:0579.1375
7/31/2015 16:40:0579.25
7/31/2015 16:50:0479.025
7/31/2015 17:00:0479.3625
7/31/2015 17:10:0479.025
7/31/2015 17:20:0479.3625
7/31/2015 17:30:0578.9125
7/31/2015 17:40:0579.3625
7/31/2015 17:50:0578.6875
7/31/2015 18:00:0579.3625
7/31/2015 18:10:0578.9125
7/31/2015 18:20:0479.3625
7/31/2015 18:30:0479.3625
7/31/2015 18:40:0479.3625
7/31/2015 18:50:0479.475
7/31/2015 19:00:0579.25
7/31/2015 19:10:0579.475
7/31/2015 19:20:0579.25
7/31/2015 19:30:0479.475
7/31/2015 19:40:0479.25
7/31/2015 19:50:0479.475
7/31/2015 20:00:0479.1375
7/31/2015 20:10:0479.475
7/31/2015 20:20:0578.9125
7/31/2015 20:30:0579.475
7/31/2015 20:40:0579.1375
7/31/2015 20:50:0579.475
7/31/2015 21:00:0578.9125
7/31/2015 21:10:0579.475
7/31/2015 21:20:0578.575
7/31/2015 21:30:0579.475
7/31/2015 21:40:0578.9125
7/31/2015 21:50:0579.3625
7/31/2015 22:00:0579.475
7/31/2015 22:10:0479.25
7/31/2015 22:20:0479.475
7/31/2015 22:30:0478.575

No comments: