Stationary sensors only measure the environmental data on one specific location. So if you want to achieve the environmental monitoring of a whole city, you need a lot of sensors, which is expensive, complicated in maintenance and, as mentioned above, only senses the environmental data of one location in the city.
The goal of the project is it to create a cheap mobile environmental monitoring system which can be mounted on a backpack, e-scooter, bike or even an EV, so you can measure the environment of a whole city with just a couple of sensors. You can easily track the environmental data such as humidity, but also the traffic, LoRaWAN coverage and more. The Seeed Grove system enables the users to change the sensors to their needs.
Prerequisites1. Install Visual Studio Code (VS code) from here
2. Create an Azure Account (more information here)
3. Create an The Things Network (TTN) account here
Cloud ArchitectureThe system is hosted in the Azure cloud, so it could be scaled up any time and I don't have to worry about uptime or any system updates.
On the left side of the above diagram you can see the gadgetry of IoT. The sensors send data via LoRa to the gateways nearby. LoRa is great for sending small data over great distances up to tenth of kilometers.
The gateways transfer the data to The Things Network (TTN), which offers a free public service. You can check the availability of gateways in your area via the TTN mapper website.
TTN offers an Azure IoT Hub integration which syncs the device, status und messages with the hub. This is great, because we want to manage our device in the Azure IoT Hub. You can find more information about the Azure IoT Hub integration here. There is also a great blog article here.
The Azure IoT Hub is a powerful tool to manage lots of IoT devices. It is the gateway to the Azure Cloud. It offers many possibilities to manage devices, their digital twins etc. You can connect via different protocols like MQTT or AMQP. Here you can find more information on that. The Azure IoT Hub gives us the opportunity to use different devices and services. So in the future we don't have to worry about how to connect devices when we want to enable devices from the Helium network or use devices which connect over NB-IoT.
The Azure IoT Hub uses an Azure Event Hub Namespace to publish device telemetry messages, twin updates and lifecycle events.
Twin updates and lifecycle events are getting processed by a function which updates the device on the TTN.
Telemetry messages are getting saved to the Azure SQL Database, which can ingest millions of events per second (Blog article).
Now we reach the right side of the above diagramm. A static website gets data from Database with the help of an Azure Function, which uses an HTTP trigger. Azure Maps serves the map tiles and helps us to visualise the data on the map.
Azure ServicesThe project needs some services to run, in the end should similar to this:
The Azure SQL Database saves the telemetry data.
For the current state of the project the standard S0 tier is enough to process the data.
Don't forget to allow the other Azure services to connect to the database server and to add your IP as an exception to the firewall rules.
The database scheme can be found in the repository. You can use Azure Data Studio to connect to the database and apply the scheme.
Save the connection string to the database for the Azure functions later.
Azure Event Hubs NamespaceIn the Azure Event Hubs Namespace two namespaces have to be created:
events
The Event Hub events has a partition count of 4 and a message retention of 7 days.
telemetry
The Event Hub telemetry has a partition count of 4 and a message retention of 7 days.
Save the connection string for the Azure Functions for later.
Azure Function - TTN EventsThe function code is in the repository and can be deployed via VS Code.
The following environment variables have to be set:
EVENTHUB_CONNECTION_STRING = YourEventHubConnectionString
STACK_API_KEY = YourSuperSecretKey
STACK_APPLICATION_ID = YourApplicationId
STACK_BASE_URL = https://eu1.cloud.thethings.network/api/v3
The stack variables can be found later when we enable the Azure IoT Hub integration in TTN.
Azure Function - Telemetry to DBThis function can also be found in the repository and can also be deployed via VS code.
The following environment variables have to be set:
SQLDBConnectionString = YourSqlConnectionString
EventHubConnectionAppSetting = YourEventHubConnectionString
Azure Function - Http TriggerThis function can also be found in the repository and can also be deployed via VS code.
The following environment variables have to be set:
SQLDBConnectionString = YourSqlConnectionString
In the CORS settings allow the frontend's domain.
Azure IoT HubAn Azure IoT Hub with the S1 standard tier should be enough for the beginning.
Two custom endpoints have to be defined:
StackEvents
Endpoint StackEvents to Event Hubs Namespace. Sends to event hub namespace instance events, uses Key-based-Authentication.
TelemetryEvents
Endpoint TelemetryEvents to Event Hubs Namespace. Sends to event hub namespace instance telemetry, uses Key-based-Authentication.
Also three routes have to be defined:
TTSTwinChangeEvents
Route TTSTwinChangeEvents sends Device Twin Change Events to endpoint StackEvents, using the routing query IS_OBJECT($body.properties.desired) OR IS_OBJECT($body.tags)
.
TTSDeviceLifecycleEvents
Route TTSDeviceLifecycleEvents sends Device Lifecycle Events to endpoint StackEvents, using the routing query true
.
NewTelemetryEvents
Route TTSDeviceLifecycleEvents sends Device Telemetry Messages to endpoint TelemetryEvents, using the routing query true
.
Set the allowed origins (CORS) to your frontend's domain.
Save the primary key for the frontend.
Azure Static WebsiteChange the primary key for Azure Maps and the url to the HttpTrigger function.
You can deploy the page from GitHub. See here for more information.
The Things Network - TTNCreate a new application, add the decode from the repository and create a new sensor as described here.
Make sure that the new device uses the application payload formatter.
Add the Azure IoT Hub integration and set the missing environmental variables for the TTN Events functions.
Create a device and remember / write down the DevEUI, AppEUI and the AppKey, we will need it later for the firmware.
HardwareHardware - Wio Terminal AdapterI designed a custom adapter for the Wio Terminal to add more sensors to the terminal. You can find the files at the end of this article.
It can be easily soldered and gives access to I2C0, I2C1, D0 & D1, D2 & D3, D4 & D5, D6 & D7, D8 and TX & RX pins.
Hardware - CasePrint the case and add the sensors as seen in the pictures. The cases use zip ties, which can easily be replaced and make it easy to mount the sensors and manage cables. For more information how to create tunnels for zip ties see this video.
Connect the I2C sensors to I2C1, the LoRa module to D0 & D1 and the GPS module to D2 & D3.
FirmwareThe firmware, which can be found in the repository, uses the Arduino platform, which makes a fast development easy.
Change the DevEUI, AppEUI and the AppKey in the platform.ini
To deploy the firmware, use VS Code with the PlatformIO extension.
The display uses the lvgl library, which makes it super easy to create a nice ui. To make it even more simple I used Squareline Studio to create the ui and generate the code. You can use it for free in non-commercial projects.
FrontendThe frontend shows the latest data. I've implemented an example to show the pollution of pm 2.5 cf1 particles. Azure Maps makes it really easy to display the information.
You can try the frontend website at https://frontend.urbaniot.de/ and have a look for yourself!
FutureThe project shows how simple and cheap these sensors can be build and it is sustainable because of the cloud architecture and the grove sensor system.
In the future, other sensors like the Seeed Vision AI Module could be added to the system to track traffic signs or the amount of dogs you meet so you know where you can probably pet some dogs in your city.
Comments