pdf-icon

Arduino Integration with AWS

Overview

This tutorial will use the M5Core2 to obtain values from the ENV Unit (temperature, humidity, and pressure sensor) and upload them to AWS IoT Core. It exemplifies how to program a device to connect to the AWS cloud service platform, enabling data subscription and publication.

Create a Device

Before connecting, you must first register a new device through the AWS Management Console. Click here to access the AWS Management Console , and log in after creating an account.

Click AWS services -> IoT Core to enter the AWS IoT management page.

Click Manage -> Things -> Create things to create a new device.

Create your device according to your needs. This example demonstrates creating a basic single thing. For more details on device types, refer to the AWS official documentation .

Enter the device name, configure the device's group and category, then click Next -> create certificate -> configure device policy -> finish creation.

Upon completing the device creation, a page for downloading keys and certificates will pop up. Note: This page will only appear once. Please follow the prompt to download the Device certificate, Public Key File, and Private Key File to your local machine. (These files will be used for communication authentication in subsequent steps.)

Install Dependencies and Example Libraries

In this tutorial, we will use M5Core2 to obtain values from the ENV II Unit (temperature, humidity, and pressure sensor) and upload them to AWS-IoT-Core. Before beginning programming, we need to download AWS-IoT related dependency libraries and example programs from Github. You can directly download using the below git commands (or click the provided link to visit the corresponding project address and download the zip package, which will need to be unzipped). Place the dependency libraries in the Arduino library management path (usually C:/Users/YourUserName/Documents/Arduino/libraries).

Github

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

Besides the previously downloaded libraries, you may also include different driver libraries based on the M5 device you are using. In this instance, we will incorporate M5Core2.h (suitable for CORE2 and CORE2 FOR AWS products). Other libraries related to M5Stack products can be found on the M5Stack official Github .

git clone git@github.com:m5stack/M5Core2.git

Configure the Project

We can copy the basic-pubsub example program from the aws-iot-esp32-arduino-examples folder as a basic template for our program.

Configure Certificates

We usually create a secrets.h in the project to store keys and certificates, WIFI information, which includes the following content.

**

Tips:**
1. Define the macro THINGNAME as the name of our created device, which needs to match the name in the AWS Management Console.
2. Open the keys and certificates obtained from creating the device in AWS IoT Core with an editor, and copy and paste the content into the corresponding positions in the code.

Copy the Endpoint field from 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";

Edit the Program

Refer to the following program 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("Temperature: %2.2f*C  Humidity: %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);
}

Subscribe and Publish Messages

Click AWS Management Console -> Test to enter the online test page. This feature is used to test MQTT messages of devices in your AWS account. Click Additional configuration to adjust the QoS level.

To publish a topic: Fill in the Topic and `Message payload

, then click Publish` to publish.

To subscribe to a topic: Fill in the Topic and click Subscribe. Click Additional configuration to set the type of messages displayed; received messages will be shown in the console below the page.

On This Page