M5Stack Core2: AWS IOT の MQTT 受信

こちらのプログラムを参考にしました。
[普通のM5Stack Core2をAWS IoT Coreにつなぐ（MQTT）](https://netmark.jp/2021/05/2021-05-09-12-03.html)

プログラムは Arduino IDE 2.0.2 で確認しました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/87d8cde3-8dd7-9549-0d41-2177d728fe2c.png)


```text:mqtt_get/mqtt_get.ino
// ---------------------------------------------------------------
/*
	mqtt_get/mqtt_get.ino

						Nov/25/2022
*/
// ---------------------------------------------------------------
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include <M5Core2.h>
#include <ArduinoJson.h>
#include "config.h" 

const int subQoS = 0;

WiFiClientSecure httpsClient;
PubSubClient mqttClient(httpsClient);

int icount = 0;

// ---------------------------------------------------------------
void connect()
{

	while (WiFi.status() != WL_CONNECTED)
	{
		WiFi.begin(SSID.c_str(), WIFI_PASSWORD.c_str());
		Serial.println("Waiting for WiFi... ");
		while (WiFi.status() != WL_CONNECTED)
		{

			delay(1000);
		}
	}
	Serial.println("WiFi connected");
	Serial.printf("IP address: %s\n", WiFi.localIP().toString().c_str());

	Serial.println("[AWS IoT]");
	while (!mqttClient.connected())
	{
		if (!mqttClient.connect(AWS_IOT_DEVICE_NAME.c_str()))
		{
			Serial.print("Connect Failed. Error state=");
			Serial.print(mqttClient.state());
			Serial.println("");
			delay(10000);
		}
	}
	Serial.println("Connected.");
	if (mqttClient.connected())
	{
		if (mqttClient.subscribe(subTopic.c_str(), subQoS))
		{
			Serial.printf("Subscribed.\ntopic=%s\n", subTopic.c_str());
		}
		else
		{
			Serial.printf("Subscribe Failed.\nError state=%d\n", mqttClient.state());
		}
	}
}

// ---------------------------------------------------------------
void mqttCallback(char *topic, byte *payload, unsigned int length)
{
	DynamicJsonDocument doc(4096);
	char data_json[2048];
	deserializeJson(doc, payload);

	Serial.println("----- mqttCallback");
	serializeJson(doc, data_json);

	Serial.println(data_json);
	Serial.println("-----");
    doc.clear();
}

// ---------------------------------------------------------------
void setup()
{
	M5.begin();

	M5.Lcd.setTextColor(WHITE);
	M5.Lcd.setTextSize(2);
	M5.Lcd.clear(BLACK);
	M5.Lcd.println("Connect to AWS IoT Core");

	Serial.println("*** setup *** aaa");
	httpsClient.setCACert(AWS_IOT_ROOT_CA);
	httpsClient.setCertificate(AWS_IOT_DEVICE_CERT);
	httpsClient.setPrivateKey(AWS_IOT_DEVICE_PRIVATE_KEY);
	mqttClient.setServer(AWS_IOT_ENDPOINT.c_str(), AWS_IOT_PORT);
	mqttClient.setCallback(mqttCallback);
	M5.Lcd.println("*** check *** bbb");
	Serial.println("*** setup *** bbb");
	connect();
	M5.Lcd.println("*** check *** ccc");
	Serial.println("*** setup *** ccc");
	Serial.println("*** setup *** Oct/01/2021 PM 19:48");
}

// ---------------------------------------------------------------
void loop()
{
	if (WiFi.status() != WL_CONNECTED || !mqttClient.connected())
	{
		connect();
	}
	mqttClient.loop();
	delay(2000);
	M5.Lcd.println("icount = " + String(icount));
	Serial.println("icount = " + String(icount));
	icount++;
}

// ---------------------------------------------------------------
```

```text:mqtt01/config.h
// ---------------------------------------------------------------
/*
	config.h

						Oct/01/2021
*/
// ---------------------------------------------------------------
const String SSID = "CHANGEME";
const String WIFI_PASSWORD = "CHANGEME";

const String AWS_IOT_ENDPOINT = "CHANGEME-ats.iot.ap-northeast-1.amazonaws.com";
const int AWS_IOT_PORT = 8883;
const String AWS_IOT_DEVICE_NAME = "CHANGEME";

const String pubTopic = "CHANGEME";
const String subTopic = "CHANGEME";

// AmazonRootCA1.cer
static const char AWS_IOT_ROOT_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
...CHANGEME...
-----END CERTIFICATE-----
)EOF";

// XXXXXXXXXX-certificate.pem.crt
static const char AWS_IOT_DEVICE_CERT[] PROGMEM = R"KEY(
-----BEGIN CERTIFICATE-----
...CHANGEME...
-----END CERTIFICATE-----
)KEY";

// XXXXXXXXXX-private.pem.key
static const char AWS_IOT_DEVICE_PRIVATE_KEY[] PROGMEM = R"KEY(
-----BEGIN RSA PRIVATE KEY-----
...CHANGEME...
-----END RSA PRIVATE KEY-----
)KEY";

// ---------------------------------------------------------------
```

シリアルの通信方法

```bash
cu -s 115200 -l /dev/ttyUSB0
```