void setup() { Serial.begin(115200); pinMode(D1,OUTPUT); digitalWrite(D1,LOW); wifiManager.setBreakAfterConfig(true); if(!wifiManager.autoConnect(“AutoConnectAP”, “password”)) { Serial.println(“failed to connect and hit timeout”); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(1000); } Serial.println(WiFi.localIP()); Serial.println(“server started”); server.on(“/LED1On”, handleLed1On); //與手機通訊定義 server.on(“/LED1Off”, handleLed1Off); server.begin(); Udp.begin(localUdpPort); }
void udpprocess(){ //UDP 通訊 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); Serial.printf(“UDP packet contents: %s\n”, incomingPacket); if(strcmp(incomingPacket,”SCAN”)==0){ // send back a reply, to the IP address and port we got the packet from Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); Udp.print(WiFi.localIP()); Udp.endPacket(); Serial.println(WiFi.localIP()); } } } void loop() { server.handleClient(); udpprocess(); }