Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import math
- from time import sleep, strftime
- import Adafruit_DHT
- import RPi.GPIO as GPIO
- from ubidots import ApiClient
- execfile('settings_sensor.py')
- api = 0
- tempVar = 0
- humidVar = 0
- #upload to ubidots every 6 measurements (free plan compatible)
- uploadEvery =6
- iterationCounter = 0
- # Load default font.
- # Get drawing object to draw on image.
- def loadApi():
- global api, tempVar, humidVar
- try:
- #Create an "API" object
- api = ApiClient("5a831f93c03f97362baf4fee")
- #Create a "Variable" object
- tempVar = api.get_variable("5a831fafc03f97362baf4ff8")
- humidVar = api.get_variable("5a831fa3c03f9736655128e0")
- except:
- e = sys.exc_info()[0]
- print "Loading ubidots api failed with exception",e,"... will retry later"
- api=0
- while True:
- humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
- if humidity is not None and temperature is not None:
- print strftime("%Y-%m-%d %H:%M:%S"),'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature,$
- if (iterationCounter % uploadEvery == 0):
- iterationCounter = 1
- if (api == 0):
- loadApi()
- if (api != 0):
- try:
- print "uploading to Ubidots..."
- tempVar.save_value({'value':temperature})
- humidVar.save_value({'value':humidity})
- print "...done"
- except:
- e = sys.exc_info()[0]
- print "Exception while connecting to Ubidots:", e
- else:
- iterationCounter += 1
- else:
- print 'Sensor reading failed. Will try again in the next cycle'
- # sleep for ten seconds
- sleep(10)
- # ...and now show the humidity graph
- # resize humidity value range to screen height
- # Draw a black filled box to clear the image.
- # sleep for another ten seconds
- sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement