ESP32: MQTT の subscribe

Ubuntu 24.04 上の Arduino IDE 2.3.2 を使いました。
ボードは、 ESP32 Dev Module です。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/7522880e-3445-4617-9bc5-87e392ae72a4.png)


## プログラム

```text:
// ---------------------------------------------------------------
//	mqtt_subscribe.ino
//
//						Jul/27/2024
// ---------------------------------------------------------------
#include <WiFi.h>
#include <PubSubClient.h>
#include "config.h"

#define	PROGRAM	"mqtt_subscribe.ino"
#define	VERSION	"2024-7-27 PM 16:15"

WiFiClient espClient;
PubSubClient client(espClient);

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

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

// ---------------------------------------------------------------
void setup()
{
	Serial.begin(115200);
	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");
}

// ---------------------------------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
	Serial.print("Message arrived [");
	Serial.print(topic);
	Serial.print("] ");
	for (int i = 0; i < length; i++) {
		Serial.print((char)payload[i]);
	}
	Serial.println();
}

// ---------------------------------------------------------------
void reConnect() {
	while (!client.connected()) {
		Serial.print("Attempting MQTT connection...");
		// Create a random client ID.	创建一个随机的客户端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
// ---------------------------------------------------------------
/*
	config.h

					Oct/02/2021
*/
// ---------------------------------------------------------------
#define MQTT_SERVER_HOST  "broker.emqx.io"
#define MQTT_SERVER_PORT  1883

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


## テスト用スクリプト

```bash:go_pub_mqtt.sh
BROKER="broker.emqx.io"
#
echo $BROKER
message="Hello Mqtt "`date`
mosquitto_pub -d -t orz -m "${message}" \
    -h  $BROKER \
    --topic emqx/esp32a
```