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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include "PCA9685.h"
  4. // Änderung
  5. // OTA & WEB
  6. #include "wifi_credentials.h"
  7. #include <ESP8266WiFi.h>
  8. #include <WiFiUdp.h>
  9. #include <ArduinoOTA.h>
  10. #include <ESP8266WebServer.h>
  11. #include <WiFiClient.h>
  12. #include "index.html.gz.h"
  13. #include "style.css.gz.h"
  14. // images are possible
  15. // maybe check out FS <- SPIFFS
  16. const char* ssid = STASSID;
  17. const char* password = STAPSK;
  18. void setup_webserver();
  19. void setup_ota();
  20. void setup_pwm_pca9685();
  21. void handleRootGz();
  22. void handleCssGz();
  23. void handleNotFound();
  24. const int led = 13;
  25. ESP8266WebServer server(80);
  26. PCA9685 pwmController;
  27. void handleRootGz() {
  28. const char* dataType = "text/html";
  29. server.sendHeader(F("Content-Encoding"), F("gzip"));
  30. server.send(200, dataType, (const char*)index_html_gz, index_html_gz_len);
  31. }
  32. void handleCssGz() {
  33. const char* dataType = "text/css";
  34. server.sendHeader(F("Content-Encoding"), F("gzip"));
  35. server.send(200, dataType, (const char*)style_css_gz, style_css_gz_len);
  36. }
  37. void handleNotFound() {
  38. digitalWrite(led, 1);
  39. String message = "File Not Found\n\n";
  40. message += "URI: ";
  41. message += server.uri();
  42. message += "\nMethod: ";
  43. message += (server.method() == HTTP_GET) ? "GET" : "POST";
  44. message += "\nArguments: ";
  45. message += server.args();
  46. message += "\n";
  47. for (uint8_t i = 0; i < server.args(); i++) {
  48. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  49. }
  50. server.send(404, "text/plain", message);
  51. digitalWrite(led, 0);
  52. }
  53. void setup_webserver() {
  54. pinMode(led, OUTPUT);
  55. digitalWrite(led, 0);
  56. Serial.println("");
  57. // Wait for connection
  58. while (WiFi.status() != WL_CONNECTED) {
  59. delay(500);
  60. Serial.print(".");
  61. }
  62. Serial.println("");
  63. Serial.print("Connected to ");
  64. Serial.println(ssid);
  65. Serial.print("IP address: ");
  66. Serial.println(WiFi.localIP());
  67. //server.on("/", handleRoot);
  68. server.on("/", handleRootGz);
  69. server.on("/styles/style.css", handleCssGz);
  70. server.onNotFound(handleNotFound);
  71. server.begin();
  72. Serial.println("HTTP server started");
  73. }
  74. void setup_ota() {
  75. ArduinoOTA.setPort(8266);
  76. ArduinoOTA.setHostname("ESP_Treppenlicht");
  77. ArduinoOTA.setPassword("admin");
  78. // Password can be set with it's md5 value as well
  79. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  80. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  81. ArduinoOTA.onStart([]() {
  82. String type;
  83. if (ArduinoOTA.getCommand() == U_FLASH) {
  84. type = "sketch";
  85. } else { // U_FS
  86. type = "filesystem";
  87. }
  88. // NOTE: if updating FS this would be the place to unmount FS using FS.end()
  89. Serial.println("Start updating " + type);
  90. });
  91. ArduinoOTA.onEnd([]() {
  92. Serial.println("\nEnd");
  93. });
  94. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  95. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  96. });
  97. ArduinoOTA.onError([](ota_error_t error) {
  98. Serial.printf("Error[%u]: ", error);
  99. if (error == OTA_AUTH_ERROR) {
  100. Serial.println("Auth Failed");
  101. } else if (error == OTA_BEGIN_ERROR) {
  102. Serial.println("Begin Failed");
  103. } else if (error == OTA_CONNECT_ERROR) {
  104. Serial.println("Connect Failed");
  105. } else if (error == OTA_RECEIVE_ERROR) {
  106. Serial.println("Receive Failed");
  107. } else if (error == OTA_END_ERROR) {
  108. Serial.println("End Failed");
  109. }
  110. });
  111. ArduinoOTA.begin();
  112. }
  113. void setup_pwm_pca9685() {
  114. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  115. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  116. pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
  117. pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land
  118. Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
  119. }
  120. void setup() {
  121. Serial.begin(115200);
  122. Serial.println(F("Booting ...."));
  123. Wire.begin(); // Wire must be started first
  124. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  125. WiFi.mode(WIFI_STA);
  126. WiFi.begin(ssid, password);
  127. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  128. Serial.println("Connection Failed! Rebooting...");
  129. delay(5000);
  130. ESP.restart();
  131. }
  132. Serial.println("Ready");
  133. Serial.print("IP address: ");
  134. Serial.println(WiFi.localIP());
  135. setup_ota();
  136. setup_webserver();
  137. setup_pwm_pca9685();
  138. }
  139. uint32_t t;
  140. #define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("µs")
  141. #define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); }
  142. void loop() {
  143. TIMEIF_US("OTA", ArduinoOTA.handle(), 1000);
  144. TIMEIF_US("HTTP", server.handleClient(), 1000);
  145. }