M5Stack Core2: MQTT 送信

Arduino IDE 2.3.4 で確認しました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/3784f199-a0a9-456f-9474-85b2e824657e.png)



```mqtt_publish/mqtt_publish.ino
// ---------------------------------------------------------------
/*
	mqtt_publish.ino

					Feb/18/2025
*/
// ---------------------------------------------------------------
#include <M5Core2.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <WiFiMulti.h>
#include <PubSubClient.h>
#include <stdio.h>
#include "config.h"

#define	PROGRAM	"mqtt_publish.ino"
#define	VERSION	"2025-2-18 AM 10:18"

#define INTERVAL	(5000)

WiFiClient wificlient;
WiFiMulti wifiMulti;

PubSubClient mqttClient(wificlient);


int	count = 0;

// ---------------------------------------------------------------
void setup()
{
	M5.begin();
  M5.Lcd.setTextSize(2);

	wifiMulti.addAP(ssid, password);

	while((wifiMulti.run() != WL_CONNECTED)) {
	delay(500);
	Serial.print(".");
	}

	M5.Lcd.print("\nConnecting Wifi...\n");
	Serial.print("\nConnecting Wifi...\n");
	delay(1000);
	M5.Lcd.println("*** setup *** aaa ***");
	Serial.println("*** setup *** aaa ***");
	delay(1000);

	setup_mqtt_proc();

	Serial.println(PROGRAM);
	Serial.println(VERSION);
	Serial.println("*** Setup completed ***");
}

// ---------------------------------------------------------------
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"] = VERSION;

	serializeJson(doc, data_json);

	// Serial.println(data_json);
	// Serial.println(strlen(data_json));
	Serial.println("MQTT_MAX_PACKET_SIZE = " + String(MQTT_MAX_PACKET_SIZE));
  Serial.println(data_json);

	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

					Oct/02/2021
*/
// ---------------------------------------------------------------
#define MQTT_SERVER_HOST  "example.com"
#define MQTT_SERVER_PORT  (1883)

#define ID      "M5Stack"
#define OUT_TOPIC   "sample/imageTopic"
#define IN_TOPIC    "sample/imageTopic"

const char *ssid = "some-ssid";
const char *password = "some-password";
// ---------------------------------------------------------------
```


送信の確認をするスクリプト

```bash:go_sub_mqtt.sh
#
BROKER="example.com"
TOPIC="sample/imageTopic"
#
echo ${BROKER}
mosquitto_sub -d -t orz -h $BROKER --topic ${TOPIC}
#
```

## MQTT ブローカーが動いているかの確認方法 ##

```text
$ sudo systemctl status mosquitto
● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/lib/systemd/system/mosquitto.service; disabled; vendor pr>
     Active: active (running) since Mon 2022-05-30 08:02:07 JST; 21s ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
```