Skip to content

Instantly share code, notes, and snippets.

@AndriyHz
Forked from whatnick/sensor_thingspeak.py
Created July 21, 2016 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndriyHz/e7541e48e49d41339be4224fd3987e6f to your computer and use it in GitHub Desktop.
Save AndriyHz/e7541e48e49d41339be4224fd3987e6f to your computer and use it in GitHub Desktop.
import time
from tentacle_pi.LM75 import LM75
import httplib, urllib
from time import localtime, strftime
lm = LM75(0x48,"/dev/i2c-1")
while(True):
temperature = lm.temperature()
print "temperature: %0.2f" % temperature
temp_raw_file = open('/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_temp_raw','r')
temp_scale_file = open('/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_temp_scale','r')
p_temp = int(temp_raw_file.read())*float(temp_scale_file.read())
print "temperature: %.2f" % p_temp
pres_raw_file = open('/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_pressure_raw','r')
pres_scale_file = open('/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_pressure_scale','r')
pressure = int(pres_raw_file.read())*float(pres_scale_file.read())
print "pressure: %.4f" % pressure
params = urllib.urlencode({'field1': temperature, 'field2': pressure,'field3':p_temp,'key':'YOUR_KEY_HERE'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print strftime("%a, %d %b %Y %H:%M:%S", localtime())
print response.status, response.reason
data = response.read()
conn.close()
except:
print "connection failed"
time.sleep(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment