本教程将使用M5Core2举例说明如何通过Arduino编程,使设备接入AWS IoT Core平台,实现订阅和发布数据。
接入前,需要先通过AWS Management Console注册新设备。 点击此处访问AWS Management Console , 完成账号创建后进行登录。
参考下方流程进行设备创建, 详情操作可参考 AWS官网文档 。
在完成设备创建后将会弹出密钥
与证书
的下载页面, 请根据提示将所有证书下载至本地保存。其中设备证书(Device certificate)
+设备私钥(Private Key File)
+CA根证书(Amazon Root CA 1)
,在后续的操作中将会用于验证使用。
该教程中,我们将使用M5Core2触摸按键控制发布数据至AWS-IoT-Core。 开始编程前,我们需要从Github
下载AWS-IoT
相关依赖库与案例程序, 你可以通过下面git
指令直接下载(或是点击下方链接访问对应的项目地址下载zip压缩包, 下载后需进行解压)。并将依赖库放置到Arduino库管理路径(通常为C:/Users/YourUserName/Documents/Arduino/libraries
)
git clone https://github.com/m5stack/M5Unified.git
git clone https://github.com/m5stack/M5GFX.git
git clone https://github.com/aws-samples/aws-iot-esp32-arduino-examples.git
git clone https://github.com/bblanchon/ArduinoJson.git
git clone https://github.com/256dpi/arduino-mqtt.git
我们可以从aws-iot-esp32-arduino-examples
文件夹中,复制basic-pubsub
案例程序, 作为程序的一个基础模板。项目中的secrets.h
文件用于存放密钥与证书
、WIFI
信息。
THINGNAME
为我们的创建的设备名称,需要与AWS Management Console
中的名称保持一致.AWS IoT Core
中创建设备获取的密钥与证书用编辑器打开,并复制粘贴内容到代码中的对应位置。AWS Management Console
->Settings
中的Endpoint
字段#include <pgmspace.h>
#define SECRET
#define THINGNAME "THINGNAME"
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";
参考下方程序, 并使用Arduino IDE将其编译上传至设备。完成上传后, 点击触摸设备按键A即可向AWS IoT Core发布数据, 当有收到新的下行数据时也将进行显示。
#include <M5Unified.h>
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
// The MQTT topics that this device should publish/subscribe
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"
WiFiClientSecure net = WiFiClientSecure();
MQTTClient client = MQTTClient(256);
void messageHandler(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
const char *message = doc["message"];
M5.Lcd.clear();
M5.Lcd.setCursor(0, 0, 2);
M5.Lcd.setTextColor(TFT_BLUE);
M5.Lcd.setTextFont(2);
M5.Lcd.println(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!");
M5.Lcd.println("AWS IoT Connected!");
}
void publishMessage() {
StaticJsonDocument<200> doc;
doc["time"] = millis();
doc["message"] = "Hello World";
char jsonBuffer[512];
serializeJson(doc, jsonBuffer); // print to client
client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}
void setup() {
M5.begin();
}
void loop() {
M5.update();
if (M5.BtnA.wasClicked()) {
Serial.println("Button pressed");
publishMessage();
Serial.println("Publishing");
}
client.loop();
}
点击AWS Management Console->Test进入在线测试页面。该功能用于测试AWS账户中设备的MQTT消息, 点击Additional configuration可调整QoS等级。