欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: 【零知ESP8266教程】WIFI TCP協議通信 TCP服務器示例 [打印本頁]

作者: roc2    時間: 2019-9-27 11:56
標題: 【零知ESP8266教程】WIFI TCP協議通信 TCP服務器示例
本帖主要講解ESP8266 WIFI功能關于TCP協議網絡傳輸的應用,這里演示了ESP8266作為TCP服務器的一個示例:
1、硬件
零知ESP8266開發板
2、軟件
(1)代碼如下:
  1. /**********************************************************
  2. *    文件: tcp-server.ino      by 零知實驗室
  3. *    -^^- 零知開源,讓電子制作變得更簡單! -^^-
  4. *    時間: 2019/06/17 14:12
  5. *    說明:
  6. ************************************************************/
  7. #include <ESP8266WiFi.h>

  8. int port = 8888;  //Port number
  9. WiFiServer server(port);

  10. //Server connect to WiFi Network
  11. const char *ssid = "xx";  //Enter your wifi SSID
  12. const char *password = "xx";  //Enter your wifi Password

  13. int count=0;

  14. // 復位或上電后運行一次:
  15. void setup() {
  16.     //在這里加入初始化相關代碼,只運行一次:
  17.     Serial.begin(115200);
  18.      
  19.     Serial.println();
  20.      
  21.     WiFi.mode(WIFI_STA);
  22.     WiFi.begin(ssid, password); //Connect to wifi
  23.      
  24.     // Wait for connection
  25.     Serial.println("Connecting to Wifi");
  26.     while (WiFi.status() != WL_CONNECTED) {
  27.         delay(500);
  28.         Serial.print(".");
  29.         delay(500);
  30.     }
  31.      
  32.     Serial.println("");
  33.     Serial.print("Connected to ");
  34.     Serial.println(ssid);
  35.      
  36.     Serial.print("IP address: ");
  37.     Serial.println(WiFi.localIP());
  38.     Serial.print("port:");
  39.     Serial.println(port);
  40.     server.begin();
  41. }

  42. //一直循環執行:
  43. void loop() {
  44.     // 在這里加入主要程序代碼,重復執行:
  45.     WiFiClient client = server.available();
  46.      
  47.     if (client) {
  48.         if(client.connected())
  49.         {
  50.             Serial.println("Client Connected");
  51.         }
  52.          
  53.         while(client.connected()){
  54.             while(client.available()>0){
  55.                 // read data from the connected client
  56.                 Serial.write(client.read());
  57.             }
  58.             //Send Data to connected client
  59.             while(Serial.available()>0)
  60.             {
  61.                 client.write(Serial.read());
  62.             }
  63.         }
  64.         client.stop();
  65.         Serial.println("Client disconnected");
  66.     }
  67. }
復制代碼
3、驗證測試
(1)將上面代碼驗證后并上傳到零知ESP8266,然后打開串口調試窗口,可以看到如下信息:


現在已經將ESP8266作為TCP服務器了,上面信息為IP和端口
(2)打開零知工具箱的網絡調試窗口,然后選擇TCP客戶端,并填寫上面的IP和端口:
PS:零知工具箱請至零知官網“軟件下載”頁面獲取


(3)點擊【連接】后,就與ESP8266建立的TCP連接了,就可以發送和接收數據了,如下:









歡迎光臨 (http://m.raoushi.com/bbs/) Powered by Discuz! X3.1