Advertisement
andrum99

temp2.py

Dec 13th, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Andrew Pattison, 11/10/2012 - 25/10/2012
  4. # script to update cosm graphs using 2 sensors:
  5. # 1. CPU temperature sensor built into CPU
  6. # 2. Ambient temperature sensor on the I2C bus
  7. # N.B. For some reason, the bash script that I
  8. # previously used to update cosm occasionally caused
  9. # cosm to report wierd temperatures like -913456 celcius
  10. # for the CPU temperature
  11. # so I do some basic sanity checking on the reading before
  12. # sending it to cosm just to be on the safe side
  13.  
  14. # Errors are logged to syslog
  15.  
  16. import quick2wire.i2c as i2c
  17. import time
  18. import json
  19. import http.client
  20. import syslog
  21. import sys
  22.  
  23. # I2C address of temperature sensor
  24. address = 0x48
  25.  
  26. while True:
  27.     try:
  28.         with i2c.I2CMaster() as bus:
  29.             sensora, sensorb = bus.transaction(i2c.writing_bytes(address, 0x00),i2c.reading(address, 2))[0]
  30.             temp = (sensora << 8 | sensorb) / 256.
  31.             tempstring = "{0:.2f}".format(temp)
  32.             print ("Ambient temperature: %02.02f" % temp)
  33.  
  34.         file = open("/sys/class/thermal/thermal_zone0/temp", "r")
  35.         string = file.readline()
  36.         file.close()
  37.         cputemp = float(string) / 1000
  38.         if cputemp > 0:
  39.             # normal reading from CPU temp sensor, so send to cosm
  40.             cputempstring = "{0:.2f}".format(cputemp)
  41.             upload = { "version": "1.0.0", "datastreams": [
  42.             { "id":"RoomTemperature", "current_value":tempstring },
  43.             { "id":"CPUTemperature", "current_value":cputempstring }
  44.             ]
  45.             }
  46.         else:
  47.             # abnormal reading from CPU temp sensor, so DON'T send to cosm
  48.             cputempstring = "0.00"
  49.             upload = { "version": "1.0.0", "datastreams": [
  50.             { "id":"RoomTemperature", "current_value":tempstring }
  51.             ]
  52.             }
  53.         print(cputempstring)
  54.         print(json.dumps(upload))
  55.         print("About to send data to cosm...")
  56.            
  57.         connection = http.client.HTTPConnection("api.cosm.com")            
  58.         data = json.dumps(upload)
  59.         header = { "X-ApiKey":"<your key goes here>" }
  60.         connection.request("PUT", "/v2/feeds/79499/", data, header)
  61.         result = connection.getresponse()
  62.         print("HTTP status code from cosm server: " + str(result.status))
  63.         print("Finished sending data to cosm")
  64.  
  65.         if result.status == 200:
  66.             if cputemp > 0:
  67.                 logstring = "CPU temp=" + cputempstring + ", room temp=" + tempstring
  68.             else:
  69.                 logstring = "room temp=" + tempstring
  70.             # syslog.syslog("Successfully sent data to cosm: " + logstring)
  71.         else:
  72.             syslog.syslog("Error sending data to cosm. HTTP response code: " + str(result.status))
  73.         time.sleep(60)
  74.     except Exception:
  75.         # N.B. we are still within the while loop!
  76.         print ("An exception occured: 0=" + str(sys.exc_info()[0]) + ", 1=" + str(sys.exc_info()[1]) + ", 2=" + str(sys.exc_info()[2]))
  77.         syslog.syslog("An exception occured: 0=" + str(sys.exc_info()[0]) + ", 1=" + str(sys.exc_info()[1]) + ", 2=" + str(sys.exc_info()[2]))
  78.         syslog.syslog("Pausing for 60 seconds to see if error condition clears...")
  79.         time.sleep(60)
  80.         syslog.syslog("Attempting to continue where I left off...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement