M5Stack Core2: AWS IOT の MQTT 送信

プログラムは Arduino IDE で作成します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/67251bbf-2c49-5b60-5e05-8cd8e42b180d.png)


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

						Oct/02/2021
*/
// ---------------------------------------------------------------
#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 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);

	M5.Lcd.println("*** check *** bbb");
	Serial.println("*** setup *** bbb");
	wifi_connect();

	M5.Lcd.println("*** check *** ccc");
	Serial.println("*** setup *** ccc");

	mqtt_connect_wait_proc();

	Serial.println("*** setup *** Oct/02/2021 PM 20:00");
}

// ---------------------------------------------------------------
void loop()
{
	if (WiFi.status() != WL_CONNECTED || !mqttClient.connected())
	{
		wifi_connect();
	}

	mqtt_send_proc(icount);	
	delay(2000);
	M5.Lcd.println("icount = " + String(icount));
	Serial.println("icount = " + String(icount));
	icount++;
}

// ---------------------------------------------------------------
void mqtt_send_proc(int count)
{
	DynamicJsonDocument doc(256);
	char data_json[256];
	data_json[0] = '\0';

	char *chx = (char *)malloc(256);
	chx[0] = '\0';

	char qq[51] = "01234567890123456789";

	strcat(chx,qq);

	doc["count"] = count;

	doc["llx"] = String(strlen(chx));
	doc["chx"] = chx;
	doc["version"] = "2021-10-02 20:00";

	serializeJson(doc, data_json);

	Serial.println(data_json);
	Serial.println(strlen(data_json));
	Serial.println("MQTT_MAX_PACKET_SIZE = " + String(MQTT_MAX_PACKET_SIZE));
	boolean flag = mqttClient.publish(OUT_TOPIC, data_json);
	Serial.println(flag);

	free(chx);
}

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

```text:mqtt_send/wifi_connect.ino
// ---------------------------------------------------------------
/*
	mqtt_send/wifi_connect.ino

						Oct/02/2021
*/
// ---------------------------------------------------------------
void wifi_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());
}

// ---------------------------------------------------------------
void mqtt_connect_wait_proc()
{
	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);
		}
	}

}

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

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

						Oct/02/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";

#define OUT_TOPIC "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";

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