// NodeMCU as UDP-slave in a WiFI-network, which get his commands from a master-NodeMCU in the same WiFI-network // // // ##### Attention: Fixed IP-Adresses are used, no DHCP! ######## // ##### therefore you have to configure on your router a non! DHCP-section ######## // // compiled with Arduino-IDE V1.6.8 // created by joergeli: http://www.arduino.joergeli.de // #include #include #include // for flashing OTA = On The Air //######################################################################################### //_________________________________________________________________________________________ // Set your own network-variables in this section !!! const char* OTA_host = "Washer-Receiver"; IPAddress ip(192, 168, 192, 15); //### local ip-address, must be the same as remoteIP in Waschmaschine-NodeMCU-Sender.ino ##### IPAddress gateway(192, 168, 192, 1); //### router IPAddress subnet(255, 255, 255, 0); //### subnet-mask IPAddress dns(192, 168, 192, 1); //### dns-server = router const char* ssid = "xxxxxxxxxxx"; //### your WLAN SSID const char* pwd = "xxxxxxxxxxx"; //### your WLAN Password unsigned int localUdpPort = 4210; //### local port to listen on, must correspond to remotePort in Waschmaschine-NodeMCU-Sender.ino ##### ! //_________________________________________________________________________________________ //######################################################################################### WiFiUDP Udp; #define Buzzer D7 // output-PIN for LED and Buzzer char command[10]; // buffer for incoming packets boolean BuzzerStatus = false; //_________________________________________________________________________________________ void setup() { Serial.begin(115200); Serial.println(); Serial.println("Start"); pinMode(Buzzer, OUTPUT); ArduinoOTA.setHostname(OTA_host); ArduinoOTA.begin(); connectWIFI(); Udp.begin(localUdpPort); Serial.println("\n connected!"); Serial.print("\n UDP-Server/WiFi-Receiver is ready at IP: "); Serial.println(WiFi.localIP()); Serial.println(""); Serial.println("\n Receiver ready."); } // end of setup //_________________________________________________________________________________________ void connectWIFI(){ WiFi.config(ip, gateway, subnet, dns); delay(100); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pwd); Serial.print("Connecting to "); Serial.println(ssid); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); digitalWrite(Buzzer, LOW); // switch off LED and Buzzer, while not joined WLAN delay(200); } Serial.print(" OK ---> "); Serial.print("IP: "); Serial.println(WiFi.localIP()); for(int i=0; i<5; i++){ //WiFi is ready digitalWrite(Buzzer, HIGH); // switch on LED and Buzzer delay(200); digitalWrite(Buzzer, LOW); // switch off LED and Buzzer delay(70); } } // end of connectWIFI //_________________________________________________________________________________________ void loop() { ArduinoOTA.handle(); //for OTA-Flashing int packetSize, len; packetSize = Udp.parsePacket(); if (packetSize) { Serial.print("Got "); Serial.print(packetSize); Serial.print(" from IP "); Serial.print(Udp.remoteIP()); Serial.print(" Port "); Serial.println(Udp.remotePort()); len = Udp.read(command, 10); BuzzerStatus = command[0]; // first byte 0 or 1 ? // Check if BuzzerStatus = 1 -> blink LED and trigger the buzzer if (BuzzerStatus) { for( int i=0; i<3; i++){ digitalWrite(Buzzer, HIGH); Serial.println("switch LED/Buzzer on"); delay (200); digitalWrite(Buzzer, LOW); Serial.println("switch LED/Buzzer off"); delay(70); } } else { digitalWrite(Buzzer, LOW); Serial.println("switch LED/Buzzer off"); } } } // end of loop