ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266mDNS.h>
  4. #include <WiFiUdp.h>
  5. #include <ArduinoOTA.h>
  6. #include <WiFiClient.h>
  7. #include <ESP8266WebServer.h>
  8. #include <ESP8266mDNS.h>
  9. #include "wifi_credentials.h"
  10. const char* ssid = STASSID;
  11. const char* password = STAPSK;
  12. const int led = 13;
  13. ESP8266WebServer server(80);
  14. void handleRoot() {
  15. digitalWrite(led, 1);
  16. char temp[400];
  17. int sec = millis() / 1000;
  18. int min = sec / 60;
  19. int hr = min / 60;
  20. snprintf(temp, 400,
  21. "<html>\
  22. <head>\
  23. <meta http-equiv='refresh' content='5'/>\
  24. <title>ESP8266 Demo</title>\
  25. <style>\
  26. body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
  27. </style>\
  28. </head>\
  29. <body>\
  30. <h1>Hello from ESP8266!</h1>\
  31. <p>Uptime: %02d:%02d:%02d</p>\
  32. <img src=\"/test.svg\" />\
  33. </body>\
  34. </html>",
  35. hr, min % 60, sec % 60
  36. );
  37. server.send(200, "text/html", temp);
  38. digitalWrite(led, 0);
  39. }
  40. void handleNotFound() {
  41. digitalWrite(led, 1);
  42. String message = "File Not Found\n\n";
  43. message += "URI: ";
  44. message += server.uri();
  45. message += "\nMethod: ";
  46. message += (server.method() == HTTP_GET) ? "GET" : "POST";
  47. message += "\nArguments: ";
  48. message += server.args();
  49. message += "\n";
  50. for (uint8_t i = 0; i < server.args(); i++) {
  51. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  52. }
  53. server.send(404, "text/plain", message);
  54. digitalWrite(led, 0);
  55. }
  56. void setup_webserver() {
  57. pinMode(led, OUTPUT);
  58. digitalWrite(led, 0);
  59. Serial.println("");
  60. // Wait for connection
  61. while (WiFi.status() != WL_CONNECTED) {
  62. delay(500);
  63. Serial.print(".");
  64. }
  65. Serial.println("");
  66. Serial.print("Connected to ");
  67. Serial.println(ssid);
  68. Serial.print("IP address: ");
  69. Serial.println(WiFi.localIP());
  70. if (MDNS.begin("esp8266")) {
  71. Serial.println("MDNS responder started");
  72. }
  73. server.on("/", handleRoot);
  74. server.on("/inline", []() {
  75. server.send(200, "text/plain", "this works as well");
  76. });
  77. server.onNotFound(handleNotFound);
  78. server.begin();
  79. Serial.println("HTTP server started");
  80. }
  81. void setup() {
  82. Serial.begin(115200);
  83. Serial.println("Booting");
  84. WiFi.mode(WIFI_STA);
  85. WiFi.begin(ssid, password);
  86. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  87. Serial.println("Connection Failed! Rebooting...");
  88. delay(5000);
  89. ESP.restart();
  90. }
  91. // Port defaults to 8266
  92. // ArduinoOTA.setPort(8266);
  93. // Hostname defaults to esp8266-[ChipID]
  94. ArduinoOTA.setHostname("myesp8266");
  95. // No authentication by default
  96. // ArduinoOTA.setPassword("admin");
  97. // Password can be set with it's md5 value as well
  98. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  99. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  100. ArduinoOTA.onStart([]() {
  101. String type;
  102. if (ArduinoOTA.getCommand() == U_FLASH) {
  103. type = "sketch";
  104. } else { // U_FS
  105. type = "filesystem";
  106. }
  107. // NOTE: if updating FS this would be the place to unmount FS using FS.end()
  108. Serial.println("Start updating " + type);
  109. });
  110. ArduinoOTA.onEnd([]() {
  111. Serial.println("\nEnd");
  112. });
  113. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  114. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  115. });
  116. ArduinoOTA.onError([](ota_error_t error) {
  117. Serial.printf("Error[%u]: ", error);
  118. if (error == OTA_AUTH_ERROR) {
  119. Serial.println("Auth Failed");
  120. } else if (error == OTA_BEGIN_ERROR) {
  121. Serial.println("Begin Failed");
  122. } else if (error == OTA_CONNECT_ERROR) {
  123. Serial.println("Connect Failed");
  124. } else if (error == OTA_RECEIVE_ERROR) {
  125. Serial.println("Receive Failed");
  126. } else if (error == OTA_END_ERROR) {
  127. Serial.println("End Failed");
  128. }
  129. });
  130. ArduinoOTA.begin();
  131. Serial.println("Ready");
  132. Serial.print("IP address: ");
  133. Serial.println(WiFi.localIP());
  134. setup_webserver();
  135. }
  136. void loop() {
  137. ArduinoOTA.handle();
  138. server.handleClient();
  139. }