I want to control one device in three ways : turn on / turn off / turn off with delay.
To do that i use an ESP8266 web server + android phone app.
The problem is that turn off with delay wont work.
It works with delay()
but i dont want to use delay()
as it stops other functions.
I would be grateful if you could help me.
here is my code :
#include <ESP8266WiFi.h>
const int device1 = 14;
unsigned long previousMillis = 0;
const long interval = 5000;
const char* ssid = "****";
const char* password = "****";
#include <Arduino.h>
WiFiServer server(80);
String data = "";
void setup() {
pinMode(device1, OUTPUT);
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
WiFi.begin(ssid, password);
IPAddress ip(*,*,*,*);
IPAddress gateway(*,*,*,*);
IPAddress subnet(*,*,*,*);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentMillis = millis();
WiFiClient client = server.available();
if (!client) {
return;
}
while(!client.available()){
}
String req = client.readStringUntil('\r');
client.flush();
if (req.indexOf("/on") != -1)
{ digitalWrite(device1, HIGH);}
if (req.indexOf("/off") != -1)
{ digitalWrite(device1, LOW);}
if (req.indexOf("/delay") != -1)
{
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
digitalWrite(device1, LOW);
}
}
/on
should set a flag ... reception of/off
and/delay
should clear the flag and set turnoff time ... the device control is done after theif
statements have completed