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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include <Wire.h>
  11. #include "PCA9685.h"
  12. const char* ssid = STASSID;
  13. const char* password = STAPSK;
  14. const int led = 13;
  15. ESP8266WebServer server(80);
  16. void handleRoot() {
  17. digitalWrite(led, 1);
  18. char temp[400];
  19. int sec = millis() / 1000;
  20. int min = sec / 60;
  21. int hr = min / 60;
  22. snprintf(temp, 400,
  23. "<html>\
  24. <head>\
  25. <meta http-equiv='refresh' content='5'/>\
  26. <title>ESP8266 Demo</title>\
  27. <style>\
  28. body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
  29. </style>\
  30. </head>\
  31. <body>\
  32. <h1>Hello from ESP8266!</h1>\
  33. <p>Uptime: %02d:%02d:%02d</p>\
  34. <img src=\"/test.svg\" />\
  35. </body>\
  36. </html>",
  37. hr, min % 60, sec % 60
  38. );
  39. server.send(200, "text/html", temp);
  40. digitalWrite(led, 0);
  41. }
  42. void handleNotFound() {
  43. digitalWrite(led, 1);
  44. String message = "File Not Found\n\n";
  45. message += "URI: ";
  46. message += server.uri();
  47. message += "\nMethod: ";
  48. message += (server.method() == HTTP_GET) ? "GET" : "POST";
  49. message += "\nArguments: ";
  50. message += server.args();
  51. message += "\n";
  52. for (uint8_t i = 0; i < server.args(); i++) {
  53. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  54. }
  55. server.send(404, "text/plain", message);
  56. digitalWrite(led, 0);
  57. }
  58. void setup_webserver() {
  59. pinMode(led, OUTPUT);
  60. digitalWrite(led, 0);
  61. Serial.println("");
  62. // Wait for connection
  63. while (WiFi.status() != WL_CONNECTED) {
  64. delay(500);
  65. Serial.print(".");
  66. }
  67. Serial.println("");
  68. Serial.print("Connected to ");
  69. Serial.println(ssid);
  70. Serial.print("IP address: ");
  71. Serial.println(WiFi.localIP());
  72. if (MDNS.begin("esp8266")) {
  73. Serial.println("MDNS responder started");
  74. }
  75. server.on("/", handleRoot);
  76. server.on("/inline", []() {
  77. server.send(200, "text/plain", "this works as well");
  78. });
  79. server.onNotFound(handleNotFound);
  80. server.begin();
  81. Serial.println("HTTP server started");
  82. }
  83. PCA9685 pwmController;
  84. void setup() {
  85. Serial.begin(115200);
  86. Serial.println("Booting");
  87. WiFi.mode(WIFI_STA);
  88. WiFi.begin(ssid, password);
  89. Wire.begin(); // Wire must be started first
  90. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  91. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  92. Serial.println("Connection Failed! Rebooting...");
  93. delay(5000);
  94. ESP.restart();
  95. }
  96. // setup OTA
  97. ArduinoOTA.setPort(8266);
  98. ArduinoOTA.setHostname("ESP_Treppenlicht");
  99. ArduinoOTA.setPassword("admin");
  100. // Password can be set with it's md5 value as well
  101. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  102. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  103. ArduinoOTA.onStart([]() {
  104. String type;
  105. if (ArduinoOTA.getCommand() == U_FLASH) {
  106. type = "sketch";
  107. } else { // U_FS
  108. type = "filesystem";
  109. }
  110. // NOTE: if updating FS this would be the place to unmount FS using FS.end()
  111. Serial.println("Start updating " + type);
  112. });
  113. ArduinoOTA.onEnd([]() {
  114. Serial.println("\nEnd");
  115. });
  116. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  117. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  118. });
  119. ArduinoOTA.onError([](ota_error_t error) {
  120. Serial.printf("Error[%u]: ", error);
  121. if (error == OTA_AUTH_ERROR) {
  122. Serial.println("Auth Failed");
  123. } else if (error == OTA_BEGIN_ERROR) {
  124. Serial.println("Begin Failed");
  125. } else if (error == OTA_CONNECT_ERROR) {
  126. Serial.println("Connect Failed");
  127. } else if (error == OTA_RECEIVE_ERROR) {
  128. Serial.println("Receive Failed");
  129. } else if (error == OTA_END_ERROR) {
  130. Serial.println("End Failed");
  131. }
  132. });
  133. ArduinoOTA.begin();
  134. Serial.println("Ready");
  135. Serial.print("IP address: ");
  136. Serial.println(WiFi.localIP());
  137. setup_webserver();
  138. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  139. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  140. pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
  141. pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land
  142. Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
  143. }
  144. unsigned long i = 0;
  145. unsigned long a = 0;
  146. unsigned long s = 0;
  147. float ard_ota_time = 0;
  148. float server_handle = 0;
  149. void loop() {
  150. a = micros();
  151. ArduinoOTA.handle();
  152. ard_ota_time += micros()-a;
  153. s = micros();
  154. server.handleClient();
  155. server_handle += micros()-s;
  156. if(++i >= 10000) {
  157. Serial.print("Mittlere Laufzeit\tOTA: ");
  158. Serial.print(ard_ota_time/10000.0);
  159. Serial.print("µs\tHTTP: ");
  160. Serial.print(server_handle/10000.0);
  161. Serial.println("µs");
  162. ard_ota_time = 0;
  163. server_handle = 0;
  164. i = 0;
  165. }
  166. }