mqttJson.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // This example uses an Arduino/Genuino Zero together with
  2. // by Gilberto Conti
  3. // https://github.com/256dpi/arduino-mqtt
  4. #include <ESP8266WiFi.h>
  5. #include <MQTT.h>
  6. #include <ArduinoJson.h>
  7. const char ssid[] = "yckj01";
  8. const char pass[] = "yckj2015";
  9. const char clientId[] = "client1";
  10. const char username[] = "user1";
  11. const char password[] = "123456";
  12. WiFiClient net;
  13. MQTTClient client;
  14. unsigned long heartLong = 30; //
  15. unsigned long lastMillis = 0;
  16. void serialRead()
  17. {
  18. String inputString = "";
  19. while (Serial.available())
  20. {
  21. inputString = inputString + char(Serial.read());
  22. delay(2);
  23. }
  24. if (inputString.length() > 0)
  25. {
  26. Serial.println("inputString:" + inputString); //把字符串传给串口
  27. }
  28. }
  29. void connect()
  30. {
  31. Serial.print("checking wifi...");
  32. while (WiFi.status() != WL_CONNECTED)
  33. {
  34. Serial.print(".");
  35. delay(1000);
  36. }
  37. Serial.print("\nconnecting...");
  38. while (!client.connect(clientId, username, password))
  39. {
  40. Serial.print(".");
  41. delay(1000);
  42. }
  43. Serial.println("\nconnected!");
  44. client.subscribe("/allot/request");
  45. // client.unsubscribe("/hello");
  46. }
  47. void messageReceived(String &topic, String &payload)
  48. {
  49. Serial.println("incoming: " + topic + " - " + payload);
  50. DynamicJsonDocument doc(1024);
  51. deserializeJson(doc, payload);
  52. JsonObject obj = doc.as<JsonObject>();
  53. String name = obj["name"];
  54. Serial.println("json:name: " + name);
  55. // Note: Do not use the client in the callback to publish, subscribe or
  56. // unsubscribe as it may cause deadlocks when other things arrive while
  57. // sending and receiving acknowledgments. Instead, change a global variable,
  58. // or push to a queue and handle it in the loop after calling `client.loop()`.
  59. }
  60. void setup()
  61. {
  62. Serial.begin(115200);
  63. WiFi.begin(ssid, pass);
  64. // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
  65. // by Arduino. You need to set the IP address directly.
  66. //
  67. // MQTT brokers usually use port 8883 for secure connections.
  68. client.begin("qqyun.itbbn.top", 1883, net);
  69. client.onMessage(messageReceived);
  70. client.setKeepAlive(heartLong); //设置心跳时长
  71. connect();
  72. }
  73. void loop()
  74. {
  75. client.loop();
  76. if (!client.connected())
  77. {
  78. connect();
  79. }
  80. // publish a message roughly every second.
  81. unsigned long heartLongL = heartLong * 1000;
  82. if (millis() - lastMillis > heartLongL)
  83. {
  84. lastMillis = millis();
  85. String heartInfo = "{\"name\":\"nodencu1\",\"time\":\"" + String(lastMillis) + "\"}";
  86. client.publish("/allot/heart", heartInfo);
  87. }
  88. serialRead();
  89. }