|
| 1 | +/* |
| 2 | +******************************************************************************* |
| 3 | +* Copyright (c) 2021 by M5Stack |
| 4 | +* Equipped with M5Core sample source code |
| 5 | +* 配套 M5Core 示例源代码 |
| 6 | +* Visit the website for more information:https://docs.m5stack.com/en/core/gray |
| 7 | +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray |
| 8 | +* |
| 9 | +* describe:MQTT. |
| 10 | +* date:2021/11/5 |
| 11 | +******************************************************************************* |
| 12 | +*/ |
| 13 | +#include "M5Stack.h" |
| 14 | +#include <WiFi.h> |
| 15 | +#include <PubSubClient.h> |
| 16 | + |
| 17 | +WiFiClient espClient; |
| 18 | +PubSubClient client(espClient); |
| 19 | + |
| 20 | +// Configure the name and password of the connected wifi and your MQTT Serve host. 配置所连接wifi的名称、密码以及你MQTT服务器域名 |
| 21 | +const char* ssid = "Explore-F"; |
| 22 | +const char* password = "xingchentansuo123"; |
| 23 | +const char* mqtt_server = "mqtt.m5stack.com"; |
| 24 | + |
| 25 | +unsigned long lastMsg = 0; |
| 26 | +#define MSG_BUFFER_SIZE (50) |
| 27 | +char msg[MSG_BUFFER_SIZE]; |
| 28 | +int value = 0; |
| 29 | + |
| 30 | +void setupWifi(); |
| 31 | +void callback(char* topic, byte* payload, unsigned int length); |
| 32 | +void reConnect(); |
| 33 | + |
| 34 | +void setup() { |
| 35 | + M5.begin(); |
| 36 | + M5.Power.begin(); |
| 37 | + setupWifi(); |
| 38 | + client.setServer(mqtt_server, 1883); //Sets the server details. 配置所连接的服务器 |
| 39 | + client.setCallback(callback); //Sets the message callback function. 设置消息回调函数 |
| 40 | +} |
| 41 | + |
| 42 | +void loop() { |
| 43 | + if (!client.connected()) { |
| 44 | + reConnect(); |
| 45 | + } |
| 46 | + client.loop(); //This function is called periodically to allow clients to process incoming messages and maintain connections to the server. |
| 47 | + //定期调用此函数,以允许主机处理传入消息并保持与服务器的连接 |
| 48 | + |
| 49 | + unsigned long now = millis(); //Obtain the host startup duration. 获取主机开机时长 |
| 50 | + if (now - lastMsg > 2000) { |
| 51 | + lastMsg = now; |
| 52 | + ++value; |
| 53 | + snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value); //Format to the specified string and store it in MSG. 格式化成指定字符串并存入msg中 |
| 54 | + M5.Lcd.print("Publish message: "); |
| 55 | + M5.Lcd.println(msg); |
| 56 | + client.publish("M5Stack", msg); //Publishes a message to the specified topic. 发送一条消息至指定话题 |
| 57 | + if(value%12==0){ |
| 58 | + M5.Lcd.clear(); |
| 59 | + M5.Lcd.setCursor(0,0); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void setupWifi() { |
| 65 | + delay(10); |
| 66 | + M5.Lcd.printf("Connecting to %s",ssid); |
| 67 | + WiFi.mode(WIFI_STA); //Set the mode to WiFi station mode. 设置模式为WIFI站模式 |
| 68 | + WiFi.begin(ssid, password); //Start Wifi connection. 开始wifi连接 |
| 69 | + |
| 70 | + while (WiFi.status() != WL_CONNECTED) { |
| 71 | + delay(500); |
| 72 | + M5.Lcd.print("."); |
| 73 | + } |
| 74 | + M5.Lcd.printf("\nSuccess\n"); |
| 75 | +} |
| 76 | + |
| 77 | +void callback(char* topic, byte* payload, unsigned int length) { |
| 78 | + M5.Lcd.print("Message arrived ["); |
| 79 | + M5.Lcd.print(topic); |
| 80 | + M5.Lcd.print("] "); |
| 81 | + for (int i = 0; i < length; i++) { |
| 82 | + M5.Lcd.print((char)payload[i]); |
| 83 | + } |
| 84 | + M5.Lcd.println(); |
| 85 | +} |
| 86 | + |
| 87 | +void reConnect() { |
| 88 | + while (!client.connected()) { |
| 89 | + M5.Lcd.print("Attempting MQTT connection..."); |
| 90 | + // Create a random client ID. 创建一个随机的客户端ID |
| 91 | + String clientId = "M5Stack-"; |
| 92 | + clientId += String(random(0xffff), HEX); |
| 93 | + // Attempt to connect. 尝试重新连接 |
| 94 | + if (client.connect(clientId.c_str())) { |
| 95 | + M5.Lcd.printf("\nSuccess\n"); |
| 96 | + // Once connected, publish an announcement to the topic. 一旦连接,发送一条消息至指定话题 |
| 97 | + client.publish("M5Stack", "hello world"); |
| 98 | + // ... and resubscribe. 重新订阅话题 |
| 99 | + client.subscribe("M5Stack"); |
| 100 | + } else { |
| 101 | + M5.Lcd.print("failed, rc="); |
| 102 | + M5.Lcd.print(client.state()); |
| 103 | + M5.Lcd.println("try again in 5 seconds"); |
| 104 | + delay(5000); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments