// NodeMCU as master in a Wifi-network // NodeMCU sends commands via UDP to a NodeMCU-Slave in the same WiFi-network // Here used in combination with a gyroscope sensor MPU6050, to detect when laundryis ready // // ##### 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 #include #include "I2Cdev.h" #include "MPU6050.h" #define LED_motionDetected D8 // LED lights up, when either LEDx, LEDy, or LEDz is on #define LEDx D7 // Movement x-direction #define LEDy D6 // Movement y-direction #define LEDz D5 // Movement z-direction #define LED_ready D0 // Power control and blink, when no movement is detected since "offTime"-seconds MPU6050 accelgyro; int16_t ax, ay, az; // not used here, only for the sake of completeness int16_t gx, gy, gz; #define OUTPUT_READABLE_ACCELGYRO //######################################################################################### //_________________________________________________________________________________________ // Set your own network-variables in this section !!! const char* OTA_host = "Washer-Sender"; const char* ssid = "xxxxxxxxxxx"; //### your WLAN SSID //### the name of your WiFi-network / <= 31 Signs const char* pwd = "xxxxxxxxxxx"; //### your WLAN Password //### the password of your WiFi-network / >= 8 or <= 63 signs or NULL IPAddress ip(192, 168, 192, 14); //### local ip-address IPAddress gateway(192, 168, 192, 1); //### router IPAddress subnet(255, 255, 255, 0); //### subnet-mask IPAddress dns(192, 168, 192, 1); //### dns-server = router IPAddress remoteIP(192,168,192,15); //### IP-Adress of the Receiver!!! NodeMCU ######## unsigned int localUdpPort = 4210; //### local port to listen on unsigned int remotePort = 4210; //### remote port to listen on, must correspond to localUdpPort ! //_________________________________________________________________________________________ /* The following variables are depending on your washer. Turn on the washer and look at the flahing of the 3 LED's (LEDx, LEDy, LEDz) They must detect the movement, on the other hand, they must be off, if the washer has finished Just try and adjust the 3 thresholds */ long offTime = 90000; // when there is no motion detected since offTime (here 90 seconds) -> trigger output int gx_thres = 300; //### threshold of x movement // depending on your washer! int gy_thres = 300; //### threshold of y movement // depending on your washer! int gz_thres = 720; //### threshold of z movement // depending on your washer! //_________________________________________________________________________________________ //######################################################################################### char command[10]; String sensor_x; String sensor_y; String sensor_z; long startTime; long duration = 0; WiFiUDP Udp; uint32_t aktMillis; uint32_t prevMillis; //_________________________________________________________________________________________________________ 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(LED_ready, 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<10; i++){ //WiFi is ready digitalWrite(LED_ready, HIGH); // switch on LED delay(200); digitalWrite(LED_ready, LOW); // switch off LED delay(70); } } // end of connectWIFI //_________________________________________________________________________________________ void setup() { pinMode(LED_motionDetected, OUTPUT); pinMode(LEDx, OUTPUT); pinMode(LEDy, OUTPUT); pinMode(LEDz, OUTPUT); pinMode(LED_ready, OUTPUT); digitalWrite(LED_motionDetected, LOW); digitalWrite(LEDx, LOW); digitalWrite(LEDy, LOW); digitalWrite(LEDz, LOW); Serial.begin(115200); Serial.println(); Serial.println("Start"); Serial.print("Connecting to "); Serial.println(ssid); ArduinoOTA.setHostname(OTA_host); ArduinoOTA.begin(); connectWIFI(); Serial.println(""); Wire.begin(); Serial.println("Initializing I2C devices..."); accelgyro.initialize(); Serial.print("Testing device connections: "); Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); delay(1000); Serial.println(F("MPU6050 is running ...")); Serial.println(""); digitalWrite(LED_ready, HIGH); // high for using also as power-control } // end of setup //_________________________________________________________________________________________________________ void loop() { ArduinoOTA.handle(); //for OTA-Flashing if(duration < offTime){ // read raw accel/gyro measurements from device accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // turn on correspondigly LEDs, if motion detected // the threshold-values are depending on the vibration of your washer, just watch the x,y,z-LEDs if( gx > gx_thres || gx < -gx_thres){ digitalWrite(LEDx, HIGH); sensor_x = "x"; } if( gy > gy_thres || gy < -gy_thres){ digitalWrite(LEDy, HIGH); sensor_y = "y"; } if( gz > gz_thres || gz < -gz_thres){ digitalWrite(LEDz, HIGH); sensor_z = "z"; } // compare if any of the three sensors (gx,gy,gz) has movement detected if (digitalRead(LEDx) == HIGH || digitalRead(LEDy) == HIGH || digitalRead(LEDz) == HIGH) { digitalWrite(LED_motionDetected, HIGH); // turn on LED_motionDetected } if (digitalRead(LED_motionDetected) == HIGH){ Serial.print("Motion detected from sensor: "); Serial.print(sensor_x); Serial.print(" "); Serial.print(sensor_y); Serial.print(" "); Serial.println(sensor_z); startTime = millis(); // reset timer } duration = millis() - startTime; delay(20); // turn off all LEDs and reset sensor-variables digitalWrite(LEDx, LOW); digitalWrite(LEDy, LOW); digitalWrite(LEDz, LOW); digitalWrite(LED_motionDetected, LOW); sensor_x = ""; sensor_y = ""; sensor_z = ""; } // end of "if duration < offTime" // if washer has finished: turn on LED_ready, respectively send trigger via WiFi to slave-NodeMCU if(duration >= offTime){ LaundryReady(); } } // end of loop //_________________________________________________________________________________________________________ void LaundryReady(){ //Send approx. every 5 seconds "1" to slave-NodeMCU (you must switch off power manual to stop) Serial.println("Laundry is ready!"); digitalWrite(LED_ready, LOW); // optical recognition, that laundry is ready digitalWrite(LED_motionDetected, HIGH); // optical recognition, that laundry is ready command[0] = 1; Udp.beginPacket(remoteIP, remotePort); Udp.write(command, 1); Udp.endPacket(); delay (5000); digitalWrite(LED_motionDetected, LOW); // optical recognition, that laundry is ready command[0] = 0; Udp.beginPacket(remoteIP, remotePort); Udp.write(command, 1); Udp.endPacket(); delay(200); }