Skip to content

Instantly share code, notes, and snippets.

@bzshang
Last active November 11, 2015 17:20
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 bzshang/c77a2ae3ba9505d7817c to your computer and use it in GitHub Desktop.
Save bzshang/c77a2ae3ba9505d7817c to your computer and use it in GitHub Desktop.
arduino-python-temperature
#!/usr/bin/python
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
import urllib2, urllib
import random
from time import gmtime, strftime, sleep
from bridgeclient import BridgeClient as bridgeclient
client = bridgeclient()
print "python script is starting"
url = 'https://<SERVER>/piwebapi/streams/{webId}/value'
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, url, 'bshang.adm', 'hidden')
auth = urllib2.HTTPBasicAuthHandler(password_manager) # create an authentication handler
opener = urllib2.build_opener(auth) # create an opener with the authentication handler
urllib2.install_opener(opener) # install the opener...
req_headers = {'Content-Type': 'application/json'}
while 1:
val = client.get('TEMP')
ts = strftime('%Y-%m-%d %H:%M:%S', gmtime()) + 'Z'
print ts, val
req_data = str({"Timestamp": ts, "Value": val})
request = urllib2.Request(url, headers = req_headers)
request.add_data(req_data)
try:
response = urllib2.urlopen(request).read()
except:
print "Unexpected error:", sys.exc_info()[0]
sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment