【零知ESP8266教程】AP模式下WIFI UDP协议通信示例

[复制链接]
 楼主| roc2two 发表于 2019-9-24 09:36 | 显示全部楼层 |阅读模式
本帖最后由 roc2two 于 2019-9-25 09:53 编辑

本帖主要讲解ESP8266 WIFI功能关于UDP协议网络传输的应用,这里演示了ESP8266在AP模式下UDP通信的示例:

1、硬件:
零知ESP8266开发板
2、软件
(1)代码如下:
  1. /**********************************************************
  2.         
  3. *    文件: udp-server.ino      by 零知实验室(www.lingzhilab.com)
  4.         
  5. *    -^^- 零知开源,让电子制作变得更简单! -^^-
  6.         
  7. *    时间: 2019/06/17 11:42
  8.         
  9. *    说明:
  10.         
  11. ************************************************************/
  12.         
  13. #include <ESP8266WiFi.h>
  14.         
  15. #include <WiFiUDP.h>
  16.         

  17.         
  18. unsigned int UDPPort = 8888;      // local port to listen on
  19.         

  20.         
  21. char packetBuffer[255]; //buffer to hold incoming packet
  22.         
  23. char  ReplyBuffer[] = "acknowledged";       // a string to send back
  24.         
  25. WiFiUDP Udp;
  26.         

  27.         
  28. // 复位或上电后运行一次:
  29.         
  30. void setup() {
  31.         
  32.         //在这里加入初始化相关代码,只运行一次:
  33.         
  34.         Serial.begin(115200);
  35.         
  36.          
  37.         
  38.         WiFi.softAP("Wi-Fi");
  39.         
  40.         Udp.begin(UDPPort);
  41.         
  42.         Serial.println();
  43.         
  44.         Serial.println("Started ap. Local ip: " + WiFi.localIP().toString());
  45.         
  46. }
  47.         

  48.         
  49. //一直循环执行:
  50.         
  51. void loop() {
  52.         
  53.         // 在这里加入主要程序代码,重复执行:
  54.         
  55.         // if there's data available, read a packet
  56.         
  57.         int packetSize = Udp.parsePacket();
  58.         
  59.         if (packetSize) {
  60.         
  61.                 Serial.print("Received packet of size ");
  62.         
  63.                 Serial.println(packetSize);
  64.         
  65.                 Serial.print("From ");
  66.         
  67.                 IPAddress remoteIp = Udp.remoteIP();
  68.         
  69.                 Serial.print(remoteIp);
  70.         
  71.                 Serial.print(", port ");
  72.         
  73.                 Serial.println(Udp.remotePort());
  74.         
  75.                  
  76.         
  77.                 // read the packet into packetBufffer
  78.         
  79.                 int len = Udp.read(packetBuffer, 255);
  80.         
  81.                 if (len > 0) {
  82.         
  83.                         packetBuffer[len] = 0;
  84.         
  85.                 }
  86.         
  87.                 Serial.println("Contents:");
  88.         
  89.                 Serial.println(packetBuffer);
  90.         
  91.                 // send a reply, to the IP address and port that sent us the packet we received
  92.         
  93.                 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  94.         
  95.                 Udp.write(ReplyBuffer);
  96.         
  97.                 Udp.endPacket();
  98.         
  99.         }
  100.         
  101. }

(2)将上述代码验证后上传到零知ESP8266,然后打开串口调试 窗口,可以看到如下信息:
(3)上面步骤完成后我们已经把ESP8266作为一个热点,SSID名字为"WI-FI”,可以在电脑上看到如下信息:
(4)我们打开零知工具箱,然后填写好IP地址和端口号,点击【连接】后就可以和ESP8266进行通信了。

3、测试验证:
可以在串口调试窗口和零知工具箱发送接收区看到如下数据传输信息:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

75

主题

85

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

75

主题

85

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部