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

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