{"id":5951,"date":"2019-11-29T00:03:12","date_gmt":"2019-11-28T16:03:12","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=5951"},"modified":"2023-04-14T09:16:24","modified_gmt":"2023-04-14T01:16:24","slug":"esp8266-%e6%87%89%e7%94%a8%e4%b8%80%ef%bc%9a%e6%99%ba%e8%83%bd%e6%8f%92%e5%ba%a7-6-%e6%8e%a5%e6%94%b6-server-%e6%8c%87%e4%bb%a4%e5%8f%8a%e7%b9%bc%e9%9b%bb%e5%99%a8%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=5951","title":{"rendered":"ESP8266 \u61c9\u7528\u4e00\uff1a\u667a\u80fd\u63d2\u5ea7 .6 \u63a5\u6536 Server \u6307\u4ee4\u53ca\u7e7c\u96fb\u5668\u63a7\u5236"},"content":{"rendered":"<p>\u70ba\u4e86\u53ef\u4ee5\u64c1\u6709\u6307\u4ee4\u64f4\u5145\u80fd\u529b\uff0c\u9084\u662f\u63a1\u7528 JSON \u683c\u5f0f<\/p>\n<p>--<\/p>\n<h2>\u589e\u52a0 JSON \u8655\u7406\u80fd\u529b<\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/bblanchon\/ArduinoJson\" target=\"_blank\" rel=\"noopener\" data-pjax=\"#js-repo-pjax-container\">ArduinoJson<\/a><\/li>\n<\/ul>\n<p>\u5230 GitHub \u4e0b\u8f09 zip \u5f8c\uff0cArduino \u2192 \u8349\u7a3f\u78bc \u2192 \u532f\u5165\u7a0b\u5f0f\u5eab \u2192 \u52a0\u5165 .ZIP \u7a0b\u5f0f\u5eab\uff0c\u9078\u64c7\u525b\u525b\u4e0b\u8f09\u7684 zip \u6a94\u6848<\/p>\n<p>\u5728\u6574\u7406\u8cc7\u6599\u7684\u6642\u5019\u7248\u672c\u5df2\u7d93\u5347\u5230 6 \u4e86\uff0c\u4ee5\u524d\u7684\u7a0b\u5f0f\u78bc\u4e0d\u9069\u7528\u4e86 ... = =a<\/p>\n<p>--<\/p>\n<h2>JSON \u63a5\u6536\u8655\u7406<\/h2>\n<ul>\n<li><a id=\"b29f181dc3dcc0a9eab0903a77cc23a3-040f8e463390d0d74454b394965fd482498000d0\" class=\"js-navigation-open\" title=\"StringExample.ino\" href=\"https:\/\/github.com\/bblanchon\/ArduinoJson\/blob\/6.x\/examples\/StringExample\/StringExample.ino\" target=\"_blank\" rel=\"noopener\">StringExample.ino<\/a><\/li>\n<\/ul>\n<p>\u53c3\u7167 GitHub \u4e0a\u7684 example \u52a0\u4e0a\u4e0a\u4e00\u7ae0 <a href=\"https:\/\/blog.hoyo.idv.tw\/?p=4593\" target=\"_blank\" rel=\"noopener\">ESP8266 \u61c9\u7528\u4e00\uff1a\u667a\u80fd\u63d2\u5ea7 .5 \u6539\u7528 Socket \u901a\u8a0a\u70ba\u4e3b\u52d5\u63a7\u5236\u505a\u6e96\u5099 TCP Client<\/a> \u7684 TCP Server \u7a0b\u5f0f\u4f86\u767c\u9001 json \u6e2c\u8a66<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;ESP8266WiFi.h&gt;\r\n#include &lt;ArduinoJson.h&gt;\r\n\r\n\/\/\r\nconst char* ssid = \"ssid name\";\r\nconst char* password = \"ssid password\";\r\n\r\n\/\/ Socket Server\r\nconst char* host = \"192.168.0.45\";\r\nconst int port = 3000;\r\n\r\nint i = 0;\r\n\r\nWiFiClient client;\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  Serial.print( \"Start...\" );\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\r\nvoid loop() {\r\n    \r\n  if (!client.connected()) {\r\n    client.connect(host, port);\r\n    client.println(\"connected...\");\r\n  }\r\n\r\n  delay(1000);\r\n\r\n  \/\/ wifi \u4e3b\u52d5\u63a7\u5236\r\n  String jsonControl = client.readStringUntil('\\n');\r\n  if ( jsonControl.length() &gt;= 1) {\r\n    Serial.println(\"loop: \" + jsonControl);\r\n  }\r\n\r\n  DynamicJsonDocument doc(1024);\r\n  deserializeJson(doc, jsonControl);\r\n  JsonObject obj = doc.as&lt;JsonObject&gt;();\r\n\r\n  String power = obj[\"power\"];\r\n  Serial.println(power);\r\n}<\/pre>\n<p>\u904b\u4f5c\u8d77\u4f86\u7684\u7d50\u679c\u5927\u6982\u5c31\u50cf\u9019\u6a23<\/p>\n<figure id=\"attachment_6536\" aria-describedby=\"caption-attachment-6536\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Push_JSON.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"wp-image-6536 size-medium\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Push_JSON-300x135.png\" alt=\"\" width=\"300\" height=\"135\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Push_JSON-300x135.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Push_JSON-768x346.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Push_JSON-500x225.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Push_JSON.png 975w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-6536\" class=\"wp-caption-text\">ESP8266 \u63a5\u6536 JSON \u5b57\u4e32\u8655\u7406<\/figcaption><\/figure>\n<p>--<\/p>\n<h2>\u7e7c\u96fb\u5668\u63a7\u5236<\/h2>\n<p>\u7e7c\u96fb\u5668\u6709\u5206\u9ad8\u96fb\u5e73\u548c\u4f4e\u96fb\u5e73\uff0c\u610f\u601d\u5c31\u662f\u6709\u96fb\u9084\u662f\u6c92\u96fb\u53ef\u4ee5\u5c0e\u901a\uff0c\u9078\u64c7\u8981\u770b\u9700\u6c42\uff0c\u4f8b\u5982\u5728\u672c\u6b21\u7684\u5c08\u6848\u662f\u667a\u80fd\u63d2\u5ea7\uff0c\u6253\u7b97\u6c92\u901a\u96fb\u7684\u6642\u5019\u5c31\u4e5f\u4e0d\u901a\u96fb\uff0c\u6240\u4ee5\u9078\u64c7\u7684\u662f\u9ad8\u96fb\u5e73\u63a7\u5236\u7e7c\u96fb\u5668<\/p>\n<p>\u628a\u4e0a\u9762\u7684\u7a0b\u5f0f\u52a0\u4e0a\u7e7c\u96fb\u5668\u7684\u63a5\u8173\u8f38\u51fa\u5b9a\u7fa9\uff0c\u4ee5\u53ca\u5c07\u5f9e TCP Server \u63a5\u6536\u7684 JSON \u6307\u4ee4\u89e3\u6790\u63a7\u5236\u7e7c\u96fb\u5668\u5373\u53ef\u5b8c\u6210<\/p>\n<pre class=\"lang:default decode:true  \">#include &lt;ESP8266WiFi.h&gt;\r\n#include &lt;ArduinoJson.h&gt;\r\n\r\n\/\/\r\nconst char* ssid = \"ssid name\";\r\nconst char* password = \"ssid password\";\r\n\r\n\/\/ Socket Server\r\nconst char* host = \"192.168.0.45\";\r\nconst int port = 3000;\r\n\r\nint i = 0;\r\n\r\nWiFiClient client;\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  Serial.print( \"Start...\" );\r\n\r\n  \/\/ \u7e7c\u96fb\u5668\r\n  pinMode(D2, OUTPUT);\r\n  digitalWrite(D2, HIGH);\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\r\nvoid loop() {\r\n\r\n  if (!client.connected()) {\r\n    client.connect(host, port);\r\n    client.println(\"connected...\");\r\n  }\r\n\r\n  delay(1000);\r\n\r\n  \/\/ wifi \u4e3b\u52d5\u63a7\u5236\r\n  String jsonControl = client.readStringUntil('\\n');\r\n  if ( jsonControl.length() &gt;= 1) {\r\n    Serial.println(\"loop: \" + jsonControl);\r\n  }\r\n\r\n  DynamicJsonDocument doc(1024);\r\n  deserializeJson(doc, jsonControl);\r\n  JsonObject obj = doc.as&lt;JsonObject&gt;();\r\n\r\n  String power = obj[\"power\"];\r\n  Serial.println(power);\r\n\r\n  if (power == \"on\") {\r\n    digitalWrite(D2, HIGH);\r\n  }\r\n\r\n  if (power == \"off\") {\r\n    digitalWrite(D2, LOW);\r\n  }\r\n}<\/pre>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"5951\" 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;3,560&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u70ba\u4e86\u53ef\u4ee5\u64c1\u6709\u6307\u4ee4\u64f4\u5145\u80fd\u529b\uff0c\u9084\u662f...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"5951\" 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;3,560&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,306,272],"tags":[275,331],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/5951"}],"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=5951"}],"version-history":[{"count":6,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/5951\/revisions"}],"predecessor-version":[{"id":6538,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/5951\/revisions\/6538"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}