Clever Weather is a simple IoT application that allows you to create random data and send them to a IoT hub. Once there, these data can be stored in a SQL database, visualized in a web app or used in one of the other functionalities of Microsoft Azure. This tutorial will only show how to visualize data in local.
All you need to create and execute this app are a python program to generate random values of sensors, an Iot Hub and nodejs. In CleverWeather values are created randomly because I don't have sensors and Arduino/Raspberry Pi to connect to the IoT hub. These values are published to IoT hub using a MQTT protocol implemented in a direct way. Finally a nodejs application will visualize the data received by the IoT hub in different charts in local.
ComponentsAzureIoThub
You can use Azure simply accessing to Azure website using your microsoft credentials or creating one. Unfortunately Azure is free only for a limited time, unless you are a student, but in this case you have less functionalities. Azure has many services, but I will focus on the IoT hub. The IoT hub will be used as a MQTT broker, but it's not complete and it doesn't support all the standard behaviours.
Pythonprogram
You can use different languages to publish messages to the IoT hub, but python is the easiest to use because it implements MQTT protocol by default. I have used python 3.7.6.
Nodejs
Nodejs is used to run an application that lets you visualize the data in local. You have to install also the package @azure/event-hubs.
Implementation- Azure IoT hub
The first thing to do is create an IoT hub. From the Azure homepage select +Create a resource > Internet of Things > IoT Hub.
At this point you have to set different parameters to maximize the user experience. Best practice is to use always the same region for all your resources in order to avoid extra cost.
For the next steps you will need Azure Cloud Shell (CLI). You can open it clicking here or selecting this button from the Azure website.
First you have to add the extension for Azure IoT and then you can create your device.
az extension add --name azure-iot
az iot hub device-identity create --hub-name IoTHubName --device-id DeviceName
IoTHubName is the name of your IoT hub and DeviceName is the device you want to create.
You can get the connection string of the device writing this on the CLI:
az iot hub device-identity show-connection-string --hub-name IoTHubName --device-id DeviceName --output table
Even in this case, IoTHubName and DeviceName are the name of your IoT hub and the name of the device previously created.
You can create a consumer group writing this on the CLI:
az iot hub consumer-group create --hub-name YourIoTHubName --name YourConsumerGroupName
YourIoTHubName is the name of your IoT hub and YourConsumerGroupName is the name of the consumer group you want to create.
You can obtain the connection string of your IoT hub writing on the CLI:
az iot hub show-connection-string --hub-name YourIotHub --policy-name service
where YourIotHub is the name of your IoT hub.
Finally you can add these values to environment variables setting in the command line of your pc:
set IotHubConnectionString=YourIoTHubConnectionString
set EventHubConsumerGroup=YourConsumerGroupName
set IOTHUB_DEVICE_CONNECTION_STRING=YourDeviceConnectionString
Where YourIoTHubConnectionString is the connection string of your IoT hub, YourConsumerGroupName is the consumer group previously created and YourDeviceConnectionString is the connection string of the device. These variables will be later used in the python program and in the nodejs application.
- Python
Now you can create your python program that generates random values and publish messages to the IoT hub containing them. You can write your program from scratch or you can use one of these as a guideline. Whatever you choose, you will need the connection string of the device to the hub and best practice is to set it as an environment variable. Of course you can set it as a string in the debugging mode, but explicitly writing your connection string is not safe.
In my program the values randomly generated are temperature, humidity, wind direction, wind intensity and rain height. They are created and published to the hub in an unique JSON string every 4 seconds. At every run the device that publishes the message is randomly selected among two.
- Nodejs
At this point you have an IoT hub already set and a program that publishes messages on it. This nodejs application allows you to visualize the data received in different charts. You can download the default app from here and adapt it to your problem or create one from scratch.
I have modified chart-device-data.js file so that I have a different chart for every typology of values that I send and I have added these charts in index.html file. Server.js file, instead, is the most important one because it connects to the IoT hub, calls the function that reads the messages arrived at the hub and reorganize them. It needs the IoT hub connection string and the consumer group previously obtained and, even in this case, best practice is to set them as environment variables.
Open the command line of your pc in the folder containing the application to install nodejs:
npm install
Run the app writing in the command line of the pc:
npm start
Then the same console will show the messages received by the hub.
Opening http://localhost:3000/
in the browser, you can see 5 charts, one for the different type of values that you send to the IoT hub.
In these charts the time when the message was received is on the x axis and the type of the value that we send on the y axis. Here I have humidity from 0% to 100%, rain height from 0 mm to 50 mm, temperature from -50°C to 50°C, wind direction from 0° to 360° and wind intensity from 0 m/s to 100 m/s. You can see the details of the value and the time when passing the mouse on a point on the chart.
ResourcesCreate an IoT hub using Azure portal
Communicate with your IoT hub using MQTT protocol
Visualize real-time sensor data from your Azure IoT hub
Comments
Please log in or sign up to comment.