Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import sys
  2. import math
  3. from time import sleep, strftime
  4.  
  5. import Adafruit_DHT
  6. import RPi.GPIO as GPIO
  7.  
  8. from ubidots import ApiClient
  9.  
  10.  
  11. execfile('settings_sensor.py')
  12.  
  13. api = 0
  14. tempVar = 0
  15. humidVar = 0
  16.  
  17. #upload to ubidots every 6 measurements (free plan compatible)
  18. uploadEvery =6
  19. iterationCounter = 0
  20.  
  21. # Load default font.
  22.  
  23.  
  24. # Get drawing object to draw on image.
  25.  
  26. def loadApi():
  27. global api, tempVar, humidVar
  28. try:
  29. #Create an "API" object
  30. api = ApiClient("5a831f93c03f97362baf4fee")
  31.  
  32. #Create a "Variable" object
  33. tempVar = api.get_variable("5a831fafc03f97362baf4ff8")
  34. humidVar = api.get_variable("5a831fa3c03f9736655128e0")
  35. except:
  36.  
  37.  
  38. e = sys.exc_info()[0]
  39. print "Loading ubidots api failed with exception",e,"... will retry later"
  40. api=0
  41.  
  42.  
  43. while True:
  44. humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  45.  
  46.  
  47.  
  48.  
  49. if humidity is not None and temperature is not None:
  50. print strftime("%Y-%m-%d %H:%M:%S"),'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature,$
  51.  
  52. if (iterationCounter % uploadEvery == 0):
  53. iterationCounter = 1
  54. if (api == 0):
  55. loadApi()
  56. if (api != 0):
  57. try:
  58. print "uploading to Ubidots..."
  59. tempVar.save_value({'value':temperature})
  60. humidVar.save_value({'value':humidity})
  61. print "...done"
  62. except:
  63. e = sys.exc_info()[0]
  64. print "Exception while connecting to Ubidots:", e
  65. else:
  66. iterationCounter += 1
  67.  
  68. else:
  69. print 'Sensor reading failed. Will try again in the next cycle'
  70.  
  71.  
  72.  
  73.  
  74. # sleep for ten seconds
  75. sleep(10)
  76.  
  77. # ...and now show the humidity graph
  78.  
  79. # resize humidity value range to screen height
  80.  
  81. # Draw a black filled box to clear the image.
  82.  
  83. # sleep for another ten seconds
  84. sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement