M5Stack is a modular, stackable, and portable device which is powered with an ESP-32 core. And .NET nanoFramework image can be loaded in the board to enable the writing of managed code applications.
In this sample M5Stack Gray will be used but same sample code can run on any ESP32 and any other nanoFramework supported devices.
This sample application includes a scenario: Every 3 seconds application reads temperature value from BMP280 sensor and turns on LED after reading the value. Then this temperature value is pushed to IoT Hub with device name and after every successfull push LED will turn off. Data will be handled by IoT Hub message routing and will be pushed to blob storage to store.
Note: this sample requires a good understanding on how certificate authentication works. And how to set them up properly in Azure IoT.
The sample is located here.
- Getting Started Guide for managed code (C#)
- List of suported devices/sensors by NanoFramework
- nanoFramework Samples
- M5Stack with nanoFramework installed
- BMP280 Pressure/Temperature sensor breakout
- Jumper wires
- 5 mm LED
- 330 Ω resistor
- Breadboard (optional)
Use the hardware components to build the circuit as depicted in the following diagram:
The following are the connections from the M5Stack to the BME280 breakout:
- 3.3V to VIN OR 3V3 (shown in red)
- Ground to GND (grey)
- SDA (GPIO 21 for ESP32) to SDI (blue)
- SCL (GPIO 22 for ESP32) to SCK (yellow)
- GPIO 18 to LED anode (longer, positive lead)
- LED cathode (shorter, negative lead) to 330 Ω resistor (either end)
- 330 Ω resistor (other end) to ground
Use your Azure Subscription for following steps:
-
Make sure your Pins are correctly connected.
-
Create an IoT Hub using your Azure Subscription, you can use free tier of IoT Hub. You'll need IoT Hub name in the next steps in your code.
-
Create certificate using OpenSLL following Create test certificate using OpenSSL documentation. Don't forget to change
raspberry-pi
to a unique name to your m5stack device in the tutorial commands. -
If you followed above tutorial, you'll already have your device, if not create a device in your IoT Hub
-
Provide your IoT Hub details,
Device ID
andIoT Hub Name
intoProgram.cs
. Open your solution with Microsoft Visual Studio 2019.const string DeviceID = "<replace-with-your-device-id>"; const string IotBrokerAddress = "<replace-with-your-iot-hub-name>.azure-devices.net";
-
Provide your
PEM
certificate,Private Key
andAzure Root CA
in string formats intoProgram.cs
. You can access these values from "Step 3 - Create test certificate using OpenSSL"/ X509Certificate certificates in PEM format and key const string cert = @"-----BEGIN CERTIFICATE----- <replace-with-your-pem-certificate-value> -----END CERTIFICATE-----"; const string privateKey = @"-----BEGIN ENCRYPTED PRIVATE KEY----- <replace-with-your-encrypted-private-key-value> -----END ENCRYPTED PRIVATE KEY-----"; const string rootCA = @"-----BEGIN CERTIFICATE----- <replace-with-your-azurePEMCertBaltimore-value> -----END CERTIFICATE-----";
-
Enter your Wifi Settings in the
Program.cs
fileconst string MySsid = "<replace-with-valid-ssid"; const string MyPassword = "<replace-with-valid-password>";
- Go to sample and Double-click the Visual Studio Solution (.sln) file.
- Press
Ctrl+Shift+B
, or select Build > Build Solution. - Make sure to click on device explorer and select your device.
The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.
- Select Build > Deploy Solution.
- To debug the sample and then run it, press F5 or select Debug > Start Debugging.
If you enable Message routing with Custom endpoint, you can save all inputs into an Azure Blob Storage
-
First Create a route
-
Create a custom endpoint to connect to your blob storage data will be save in below format:
{iothub}/{partition}/{YYYY}/{MM}/{DD}/{HH}/{mm}
Once connection is succesfull you can see the status as healty -
Blob storage contains all data from IoT Hub in a container
and single JSON file structure looks like below:
{
"EnqueuedTimeUtc": "2021-09-09T13:12:18.3950000Z",
"Properties": {},
"SystemProperties": {
"connectionDeviceId": "m5stack",
"connectionAuthMethod": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
"connectionDeviceGenerationId": "637667231818779957",
"contentType": "application/json",
"contentEncoding": "",
"enqueuedTime": "2021-09-09T13:12:18.3950000Z"
},
"Body": "eyJUZW1wZXJhdHVyZSI6MjcuNTYsIlByZXNzdXJlIjoxMDA2LjY5LCJEZXZpY2VJRCI6Im01c3RhY2sifQ=="
}
Body message is encoded with Base64 format, if you decode the value, you'll retrieve the message we sent to Azure IoT Hub:
{
"Temperature": 27.56,
"Pressure": 1006.69,
"DeviceID": "m5stack"
}