Skip to content

Instantly share code, notes, and snippets.

@vwillcox
Created June 24, 2018 20:58
Show Gist options
  • Save vwillcox/880cbae44b1bacbeadda5c1c83891680 to your computer and use it in GitHub Desktop.
Save vwillcox/880cbae44b1bacbeadda5c1c83891680 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
from envirophat import weather
import time, os, sys, urllib, urllib2
THINGSPEAKKEY = 'ENTERYOURAPIKEY'
THINGSPEAKURL = 'https://api.thingspeak.com/update'
def sendData(url, key, field1, field2, temp, pres):
values ={'api_key' : key, 'field1' : temp, 'field2' : pres}
postdata = urllib.urlencode(values)
req = urllib2.Request(url, postdata)
log = time.strftime("%d-%m-%Y,%H:%M:%S")
# Send data to Thingspeak
response = urllib2.urlopen(req, None, 5)
html_string = response.read()
response.close()
def main():
global THINGSPEAKKEY
global THINGSPEAKURL
while True:
roomtemp=str(round(weather.temperature(),2))
roompresure=str(weather.pressure(unit='hPa'))
sendData(THINGSPEAKURL, THINGSPEAKKEY, 'field1','field2',roomtemp,roompresure)
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment