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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. PCA9685 pwmController;
  66. void setup() {
  67. Serial.begin(115200);
  68. Serial.println("Booting");
  69. WiFi.mode(WIFI_STA);
  70. WiFi.begin(ssid, password);
  71. Wire.begin(); // Wire must be started first
  72. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  73. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  74. Serial.println("Connection Failed! Rebooting...");
  75. delay(5000);
  76. ESP.restart();
  77. }
  78. // setup OTA
  79. ArduinoOTA.setPort(8266);
  80. ArduinoOTA.setHostname("ESP_Treppenlicht");
  81. ArduinoOTA.setPassword("admin");
  82. // Password can be set with it's md5 value as well
  83. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  84. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  85. ArduinoOTA.onStart([]() {
  86. String type;
  87. if (ArduinoOTA.getCommand() == U_FLASH) {
  88. type = "sketch";
  89. } else { // U_FS
  90. type = "filesystem";
  91. }
  92. // NOTE: if updating FS this would be the place to unmount FS using FS.end()
  93. Serial.println("Start updating " + type);
  94. });
  95. ArduinoOTA.onEnd([]() {
  96. Serial.println("\nEnd");
  97. });
  98. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  99. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  100. });
  101. ArduinoOTA.onError([](ota_error_t error) {
  102. Serial.printf("Error[%u]: ", error);
  103. if (error == OTA_AUTH_ERROR) {
  104. Serial.println("Auth Failed");
  105. } else if (error == OTA_BEGIN_ERROR) {
  106. Serial.println("Begin Failed");
  107. } else if (error == OTA_CONNECT_ERROR) {
  108. Serial.println("Connect Failed");
  109. } else if (error == OTA_RECEIVE_ERROR) {
  110. Serial.println("Receive Failed");
  111. } else if (error == OTA_END_ERROR) {
  112. Serial.println("End Failed");
  113. }
  114. });
  115. ArduinoOTA.begin();
  116. Serial.println("Ready");
  117. Serial.print("IP address: ");
  118. Serial.println(WiFi.localIP());
  119. setup_webserver();
  120. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  121. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  122. pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
  123. pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land
  124. Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
  125. }
  126. unsigned long i = 0;
  127. unsigned long a = 0;
  128. unsigned long s = 0;
  129. float ard_ota_time = 0;
  130. float server_handle = 0;
  131. void loop() {
  132. a = micros();
  133. ArduinoOTA.handle();
  134. ard_ota_time += micros()-a;
  135. s = micros();
  136. server.handleClient();
  137. server_handle += micros()-s;
  138. if(++i >= 10000) {
  139. Serial.print("Mittlere Laufzeit\tOTA: ");
  140. Serial.print(ard_ota_time/10000.0);
  141. Serial.print("µs\tHTTP: ");
  142. Serial.print(server_handle/10000.0);
  143. Serial.println("µs");
  144. ard_ota_time = 0;
  145. server_handle = 0;
  146. i = 0;
  147. }
  148. }