This tutorial will use M5Core2 to obtain the ENV Unit (temperature and humidity, air pressure sensor) value and upload it to AWS-IoT-Core. Give an example of how to use Arduino programming to connect the device to the AWS cloud service platform to subscribe and publish data.
Before going to the next step, you need to register a new device via AWS Management Console click here to view..... .
Click AWS services
->IoT Core
to enter the AWS IoT management page
Click Manage
->Things
->Create things
to create a new device
Set up a device according to your needs, here will demonstrate the most basic single thing
, for more details on device types, please visit:
AWS Official Document
Enter the device name, Configuration and classification for different sets of devices, click Next
->Set up DeviceCertificate
->Device Configuration Strategy
->Finish setting up
After Setting up your device, the download page of Key
and Certificate
will pop up. (Note: This page will be shown once)please follow the prompts to download Device certificate (Device certificate)
+Device public key (Public Key File) )
+Private Key File
. (These files will be used for verification in subsequent operations)
In this tutorial, we will use M5Core2 to obtain the ENV II Unit (temperature and humidity, air pressure sensor) value and upload it to AWS-IoT-Core. Before starting programming, we need to download the AWS-IoT
related dependency library and case program from Github
, you can download it directly through the following git
command (or click the link below to visit the corresponding project address to download the zip archive, download It needs to be decompressed later). And place the dependent library to the Arduino library management path (usually C:/Users/YourUserName/Documents/Arduino/libraries
)
git clone git@github.com:aws-samples/aws-iot-esp32-arduino-examples.git
git clone git@github.com:bblanchon/ArduinoJson.git
git clone git@github.com:256dpi/arduino-mqtt.git
git clone git@github.com:adafruit/Adafruit_BMP280_Library.git
git clone git@github.com:adafruit/Adafruit_Sensor.git
git clone git@github.com:m5stack/UNIT_ENV.git
In addition to the previously downloaded dependent libraries, you can also introduce different driver libraries according to the type of M5 device you are using. In this example, we will introduce M5Core2.h (applicable to CORE2 and CORE2 FOR AWS products). Other libraries related to M5Stack products can be found in the official M5Stack Github .
git clone git@github.com:m5stack/M5Core2.git
We can copy the basic-pubsub
example program from the aws-iot-esp32-arduino-examples
folder as a basic template for the program.
We usually create a secrets.h
in the project to store keys and certificates
and WIFI
information, which contains the following content.
Reminder:
1. The macro definition THINGNAME
is the name of the device created by us, which needs to be consistent with the name in the AWS Management Console
.
2. Put us in AWS Open the key and certificate obtained by creating the device in IoT Core
with an editor, and copy and paste the content to the corresponding position in the code.
Copy the Endpoint
field in AWS Management Console
->Settings
#include <pgmspace.h>
#define SECRET
#define THINGNAME "ENV-TEST"
const char WIFI_SSID[] = "WIFI_SSID";
const char WIFI_PASSWORD[] = "WIFI_PASSWORD";
const char AWS_IOT_ENDPOINT[] = "xxxxx.amazonaws.com";
// Amazon Root CA 1
static const char AWS_CERT_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgIT........................
-----END CERTIFICATE-----
)EOF";
// Device Certificate
static const char AWS_CERT_CRT[] PROGMEM = R"KEY(
-----BEGIN CERTIFICATE-----
MIIDWjCCAkKgAwIBAgIVA........................
-----END CERTIFICATE-----
)KEY";
// Device Private Key
static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY(
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAq........................
-----END RSA PRIVATE KEY-----
)KEY";
Refer to the program below, and use the Arduino IDE to compile and upload it to the device.
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include <M5Core2.h>
#include "Adafruit_Sensor.h"
#include "Adafruit_BMP280.h"
#include "SHT3X.h"
SHT3X sht30;
Adafruit_BMP280 bme;
float tmp = 0.0;
float hum = 0.0;
float pressure = 0.0;
// The MQTT topics that this device should publish/subscribe
#define AWS_IOT_PUBLISH_TOPIC "core2/env"
#define AWS_IOT_SUBSCRIBE_TOPIC "core2/msg"
WiFiClientSecure net = WiFiClientSecure();
MQTTClient client = MQTTClient(256);
void messageHandler(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}
void connectAWS()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
// Configure WiFiClientSecure to use the AWS IoT device credentials
net.setCACert(AWS_CERT_CA);
net.setCertificate(AWS_CERT_CRT);
net.setPrivateKey(AWS_CERT_PRIVATE);
// Connect to the MQTT broker on the AWS endpoint we defined earlier
client.begin(AWS_IOT_ENDPOINT, 8883, net);
// Create a message handler
client.onMessage(messageHandler);
Serial.print("Connecting to AWS IOT");
while (!client.connect(THINGNAME)) {
Serial.print(".");
delay(100);
}
if(!client.connected()){
Serial.println("AWS IoT Timeout!");
return;
}
// Subscribe to a topic
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
Serial.println("AWS IoT Connected!");
}
void publishMessage()
{
pressure = bme.readPressure();
if(sht30.get()==0){
tmp = sht30.cTemp;
hum = sht30.humidity;
}
Serial.printf("Temperatura: %2.2f*C Humedad: %0.2f%% Pressure: %0.2fPa\r\n", tmp, hum, pressure);
delay(100);
StaticJsonDocument<200> doc;
doc["tmp"] = tmp;
doc["hum"] = hum;
doc["pressure"] = pressure;
char jsonBuffer[512];
serializeJson(doc, jsonBuffer); // print to client
client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}
void setup() {
M5.begin();
Wire.begin();
bme.begin(0x76);
connectAWS();
}
void loop() {
publishMessage();
client.loop();
delay(1000);
}
Click AWS Management Console
->Test
to enter the online test page. This function is used to test the MQTT message of the device in the AWS account, click Additional configuration
to adjust the QoS level.
Publish topic: fill in Topic
and Message payload
, click Publish
.
Subscribe topic: fill in Topic
, click Subcribe
. and Click Additional configuration
to set the type of message displayed, and all received message will be displayed on the console at the bottom of the page.