Status aktualisierung am esp8266 webserver

beim klick auf button ON wechselt dieser auf OFF und schaltet ausgang 16 (D0) nach ablauf einer zeit, schaltet ausgang 16 aus aber der button steht immer noch auf OFF. ich möchte das dieser nach der zeit zurück auf ON geht.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoOTA.h>

/*Put your SSID & Password*/
const char* ssid = "XXXXXXXX";           // Enter SSID here
const char* password = "XXXXXXXX";  //Enter Password here
const char* sendHttpTo = "192.168.2.100/led5off";
ESP8266WebServer server(80);

uint8_t LED1pin = D7;
bool LED1status = LOW;

uint8_t LED2pin = D6;
bool LED2status = LOW;

uint8_t LED3pin = D3;
bool LED3status = LOW;

uint8_t LED4pin = D1;
bool LED4status = LOW;

uint8_t LED5pin = D2;
bool LED5status = LOW;

uint8_t LED16pin = D0;
bool LED16status = LOW;


int counter = 1;
//const unsigned long timeoutDuration = 5000;  // 1 Minuten


//const unsigned long period = 10000;

void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(LED1pin, OUTPUT);
  pinMode(LED2pin, OUTPUT);
  pinMode(LED3pin, OUTPUT);
  pinMode(LED4pin, OUTPUT);
  pinMode(LED5pin, OUTPUT);
  pinMode(LED16pin, OUTPUT);
  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);
  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");
  Serial.println(WiFi.localIP());
  server.on("/", handle_OnConnect);
  server.on("/led1on", handle_led1on);
  server.on("/led1off", handle_led1off);
  server.on("/led2on", handle_led2on);
  server.on("/led2off", handle_led2off);
  server.on("/led3on", handle_led3on);
  server.on("/led3off", handle_led3off);
  server.on("/led4on", handle_led4on);
  server.on("/led4off", handle_led4off);
  server.on("/led5on", handle_led5on);
  server.on("/led5off", handle_led5off);
  server.onNotFound(handle_NotFound);
  server.begin();
  Serial.println("HTTP server started");
}
void loop() {
  server.handleClient();

  if (LED1status) {  // ist 0 oder 1
    Serial.println("");
    digitalWrite(LED1pin, HIGH);
    digitalWrite(LED16pin, LOW);
    if (LED1status = 1) {
      counter++;
      //Serial.println(counter);
    }
    //Serial.println(counter);
    if (counter > 5000) {
      digitalWrite(LED1pin, LOW);
      digitalWrite(LED16pin, HIGH);
      LED1status = 0;
      counter = 1;
    }


  } else {
    digitalWrite(LED1pin, LOW);
    digitalWrite(LED16pin, HIGH);
  }

  if (LED2status) {
    digitalWrite(LED2pin, HIGH);
  } else {
    digitalWrite(LED2pin, LOW);
  }

  if (LED3status) {
    digitalWrite(LED3pin, HIGH);
  } else {
    digitalWrite(LED3pin, LOW);
  }

  if (LED4status) {
    digitalWrite(LED4pin, HIGH);
  } else {
    digitalWrite(LED4pin, LOW);
  }

  if (LED5status) {
    digitalWrite(LED5pin, HIGH);
  } else {
    digitalWrite(LED5pin, LOW);
  }
}
void update_status() {
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));  // we send HTML page alng with updated LED status.
}
void handle_OnConnect() {
  LED1status = LOW;
  LED2status = LOW;
  LED3status = LOW;
  LED4status = LOW;
  LED5status = LOW;
  LED16status = LOW;
  //Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led1on() {
  LED1status = HIGH;
  //Serial.println("GPIO7 Status: ON");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led1off() {
  LED1status = LOW;
  //Serial.println("GPIO7 Status: OFF");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led2on() {
  LED2status = HIGH;
  Serial.println("GPIO6 Status: ON");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led2off() {
  LED2status = LOW;
  Serial.println("GPIO6 Status: OFF");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led3on() {
  LED3status = HIGH;
  Serial.println("GPIO8 Status: ON");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led3off() {
  LED3status = LOW;
  Serial.println("GPIO8 Status: OFF");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led4on() {
  LED4status = HIGH;
  Serial.println("GPIO9 Status: ON");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led4off() {
  LED4status = LOW;
  Serial.println("GPIO9 Status: OFF");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led5on() {
  LED5status = HIGH;
  Serial.println("GPIO9 Status: ON");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}

void handle_led5off() {
  LED5status = LOW;
  Serial.println("GPIO9 Status: OFF");
  server.send(200, "text/html", SendHTML(LED1status, LED2status, LED3status, LED4status, LED5status));
}


void handle_NotFound() {
  server.send(404, "text/plain", "Not found");
}

String SendHTML(uint8_t LED1status, uint8_t LED2status, uint8_t LED3status, uint8_t LED4status, uint8_t LED5status) {
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr += "<title>LED Control</title>\n";
  ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h1 {color: #444444;margin-bottom: 50px;}\n";
  ptr += ".button {display: block;width: 40px;background-color: #1abc9c;border: none;color: white;padding: 7px 15px;text-decoration: none;font-size: 12px;margin: 0px auto 17px;cursor: pointer;border-radius: 4px;}\n";
  ptr += ".button-on {background-color: #1abc9c;}\n";
  ptr += ".button-on:active {background-color: #16a085;}\n";
  ptr += ".button-off {background-color: #34495e;}\n";
  ptr += ".button-off:active {background-color: #2c3e50;}\n";

  ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  ptr += "</style>\n";
  ptr += "</head>\n";
  ptr += "<body>\n";
  ptr += "<h1>Haus Steuerung</h1>\n";
  ptr += "<h3>Jalousien Garage Zirki Licht</h3>\n";

  if (LED1status == 1) {
    //Serial.println("AN");
    ptr += "<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";
  }  //else {
  if (LED1status == 0) {
    //Serial.println("AUS");
    ptr += "<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";
  }

  if (LED2status) {
    ptr += "<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";
  } else {
    ptr += "<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";
  }

  if (LED3status) {
    ptr += "<p>LED3 Status: ON</p><a class=\"button button-off\" href=\"/led3off\">OFF</a>\n";
  } else {
    ptr += "<p>LED3 Status: OFF</p><a class=\"button button-on\" href=\"/led3on\">ON</a>\n";
  }

  if (LED4status) {
    ptr += "<p>LED4 Status: ON</p><a class=\"button button-off\" href=\"/led4off\">OFF</a>\n";
  } else {
    ptr += "<p>LED4 Status: OFF</p><a class=\"button button-on\" href=\"/led4on\">ON</a>\n";
  }  // Beschriftung <> <Hintergrundfarbe <> URL <> Beschriftung


  if (LED5status) {
    ptr += "<p>LED5 Status: An</p><a class=\"button button-off\" href=\"/led5off\">OFF</a>\n";
  } else {
    ptr += "<p>LED5 Status: Aus</p><a class=\"button button-on\" href=\"/led5on\">ON</a>\n";
  }



  ptr += "</body>\n";
  ptr += "</html>\n";
  return ptr;
}

HTTP ist erst einmal eine Einzelaktion. Der Browser ruft eine Website ab (die auch eine Aktion auslösen kann) und bekommt eine Antwort. Das war es dann.

Entweder Du baust in die Antwortwebsite ein refresh-Tag ein, so dass die Abfrage des Status regelmäßig erfolgt oder Du beschäftigs Dich ausführlich mit Javascript und Websockets. Damit kannst Du Push-Nachrichten vom Server zum Browser schicken.

Gruß Tommy

ergänze das im html head dann hast ein fünf Sekunden refresh

<meta http-equiv='refresh' content='5>

Beim Rest vom Sketch müsstest du erst die ganzen Code-Duplikaten entfernen und zusammenräumen.

danke für eure vorschläge
mit einem refresh gehts nicht und das andere verstehe ich nicht ich bin ein abloluter neuling in sachen arduino

Dann machst Du was falsch, was wir nicht sehen können. Die Seite mit Refresh muss den aktuellen Status des Ausgangs vom Webserver liefern.
Den Status "Neuling" kann man durch Lernen verlassen.

Gruß Tommy

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.