Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Andrew Pattison, 11/10/2012 - 25/10/2012
- # script to update cosm graphs using 2 sensors:
- # 1. CPU temperature sensor built into CPU
- # 2. Ambient temperature sensor on the I2C bus
- # N.B. For some reason, the bash script that I
- # previously used to update cosm occasionally caused
- # cosm to report wierd temperatures like -913456 celcius
- # for the CPU temperature
- # so I do some basic sanity checking on the reading before
- # sending it to cosm just to be on the safe side
- # Errors are logged to syslog
- import quick2wire.i2c as i2c
- import time
- import json
- import http.client
- import syslog
- import sys
- # I2C address of temperature sensor
- address = 0x48
- while True:
- try:
- with i2c.I2CMaster() as bus:
- sensora, sensorb = bus.transaction(i2c.writing_bytes(address, 0x00),i2c.reading(address, 2))[0]
- temp = (sensora << 8 | sensorb) / 256.
- tempstring = "{0:.2f}".format(temp)
- print ("Ambient temperature: %02.02f" % temp)
- file = open("/sys/class/thermal/thermal_zone0/temp", "r")
- string = file.readline()
- file.close()
- cputemp = float(string) / 1000
- if cputemp > 0:
- # normal reading from CPU temp sensor, so send to cosm
- cputempstring = "{0:.2f}".format(cputemp)
- upload = { "version": "1.0.0", "datastreams": [
- { "id":"RoomTemperature", "current_value":tempstring },
- { "id":"CPUTemperature", "current_value":cputempstring }
- ]
- }
- else:
- # abnormal reading from CPU temp sensor, so DON'T send to cosm
- cputempstring = "0.00"
- upload = { "version": "1.0.0", "datastreams": [
- { "id":"RoomTemperature", "current_value":tempstring }
- ]
- }
- print(cputempstring)
- print(json.dumps(upload))
- print("About to send data to cosm...")
- connection = http.client.HTTPConnection("api.cosm.com")
- data = json.dumps(upload)
- header = { "X-ApiKey":"<your key goes here>" }
- connection.request("PUT", "/v2/feeds/79499/", data, header)
- result = connection.getresponse()
- print("HTTP status code from cosm server: " + str(result.status))
- print("Finished sending data to cosm")
- if result.status == 200:
- if cputemp > 0:
- logstring = "CPU temp=" + cputempstring + ", room temp=" + tempstring
- else:
- logstring = "room temp=" + tempstring
- # syslog.syslog("Successfully sent data to cosm: " + logstring)
- else:
- syslog.syslog("Error sending data to cosm. HTTP response code: " + str(result.status))
- time.sleep(60)
- except Exception:
- # N.B. we are still within the while loop!
- print ("An exception occured: 0=" + str(sys.exc_info()[0]) + ", 1=" + str(sys.exc_info()[1]) + ", 2=" + str(sys.exc_info()[2]))
- syslog.syslog("An exception occured: 0=" + str(sys.exc_info()[0]) + ", 1=" + str(sys.exc_info()[1]) + ", 2=" + str(sys.exc_info()[2]))
- syslog.syslog("Pausing for 60 seconds to see if error condition clears...")
- time.sleep(60)
- syslog.syslog("Attempting to continue where I left off...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement