This article shows how to implement the sending through MQTT of randomly generated telemetry data (temperature, humidity, wind direction, wind intensity, rain height) to a cloud-based backend (Azure IoT) to store it, and a possible implementation of a web service to operate query on stored data and display it.
Github repository: https://github.com/gabr-iele/DevToAzureTelemetry
What is needed:- An Azure IoT Hub
- An Azure Storage account
- An Azure Function App
The following parameters characterize the behavior of the application:
- NUM_STATIONS: the number of stations publishing their rilevations
- DELAY_TIME: time occurring between two publications
- MIN_PARAM, MAX_PARAM: the minimum and maximum values that parameters can assume
First thing to do is to assing a name to our program and use it to registrate a new IoT Device in the Hub. Then the following informations have to be retrieved from the Hub:
- Hostname: homework1.azure-devices.net
- Device ID: the name given before
- Shared Access Key
The device ID is used to create a MQTT client.
Then the application generates a Shared Access Signature from the key (https://docs.microsoft.com/en-us/azure/event-hubs/authenticate-shared-access-signature). The SAS will be used as password for broker authentication when publishing messages.
Finally, to connect through MQTT on port 8883, the client must connect itself through TLS/SSL; it is then necessary to authenticate to Azure through DigiCert Baltimore Root Certificate, the certificate used by Azure to protect connections. After retrieving and setting the certificate, the client can finally connect to Azure through the hostname and start publishing data relative to parameters and virtual environmental stations.
The topics at which the device will publish and subscribe are so composed:
- Topic to publish: devices/device_id/messages/events/
- Topic to subscribe: devices/device_id/messages/devicebound/#
The next step is sending the values received by the Hub to the storage account we created before, in order to save them and implement some persistence that will be useful to operate queries on received information. In particular, the Table service by Azure storage is employed.
This can be achieved through writing some logic with the Function App service; to configure the App in order to link Hub and storage we need to retrieve some connection strings from the Hub and create a consumer group for our device within the storage. A precise description can be found here.
The function will be called every time a message is received by the hub, and will envelop all the info into a JSON object, representing the new entry of the table.
Visualize data in a web siteThe web site displaying the received values is pretty simple: the user inputs an environmental station for the first query or a sensor for the second one, then the scripts operate a call to the REST APIs given for the Azure Table service.
Before this can be done, the scripts need to know the shared access key of the storage to have their requests authorized by the server. The key will be used to generate a signature, which will be verified by the server once a request arrives. Here is explained how the signature is built by the key.
Then the result is retrieved by the response of the server and displayed into two tables, each one showing the result of a query.
- Save IoT Hub messages to Table storage: https://tlaothong.gitbooks.io/azure-iot-workshop/content/iot-hub-store-data-in-azure-table-storage.html
- Authenticate access to Event Hubs resources using shared access signatures: https://docs.microsoft.com/en-us/azure/event-hubs/authenticate-shared-access-signature
- Query table and entities of Azure Table: https://docs.microsoft.com/en-us/rest/api/storageservices/querying-tables-and-entities
- Communicate to IoT Hub with MQTT protocol: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly
- paho-mqtt: https://pypi.org/project/paho-mqtt/
Comments
Please log in or sign up to comment.