Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions uploadDHT22Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,47 @@
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/<tenant id>/datasets/< dataset id>/rows?key=<key id>
REST_API_URL = " *** Your Push API URL goes here *** "

# 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))