From e216cc476c1ce62aa0f77202759bc49930328125 Mon Sep 17 00:00:00 2001 From: Stephen Tramer Date: Mon, 1 Jul 2019 11:17:09 -0700 Subject: [PATCH] Updated Python content to comply with PEP8 (via autopep8) --- uploadDHT22Data.py | 58 +++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/uploadDHT22Data.py b/uploadDHT22Data.py index 17a5801..90c560e 100644 --- a/uploadDHT22Data.py +++ b/uploadDHT22Data.py @@ -3,15 +3,17 @@ a DHT22 sensor, and sends that data to Power BI for use in a streaming dataset. """ -import urllib, urllib2, time +import urllib +import urllib2 +import time from datetime import datetime import Adafruit_DHT as dht # type of sensor that we're using -SENSOR = dht.DHT22 +SENSOR = dht.DHT22 # pin which reads the temperature and humidity from sensor -PIN = 4 +PIN = 4 # REST API endpoint, given to you when you create an API streaming dataset # Will be of the format: https://api.powerbi.com/beta//datasets/< dataset id>/rows?key= @@ -19,27 +21,29 @@ # Gather temperature and sensor data and push to Power BI REST API while True: - try: - # read and print out humidity and temperature from sensor - humidity,temp = dht.read_retry(SENSOR, PIN) - print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temp, humidity) - - # ensure that timestamp string is formatted properly - now = datetime.strftime(datetime.now(), "%Y-%m-%dT%H:%M:%S%Z") - - # data that we're sending to Power BI REST API - data = '[{{ "timestamp": "{0}", "temperature": "{1:0.1f}", "humidity": "{2:0.1f}" }}]'.format(now, temp, humidity) - - # make HTTP POST request to Power BI REST API - req = urllib2.Request(REST_API_URL, data) - response = urllib2.urlopen(req) - print("POST request to Power BI with data:{0}".format(data)) - print("Response: HTTP {0} {1}\n".format(response.getcode(), response.read())) - - time.sleep(1) - except urllib2.HTTPError as e: - print("HTTP Error: {0} - {1}".format(e.code, e.reason)) - except urllib2.URLError as e: - print("URL Error: {0}".format(e.reason)) - except Exception as e: - print("General Exception: {0}".format(e)) + try: + # read and print out humidity and temperature from sensor + humidity, temp = dht.read_retry(SENSOR, PIN) + print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temp, humidity) + + # ensure that timestamp string is formatted properly + now = datetime.strftime(datetime.now(), "%Y-%m-%dT%H:%M:%S%Z") + + # data that we're sending to Power BI REST API + data = '[{{ "timestamp": "{0}", "temperature": "{1:0.1f}", "humidity": "{2:0.1f}" }}]'.format( + now, temp, humidity) + + # make HTTP POST request to Power BI REST API + req = urllib2.Request(REST_API_URL, data) + response = urllib2.urlopen(req) + print("POST request to Power BI with data:{0}".format(data)) + print("Response: HTTP {0} {1}\n".format( + response.getcode(), response.read())) + + time.sleep(1) + except urllib2.HTTPError as e: + print("HTTP Error: {0} - {1}".format(e.code, e.reason)) + except urllib2.URLError as e: + print("URL Error: {0}".format(e.reason)) + except Exception as e: + print("General Exception: {0}".format(e))