M5Paper: MQTT 送信

ブローカー　mqtt.m5stack.com
トピック   m5paper

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/0796055c-6233-8d4b-e3da-d0e30f0f61f0.png)

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

					Jun/03/2022
*/
// ---------------------------------------------------------------
#include <M5EPD.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <stdio.h>
#include "config.h"

#define INTERVAL	(5000)

WiFiClient wificlient;
PubSubClient mqttClient(wificlient);

M5EPD_Canvas canvas(&M5.EPD);

int	count = 0;

// ---------------------------------------------------------------
void setup()
{
	M5.begin();
	M5.EPD.Clear(true);
	canvas.createCanvas(960, 540);
	canvas.setTextSize(4);

	WiFi.begin(SSID, PASSWORD);

	while (WiFi.status() != WL_CONNECTED) {
		delay(500);
		Serial.print(".");
	}

//	M5.Lcd.print("\nConnecting Wifi...\n");
	int xx = 100;
	int yy = 30;
	canvas.drawString("Connecting Wifi...", xx, yy);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);

	Serial.print("\nConnecting Wifi...\n");
	delay(1000);
	yy = 80;
	canvas.drawString("*** setup *** aaa ***", xx, yy);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
	Serial.println("*** setup *** aaa ***");
	delay(1000);

	setup_mqtt_proc();

	yy = 130;
	canvas.drawString("*** Setup completed *** Jun/03/2022 PM 14:00 ***", xx, yy);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
	Serial.println("*** Setup completed *** Jun/03/2022 PM 14:00 ***");
}

// ---------------------------------------------------------------
void loop()
{
	Serial.println("*** check *** " + String(count));

	mqtt_send_proc(count);

	delay(INTERVAL);

	count++;
}

// ---------------------------------------------------------------
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["state"]["reported"]["led"] = "on";

	doc["count"] = count;

	doc["llx"] = String(strlen(chx));
	doc["chx"] = chx;
	doc["version"] = "2022-6-03 PM 14:20";

	serializeJson(doc, 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);
}

// ---------------------------------------------------------------
void setup_mqtt_proc()
{
	Serial.println("### Connecting to MQTT server \""MQTT_SERVER_HOST"\"");
//	M5.Lcd.println("### Connecting to MQTT server \""MQTT_SERVER_HOST"\"");
	mqttClient.setServer(MQTT_SERVER_HOST, MQTT_SERVER_PORT);

	if (!mqttClient.connect(ID))
		{
		Serial.println("### Error! connect ###");
		}
	else
		{
		Serial.println("### Success! connect ###");
		}
}

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

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

					Jun/03/2022
*/
// ---------------------------------------------------------------
#define MQTT_SERVER_HOST  "mqtt.m5stack.com"
#define MQTT_SERVER_PORT  (1883)

#define ID		"M5Paper"
#define OUT_TOPIC	"m5paper"
#define IN_TOPIC	"m5paper"

#define SSID		"******************"
#define PASSWORD	"***************"
// ---------------------------------------------------------------
```

## 実行時のモニター ##

サブスクライブ

```bash:go_sub_mqtt.sh
BROKER="mqtt.m5stack.com"
TOPIC="m5paper"
#
echo ${BROKER}
mosquitto_sub -d -t orz -h $BROKER --topic ${TOPIC}
```

```text
$ ./go_sub_mqtt.sh 
mqtt.m5stack.com
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending SUBSCRIBE (Mid: 1, Topic: orz, QoS: 0, Options: 0x00)
Client (null) sending SUBSCRIBE (Mid: 1, Topic: m5paper, QoS: 0, Options: 0x00)
Client (null) received SUBACK
Subscribed (mid: 1): 0, 0
Client (null) received PUBLISH (d0, q0, r0, m0, 'm5paper', ... (117 bytes))
{"state":{"reported":{"led":"on"}},"count":85,"llx":"20","chx":"01234567890123456789","version":"2022-6-03 PM 14:20"}
Client (null) received PUBLISH (d0, q0, r0, m0, 'm5paper', ... (117 bytes))
{"state":{"reported":{"led":"on"}},"count":86,"llx":"20","chx":"01234567890123456789","version":"2022-6-03 PM 14:20"}
Client (null) received PUBLISH (d0, q0, r0, m0, 'm5paper', ... (117 bytes))
{"state":{"reported":{"led":"on"}},"count":87,"llx":"20","chx":"01234567890123456789","version":"2022-6-03 PM 14:20"}
```

シリアルモニター

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