├── LICENSE ├── README.md └── uploadDHT22Data.py /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Microsoft Corporation 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | services: power-bi 3 | platforms: raspberry-pi 4 | author: sirui-sun 5 | --- 6 | 7 | # Building a Real-time IoT Dashboard with Power BI: A Step-by-Step Tutorial 8 | This sample code will read from a DHT22 sensor wired to a Raspberry Pi running Raspbian, and send data to a Power BI dashboard for display. Full walkthrough [here](https://powerbi.microsoft.com/blog/using-power-bi-real-time-dashboards-to-display-iot-sensor-data-a-step-by-step-tutorial). 9 | 10 | 11 | ## Running this sample locally on Raspbian 12 | ### Clone the repository 13 | ``` 14 | git clone https://github.com/Azure-Samples/powerbi-python-iot-client 15 | ``` 16 | 17 | ### Install dependencies 18 | ``` 19 | sudo apt-get install build-essential python-dev 20 | git clone https://github.com/adafruit/Adafruit_Python_DHT.git && cd Adafruit_Python_DHT && sudo python setup.py install 21 | ``` 22 | 23 | ### Update uploadDHT22Data.py with your REST API push URL 24 | ``` 25 | REST_API_URL = " *** Your Push API URL goes here *** " 26 | ``` 27 | 28 | ### Run script 29 | ``` 30 | python uploadDHT22Data.py 31 | ``` -------------------------------------------------------------------------------- /uploadDHT22Data.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python sample for Raspberry Pi which reads temperature and humidity values from 3 | a DHT22 sensor, and sends that data to Power BI for use in a streaming dataset. 4 | """ 5 | 6 | import urllib, urllib2, time 7 | from datetime import datetime 8 | import Adafruit_DHT as dht 9 | 10 | # type of sensor that we're using 11 | SENSOR = dht.DHT22 12 | 13 | # pin which reads the temperature and humidity from sensor 14 | PIN = 4 15 | 16 | # REST API endpoint, given to you when you create an API streaming dataset 17 | # Will be of the format: https://api.powerbi.com/beta//datasets/< dataset id>/rows?key= 18 | REST_API_URL = " *** Your Push API URL goes here *** " 19 | 20 | # Gather temperature and sensor data and push to Power BI REST API 21 | while True: 22 | try: 23 | # read and print out humidity and temperature from sensor 24 | humidity,temp = dht.read_retry(SENSOR, PIN) 25 | print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temp, humidity) 26 | 27 | # ensure that timestamp string is formatted properly 28 | now = datetime.strftime(datetime.now(), "%Y-%m-%dT%H:%M:%S%Z") 29 | 30 | # data that we're sending to Power BI REST API 31 | data = '[{{ "timestamp": "{0}", "temperature": "{1:0.1f}", "humidity": "{2:0.1f}" }}]'.format(now, temp, humidity) 32 | 33 | # make HTTP POST request to Power BI REST API 34 | req = urllib2.Request(REST_API_URL, data) 35 | response = urllib2.urlopen(req) 36 | print("POST request to Power BI with data:{0}".format(data)) 37 | print("Response: HTTP {0} {1}\n".format(response.getcode(), response.read())) 38 | 39 | time.sleep(1) 40 | except urllib2.HTTPError as e: 41 | print("HTTP Error: {0} - {1}".format(e.code, e.reason)) 42 | except urllib2.URLError as e: 43 | print("URL Error: {0}".format(e.reason)) 44 | except Exception as e: 45 | print("General Exception: {0}".format(e)) 46 | --------------------------------------------------------------------------------