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
- Open a terminal on the Pi or use SSH
- Type the following on the terminal to install support tools:
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
- Use raspi-config to enable the i2c drivers:
sudo raspi-config
- Select "Advanced Options"
- Select "I2C"
- Select "Yes"
- Select "Yes"
- Reboot the Pi
Connecting the wires
Raspberry Pi |
TMP102 Board |
3v3 Power |
VCC |
Ground |
GND |
SDA |
SDA |
SCL |
SCL |
Ground |
ADD0 |
Testing the connection
- Open a terminal on the Pi or use SSH
- Type the following:
sudo i2cdetect -y 0
- 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:
- Add the following lines to /etc/modules:
i2c_bcm2708
i2c_dev
- Reboot the pi
- Alternatively load the modules at runtime by typing the following:
sudo modprobe i2c_bcm2708
sudo modprobe i2c_dev
- You should see the temp sensor at address 0x48.
- 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
import smbus
I2C_BUS = 0
bus = smbus.SMBus(I2C_BUS)
DEVICE_ADDRESS = 0x48
temp_reg_12bit = bus.read_word_data(DEVICE_ADDRESS , 0 )
temp_low = (temp_reg_12bit & 0xff00) >> 8
temp_high = (temp_reg_12bit & 0x00ff)
temp = ((( temp_high * 256 ) + temp_low) >> 4 )
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
- If you don't have a Google account, create one.
- Create a new sheet and create a header in column A for date and column B for temp.
- 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
- Obtain OAuth2 credentials by following the steps outlined by Google below. Follow steps 1-4, you only need the json file.
- 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
- Copy the json file to a folder on the Raspberry Pi.
- In the sheet created in the previous section, click File>Share.
Select "Can Edit" and paste the "client-email" above into the email
line.
- 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
- 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
I2C_BUS = 0
DEVICE_ADDRESS = 0x48
JSON_FILENAME = 'Temp Logger-68e32d47588c.json'
GSHEET_NAME = 'temp_logging_demo'
"""
Write TMP102 data to google sheets
"""
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
bus = smbus.SMBus(I2C_BUS)
temp_reg_12bit = bus.read_word_data(DEVICE_ADDRESS , 0 )
temp_low = (temp_reg_12bit & 0xff00) >> 8
temp_high = (temp_reg_12bit & 0x00ff)
temp = ((( temp_high * 256 ) + temp_low) >> 4 )
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)
gsheet.append_row((curr_time, temp_F))
Automatically run
- 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.
- Add how often you want the script to run using crontab:
crontab -e
- 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
- Get a share link from the google sheets by clicking File>Share>Get Shareable Link
- Download the .zip file below
- Paste the link into the code below where "https://docs.google.com/spreadsheets/d/1DRIfCrX7HUyIeMmd2c0A6k1pNcZ7zQXFaNhOE0Rc3PM" is.
- Open the webpage, and it should look like the following. You could also paste the javascript into an existing webpage.
No comments:
Post a Comment