In this project we're going to learn how easy is to send commands from Meadow.Cloud to a Meadow-powered Project Lab to toggle a Seeed Studio's four channel relay module connected via Qwik connector.
Project Lab is the most functional IoT prototyping platform on the planet. No more breadboards, complicated wiring, or soldering. Project Lab was built from the ground up using the industry's most powerful, capable, and reliable sensors, components, and connectors.
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 - Provision your Project Lab to Meadow.CloudSince we're sending telemetry data to Meadow Cloud, you'll need to create an account and provision your Project Lab.
Step 2 - Create a Meadow Application projectCreate a new Meadow Application project in Visual Studio 2022 for Windows or macOS and name it Meadow.Cloud_Command.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
- Meadow.ProjectLab
- Meadow.Foundation.Grove.Relays.4ChannelSpdtRelay
- Meadow.Foundation.Graphics.MicroLayout
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:
# Main Device Config
Device:
# Name of the device on the network.
Name: MeadowDevice
# 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.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:
# Network Interface (Default value is WiFi with DHCP)
Interfaces:
- Name: WiFi
UseDHCP: true
IPAddress:
NetMask:
Gateway:
# Default Interface
DefaultInterface: Wifi
# Automatically attempt to get the time at startup?
GetNetworkTimeAtStartup: true
# Time synchronization period in seconds.
NtpRefreshPeriod: 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: YourSSID
Password: SSIDPassword
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.
Lifecycle:
RestartOnAppFailure: false
Logging:
LogLevel:
Default: Trace
MeadowCloud:
Enabled: true
Step 6 - Write the code for Meadow.Cloud_LoggingThis project has the following structure:
Lets briefly go over every file:
- IMeadowCloudCommandHardware- The intention of this interface is to define what components we're using for this project, which in this case is a display, an BME68x environmental sensor and an RGB LED, which they're all onboard on a Project Lab.
- MeadowCloudCommandHardware- This is a particular definition of
IMeadowCloudLoggingHardware
where we declare and create a Project Lab instance, and initialize every component defined. - 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. - ToggleRelayCommand - Class that extends from
IMeadowCommand
, used to define the type of data we're expecting from a command. In this case, an integerRelay
to refer as the index, and aIsOn
boolean to toggle the relay on/off. - MainController - This class is the main business logic that has access to a hardware object and all sub-controllers (like the DisplayController in this case). In the
Initialize
method, notice the logic to register a Cloud Logger service, which is then used in theEnvironmentalSensorUpdated
event handler whenever the environmental sensor does a read, it usescloudLogger.LogEvent()
method to sendtemperature
,pressure
andhumidity
values to Meadow.Cloud - MeadowApp - In the app's main class, we create a hardware object (
MeadowCloudLoggingHardware
in this case), we also take the WiFi network adapters and both are passed to the MainController when creating an object, and finally call theInitialize
andRun
methods respectively.
What's left is to run you Meadow app, and wait for it to log into Meadow.Cloud and the final output you should see are event subscriptions, which are the ones that will be triggered whenever we send a Command from Meadow.Cloud
Updater State -> Connecting
Connecting MQTT client
MQTT connected
Updater State -> Connected
Updater State -> Idle
Update service subscribing to 'd82f8ec7243a461581c2544070209fbd/ota/23-00-3D-00-15-50-33-4D-35-34-33-20'
Update service subscribing to 'd82f8ec7243a461581c2544070209fbd/commands/23-00-3D-00-15-50-33-4D-35-34-33-20'
Once you see a similar output, head over to Meadow.Cloud and in the Devices section, pick the device that's provisioned and running this project, and click Send Command:
Now, in the Send a Command dialog window, enter name and payload like below:
Click send and your Project Lab should get the command and toggle the corresponding relay and updating the virtual relay status on the screen.
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