标题: 【零知ESP8266教程】STATION模式下WIFI UDP协议通信示例 [打印本页]

作者: roc2    时间: 2019-9-25 10:20
标题: 【零知ESP8266教程】STATION模式下WIFI UDP协议通信示例
本帖主要讲解ESP8266 WIFI功能关于UDP协议网络传输的应用,这里演示了ESP8266在STATION模式下UDP通信的示例:
1、硬件
零知ESP8266

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开发板中,然后打开串口调试窗口,可以看到如下信息:


上面显示了ESP8266的IP地址和端口号信息。


(3)现在打开零知工具箱(可以在“软件下载”页面下载),然后打开“网络调试”界面,选择UDP模式并选择UDP的IP和端口号,如下:

TIP:零知工具箱请在零知实验室官网下载哦





3、测试验证
在零知工具箱中,点击【连接】,然后就可以和零知ESP8266开始通信了,我们在发送窗口填写发送的信息,点击【发送】,可以看到如下信息,表明通信双方成功:


更多精彩请访问零知官网




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1