(Project updated to v1.1 (released on August 1st, 2023))
This project shows you how to connect send environmental data from your ProjectLab to Azure using IoT Hub!
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.
Before you dive in this project, ensure that your Meadow board and development environment is fully up to date. Check the Release Notes section to double check.
Step 1 - Create a Meadow Application projectCreate a new Meadow Application project in Visual Studio 2022 for Windows or macOS and name it MeadowAzureIoTHub.
Step 2 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Add Image AssetsAdd 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 Project Lab's environmental sensor via AQMP.
- IoTHubMqttController - This class is used to establish a connection with Azure IoT Hub and send the Environmental readings by Project Lab's environmental sensor via MQTT.
- MainController - This main class initializes the IoT Hub controllers, display controller to draw the HMI screen, and Project Lab's BME688 sensor which does a reading every 15 seconds and sends data to Azure.
- MeadowApp - This main class instantiates the MainController class and calls its corresponding
Initialize
andRun
methods. - 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, Pressure - 1020.90879412913 ***
Create payload
Create message
Send message
*** DATA SENT - Temperature - 28.0586979364511, Humidity - 29.1871445300884, Pressure - 1020.8916192437 ***
Create payload
Create message
Send message
*** DATA SENT - Temperature - 28.179768034583, Humidity - 28.9781536413303, Pressure - 1020.90573418024 ***
...
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.
- 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
Please log in or sign up to comment.