M5Core2: MQTT を使った Lチカ

Ubuntu 24.10 上の Arduino IDE 2.3.4 を使いました。

![a003.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/3694d8c2-e18b-46e8-b46a-51ce0da73791.jpeg)

![a004.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/b93d78c8-cd9f-4f10-8644-e3fd488a1700.jpeg)



## プログラム

```text:mqtt_m5core2_subscribe.ino
// ---------------------------------------------------------------
//	mqtt_m5core2_subscribe.ino
//
//						Feb/19/2025
// ---------------------------------------------------------------
#include "M5Core2.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <Adafruit_NeoPixel.h>

#define PIN 25 
#define NUMPIXELS 9 //LEDの数を指定
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //800kHzでNeoPixelを駆動


#include "config.h"

#define	PROGRAM	"mqtt_m5core2_subscribe.ino"
#define	VERSION	"2025-2-19 PM 12:35"

WiFiClient espClient;
PubSubClient client(espClient);

const char* topic = IN_TOPIC;
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

DynamicJsonDocument doc(256);

void setupWifi();
void callback(char* topic, byte* payload, unsigned int length);
void reConnect();

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

	M5.Lcd.setTextColor(WHITE);
	M5.Lcd.setTextSize(2);
	M5.Lcd.clear(BLACK);
	M5.Lcd.println(PROGRAM);
	M5.Lcd.println(VERSION);

	delay(3000);
	Serial.println(PROGRAM);
	Serial.println(VERSION);
	Serial.println();

	setupWifi();
	client.setServer(MQTT_SERVER_HOST, MQTT_SERVER_PORT);
	client.setCallback( callback);
}

// ---------------------------------------------------------------
void loop()
{
	if (!client.connected())
		{
		reConnect();
		}

	client.loop();

	Serial.println("*** loop ***");
	delay(2000);	
}

// ---------------------------------------------------------------
void setupWifi()
{
	delay(10);
	Serial.printf("Connecting to %s", ssid);
	WiFi.mode( WIFI_STA);
	WiFi.begin(ssid, password);

	while (WiFi.status() != WL_CONNECTED) {
		delay(500);
		Serial.print(".");
	}
	Serial.printf("\r\nSuccess\r\n");

	M5.Lcd.println("Ready");
}

// ---------------------------------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
	Serial.print("Message arrived [");
	Serial.print(topic);
	Serial.print("] ");

	payload[length] = '\0';
	String msg = String((char*) payload);
	Serial.println(msg);

	deserializeJson(doc, msg);
	const char* device = doc["device"];
	const char* status = doc["status"];

	const String str_device = String(device);
	const String str_status = String(status);

	M5.Lcd.setCursor(10, 50);
	M5.Lcd.setTextSize(3);
	M5.Lcd.clear(BLACK);

	if (str_device == "aa")
		{
		if (str_status == "on")
			{
			M5.Lcd.print("Device aa: on");
			pixels.setPixelColor(0, pixels.Color(0, 150, 0));
			}
		else if (str_status == "off")
			{
			M5.Lcd.print("Device aa: off");
			pixels.setPixelColor(0, pixels.Color(0, 0, 0));
			}
		}
	else if (str_device == "bb")
		{
		if (str_status == "on")
			{
			M5.Lcd.print("Device bb: on");
			pixels.setPixelColor(8, pixels.Color(150, 0, 0));
			}
		else if (str_status == "off")
			{
			M5.Lcd.print("Device bb: off");
			pixels.setPixelColor(8, pixels.Color(0, 0, 0));
			}
		}

	pixels.show();
}

// ---------------------------------------------------------------
void reConnect() {
	while (!client.connected()) {
		Serial.print("Attempting MQTT connection...");
		// Create a random client ID.
		String clientId = "M5Stack-";
		clientId += String(random(0xffff), HEX);
		// Attempt to connect.
		if (client.connect(clientId.c_str())) {
			Serial.printf("\nSuccess\n");
		// Once connected, publish an announcement to the topic.
			client.publish("M5Stack", "hello world");
			// ... and resubscribe.
			client.subscribe(topic);
		} else {
			Serial.print("failed, rc=");
			Serial.print(client.state());
			Serial.println("try again in 5 seconds");
			delay(5000);
		}
	}
}

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

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

					Feb/19/2025
*/
// ---------------------------------------------------------------
#define MQTT_SERVER_HOST  "mqtt.eclipseprojects.io"
#define MQTT_SERVER_PORT  1883
#define IN_TOPIC	"testmqt/topic"

const char *ssid = "*****";
const char *password = "***";

// ---------------------------------------------------------------