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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4080|回復(fù): 0
收起左側(cè)

零知ESP8266 AP模式下WIFI UDP協(xié)議通信示例

[復(fù)制鏈接]
ID:349555 發(fā)表于 2019-9-24 13:49 | 顯示全部樓層 |閱讀模式
本帖最后由 roc2 于 2019-9-25 09:49 編輯

本帖主要講解ESP8266 WIFI功能關(guān)于UDP協(xié)議網(wǎng)絡(luò)傳輸?shù)膽?yīng)用,這里演示了ESP8266在AP模式下UDP通信的示例:
1、硬件
- 零知ESP8266開發(fā)板  
2、軟件
(1)代碼如下:
  1. #include <WiFiUDP.h>

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

  3. char packetBuffer[255]; //buffer to hold incoming packet
  4. char  ReplyBuffer[] = "acknowledged";       // a string to send back
  5. WiFiUDP Udp;

  6. // 復(fù)位或上電后運(yùn)行一次:
  7. void setup() {
  8.         //在這里加入初始化相關(guān)代碼,只運(yùn)行一次:
  9.         Serial.begin(115200);
  10.          
  11.         WiFi.softAP("Wi-Fi");
  12.         Udp.begin(UDPPort);
  13.         Serial.println();
  14.         Serial.println("Started ap. Local ip: " + WiFi.localIP().toString());
  15. }

  16. //一直循環(huán)執(zhí)行:
  17. void loop() {
  18.         // 在這里加入主要程序代碼,重復(fù)執(zhí)行:
  19.         // if there's data available, read a packet
  20.         int packetSize = Udp.parsePacket();
  21.         if (packetSize) {
  22.                 Serial.print("Received packet of size ");
  23.                 Serial.println(packetSize);
  24.                 Serial.print("From ");
  25.                 IPAddress remoteIp = Udp.remoteIP();
  26.                 Serial.print(remoteIp);
  27.                 Serial.print(", port ");
  28.                 Serial.println(Udp.remotePort());
  29.                  
  30.                 // read the packet into packetBufffer
  31.                 int len = Udp.read(packetBuffer, 255);
  32.                 if (len > 0) {
  33.                         packetBuffer[len] = 0;
  34.                 }
  35.                 Serial.println("Contents:");
  36.                 Serial.println(packetBuffer);
  37.                 // send a reply, to the IP address and port that sent us the packet we received
  38.                 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  39.                 Udp.write(ReplyBuffer);
  40.                 Udp.endPacket();
  41.         }
  42. }
復(fù)制代碼

(2)將上述代碼驗(yàn)證后上傳到零知ESP8266,然后打開串口調(diào)試 窗口,可以看到如下信息:
1.jpg


(3)上面步驟完成后我們已經(jīng)把ESP8266作為一個熱點(diǎn),SSID名字為"WI-FI”,可以在電腦上看到如下信息:
2.jpg 3.jpg


(4)我們打開零知工具箱,然后填寫好IP地址和端口號,點(diǎn)擊【連接】后就可以和ESP8266進(jìn)行通信了。
3、測試驗(yàn)證:
可以在串口調(diào)試窗口和零知工具箱發(fā)送接收區(qū)看到如下數(shù)據(jù)傳輸信息:
4.jpg

回復(fù)

使用道具 舉報(bào)

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

本版積分規(guī)則

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

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表