こちらのサンプルを実行してみました。
M5Core2/examples/Advanced/MQTT/MQTT.ino
次の2行を自分の環境に変更するだけで動きます。
const char* ssid = "********"
const char* password = "********"
MQTT.ino
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Core2 sample source code
* 配套 M5Core2 示例源代码
* Visit the website for more information: https://docs.m5stack.com/en/core/core2
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/core2
*
* describe: MQTT.
* date: 2021/11/5
*******************************************************************************
*/
#include "M5Core2.h"
#include <WiFi.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
// Configure the name and password of the connected wifi and your MQTT Serve host. 配置所连接wifi的名称、密码以及你MQTT服务器域名
const char* ssid = "Explore-F";
const char* password = "xingchentansuo123";
const char* mqtt_server = "mqtt.m5stack.com";
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() {
M5.begin();
setupWifi();
client.setServer(mqtt_server, 1883); //Sets the server details. 配置所连接的服务器
client.setCallback(callback); //Sets the message callback function. 设置消息回调函数
}
void loop() {
if (!client.connected()) {
reConnect();
}
client.loop(); //This function is called periodically to allow clients to process incoming messages and maintain connections to the server.
//定期调用此函数,以允许主机处理传入消息并保持与服务器的连接
unsigned long now = millis(); //Obtain the host startup duration. 获取主机开机时长
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value); //Format to the specified string and store it in MSG. 格式化成指定字符串并存入msg中
M5.Lcd.print("Publish message: ");
M5.Lcd.println(msg);
client.publish("M5Stack", msg); //Publishes a message to the specified topic. 发送一条消息至指定话题
if(value%12==0){
M5.Lcd.clear();
M5.Lcd.setCursor(0,0);
}
}
}
void setupWifi() {
delay(10);
M5.Lcd.printf("Connecting to %s",ssid);
WiFi.mode(WIFI_STA); //Set the mode to WiFi station mode. 设置模式为WIFI站模式
WiFi.begin(ssid, password); //Start Wifi connection. 开始wifi连接
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.printf("\nSuccess\n");
}
void callback(char* topic, byte* payload, unsigned int length) {
M5.Lcd.print("Message arrived [");
M5.Lcd.print(topic);
M5.Lcd.print("] ");
for (int i = 0; i < length; i++) {
M5.Lcd.print((char)payload[i]);
}
M5.Lcd.println();
}
void reConnect() {
while (!client.connected()) {
M5.Lcd.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())) {
M5.Lcd.printf("\nSuccess\n");
// Once connected, publish an announcement to the topic. 一旦连接,发送一条消息至指定话题
client.publish("M5Stack", "hello world");
// ... and resubscribe. 重新订阅话题
client.subscribe("M5Stack");
} else {
M5.Lcd.print("failed, rc=");
M5.Lcd.print(client.state());
M5.Lcd.println("try again in 5 seconds");
delay(5000);
}
}
}
MQTT ブローカーは、 mqtt.m5stack.com
トピックは、 M5Stack
です。
動作を確認するスクリプト
go_sub_mqtt.sh
BROKER="mqtt.m5stack.com"
TOPIC="M5Stack"
#
echo ${BROKER}
mosquitto_sub -d -t orz -h $BROKER --topic ${TOPIC}
実行時の様子
$ ./go_sub_mqtt.sh
mqtt.m5stack.com
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: M5Stack, QoS: 0, Options: 0x00)
Client (null) received SUBACK
Subscribed (mid: 1): 0, 0
Client (null) received PUBLISH (d0, q0, r0, m0, 'M5Stack', ... (14 bytes))
hello world #1
Client (null) received PUBLISH (d0, q0, r0, m0, 'M5Stack', ... (14 bytes))
hello world #2
Client (null) received PUBLISH (d0, q0, r0, m0, 'M5Stack', ... (14 bytes))
hello world #3
go_pub_mqtt.sh
BROKER="mqtt.m5stack.com"
TOPIC="M5Stack"
#
MESSAGE="Good Afternoon May/30/2022"
mosquitto_pub -d -t orz -h $BROKER --topic ${TOPIC} -m "${MESSAGE}"
実行時の様子
$ ./go_pub_mqtt.sh
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending PUBLISH (d0, q0, r0, m1, 'M5Stack', ... (26 bytes))
Client (null) sending DISCONNECT