Hi - thanks for looking at this one! I am the tester / editor and this is the first time I've got anywhere near a machine learning project. I've used Azure and Power BI a bit, and have coded a few edge devices, but this was new territory for me. My point is - if its your first time you are in the same boat I was in a week ago:
As an inexperienced Azure user I found it hard going the first time I went through it, second time a bit tough, but by the third time I rattled through it quickly. So it might take you several hours to get through this, or perhaps less than an hour if you are familiar with things. But there is an underlying logic to the whole process - I hope that comes through in the tutorial - if not it should come through as you use the tools.
The pay off is worth it - once you are comfortable with the process outlined here it is easy to switch out elements: e.g. change the edge device to feed in different data, or use a different Machine Learning (ML) add-in ('experiment'). Picking a Machine Learning experiment is very simple and there are LOADS to choose from.
So if this is your first time delving into ML: persevere, bear in mind the logic behind some of the steps may only be revealed towards the end, and don't get your primary keys muddled up!!! Good luck.
How it works:The weather sensor xChip (SW01) gets temperature and humidity from the environment and passes the data to the processor and Wi-fi core (the CW02 xChip). The CW02 then transmits the data to Azure, which in turn feeds the data into a Machine Learning algorithm to give rain prediction data. This data is fed back to Azure then passed to Power BI, where we build a visualistion.We will use a pre-built Weather prediction model that we can access in Azure for this project.
Steps in this tutorial:- Hardware setup
- Set up Zerynth Studio
- Set up Azure and related services
- The code
- Switching it all on
- Summary
Connect the CW02, SW01, IP01 and MD01 together using the XC10 xBUS connectors. You may connect it as shown in the diagram below. Please see this guide on how to assemble xChips generally.
Notes:
- The xChip CW02 is based on the ESP32, which is a single 2.4 GHz Wi-Fi and BLE combo chip designed with ultra-low-power technology and an Xtensa® Dual-core 32-bit LX6 microprocessor. We use the IP01 to connect it to your laptop / computer, which powers the device and allows us to flash code onto the CW02.
- The xChip SW01 is a Weather sensor (based on Bosch Sensortec’s BME280) that can measure temperature, relative humidity and pressure.
- The MD01 is a ‘blank’ xChip that is included to add mechanical strength to the circuit as well as redundancy (so, if the connection between the IP01 and the CW02 is lost the circuit above will still work). You can omit the MD01 if you do not have one.
Another way to add strength is to build an ‘enclosure’ for the instrument, and the easiest way to do this is to 3D print one. Enclosures for XinaBox kit can be very easily designed using this application (which is a beta release at the moment):
Once you have connected your xChips together plug the IP01 into your laptop / computer.
2. Set up Zerynth Studio- Download and Install the latest version of Zerynth Studio.
IMPORTANT NOTE: If you are not familiar with Zerynth Studio, please refer to the link: Downloading and Installing Zerynth Studio.
- With Zerynth Installed click to Create a New Project.
- Ensure your hardware is connected to the laptop / computer and select XinaBox CW02 (ESP32) from the Device dropdown
- You will then need to Connect, Register and Virtualize your Device
The process of setting up all the online services has many steps and if you are doing this for the first time it will take a while. None of the steps are especially tricky by themselves, but they are finicky and entering a single wrong parameter will prevent the system from working. Make sure you have some time on your hands before progressing. We will work through the following steps in this section:
- Create An IoT hub in your Azure workspace
- Create an Azure Stream Analytics resource
- Set up a new device in your IoT Hub
- Setting up our Weather Prediction Model in Azure ML studio
- Setting up the Stream Analytics job
Before we start you will need an Azure subscription. If you don't have one you can create a free 30 day account which will grant you access to all the services used here.
Once you have set up your Azure subscription you will need to ensure you have access to Azure ML Studio and Power BI:
- Sign up for Azure ML studio
- Sign up for Power BI
Note: in this section we have borrowedbits and pieces from Microsoft’s excellent tutorial. If you need an alternative source of info it is worth a look, but bear in mind that our steps diverge from theirs in a few places (e.g. we use PowerBi as an end-point and they use Excel).
- Create an IoT hub in your Azure workspace:
Follow the steps in this tutorial to set up an Azure IoT Hub
Make a note of the name you give your IoT Hub:
- Create an Azure Stream Analytics job resource:
Click on Resources then on New.
Select Internet of Things then Stream Analytics job, as shown in the following screen-grab:
Give the Stream Analytics job a name then save (default values are OK for the other fields).
At this point, when you view your Resources in Azure it should list 2 items - the IoT hub and the Stream Analytics job.
- Set up a new device in your IoT Hub:
This will represent the physical device we built in the first section.
With your IoT Hub selected in Azure click IoT devices then new:
Give your IoT Device a name and make a note of it.
- Setting up our Weather Prediction Model in Azure ML studio
To use a weather prediction model we first we choose one - there are several options. Then we have to import the weather prediction experiment we will use into Azure ML studio:
- Open ML Studio - check that you are signed in
- In a separate tab in the same browser go to this URL to locate the weather prediction AI ‘Experiment’ that we use. There are several others available that you could also try as well as loads of other cool experiments to play with.
- Click on Open In Studio. You will then have to choose a location from a modal window that corresponds to the location of the Azure workspace you have set up.
Once the experiment has imported your ML Studio interface should look something like this:
Click run (in the menu at the bottom of the screen) to validate the steps.
- As each step in the process runs it also validates, and you will see a green tick get added. There shouldn’t be any errors. It should stop running once all steps have been validated - it may take a minute or so.
Click SETUP WEB SERVICE> Predictive Web Service. This can be found in the bottom menu next to run.
- Note: this option won’t be selectable unless the system has run all the way through in the previous step.
- After you click to set up the web service your view updates as a new version of the model is generated for our Predictive Experiment. Once complete there should be 2 tabs - one labelled Training Experiment and one labelled Predictive Experiment. We will use the Predictive Experiment tab.
- We are going to MOVE the Web Services Input block. This tells the experiment where to inject the data from the web service (i.e. from our hardware device) into the model.
- Click on the line joining Web Services Input to Weather Dataset (click where it joins the Web Services Input block) - this will disconnect them.
- Now connect the line to the Score Model block as shown below:
- Once you have completed the above click Run to let the new model validate. Remember to wait for it to complete.
Click DEPLOY WEB SERVICE to deploy the model as a web service.
Note: In this project we will use Power BI as our end-point - the place where we visualise the data from the ML service. You could set other end-points if you prefer - there is even an option to use a macro-enabled Excel spreadsheet.
You have now built an Experiment which you can see by clicking on the experiments list in the left hand menu. You have also created a Web Service object which we will link to from Azure - we will pass data into this Web Service. To do so we need to take note of a couple of details (URL and API Key):
- Click on the Web Services link on the left hand menu. You will see a list showing at least 1 one web-service.
- Click on the name of the web service we just created - by default it will be named Weather prediction model (Predictive Exp):
- In the Web Service view make a note of the API Key listed
- Click on the REQUEST / RESPONSE link.
- A new tab should open with a page titled Request Response API Documentation for Weather prediction model [Predictive Exp.]. This has the final detail we’ll need to use in Azure: Make note of the POST URL from this page
Before we finish do a quick Test of your web-service :
- Test the webservice by clicking on the Test link highlighted in the following screen grab:
- You should be prompted to add dummy variables for Temperature and Humidity - enter some values then click the tick.
- The test should work fine (it always passed when we tested it). If not try deleting the web service and deploying again, or importing the experiment again.
We are done with Azure ML Studio! Back to Azure...
- Setting up the Stream Analytics job:
What we are going to do next is link up our Input (data from the hardware we already built) with our output (Power BI). We will tell Azure to use the ML Studio Weather Station service to process the data. We will then set up a SQL query to get the data from the ML service into the right shape to send to Power BI. This is the ‘workflow’ of a Stream Analytics job: input-function-query-output. In this section we will hook it all together.
- Go back into your Azure portal and select your IoT Hub
First we need to set up a Consumer Group:
- Click Built-in endpoints in your left-pane
Click on the text field labelled Create new consumer group, enter a name then click outside the text field (to get it to save):
Next we are going to configure the Stream Analytics job resource that we set up earlier. First we specify the Input Stream:
- Open your Stream analytics job resource.
- Click Inputs then Add input stream and select IoT Hub.
- You will now need to add some details:
- Give the input stream a name.
- Ensure Provide IoT Hub settings manually is selected.
- Enter the name of the consumer group we added in the previous step (airain). Note - don't muddle up the resource group and consumer group.
- Leave the other values as default then click Save. Work through any prompts that follow.
Our input is now defined.
Next we specify our output stream (Power BI):
- With the Stream analytics job resource open click on Outputs then on Add. Select Power BI from the list of options.
- Enter names for Output Alias, Dataset name and Table name. These can be anything.
- Click Authorize to establish a connection with Power BI. You will be asked to enter the username and password associated with your Power BI account (even if they are the same as your Azure credentials).
Click Save when you are done.
We have now specified the Input that will be fed into the Stream Analytics job as well as identifying where the Output should be directed. We still need to tell Stream Analytics what to do with the data though - how to process our raw inputs into weather predictions. To do this we need to specify a Function
Click Functions in your left-pane then click Add and select Azure ML from the available options:
- Give the function a name (Function alias) - we use machinelearning (IF you change this remember your new function name as you will have to amend a SQL query we enter later).
- Select Provide Azure Machine Learning function settings manually
- Enter URL and Key that we noted earlier from ML Studio.
Once you have entered the required details click Save.
Let’s review this function and check it has been set up properly:
- Select the Stream Analytics job resource
- Click on Job Topology then on Functions.
- Click on the function you just set up.
- Function details should load, as shown in the following image.
- NB - check that the FUNCTION SIGNATURE looks the same as is shown below. We need to write a SQL Query later that is based on this signature, so if it is different you may have problems with the SQL.
If the signature is different try going back to your Machine Learning Studio, select the web-service and work through the steps listed here, taking care to test at the end. Then try and set up a new function and see if that helps.
The data from our input is being passed to the Weather Predictor experiment, which is processing it and sending data back to Azure. All we need to do now is to tell Azure how to repackage that data so that we can send it to Power BI.
With the Stream Analytics job selected click on Job Topology then Query.
Enter the query below, ensuring you update the parameters:
WITH machinelearning AS (
SELECT EventEnqueuedUtcTime, temperature, humidity, machinelearning(temperature, humidity) as result from [Your-Input-Alias]
)
Select System.Timestamp time, CAST (result.[temperature] AS FLOAT) AS temperature, CAST (result.[humidity] AS FLOAT) AS humidity, CAST (result.[Scored Probabilities] AS FLOAT ) AS 'probabilities of rain'
Into [Your-Output-Alias]
From machinelearning
Replace Your-Input-Alias and Your-Output-Alias with your Input and Output aliases respectively.
If the query is fine you can save it.
We have now completed Section 3 - our Azure portal is now mostly setup. Next we'll code the edge device we built in section 2.
4. The codeThe code contains four files.
Open Zerynth IDE, create a new project then add these 4 files:
- device.conf.json
- helpers.py
- main.py
- private.base64.key
1. Device.conf.json contains device configuration.
Replace iot-hub-id and device-name with your IoT Hub name and Device name:
{
"hub_id": "iot-hub-id",
"device_id": "device-name",
"api_version": "2017-06-30"
}
2. helpers.py contains helper functions to load device configuration and IoT hub key.
You don’t need to make any amends to this code:
# -*- coding: utf-8 -*-
# @Author: Lorenzo
# @Date: 2017-10-03 10:56:02
# @Last Modified by: lorenzo
# @Last Modified time: 2017-10-26 10:44:43
import json
def load_key(key_file):
mstream = open('resource://' + key_file)
pkey = bytearray()
while True:
rd = mstream.read(1)
if not rd:
break
pkey.append(rd[0])
return pkey
def load_device_conf():
confstream = open('resource://device.conf.json')
conf = ''
while True:
line = confstream.readline()
if not line:
break
conf += line
return json.loads(conf)
3. main.py contains the main body of code:
The code for this is long, and it is attached to this project.
You will need to amend only the following line:
# place here your wifi configuration
wifi.link("SSID",wifi.WIFI_WPA2,"PSK")
Replace "SSID" with your Wi-fi name and "PSK" with the password.
4. private.base64.key contains the Primary Key that is associated with the Device we set up in Azure.
To find this:
- Go into your Azure workspace and select the IoT Hub we set up.
- Click on IoT Devices then on the device we set up earlier.
- You should see a view similar to the one shown below:
Copy and paste the primary key into the private.base64.key - that is the only item in the file.
Save the file - we are done coding now. What remains is to verify and uplink the code:
- Make sure that the device is Connected, Registered and Virtualized.
- Verify and uplink the code:
In Azure navigate to the Stream Analytics job.
In the Overview section click on Run.
If the Stream Analytics job has been set up properly it should run. If not you will get an error message - read this carefully to check how to troubleshoot.
Power up your xChip instrument: with the IP01 plugged in to your laptop / computer, click on the Monitor option in Zerynth. You should see something that looks like the screen-grab below:
The CW02 publishes temperature and humidity values every 20 seconds (you can change the publish period in main.py).
Once you have verified the CW02 is working correctly you could power it using a power bank.
Next open Power BI in a new browser.
Click on Get Data
The dataset SHOULD show up as an option. You will see the database name you entered when you set up the output. Select it.
- If the data set is not showing up wait a while - it can take a few minutes.
- If you still don’t see the data set check your Outputs (in the Stream Analytics job view). Test to ensure it is working correctly
The data is now available in Power BI to use in visualisations / charts.
If you are unfamiliar with Power BI there are some excellent tutorials online.
You can use one of the built-in visualisations, e.g:
The chart suggested above shows the weather data received and the associated probability of rain, but the visual is very bland. Try the other built in visualisations to see if there are any that you like.
If none of the built-in visualisations are appealing you can search the market place for new ones.
- Click on the 3 dots in the Visualisations area to open the Import Custom Visual dialogue.
The Dial Gauge option is quite suitable for showing off the rain prediction:
Looks safe to plan for a barbeque later on!
6. SummaryIn this tutorial we:
- Set up an Azure portal then within that an IoT hub.
- Set up a machine learning service to analyse our weather data and make predictions based on it.
- Set up a Stream Analytics job to feed the data from our weather station into the machine learning service, then to feed that data into Power BI.
- Set up then coded a weather station, which sends data to our Azure IoT Hub.
- Set up Power BI to show visualisations of our data.
You might have noticed that Azure ML Studio has a large range of machine learning experiments - the steps shown here are the same regardless of which one you use. There are a load of different XinaBox sensors that you could use to generate data, so if you have other xChips lying about why not see what other ML services you can use!
Comments