ESP32: MQTT 送信

次のページを参考にしました。
[ESP32をMQTTでPublishする](https://qiita.com/hilucky/items/0e394760a1445593cea5)

Ubuntu 24.10 上の Arduino IDE 2.3.4 で確認しました。
ボードは、
ESP32 Dev Module
とします。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/3722110e-dc3e-ccb8-7c33-86923b840f44.png)


## プログラム

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

WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

const char* topic = "emqx/esp32a";
char payload[40];

int count = 0;
// ---------------------------------------------------------------------
void setup() {
	Serial.begin(115200);
	Serial.println("*** start ***");

	// Connect WiFi
	connectWiFi();
	Serial.println("*** aaa ***");

	// Connect MQTT
	connectMqtt();
	Serial.println("*** ccc ***");
}

// ---------------------------------------------------------------------
void loop()
{
	sprintf(payload,"payload_%04d",count);
	mqttClient.publish(topic, payload);

	Serial.println("*** loop ***" + String(count));
	Serial.println(WiFi.localIP());
	delay(3000);
	count++;

	// WiFi
	if ( WiFi.status() == WL_DISCONNECTED ) {
		connectWiFi(); 
	}
	// MQTT
	if ( ! mqttClient.connected() ) {
		connectMqtt();
	}
	mqttClient.loop();  
}

// ---------------------------------------------------------------------
void connectWiFi()
{
	WiFi.begin(ssid, password);
	Serial.print("WiFi connecting...");
	while(WiFi.status() != WL_CONNECTED) {
		Serial.print(".");
		delay(100);
	}
	Serial.print(" connected. ");
	Serial.println(WiFi.localIP());
}

// ---------------------------------------------------------------------
void connectMqtt()
{
	mqttClient.setServer(MQTT_SERVER_HOST, MQTT_SERVER_PORT);
	while( ! mqttClient.connected() ) {
		Serial.println("Connecting to MQTT...");
		String clientId = "ESP32-" + String(random(0xffff), HEX);
		if ( mqttClient.connect(clientId.c_str()) ) {
			Serial.println("connected"); 
		}
		delay(1000);
		randomSeed(micros());
	}
}

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

```

```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_sub_mqtt.sh
BROKER="broker.emqx.io"
#
mosquitto_sub -d -t orz -h $BROKER \
    --topic emqx/esp32a
```

実行結果

```text
$ ./go_sub_mqtt.sh 
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: emqx/esp32a, QoS: 0, Options: 0x00)
Client null received SUBACK
Subscribed (mid: 1): 0, 0
Client null received PUBLISH (d0, q0, r0, m0, 'emqx/esp32a', ... (12 bytes))
payload_0000
Client null received PUBLISH (d0, q0, r0, m0, 'emqx/esp32a', ... (12 bytes))
payload_0001
Client null received PUBLISH (d0, q0, r0, m0, 'emqx/esp32a', ... (12 bytes))
payload_0002
```