Transmit Temperature with Raspberry Pi - LEKULE

Breaking

21 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.

No comments: