我已经做了很多次UDP发送/接收。但现在我被困住了。
也许我遗漏了一些代码中的愚蠢错误,或者我使用的库中存在问题。不管怎样,如果有人能帮忙,请看一下。
接收器代码
#include
#include
const char* ssid = "M3-L7";
const char* password = "mySmartChoice";
unsigned int localPort = 2390;
char packetBuffer[255];
WiFiUDP Udp;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Udp.begin(localPort);
}
void loop() {
delay(10);
if (Udp.parsePacket()) {
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
Serial.println(packetBuffer);
}
}
}
发送方/访问点的代码
#include
#include
const char *ssid = "M3-L7";
const char *password = "mySmartChoice";
unsigned int localPort = 2390;
WiFiUDP Udp;
void setup() {
delay(1000);
Serial.begin(115200);
WiFi.softAP(ssid, password);
Udp.begin(localPort);
}
void loop() {
Udp.beginPacket("192.168.4.1", localPort);
Udp.write("Hello");
Udp.endPacket();
delay(10);
}
嗯,我希望“你好”能反复印刷:D.。他们到达了连接点,但那就结束了。