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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // setup OTA
  92. ArduinoOTA.setPort(8266);
  93. ArduinoOTA.setHostname("ESP_Treppenlicht");
  94. ArduinoOTA.setPassword("admin");
  95. // Password can be set with it's md5 value as well
  96. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  97. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  98. ArduinoOTA.onStart([]() {
  99. String type;
  100. if (ArduinoOTA.getCommand() == U_FLASH) {
  101. type = "sketch";
  102. } else { // U_FS
  103. type = "filesystem";
  104. }
  105. // NOTE: if updating FS this would be the place to unmount FS using FS.end()
  106. Serial.println("Start updating " + type);
  107. });
  108. ArduinoOTA.onEnd([]() {
  109. Serial.println("\nEnd");
  110. });
  111. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  112. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  113. });
  114. ArduinoOTA.onError([](ota_error_t error) {
  115. Serial.printf("Error[%u]: ", error);
  116. if (error == OTA_AUTH_ERROR) {
  117. Serial.println("Auth Failed");
  118. } else if (error == OTA_BEGIN_ERROR) {
  119. Serial.println("Begin Failed");
  120. } else if (error == OTA_CONNECT_ERROR) {
  121. Serial.println("Connect Failed");
  122. } else if (error == OTA_RECEIVE_ERROR) {
  123. Serial.println("Receive Failed");
  124. } else if (error == OTA_END_ERROR) {
  125. Serial.println("End Failed");
  126. }
  127. });
  128. ArduinoOTA.begin();
  129. Serial.println("Ready");
  130. Serial.print("IP address: ");
  131. Serial.println(WiFi.localIP());
  132. setup_webserver();
  133. }
  134. unsigned long i = 0;
  135. unsigned long a = 0;
  136. unsigned long s = 0;
  137. float ard_ota_time = 0;
  138. float server_handle = 0;
  139. void loop() {
  140. a = micros();
  141. ArduinoOTA.handle();
  142. ard_ota_time += micros()-a;
  143. s = micros();
  144. server.handleClient();
  145. server_handle += micros()-s;
  146. if(++i >= 10000) {
  147. Serial.print("Mittlere Laufzeit\tOTA: ");
  148. Serial.print(ard_ota_time/10000.0);
  149. Serial.print("µs\tHTTP: ");
  150. Serial.print(server_handle/10000.0);
  151. Serial.println("µs");
  152. ard_ota_time = 0;
  153. server_handle = 0;
  154. i = 0;
  155. }
  156. }