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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include "favicon.png.gz.h"
  15. // images are possible
  16. // maybe check out FS <- SPIFFS
  17. const char* ssid = STASSID;
  18. const char* password = STAPSK;
  19. void setup_webserver();
  20. void setup_ota();
  21. void setup_pwm_pca9685();
  22. void handleRootGz();
  23. void handleCssGz();
  24. void handleNotFound();
  25. const int led = 13;
  26. ESP8266WebServer server(80);
  27. PCA9685 pwmController;
  28. void handleRootGz() {
  29. const char* dataType = "text/html";
  30. server.sendHeader(F("Content-Encoding"), F("gzip"));
  31. server.send(200, dataType, (const char*)index_html_gz, index_html_gz_len);
  32. }
  33. void handleCssGz() {
  34. const char* dataType = "text/css";
  35. server.sendHeader(F("Content-Encoding"), F("gzip"));
  36. server.send(200, dataType, (const char*)style_css_gz, style_css_gz_len);
  37. }
  38. void handleFaviconGz() {
  39. const char* dataType = "image/png";
  40. server.sendHeader(F("Content-Encoding"), F("gzip"));
  41. server.send(200, dataType, (const char*)favicon_png_gz, favicon_png_gz_len);
  42. }
  43. void handleNotFound() {
  44. digitalWrite(led, 1);
  45. String message = "File Not Found\n\n";
  46. message += "URI: ";
  47. message += server.uri();
  48. message += "\nMethod: ";
  49. message += (server.method() == HTTP_GET) ? "GET" : "POST";
  50. message += "\nArguments: ";
  51. message += server.args();
  52. message += "\n";
  53. for (uint8_t i = 0; i < server.args(); i++) {
  54. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  55. }
  56. server.send(404, "text/plain", message);
  57. digitalWrite(led, 0);
  58. }
  59. void setup_webserver() {
  60. pinMode(led, OUTPUT);
  61. digitalWrite(led, 0);
  62. Serial.println("");
  63. // Wait for connection
  64. while (WiFi.status() != WL_CONNECTED) {
  65. delay(500);
  66. Serial.print(".");
  67. }
  68. Serial.println("");
  69. Serial.print("Connected to ");
  70. Serial.println(ssid);
  71. Serial.print("IP address: ");
  72. Serial.println(WiFi.localIP());
  73. server.on("/", handleRootGz);
  74. server.on("/style.css", handleCssGz);
  75. server.on("/favicon.png", handleFaviconGz);
  76. server.onNotFound(handleNotFound);
  77. server.begin();
  78. Serial.println("HTTP server started");
  79. }
  80. void setup_ota() {
  81. ArduinoOTA.setPort(8266);
  82. ArduinoOTA.setHostname("ESP_Treppenlicht");
  83. ArduinoOTA.setPassword("admin");
  84. // Password can be set with it's md5 value as well
  85. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  86. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  87. ArduinoOTA.onStart([]() {
  88. String type;
  89. if (ArduinoOTA.getCommand() == U_FLASH) {
  90. type = "sketch";
  91. } else { // U_FS
  92. type = "filesystem";
  93. }
  94. // NOTE: if updating FS this would be the place to unmount FS using FS.end()
  95. Serial.println("Start updating " + type);
  96. });
  97. ArduinoOTA.onEnd([]() {
  98. Serial.println("\nEnd");
  99. });
  100. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  101. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  102. });
  103. ArduinoOTA.onError([](ota_error_t error) {
  104. Serial.printf("Error[%u]: ", error);
  105. if (error == OTA_AUTH_ERROR) {
  106. Serial.println("Auth Failed");
  107. } else if (error == OTA_BEGIN_ERROR) {
  108. Serial.println("Begin Failed");
  109. } else if (error == OTA_CONNECT_ERROR) {
  110. Serial.println("Connect Failed");
  111. } else if (error == OTA_RECEIVE_ERROR) {
  112. Serial.println("Receive Failed");
  113. } else if (error == OTA_END_ERROR) {
  114. Serial.println("End Failed");
  115. }
  116. });
  117. ArduinoOTA.begin();
  118. }
  119. void setup_pwm_pca9685() {
  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. void setup() {
  127. Serial.begin(115200);
  128. Serial.println(F("Booting ...."));
  129. Wire.begin(); // Wire must be started first
  130. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  131. WiFi.mode(WIFI_STA);
  132. WiFi.begin(ssid, password);
  133. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  134. Serial.println("Connection Failed! Rebooting...");
  135. delay(5000);
  136. ESP.restart();
  137. }
  138. Serial.println("Ready");
  139. Serial.print("IP address: ");
  140. Serial.println(WiFi.localIP());
  141. setup_ota();
  142. setup_webserver();
  143. setup_pwm_pca9685();
  144. }
  145. uint32_t t;
  146. #define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("µs")
  147. #define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); }
  148. uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor){
  149. static uint8_t lastled = 255;
  150. static uint8_t current_pwm = 0;
  151. if(led != lastled){
  152. pwmController.setChannelPWM(led, startval);
  153. lastled = led;
  154. current_pwm = startval;
  155. return 1;
  156. }
  157. if(current_pwm == stopval){
  158. return 0;
  159. }
  160. else if(startval > stopval){
  161. current_pwm -= 1;
  162. }
  163. else {
  164. current_pwm += 1;
  165. }
  166. pwmController.setChannelPWM(led, current_pwm*factor);
  167. return 1;
  168. }
  169. #define LEDCOUNT 13
  170. void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){
  171. static int8_t led = 0;
  172. static uint8_t brightness = 0;
  173. static uint8_t lastbrightness = 0;
  174. static uint8_t finish = 1;
  175. static uint32_t status = 0;
  176. uint32_t status_build = 0;
  177. status_build |= direction << 16;
  178. status_build |= onoff << 8;
  179. status_build |= factor;
  180. if(status_build != status){ // check if any parameter changed
  181. finish = 0; // set state unfinished -> start action
  182. if(direction) led = 0; // reset led counter depending of direction
  183. else led = LEDCOUNT-1;
  184. if(onoff){
  185. brightness = 127; // set brightness value depending of on/off
  186. lastbrightness = 0;
  187. }
  188. else{
  189. brightness = 0;
  190. lastbrightness = 127;
  191. }
  192. status = status_build; // set parameter memory
  193. Serial.print("----Status Changed! onoff: ");
  194. Serial.print(onoff);
  195. Serial.print(" dir: ");
  196. Serial.println(direction);
  197. }
  198. if(!finish){ // finish == 0 -> action pending
  199. if(!softstart_led(led,lastbrightness, brightness, factor)){
  200. Serial.print("one LED finished, new set led: ");
  201. Serial.print(led);
  202. Serial.print(" last: ");
  203. Serial.print(lastbrightness);
  204. Serial.print(" curr: ");
  205. Serial.println(brightness);
  206. if(direction){
  207. led++;
  208. if(led >= LEDCOUNT) {
  209. finish = 1;
  210. //lastbrightness = brightness;
  211. }
  212. }
  213. else{
  214. led--;
  215. if(led < 0){
  216. //lastbrightness = brightness;
  217. finish = 1;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. void loop() {
  224. static uint32_t dimmtimer = 0;
  225. static uint8_t direction = 1;
  226. static uint8_t onoff = 1;
  227. if(millis() - dimmtimer > 2){
  228. ledsequence(direction, onoff, 4);
  229. dimmtimer = millis();
  230. }
  231. if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
  232. if(millis() > 35000 && direction == 1){
  233. onoff = 1;
  234. direction = 0;
  235. }
  236. TIMEIF_US("OTA", ArduinoOTA.handle(), 20000);
  237. TIMEIF_US("HTTP", server.handleClient(), 20000);
  238. TIMEIF_US("OTA", ArduinoOTA.handle(), 1000);
  239. TIMEIF_US("HTTP", server.handleClient(), 1000);
  240. }