char ssid[] = "\*\*\*\*"; char pass[] = "\*\*\*\*"; // 无线状态 int status = WL_IDLE_STATUS; int reqCount = 0;
unsigned int localPort = 8883; //10002; // local port to listen on
char packetBuffer[255]; // buffer to hold incoming packet char ReplyBuffer[] = "ACK"; // a string to send back
ESP8266_AT_UDP Udp;
void setup() { Serial.begin(115200); while (!Serial);
Serial.print(F("\nStarting ConnectWPA on ")); Serial.print(BOARD_NAME); Serial.print(F(" with ")); Serial.println(SHIELD_TYPE); Serial.println(ESP8266_AT_WEBSERVER_VERSION);
// 检查ESP-01与开发板连接状态 if (WiFi.status() == WL_NO_SHIELD) { Serial.println(F("WiFi shield not present")); while (true); }
// 连接网络 while ( status != WL_CONNECTED) { Serial.print(F("Connecting to WPA SSID: ")); Serial.println(ssid); // 连接WiFi热点 status = WiFi.begin(ssid, pass); }
Serial.print(F("You're connected to the network, IP = ")); Serial.println(WiFi.localIP()); Serial.println(F("\nStarting connection to server...")); Udp.begin(localPort);
}
void loop() { // 接收数据 int packetSize = Udp.parsePacket();
if (packetSize) { Serial.print(F("Received packet of size ")); Serial.println(packetSize); Serial.print(F("From ")); IPAddress remoteIp = Udp.remoteIP(); Serial.print(remoteIp); Serial.print(F(", port ")); Serial.println(Udp.remotePort());
// 读取数据 int len = Udp.read(packetBuffer, 255); if (len > 0) { packetBuffer[len] = 0; }