Tuesday, August 26, 2014

Raspberry Pi, Parse.com, and Temperature Logging



As part of a future project I may be working on(details coming soon). I wanted to get some logging done with some temperature sensors to the online cloud database called Parse.com
If you want your data stored on the cloud to be able to access anywhere this is what you will need to do:
  • install the DS18b20 temperature probes

Parse App Setup

 copy the application id and the rest api key to your python script

Get the ParsePy library:

install on your pi using the command

git clone https://github.com/dgrtwo/ParsePy
cd ParsePy
sudo python setup.py install

Getting my Logging Library:

download here:
https://github.com/SimplyAutomationized/raspberrypi/raw/master/ParseTempLogging/ParseTemperature.py
usage:
from ParseTemperature import *
logger = TempLogger(appkey='yourappkey',apikey='yourapikey')
logger.start()#will create the class in your app and start appending temperature data to it whenever the temperature changes. 

Now the two sensors started populating my online db. 

for reference here's the logging libarary:

from parse_rest.connection import register
from parse_rest.datatypes import Object

from threading import Thread
from time import sleep
import os
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
class Temperature(Object):
 pass


class TempLogger(Thread):
 """
 A class for getting the current temp of a DS18B20
 """
 def __init__(self, fileName='',debug=False,appkey='',apikey=''):
  Thread.__init__(self)
  register(appkey,apikey)#put your api key and app id here
  self.debug = debug
  self.probes = {}
  self.tempDir = '/sys/bus/w1/devices/'
  self.currentTemp = -999
  self.correctionFactor = 1;
  self.enabled = True
  self.repopulateprobes()
 def repopulateprobes(self):
  list = os.listdir(self.tempDir)#here we create a dictionary with the probe id's
  for item in list:
   if(item[:2]=="28"):
   if(self.debug):
    print item
   if(self.probes.has_key(item)==False):
    self.probes[item]=0

 def getTempForFile(self,file):
  try:
   f = open(self.tempDir + file + "/w1_slave", 'r')
  except IOError as e:
   print "Error: File " + self.tempDir + file + "/w1_slave" + " doesn't exist"
   return;
  lines=f.readlines()
  crcLine=lines[0]
  tempLine=lines[1]
  result_list = tempLine.split("=")
  temp = float(result_list[-1])/1000 # temp in Celcius
  temp = temp + self.correctionFactor # correction factor
  #if you want to convert to Celcius, comment this line
  temp = (9.0/5.0)*temp + 32
  if crcLine.find("NO") > -1:
   temp = -999
  if(self.debug):
   print "Current: " + str(temp) + " " + str(file)
  return float(int(temp*100))/100    
 def run(self):
  while self.enabled:
   for item in self.probes.items(): #we iterate through our probes we scanned for earlier and save temperatures to the dictionary
    temp = self.getTempForFile(item[0])
    if(item[1]!=temp):#if there is a change in the temperature we send it to parse
     parseDBObject=Temperature()
     parseDBObject.Probe=item[0]
     parseDBObject.Temperature=temp
     try:
      parseDBObject.save()
     except:
      pass
     self.probes[item[0]]=temp
 def stop(self):
  self.enabled=False
    #returns the current temp for the probe
 def getCurrentTemp(self,file):
  return self.probes[file]