This is best Fabric, Power BI, SQL and AI community event. How do we know? The last event sold out! Save €200 with code FABCMTY200.
Register nowA new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.
Last month, we released a set of features in preview designed to make it easier than ever to display real-time streaming data in your Power BI dashboards. Check out the announcement if you haven’t already. Today, I want to show you just how easy this can be.
Many of you have expressed interest in using Power BI to display real-time sensor data. In this tutorial, we will walk you step-by-step through the entire process of setting up a Raspberry Pi weather station, and showing the resulting temperature and humidity data in real-time with Power BI. Here's a sneak peek of the end result:
Have other use cases in mind? Don't have access to the materials below? No sweat. The lessons and sample code in this tutorial are broadly applicable for a wide range of IoT scenarios. And, as always, please let us know if you have any questions, either in the comments below, or in our community forums.
With that in mind, let's get started! For this tutorial, we'll be using the following materials:
Using Windows 10 IoT instead? No problem. Most of the steps below will apply to you as well, and we'll call out the differences where necessary.
To create a streaming dataset, expand the navigation bar at the left, and click on the "Streaming datasets" button at under the Datasets tab.
From there, click the "Add streaming dataset" button at the top right.
Select the API option.
Give your new streaming dataset a name, and add three fields: timestamp, temperature and humidity. In this step, we're specifying that the data stream has three fields:
1.timestamp: when the weather measurements were taken
2.temperature: the temperature at that point time
3.humidity: the humidity at that point in time
Important: to use the sample code as is, it is important that your data fields (e.g. “temperature”) and their types (e.g. “Number”) exactly match the ones in the screenshot below!
Select "Create" to confirm the creation of the streaming dataset. On the following screen, copy the Push URL and set that aside - we'll need it for later.
We've now successfully created a streaming dataset.
First, we'll want to wire the temperature sensor to the Raspberry Pi. We won't go into the details of wiring the Raspberry Pi in this tutorial, Adafruit has a great wiring schematic here. Hook up your Raspberry Pi to the DHT22 according to that schematic.
First, some setup: assuming you have Raspbian installed and connected to the internet, you'll want to run the following commands in the bash terminal. This sets up your development environment. The first two lines make sure that your system is ready for Python extensions. The last line installs the Adafruit_DHT Python libraries which accompany the DHT22 sensor, which will allow us to easily access temperature data.
> sudo apt-get update
> sudo apt-get install build-essential python-dev
> git clone https://github.com/adafruit/Adafruit_Python_DHT.git && cd Adafruit_Python_DHT && sudo python setup.py install# 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 *** 'Using Windows 10 IoT? The library above may not work for your purposes. Take a look at this guide for a step-by-step walkthrough of reading from the DHT22 sensor.
Before we run the script, let's dive in and understand what the script is doing. The script executes indefinitely, so all our logic is wrapped in a while True loop. In the loop body, we first use the Adafruit_DHT library to grab the humidity and temperature readings from the DHT22 sensor. The library abstracts all the complicated logic into one line! Then, just for fun, we print out the current readings.# 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)[ {
“timestamp” : “2016-09-10T01:26:45.030Z”,
“temperature” : 98.6,
“humidity” : 98.6
} ]# 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)
Tip: no matter what application you're building, we recommend sending a timestamp to Power BI as part of your streaming dataset. When sending timestamp, try to match the sample format as closely as possible: “2016-09-10T01:26:45.030Z” – feel free to use the code snippet above for that purpose.
Using Windows 10 IoT? Check out our sample code here to see how to make a sample request to the Power BI REST API Streaming endpoint.
With our data payload ready for sending, we use Python's built-in networking libraries to make the HTTP POST request to Power BI.# make HTTP POST request to Power BI REST API
req = urllib2.Request(REST_API_URL, data)
response = urllib2.urlopen(req)time.sleep(1)> python uploadDHT22Data.pyTroubleshooting Tips
Something go wrong? Here are some diagnosis tips for common issues.
Within the new dashboard, click "Add tile" on the top right.
Select "Custom Streaming Data" and then Next.
On the next screen, select the dataset that you created in the first step, and click Next.
We'll start by creating a card that displays the most recently received value. In the next screen, under Field, select temperature.
Click next, and you'll be given the option to customize the title and other similar options. Finalize your tile by clicking "Apply" - you should see the tile appear in your new dashboard, and it should be automatically updating with your temperature data.
And there we have it! In a few easy steps, we've taken a stream of raw data and visualized it in Power BI.
Next we'll create a line chart, so that we can see the changes in the data over time. Go back to "add tile" experience, and this time select "Line chart" under the "Visualization type." From there, set Axis to timestamp, and value to temperature.
You can also repeat the same steps with humidity data that's flowing into the streaming dataset. And voila! We've successfully built a rudimentary real-time dashboard representing the data from our mini-weather station.
Fun thing to try: gently blow on the humidity sensor - the moisture from your breath will cause the humidity to spike, and then gradually decline.
What's next?You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.