1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32: MQTT 送信

Last updated at Posted at 2022-11-17

次のページを参考にしました。
ESP32をMQTTでPublishする

Ubuntu 24.04 上の Arduino IDE 2.3.2 を使いました。
ボードは、
ESP32 Dev Module
とします。
image.png

プログラム

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

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

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";
// ---------------------------------------------------------------

確認のスクリプト

go_sub_mqtt.sh
BROKER="broker.emqx.io"
#
mosquitto_sub -d -t orz -h $BROKER \
    --topic emqx/esp32a

実行結果

$ ./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
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?