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

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