📜  使用静态 ip 运行时无法通过 udp 访问 esp8266 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:40.591000             🧑  作者: Mango

代码示例3
#include 
#include 

IPAddress ip_sta;
WiFiUDP udp;

void setup() {
    Serial1.begin(921600);
    WiFi.begin("testtest", "testtest");
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial1.print(".");
    }

    //Set client to static IP
    ip_sta = WiFi.localIP();
    ip_sta[3] = 115;
    WiFi.config(ip_sta, WiFi.gatewayIP(), WiFi.subnetMask()); //if comment this line code runs perfectly

    udp.begin(2424);
}

void loop() {
    int packetsize = udp.parsePacket();
    if (packetsize) {
        char packetBuffer[32];
        int len = udp.read(packetBuffer, 32);
        packetBuffer[31] = 0;
        Serial1.print("PACKET: "); Serial1.println(packetBuffer);
        Serial1.print("LENGTH: "); Serial1.println(len);
    }
}