This project shows you how to connect send environmental data from your Meadow to Azure using IoT Hub! It will use a ST7789 TFT SPI display to give user feedback and gather room temperature/humidity with a HTU21D I2C sensor by Adafruit, both drivers available in Meadow.Foundation!
Meadow.Foundation a platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LED project to properly set up your development environment.
Step 1 - Assemble the circuitWire your the display and environmental sensor like the following diagram:
Create a new Meadow Application project in Visual Studio 2022 for Windows or macOS and name it MeadowAzureIoTHub.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
- Meadow.F7
- Meadow.AmqpNetLite
- Meadow.Foundation
- Meadow.Foundation.Graphics.MicroLayout
- Meadow.Foundation.Sensors.Atmospheric.Htux1d
- Meadow.Foundation.Displays.TftSpi
Add a Resources folder and add the following image assets.
Select all these images and set the Build Action to Embedded Resource.
Step 5 - Setup Configuration FilesUpdate the meadow.config.yaml
file below:
# Uncommented these options as needed.
# To learn more about these config options, check out the OS & Device Configuration documentation.
# http://developer.wildernesslabs.co/Meadow/Meadow.OS/Configuration/OS_Device_Configuration/
Device:
# Name of the device on the network.
Name: MeadowAzureIoTHub
# Uncomment if SD card hardware present on this hardware (e.g., Core-Compute module with SD add-on)? Optional; default value is `false`.
SdStorageSupported: true
# Control how the ESP coprocessor will start and operate.
Coprocessor:
# Should the ESP32 automatically attempt to connect to an access point at startup?
# If set to true, wifi.config.yaml credentials must be stored in the device.
AutomaticallyStartNetwork: true
# Should the ESP32 automatically reconnect to the configured access point?
AutomaticallyReconnect: true
# Maximum number of retry attempts for connections etc. before an error code is returned.
MaximumRetryCount: 7
# Network configuration.
Network:
Interfaces:
# - Name: Ethernet
# UseDHCP: false
# IPAddress: 192.168.1.60
# NetMask: 255.255.255.0
# Gateway: 192.168.1.254
- Name: WiFi
UseDHCP: true
IPAddress:
NetMask:
Gateway:
# Which interface should be used?
DefaultInterface: WiFi
# Automatically attempt to get the time at startup?
GetNetworkTimeAtStartup: true
# Time synchronization period in seconds.
NtpRefreshPeriodSeconds: 600
# Name of the NTP servers.
NtpServers:
- 0.pool.ntp.org
- 1.pool.ntp.org
- 2.pool.ntp.org
- 3.pool.ntp.org
# IP addresses of the DNS servers.
DnsServers:
- 1.1.1.1
- 8.8.8.8
These
settings enable the Project Lab to connect to WiFi when powered on and when connected, automatically get date and time from an NTP server.
Update wifi.config.yaml
uncommenting the WiFi fields and enter your SSID and password credentials:
Credentials:
Ssid: Ssid
Password: Password
Its recommended to set the Log level to Trace in the app.config.yaml
file to keep an eye on the console output for when the board is logging in to Meadow.Cloud and sending event logs successfully.
# Adjust the level of logging detail.
Logging:
LogLevel:
Default: Trace
Step 6 - Write the code for MeadowAzureIoTHubThis project has the following structure:
Lets briefly go over every file:
- DisplayController - This class does all the drawing of the HMI screen on the display. We use MicroLayouts, so you can see how we can use Label to show text, Picture to draw images like the sync and WiFi icons, Box to draw colored rectangles for backgrounds, etc. You can read more on MicroLayouts on our docs.
- IIoTHubController - This interface defines the methods used to initialize and send sensor data to Azure IoT Hub (in this can either be via AQMP or MQTT)
- IoTHubAmqpController - This class is used to establish a connection with Azure IoT Hub and send the Environmental readings by the HTU21D sensor via AQMP.
- IoTHubMqttController - This class is used to establish a connection with Azure IoT Hub and send the Environmental readings by the HTU21D sensor via MQTT.
- MainController - This main class initializes the IoT Hub controllers, display controller to draw the HMI screen, and the HTU21D sensor which does a reading every 15 seconds and sends data to Azure.
- MeadowApp - This main class Initializes both the ST7789 display and the STU21D atmospheric sensor, and it will start reading the room temperature/humidity every 15 seconds once Meadow joins the WiFi network and stablishes connection with Azure.
- Secrets - In this class you'll need to set the appropriate values depending on your Azure IoT Hub configuration. You can follow this step by step blog post that shows you how to do that.
Click the Run button in Visual Studio. It should look like to the following GIF:
When the app is running, you should have an output similar to this:
Create connection factory...
Create connection ...
Create session ...
Create SenderLink ...
Create payload
Create message
Send message
*** DATA SENT - Temperature - 27.9073605400976, Humidity - 29.4871487326418 ***
Create payload
Create message
Send message
*** DATA SENT - Temperature - 28.0586979364511, Humidity - 29.1871445300884 ***
Create payload
Create message
Send message
*** DATA SENT - Temperature - 28.179768034583, Humidity - 28.9781536413303 ***
...
This ensures you've establish connection to Azure and the app is sending data to IoT Hub every 15 seconds.
You can also go to your Azure portal, open the IoT Hub overview section and see the number of incoming messages increasing overtime.
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments