Posts Tagged ‘Raspberry Pi’

The more I work with ThingSpeak the more impressed I am.  Just a quick post here to talk about the graphing function and how to tailor it to best represent your data.  I have three feeds going to ThingSpeak – temperature, humidity, and atmospheric pressure.  The public view of these feeds can be found here.  

Each of my feeds updates to ThingSpeak every 5 minutes but if you notice the graphs show only one data point per hour.  Clicking on the little pencil in the upper right corner of each graph brings up a menu that lets you tailor exactly how your data is graphed.  Since I upload data every 5 minutes, or 12 times an hour, or 288 times over 24 hours – I’ve chosen to have 288 results graphed.  A really cool feature is the ability to average the data over a specific duration – I chose 60 minutes.  So my graphs display average hourly results over the course of 24 hours.  

Image

 

You can separately set these chart options for the publicly viewable charts and the private ones displayed when you’re logged into the site.  

The more that I work with ThingSpeak the more I like what I find.

The new Xively site left me a little disappointed.  Realtime graphing took a step back in my opinion and it seems like the whole site is now geared less toward the individual…  No worries though, I’ve moved on to ThingSpeak which is an interesting alternative.  

To start using ThingSpeak signup for a new account on https://www.thingspeak.com.  The first thing you’ll want to do is create a “New Channel”.  Give it a nice descriptive title and add fields for each of the streams of data you want to send.  I have 3 streams, temperature, humidity, and pressure.  Over on the “API Keys” tab copy the “Write API Key” – you’ll need to add this to the python script later.

There’s a great example script here that I used to get started pushing data to ThingSpeak.  It’s a fairly straightforward process that seemed much easier to get going than I expected.  In any case, hope my python script below is of some use to others.  

Here’s the new python script I’m using to send temperature, humidity, and atmospheric pressure to ThingSpeak…

#!/usr/bin/python

import os
import re
import subprocess
import time
import datetime
import httplib
import urllib
import sys

sys.path.append(‘/home/pi/python/Adafruit-Raspberry-Pi-Python-Code/Adafruit_BMP085′)

from Adafruit_BMP085 import BMP085

 

API_KEY=”XXXXXXXXXXXXXX”
DEBUG=True

# function to read temperature and pressure from bmp085
def read_bmp085():
if DEBUG:
print “reading bmp085 temperature and pressure”

bmp = BMP085(0x77)
temp = bmp.readTemperature()
pressure = bmp.readPressure()

temp = round(((9.0/5.0)*temp + 32), 2)
pressure = (pressure / 100)*0.0295301
pressure = round(pressure, 2)

if DEBUG:
print “bmp085 Temperature: %.2f F” % temp
print “Pressure: %.2f in.” % pressure

return {‘temp':temp, ‘pressure':pressure}

def read_dht():
if DEBUG:
print “reading dht temperature and humidity”

while True:
output = subprocess.check_output(["./Adafruit_DHT", "2302", "4"]);
if DEBUG:
print output

# search for humidity
matches = re.search(“Hum =\s+([0-9.]+)”, output)
if (matches):
humidity = float(matches.group(1))
if DEBUG:
print “Humidity: %.1f %%” % humidity

# search for temperature
matches = re.search(“Temp =\s+([0-9.]+)”, output)
if (matches):
temperature = float(matches.group(1))*(9.0/5.0) + 32
if DEBUG:
print “Temperature: %.1f F” % temperature
return {‘temperature':temperature,’humidity':humidity}

time.sleep(3)

# main program entry point – runs continuously updating our datastreams with the
# with the current temperature, pressure, and humidity
def run():
print “Starting environmental monitoring script”

while True:
bmpdata = read_bmp085()
dhtdata = read_dht()

if DEBUG:
print “Updating ThingSpeak feed with temperature: %.2f F” % dhtdata['temperature']
print “Updating ThingSpeak feed with pressure: %.2f in” % bmpdata['pressure']
print “Updating ThingSpeak feed with humidity: %.2f percent” % dhtdata['humidity']

params = urllib.urlencode({‘field1′: dhtdata['temperature'], ‘field2′: bmpdata['pressure'], ‘field3′: dhtdata['humidity']})
headers = {“Content-type”: “application/x-www-form-urlencoded”,”Accept”: “text/plain”}
conn = httplib.HTTPConnection(“api.thingspeak.com:80″)
conn.request(“POST”, “/update?key=xxxxxxxxxxxxxx”, params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()

time.sleep(300)

run()

After I spent a few hours getting my Raspberry Pi sensors logging to Cosm, LogMeIn updated their site to Xively.  The way  updates are sent to the site using python also changed.  Parsing through Xively’s Raspberry Pi python example I managed to come up with a working script.  I have to admit it’s much more modular and handles errors more gracefully than my original Cosm script did.

#!/usr/bin/python

import os
import re
import xively
import subprocess
import time
import datetime
import sys

sys.path.append(‘/home/pi/python/Adafruit-Raspberry-Pi-Python-Code/Adafruit_BMP085′)

from Adafruit_BMP085 import BMP085

FEED_ID=xxxxxxxxxx
API_KEY=”xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
DEBUG=True

# initialize api client
api = xively.XivelyAPIClient(API_KEY)

# function to read temperature and pressure from bmp085
def read_bmp085():
if DEBUG:
print “reading bmp085 temperature and pressure”

bmp = BMP085(0x77)
temp = bmp.readTemperature()
pressure = bmp.readPressure()

temp = round(((9.0/5.0)*temp + 32), 2)
pressure = (pressure / 100)*0.0295301
pressure = round(pressure, 2)

if DEBUG:
print “bmp085 Temperature: %.2f F” % temp
print “Pressure: %.2f in.” % pressure

return {‘temp':temp, ‘pressure':pressure}

def read_dht():
if DEBUG:
print “reading dht temperature and humidity”

while True:
output = subprocess.check_output(["./Adafruit_DHT", "2302", "4"]);
if DEBUG:
print output

# search for humidity
matches = re.search(“Hum =\s+([0-9.]+)”, output)
if (matches):
humidity = float(matches.group(1))
if DEBUG:
print “Humidity: %.1f %%” % humidity

# search for temperature
matches = re.search(“Temp =\s+([0-9.]+)”, output)
if (matches):
temperature = float(matches.group(1))*(9.0/5.0) + 32
if DEBUG:
print “Temperature: %.1f F” % temperature
return {‘temperature':temperature,’humidity':humidity}

time.sleep(3)

# function to return a datastream object. This either creates a new datastream,
# or returns an existing one
def get_datastream(feed):
try:
temp_datastream = feed.datastreams.get(“temperature”)
if DEBUG:
print “Found existing temperature datastream”
except:
if DEBUG:
print “Creating new temperature datastream”
temp_datastream = feed.datastreams.create(“temperature”, tags=”temperature”)

try:
pressure_datastream = feed.datastreams.get(“pressure”)
if DEBUG:
print “Found existing pressure datastream”
except:
if DEBUG:
print “Creating new pressure datastream”
pressure_datastream = feed.datastreams.create(“pressure”, tags=”pressure”)

try:
humidity_datastream = feed.datastreams.get(“humidity”)
if DEBUG:
print “Found existing humidity datastream”
except:
if DEBUG:
print “Creating new humidity datastream”
humidity_datastream = feed.datastreams.create(“humidity”, tags=”humidity”)

return {‘tempds':temp_datastream, ‘pressureds':pressure_datastream, ‘humidityds':humidity_datastream}
# main program entry point – runs continuously updating our datastreams with the
# with the current temperature, pressure, and humidity
def run():
print “Starting environmental monitoring script”

feed = api.feeds.get(FEED_ID)

datastreams = get_datastream(feed)

datastreams['tempds'].max_value = None
datastreams['tempds'].min_value = None
datastreams['pressureds'].max_value = None
datastreams['pressureds'].max_value = None
datastreams['humidityds'].min_value = None
datastreams['humidityds'].min_value = None

while True:
bmpdata = read_bmp085()
dhtdata = read_dht()

if DEBUG:
print “Updating Xively feed with temperature: %.2f F” % dhtdata['temperature']
print “Updating Xively feed with pressure: %.2f in” % bmpdata['pressure']
print “Updating Xively feed with humidity: %.2f percent” % dhtdata['humidity']

datastreams['tempds'].current_value = dhtdata['temperature']
datastreams['tempds'].at = datetime.datetime.utcnow()
datastreams['tempds'].update()

datastreams['pressureds'].current_value = bmpdata['pressure']
datastreams['pressureds'].at = datetime.datetime.utcnow()
datastreams['pressureds'].update()

datastreams['humidityds'].current_value = dhtdata['humidity']
datastreams['humidityds'].at = datetime.datetime.utcnow()
datastreams['humidityds'].update()

time.sleep(600)

run()

Raspberry Pi Sensors

Posted: May 18, 2013 in Uncategorized
Tags: ,

I recently purchased a Raspberry Pi from Adafruit Industries along with several sensors.  The sensors that I’m currently using are a DHT22 Temperature and Humidity Sensor along with a BMP085 Pressure Sensor.  I have those interfacing with the RaPi via GPIO.  Adafruit does an excellent job with their Learning System providing example python scripts to get started.

DHT22 and BMP085

DHT22 and BMP085

Top View
Top View