{"id":7737,"date":"2023-06-14T16:19:52","date_gmt":"2023-06-14T08:19:52","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=7737"},"modified":"2023-06-14T16:19:52","modified_gmt":"2023-06-14T08:19:52","slug":"esp8266-mqtt","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=7737","title":{"rendered":"ESP8266 - MQTT"},"content":{"rendered":"<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.emqx.io\/blog\/esp8266-connects-to-the-public-mqtt-broker\" target=\"_blank\" rel=\"noopener\">ESP8266 connects to the free public MQTT broker<\/a><\/li>\n<li><a href=\"https:\/\/swf.com.tw\/?p=1021\" target=\"_blank\" rel=\"noopener\">MQTT\u6559\u5b78\uff08\u516d\uff09\uff1a\u4f7f\u7528PubSubClient\u7a0b\u5f0f\u5eab\u958b\u767cArduino MQTT\u61c9\u7528<\/a><\/li>\n<li><a href=\"https:\/\/www.emqx.io\/cn\/blog\/esp8266-connects-to-the-public-mqtt-broker\" target=\"_blank\" rel=\"noopener\">ESP8266 \u8fde\u63a5\u5230\u514d\u8d39\u7684\u516c\u5171 MQTT \u670d\u52a1\u5668<\/a><\/li>\n<li><a href=\"https:\/\/techtutorialsx.com\/2017\/04\/09\/esp8266-connecting-to-mqtt-broker\/comment-page-3\/#comments\" target=\"_blank\" rel=\"noopener\">ESP8266: Connecting to MQTT broker<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/knolleary\/pubsubclient\/issues\/390\" target=\"_blank\" rel=\"noopener\">HOW TO PUBLISH WHILE SUBSCRIBING IN MQTT PROTOCOL WHILE USING ESP8266?<\/a><\/li>\n<li><a href=\"https:\/\/gist.github.com\/virgilvox\/ffe1cc08a240db9792d3\" target=\"_blank\" rel=\"noopener\">mqtt_meshblu_send_receive.ino<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u767c\u9001\u53ca\u8a02\u95b1<\/h2>\n<p>\u4e00\u6a23\u5148\u4f7f\u7528\u514d\u8cbb\u516c\u958b\u7684\u00a0test.mosquitto.org \u5be6\u9a57<\/p>\n<p>\u76ee\u6a19\u662f\u5b9a\u6642\u5f80\u4e0a\u767c\u9001\u8cc7\u6599\u4e26\u4e14\u8a02\u95b1\u63a7\u5236 LED \u7684\u4e3b\u984c\uff0cLED \u4e00\u6a23\u662f Onboard \u7684 D4<\/p>\n<pre class=\"lang:arduino decode:true\">#include &lt;ESP8266WiFi.h&gt;\r\n#include &lt;PubSubClient.h&gt;\r\n#include &lt;ArduinoJson.h&gt;\r\n\r\n\/\/\r\nconst char* ssid = \"ssid\";\r\nconst char* password = \"passowrd\";\r\nconst char *mqtt_broker = \"test.mosquitto.org\";\r\nconst int mqtt_port = 1883;\r\n\r\nWiFiClient wifi;\r\nPubSubClient client(wifi);\r\n\r\n\/\/ \u8a2d\u5b9a\u7528\u6236\u7aefID\r\nconst char clientID[] = \"hoyo_esp8266\";\r\n\/\/ \u8a2d\u5b9a\u4e3b\u984c\u540d\u7a31\r\nconst char topic[] = \"Hoyo\/ESP8266\";\r\n\/\/ \u5132\u5b58\u8a0a\u606f\u7684\u5b57\u4e32\u8b8a\u6578\r\nString msgStr = \"\";\r\n\/\/ \u5132\u5b58\u5b57\u5143\u9663\u5217\u683c\u5f0f\u7684\u8a0a\u606f\u5b57\u4e32\uff08\u53c3\u95b1\u4e0b\u6587\u8aaa\u660e\uff09\r\nchar json[25];\r\n\r\nvoid reconnect() {\r\n  \/\/ \u82e5\u76ee\u524d\u6c92\u6709\u548c\u4f3a\u670d\u5668\u76f8\u9023\uff0c\u5247\u53cd\u8986\u57f7\u884c\u76f4\u5230\u9023\u7d50\u6210\u529f\u2026\r\n  while (!client.connected()) {\r\n    \/\/ \u6307\u5b9a\u7528\u6236\u7aefID\u4e26\u9023\u7d50MQTT\u4f3a\u670d\u5668\r\n    \/\/ client.connect(clientID, mqtt_username, mqtt_password))\r\n    if (client.connect(clientID)) {\r\n      Serial.println(\"connected\");\r\n\r\n      \/\/ \u6210\u529f\u9023\u7dda\u8a02\u95b1\u4e3b\u984c\r\n      client.subscribe(\"esp8266\/test\");\r\n    }\r\n    else {\r\n      \/\/ \u82e5\u9023\u7dda\u4e0d\u6210\u529f\uff0c\u5247\u986f\u793a\u932f\u8aa4\u8a0a\u606f\r\n      Serial.print(\"failed, rc=\");\r\n      Serial.print(client.state());\r\n      Serial.println(\" try again in 5 seconds\");\r\n      \/\/ \u7b49\u50195\u79d2\uff0c\u518d\u91cd\u65b0\u5617\u8a66\u9023\u7dda\u3002\r\n      delay(5000);\r\n    }\r\n  }\r\n}\r\n\r\nvoid callback(char *topic, byte *payload, unsigned int length) {\r\n  char inData[256];\r\n\r\n  Serial.print(\"Message arrived in topic: \");\r\n  Serial.println(topic);\r\n  Serial.print(\"Message:\");\r\n  for (int i = 0; i &lt; length; i++) {\r\n    inData[i] = (char)payload[i];\r\n    Serial.print((char) payload[i]);\r\n  }\r\n\r\n  DynamicJsonDocument doc(1024);\r\n  deserializeJson(doc, inData);\r\n  JsonObject obj = doc.as&lt;JsonObject&gt;();\r\n  \/\/  Serial.println(obj);\r\n  String led = obj[\"LED\"];\r\n  if (led == \"0\") digitalWrite(D4, HIGH);\r\n  if (led == \"1\") digitalWrite(D4, LOW);\r\n  Serial.println(led);\r\n\r\n  Serial.println();\r\n  Serial.println(\"-----------------------\");\r\n}\r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n\r\n  pinMode(D4, OUTPUT);\r\n\r\n  WiFi.mode(WIFI_STA);\r\n  WiFi.begin(ssid, password);\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    delay(500);\r\n    Serial.println(\".\");\r\n  }\r\n\r\n  Serial.println(\"WiFi connected\");\r\n\r\n  client.setServer(mqtt_broker, mqtt_port);\r\n  client.setCallback(callback);\r\n}\r\n\r\nvoid loop() {\r\n\r\n  \/\/ \u78ba\u8a8d\u7528\u6236\u7aef\u662f\u5426\u5df2\u9023\u4e0a\u4f3a\u670d\u5668\r\n  if (!client.connected()) {\r\n    \/\/ \u82e5\u6c92\u6709\u9023\u4e0a\uff0c\u5247\u57f7\u884c\u6b64\u81ea\u8a02\u51fd\u5f0f\u3002\r\n    reconnect();\r\n  }\r\n  \/\/ \u66f4\u65b0\u7528\u6236\u7aef\u72c0\u614b\r\n  client.loop();\r\n\r\n  \/\/ \u5efa\u7acbMQTT\u8a0a\u606f\uff08JSON\u683c\u5f0f\u7684\u5b57\u4e32\uff09\r\n  msgStr = msgStr + \"{\\\"temp\\\":\" + 10 + \",\\\"humid\\\":\" + 20 + \"}\";\r\n  \/\/ \u628aString\u5b57\u4e32\u8f49\u63db\u6210\u5b57\u5143\u9663\u5217\u683c\u5f0f\r\n  msgStr.toCharArray(json, 25);\r\n  \/\/ \u767c\u5e03MQTT\u4e3b\u984c\u8207\u8a0a\u606f\r\n  client.publish(topic, json);\r\n  \/\/ \u6e05\u7a7aMQTT\u8a0a\u606f\u5167\u5bb9\r\n  msgStr = \"\";\r\n\r\n  delay(2000);\r\n}<\/pre>\n<p>--<\/p>\n<h2>Windows MQTT Client<\/h2>\n<ul>\n<li><a href=\"http:\/\/mqtt-explorer.com\/\" target=\"_blank\" rel=\"noopener\">MQTT Explorer<\/a> (<a href=\"https:\/\/github.com\/thomasnordquist\/MQTT-Explorer\/releases\/download\/0.0.0-0.4.0-beta1\/MQTT-Explorer-0.4.0-beta1.exe\" target=\"_blank\" rel=\"noopener\">portable<\/a>)<\/li>\n<\/ul>\n<p>\u514d\u5b89\u88dd\uff0c\u6e2c\u8a66\u7d50\u679c\u6b63\u5e38\u3002\u767c\u9001 LED \u63a7\u5236\u5230 ESP8266 \u8a02\u95b1<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/1607236213.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-7750\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/1607236213-300x203.png\" alt=\"\" width=\"300\" height=\"203\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/1607236213-300x203.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/1607236213-768x520.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/1607236213-443x300.png 443w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/1607236213.png 829w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>--<\/p>\n<h2>Publish \u6700\u5927\u5b57\u4e32\u9577\u5ea6 128 \u9650\u5236<\/h2>\n<p>MQTT \u767c\u9001\u9810\u8a2d\u6709 128 \u500b\u5b57\u5143\u7684\u9650\u5236\uff0c<\/p>\n<pre class=\"lang:default decode:true \">    \/\/ client.setCallback(callback);\r\n    \/\/ client.setBufferSize(1024);\r\n    \/\/ client.setSocketTimeout(1);\r\n<\/pre>\n<p>--<\/p>\n<h2>\u7bc4\u4f8b<\/h2>\n<ul>\n<li><a href=\"https:\/\/gist.github.com\/boverby\/d391b689ce787f1713d4a409fb43a0a4\" target=\"_blank\" rel=\"noopener\">mqtt_esp8266wemos.ino<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7737\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;6,847&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u53c3\u8003\u8cc7\u6e90 ESP8266...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7737\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;6,847&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[273],"tags":[310],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7737"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7737"}],"version-history":[{"count":16,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7737\/revisions"}],"predecessor-version":[{"id":12343,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7737\/revisions\/12343"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}