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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1591|回復: 0
收起左側

【零知ESP8266教程】STATION模式下WIFI UDP協議通信示例

[復制鏈接]
ID:349555 發表于 2019-9-25 10:20 | 顯示全部樓層 |閱讀模式
本帖主要講解ESP8266 WIFI功能關于UDP協議網絡傳輸的應用,這里演示了ESP8266在STATION模式下UDP通信的示例:
1、硬件
零知ESP8266
1.png
2、軟件
(1)代碼如下:
  1.     /**********************************************************
  2.     *    文件: esp8266-udp-clinet.ino      by 零知實驗室
  3.     *    -^^- 零知開源,讓電子制作變得更簡單! -^^-
  4.     *    時間: 2019/06/17 11:01
  5.     *    說明:
  6.     ************************************************************/
  7.     #include <ESP8266WiFi.h>
  8.     #include <WiFiUDP.h>

  9.     #define SSID "xx"
  10.     #define PSSWD "xx"

  11.     unsigned int UDPPort = 8888;      // local port to listen on

  12.     char packetBuffer[255]; //buffer to hold incoming packet
  13.     char  replyBuffer[] = "send-back-ack";       // a string to send back

  14.     WiFiUDP Udp;

  15.     // 復位或上電后運行一次:
  16.     void setup() {
  17.             //在這里加入初始化相關代碼,只運行一次:
  18.             Serial.begin(115200);
  19.             WiFi.mode(WIFI_STA);
  20.             WiFi.begin(SSID, PSSWD);
  21.             while (WiFi.status() != WL_CONNECTED) {
  22.                     Serial.print('.');
  23.                     delay(500);
  24.             }
  25.             Serial.print("Connected! IP address: ");
  26.             Serial.println(WiFi.localIP());
  27.             Serial.printf("UDP server on port %d\n", UDPPort);
  28.             Udp.begin(UDPPort);
  29.             
  30.     //        Udp.beginPacket("192.168.4.1", UDPPort);//send ip to server
  31.     //        char ipBuffer[255];
  32.     //        WiFi.localIP().toString().toCharArray(ipBuffer, 255);
  33.     //        Udp.write(ipBuffer);
  34.     //        Udp.endPacket();
  35.     //        Serial.println("Sent ip adress to server");
  36.     }

  37.     //一直循環執行:
  38.     void loop() {
  39.             // 在這里加入主要程序代碼,重復執行:
  40.             // if there's data available, read a packet
  41.             int packetSize = Udp.parsePacket();
  42.             if (packetSize) {
  43.                     Serial.print("Received packet of size ");
  44.                     Serial.println(packetSize);
  45.                     Serial.print("From ");
  46.                     IPAddress remoteIp = Udp.remoteIP();
  47.                     Serial.print(remoteIp);
  48.                     Serial.print(", port ");
  49.                     Serial.println(Udp.remotePort());
  50.                      
  51.                     // read the packet into packetBufffer
  52.                     int len = Udp.read(packetBuffer, 255);
  53.                     if (len > 0) {
  54.                             packetBuffer[len] = 0;
  55.                     }
  56.                     Serial.println("Contents:");
  57.                     Serial.println(packetBuffer);
  58.                      
  59.                     // send a reply, to the IP address and port that sent us the packet we received
  60.                     Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  61.                     Udp.write(replyBuffer);
  62.                     Udp.endPacket();
  63.             }
  64.     }
復制代碼
(2)將上面代碼驗證后并上傳到零知ESP8266開發板中,然后打開串口調試窗口,可以看到如下信息:
2.jpg

上面顯示了ESP8266的IP地址和端口號信息。


(3)現在打開零知工具箱(可以在“軟件下載”頁面下載),然后打開“網絡調試”界面,選擇UDP模式并選擇UDP的IP和端口號,如下:

TIP:零知工具箱請在零知實驗室官網下載哦
3.jpg




3、測試驗證
在零知工具箱中,點擊【連接】,然后就可以和零知ESP8266開始通信了,我們在發送窗口填寫發送的信息,點擊【發送】,可以看到如下信息,表明通信雙方成功:
4.jpg

更多精彩請訪問零知官網
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表