{"id":4593,"date":"2019-11-30T00:21:47","date_gmt":"2019-11-29T16:21:47","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=4593"},"modified":"2023-04-14T09:16:17","modified_gmt":"2023-04-14T01:16:17","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-5-%e6%94%b9%e7%94%a8-socket-%e9%80%9a%e8%a8%8a%e7%82%ba%e4%b8%bb%e5%8b%95%e6%8e%a7%e5%88%b6%e5%81%9a%e6%ba%96%e5%82%99","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=4593","title":{"rendered":"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"},"content":{"rendered":"<p>\u5728\u5b8c\u6574\u7684\u61c9\u7528\uff0cESP8266 \u9664\u4e86\u4e0a\u50b3\u76e3\u6e2c\u5668\u6578\u64da\uff0c\u9084\u9700\u8981\u63a5\u6536\u4e3b\u6a5f\u9001\u904e\u4f86\u7684\u7e7c\u96fb\u5668\u63a7\u5236\u8a0a\u865f\uff0c\u5fc5\u9808\u5efa\u7acb\u5373\u6642\u3001\u53ef\u63a7\u7684\u96d9\u5411\u901a\u8a0a\u6a5f\u5236<\/p>\n<p>\u70ba\u4e86\u7c21\u7d04\u958b\u767c\u4f7f\u7528 TCP Text \u901a\u8a0a JSON \u7576\u6210\u901a\u8a0a\u683c\u5f0f\uff0c\u6700\u5f8c\u52a0\u4e0a SSL \u52a0\u5bc6\u5c31\u5b8c\u6210\u898f\u5283<\/p>\n<p>\u6b64\u968e\u6bb5\u53ea\u505a\u5230 Text \u96d9\u5411\u901a\u8a0a\u5b8c\u7d50<\/p>\n<p>--<\/p>\n<h2>TCP Server<\/h2>\n<p>\u9019\u88e1\u9084\u662f\u4f7f\u7528\u591a\u5e74\u4ee5\u524d\u4f7f\u7528\u7684 TCP Server Tool \u4f86\u64d4\u4efb Server \u89d2\u8272\uff0c\u7b49 Client \u958b\u767c\u5b8c\u6210\u518d\u81ea\u884c\u67b6\u8a2d<\/p>\n<figure id=\"attachment_6513\" aria-describedby=\"caption-attachment-6513\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Socket_TCPServerTool.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"wp-image-6513 size-medium\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Socket_TCPServerTool-300x282.png\" alt=\"\" width=\"300\" height=\"282\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Socket_TCPServerTool-300x282.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Socket_TCPServerTool-319x300.png 319w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/Socket_TCPServerTool.png 700w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-6513\" class=\"wp-caption-text\">Socket_TCPServerTool<\/figcaption><\/figure>\n<p>--<\/p>\n<h2>ESP8266 TCP Client<\/h2>\n<p>ESP8266 \u4f7f\u7528 TCP Socket \u505a\u901a\u8a0a\u6c92\u6709\u60f3\u50cf\u4e2d\u7684\u56f0\u96e3\uff0c\u53cd\u800c\u662f\u76f8\u7576\u7684\u5bb9\u6613<\/p>\n<pre class=\"lang:arduino decode:true\">#include &lt;ESP8266WiFi.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  client.println(i++);\r\n  delay(1000);\r\n}<\/pre>\n<p>\u4f7f\u7528 netstat -nt \u6307\u4ee4\u67e5\u770b\u78ba\u5b9a\u7db2\u8def\u901a\u8a0a\u72c0\u614b\u6b63\u5e38<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266Client_netstat.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-6515\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266Client_netstat-300x134.png\" alt=\"\" width=\"300\" height=\"134\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266Client_netstat-300x134.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266Client_netstat-768x343.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266Client_netstat-500x223.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266Client_netstat.png 813w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>--<\/p>\n<h2>TCP Server \u63a8\u9001<\/h2>\n<p>ESP8266 Arduino<\/p>\n<pre class=\"lang:arduino decode:true \">#include &lt;ESP8266WiFi.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  \/\/\r\n  if (!client.connected()) {\r\n    client.connect(host, port);\r\n    client.println(\"connected...\");\r\n  }\r\n\r\n  String Control = client.readStringUntil('\\n');\r\n  Serial.println(Control);\r\n\r\n  delay(1000);\r\n}<\/pre>\n<ul>\n<li>client.readStringUntil('\\n'); \u5f9e client \u63a5\u6536\u5b57\u4e32\uff0c\u4f7f\u7528 \\n \u63db\u884c\u5340\u9694<\/li>\n<li>Serial.println(Control); \u5c07\u6536\u5230\u7684\u5b57\u4e32\u8f38\u51fa\u770b\u770b<\/li>\n<\/ul>\n<p>\u5f9e TCP Server Tool \u767c\u9001\u8a0a\u606f\uff0c\u6703\u81ea\u52d5\u9001\u51fa\u63db\u884c\uff0c\u5176\u4ed6\u8edf\u9ad4\u8acb\u6ce8\u610f\u8a2d\u5b9a<\/p>\n<figure id=\"attachment_6517\" aria-describedby=\"caption-attachment-6517\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_TCPServer_Push.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"size-medium wp-image-6517\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_TCPServer_Push-300x282.png\" alt=\"\" width=\"300\" height=\"282\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_TCPServer_Push-300x282.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_TCPServer_Push-319x300.png 319w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_TCPServer_Push.png 700w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-6517\" class=\"wp-caption-text\">ESP8266_SocketClient_TCPServer_Push<\/figcaption><\/figure>\n<p>\u5728\u4e32\u5217\u76e3\u8996\u5c31\u53ef\u4ee5\u6536\u5230\u8a0a\u606f<\/p>\n<figure id=\"attachment_6516\" aria-describedby=\"caption-attachment-6516\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_Serial.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"size-medium wp-image-6516\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_Serial-300x207.png\" alt=\"\" width=\"300\" height=\"207\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_Serial-300x207.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_Serial-435x300.png 435w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/ESP8266_SocketClient_Serial.png 656w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-6516\" class=\"wp-caption-text\">ESP8266_SocketClient_Seria<\/figcaption><\/figure>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"4593\" 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;5,713&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b8c\u6574\u7684\u61c9\u7528\uff0cESP8266 ...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"4593\" 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;5,713&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\/4593"}],"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=4593"}],"version-history":[{"count":8,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4593\/revisions"}],"predecessor-version":[{"id":6519,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4593\/revisions\/6519"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}