|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include <Arduino.h>
-
- #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #include "eboot_command.h"
- #include "creds.h"
-
- const char* ssid = STASSID;
- const char* password = STAPSK;
-
- WiFiServer server(80);
-
- void ota_setup() {
- ArduinoOTA.onStart([]() {
- Serial.flush();
- Serial.begin(74880);
- Serial.flush();
- String type;
- if (ArduinoOTA.getCommand() == U_FLASH) {
- type = "sketch";
- } else { // U_FS
- type = "filesystem";
- }
- // NOTE: if updating FS this would be the place to unmount FS using FS.end()
- Serial.println("[OTA] Start updating " + type);
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\n[OTA] End");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("[OTA] Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("[OTA] Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) {
- Serial.println("[OTA] Auth Failed");
- } else if (error == OTA_BEGIN_ERROR) {
- Serial.println("[OTA] Begin Failed");
- } else if (error == OTA_CONNECT_ERROR) {
- Serial.println("[OTA] Connect Failed");
- } else if (error == OTA_RECEIVE_ERROR) {
- Serial.println("[OTA] Receive Failed");
- } else if (error == OTA_END_ERROR) {
- Serial.println("[OTA] End Failed");
- }
- });
- }
-
- void setup() {
- Serial.begin(74880);
- Serial.println("\n[Program] Booting");
-
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.waitForConnectResult() != WL_CONNECTED) {
- Serial.println("[Program] Connection Failed! Rebooting...");
- delay(5000);
- ESP.restart();
- }
-
- ota_setup();
- ArduinoOTA.begin();
- server.begin();
-
- Serial.printf("[Program] OTA and HTTPServer ready !\r\n");
- Serial.printf("[Program] IP address: ");
- Serial.println(WiFi.localIP());
-
- Serial.flush();
- Serial.begin(9600);
- Serial.flush();
- }
-
- int value = 0;
- void client_handler(WiFiClient* cl) {
- String currentLine = "";
- while (cl->connected()) {
- if (cl->available()) {
- char c = cl->read();
- Serial.write(c);
- if (c == '\n') {
- if (currentLine.length() == 0) {
- cl->println("HTTP/1.1 200 OK");
- cl->println("Content-type:text/html");
- cl->println();
- cl->print("Relais <a href=\"/H\">Einschalten</a><br>");
- cl->print("Relais <a href=\"/L\">Ausschalten</a><br>");
- cl->println();
- break;
- } else {
- currentLine = "";
- }
- } else if (c != '\r') {
- currentLine += c;
- }
- if (currentLine.endsWith("GET /H")) {
- Serial.println("EIN");
- delay(10);
- byte close[] = {0xA0, 0x01, 0x01, 0xA2};
- Serial.write(close, sizeof(close));
- }
- if (currentLine.endsWith("GET /L")) {
- Serial.println("AUS");
- delay(10);
- byte open[] = {0xA0, 0x01, 0x00, 0xA1};
- Serial.write(open, sizeof(open));
- }
- }
- }
- cl->stop();
- }
-
- void loop() {
- ArduinoOTA.handle();
- WiFiClient client = server.available();
- if (client) {
- client_handler(&client);
- }
- }
|