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 の subscribe

Last updated at Posted at 2023-02-07

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

プログラム

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

// ---------------------------------------------------------------
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_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
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?