赞
踩
arduino代码如下:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "HelloWifi";
const char* password = "123ab";
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyPacket);
Udp.endPacket();
}
}
输出log:
SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)
Connecting to HelloWifi mode : sta(b4:e6:2d:3f:09:25) + softAP(b6:e6:2d:3f:09:25)
add if0
.....scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 3
connected with HelloWifi, channel 1
dhcp client start...
cnt
.........ip:192.168.0.103,mask:255.255.255.0,gw:192.168.0.1
. connected
Now listening at IP 192.168.0.103, UDP port 4210
Received 12 bytes from 192.168.0.101, port 64068
UDP packet contents: Hello World!
Received 12 bytes from 192.168.0.101, port 64068
UDP packet contents: Hello World!
Received 12 bytes from 192.168.0.101, port 64068
UDP packet contents: Hello World!
测试工具:
https://packetsender.com/download
How to Check It?
Upload sketch to module and open serial monitor. You should see confirmation that ESP has connected to Wi-Fi and started listening to UDP packets:
Connecting to twc-net-3 …….. connected
Now listening at IP 192.168.1.104, UDP port 4210
Now we need another application to send some packets to IP and port shown by ESP above.
Instead of programming another ESP, let’s make it easier and use a purpose build application. I have selected the Packet Sender. It is available for popular operating systems. Download, install and execute it.
Once Packet Sender’s window show up enter the following information: * Name of the packet * ASCII text of the message to be send inside the packet * IP Address shown by our ESP * Port shown by the ESP * Select UDP
What I have entered is shown below:
Now click Send.
Immediately after that you should see the following on ESP’s serial monitor:
Received 12 bytes from 192.168.1.106, port 55056
UDP packet contents: Hello World!
The text 192.168.1.106, port 55056 identifies a PC where the packet is send from. You will likely see different values.
As ESP sends an acknowledge packet back, you should see it in the log in the bottom part of the Packet Sender’s window.
Conclusion
This simple example shows how to send and receive UDP packets between ESP and an external application. Once tested in this minimal set up, you should be able to program ESP to talk to any other UDP device. In case of issues to establish communication with a new device, use the Packet Sender or other similar program for troubleshooting
For review of functions provided to send and receive UDP packets, please refer to the UDP Class documentation.
参考网址:
https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/udp-examples.rst
ESP8266–学习笔记(三)AP和UDP通信
https://blog.csdn.net/qq_28877125/article/details/62448119
PacketSenderPortable_v5.7.52.zip 工具
另外一个备份地址:https://download.csdn.net/download/wowocpp/10556531
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。