MQTT.ino 4.0 KB

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