esp8266 problem no delay time possible in my programm

Dear everybody,

I want to control a pump via alexa and an esp8266 (plus relais). The problem I have is that I just wanna switch on the pump for a small time 5s therefore I need to insert a delay for my GPIO somehow The Code i use looksas followed:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID ""//change your Wifi name
#define WIFI_PASS "
**"//Change your Wifi Password
#define SERIAL_BAUDRATE 115200

fauxmoESP fauxmo;
//declare switching pins
//Change pins according to your NodeMCU pinouts
#define Kitchen D1
#define Bedroom D2
#define Living D3
// -----------------------------------------------------------------------------
// Wifi Setup
// -----------------------------------------------------------------------------

void wifiSetup() {

// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);

// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);

// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();

// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
// -----------------------------------------------------------------------------
// Device Callback
// -----------------------------------------------------------------------------
void callback(uint8_t device_id, const char * device_name, bool state) {
Serial.print("Device "); Serial.print(device_name);
Serial.print(" state: ");
if (state) {
Serial.println("ON");
} else {
Serial.println("OFF");
}
//Switching action on detection of device name
if ( (strcmp(device_name, "Pumpe") == 0) ) {
// adjust the relay immediately!
if (state) {
digitalWrite(Kitchen, HIGH);

} else {
digitalWrite(Kitchen, LOW);
}
}delay (500);

}
void setup() {
//Initialize pins to Low on device start
pinMode(Kitchen, OUTPUT);
digitalWrite(Kitchen, LOW);

// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println("FauxMo demo sketch");
Serial.println("After connection, ask Alexa/Echo to 'turn on' or 'off'");

// Wifi
wifiSetup();

// Device Names for Simulated Wemo switches
fauxmo.addDevice ("Pumpe");
fauxmo.onMessage(callback);

}

void loop() {
fauxmo.handle();
}

could someone please tell were and how I can install the delay/timer

thank you very much for you help.

best regards

Toshiro

First of all, please use code tags when posting code on this forum.

Here's something you can try:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID "*******"//change your Wifi name
#define WIFI_PASS "*********"//Change your Wifi Password
#define SERIAL_BAUDRATE                 115200

fauxmoESP fauxmo;
//declare switching pins
//Change pins according to your NodeMCU pinouts
#define Kitchen D1
#define Bedroom D2
#define Living D3
// -----------------------------------------------------------------------------
// Wifi Setup
// -----------------------------------------------------------------------------

bool turnOn = false;
bool timerStarted = false;
unsigned long previousMillis = 0;

void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);

  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // Wait
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();

  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
// -----------------------------------------------------------------------------
// Device Callback
// -----------------------------------------------------------------------------
void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.print("Device "); Serial.print(device_name);
  Serial.print(" state: ");
  if (state) {
    Serial.println("ON");
  } else {
    Serial.println("OFF");
  }
  
  // Switching action on detection of device name
  if ( (strcmp(device_name, "Pumpe") == 0) ) { // You might have spelled "Pump" incorrectly
    // adjust the relay immediately!
    if (state) {
      turnOn = true; // Set flag to turn appliance on
    }
  }
  
  delay(500); // What's this for?
}
void setup() {
  //Initialize pins to Low on device start
  pinMode(Kitchen, OUTPUT);
  digitalWrite(Kitchen, LOW);

  // Init serial port and clean garbage
  Serial.begin(SERIAL_BAUDRATE);
  Serial.println("FauxMo demo sketch");
  Serial.println("After connection, ask Alexa/Echo to 'turn <devicename> on' or 'off'");

  // Wifi
  wifiSetup();

  // Device Names for Simulated Wemo switches
  fauxmo.addDevice ("Pumpe");
  fauxmo.onMessage(callback);

}

void loop() {
  fauxmo.handle();

  if (turnOn) {
    digitalWrite(Kitchen, HIGH);
    turnOn = false; // Reset flag so this won't run again repeatedly
    timerStarted = true; // Set a new flag indicating that the timer was started
    previousMillis = millis(); // Get the initial time the appliance was turned on
  }
  else {
    digitalWrite(Kitchen, LOW);
  }
  
  if (timerStarted && millis() - previousMillis > 5000) { // Check if appliance was turned on for 5s
    digitalWrite(Kitchen, LOW); // Turn off the pump after 5s
    timerStarted = false; // Reset flag so it won't keep checking this if statement
  }
}

First thank you very much for you help so far your code is not working the error message is flag1 is not declared in this scope. could you tell me want I have to change? very much for your help!!

best regards

Sorry, I didn't have the fauxmo library so I couldn't really compile it. Please see the updated code (I edited my previous post).

mhh now the error message is that priviousMillis is not declared inthis slope?? (thanks you so much that you help me)

See the new code.

androidfanboy:
See the new code.

Please don't make a habit of going back and editing previously posted code. It can make the thread confusing to follow.

OK sorry. I just didn't want to add one line and post the new code again. I guess I could just mention what to add instead. I'll note that for next time :slight_smile:

the code is now correct but it is still not working. I attached a LED so that I can see the results easier. When I start the pump by alexa the LED is just blinking for less then 1 s.

I put some serial prints and this should help you debug:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID "*******"//change your Wifi name
#define WIFI_PASS "*********"//Change your Wifi Password
#define SERIAL_BAUDRATE                 115200

fauxmoESP fauxmo;
//declare switching pins
//Change pins according to your NodeMCU pinouts
#define Kitchen D1
#define Bedroom D2
#define Living D3
// -----------------------------------------------------------------------------
// Wifi Setup
// -----------------------------------------------------------------------------

bool turnOn = false;
bool timerStarted = false;

void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);

  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // Wait
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();

  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
// -----------------------------------------------------------------------------
// Device Callback
// -----------------------------------------------------------------------------
void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.print("Device "); Serial.print(device_name);
  Serial.print(" state: ");
  if (state) {
    Serial.println("ON");
  } else {
    Serial.println("OFF");
  }
  
  // Switching action on detection of device name
  if ( (strcmp(device_name, "Pumpe") == 0) ) { // You might have spelled "Pump" incorrectly
    // adjust the relay immediately!
    if (state) {
      turnOn = true; // Set flag to turn appliance on
      Serial.println("Flag set to true!");
    }
  }
  
  delay(500); // What's this for?
}

void setup() {
  //Initialize pins to Low on device start
  pinMode(Kitchen, OUTPUT);
  digitalWrite(Kitchen, LOW);

  // Init serial port and clean garbage
  Serial.begin(SERIAL_BAUDRATE);
  Serial.println("FauxMo demo sketch");
  Serial.println("After connection, ask Alexa/Echo to 'turn <devicename> on' or 'off'");

  // Wifi
  wifiSetup();

  // Device Names for Simulated Wemo switches
  fauxmo.addDevice ("Pumpe");
  fauxmo.onMessage(callback);
}

void loop() {
  fauxmo.handle();

  if (turnOn) {
    digitalWrite(Kitchen, HIGH);
    turnOn = false; // Reset flag so this won't run again repeatedly
    timerStarted = true; // Set a new flag indicating that the timer was started
    previousMillis = millis(); // Get the initial time the appliance was turned on
    Serial.println("Pump turned on!");
  }
  else {
    digitalWrite(Kitchen, LOW);
    Serial.println("Pump is off");
  }
  
  if (timerStarted && millis() - previousMillis > 5000) { // Check if appliance was turned on for 5s
    digitalWrite(Kitchen, LOW); // Turn off the pump after 5s
    timerStarted = false; // Reset flag so it won't keep checking this if statement
    Serial.println("Pump turned off after 5s!");
  }
}

Let me know which ones print out and when.

=( still not working and when look at the serial monitor I just see "undefined letters"

You don't seem to be setting the variable turnOn to false so I guess it keeps turning on. Try this

  if (turnOn) {
    turnOn = false;
    digitalWrite(Kitchen, HIGH);
    turnOn = false; // Reset flag so this won't run again repeatedly
    timerStarted = true; // Set a new flag indicating that the timer was started
    previousMillis = millis(); // Get the initial time the appliance was turned on
    Serial.println("Pump turned on!");
  }

And wouldn't it be a lot more sensible to call it timerStartMillis rather than previousMillis?

...R

arduToshiro:
=( still not working and when look at the serial monitor I just see "undefined letters"

In your setup() you turned on the Serial and set a baud rate (ie, 9600). In the lower right corner of your Serial monitor there is a pull down menu. Select the one that matches your setup.