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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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("/", handleRootGz);
  68. server.on("/style.css", handleCssGz);
  69. server.onNotFound(handleNotFound);
  70. server.begin();
  71. Serial.println("HTTP server started");
  72. }
  73. void setup_ota() {
  74. ArduinoOTA.setPort(8266);
  75. ArduinoOTA.setHostname("ESP_Treppenlicht");
  76. ArduinoOTA.setPassword("admin");
  77. // Password can be set with it's md5 value as well
  78. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  79. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  80. ArduinoOTA.onStart([]() {
  81. String type;
  82. if (ArduinoOTA.getCommand() == U_FLASH) {
  83. type = "sketch";
  84. } else { // U_FS
  85. type = "filesystem";
  86. }
  87. // NOTE: if updating FS this would be the place to unmount FS using FS.end()
  88. Serial.println("Start updating " + type);
  89. });
  90. ArduinoOTA.onEnd([]() {
  91. Serial.println("\nEnd");
  92. });
  93. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  94. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  95. });
  96. ArduinoOTA.onError([](ota_error_t error) {
  97. Serial.printf("Error[%u]: ", error);
  98. if (error == OTA_AUTH_ERROR) {
  99. Serial.println("Auth Failed");
  100. } else if (error == OTA_BEGIN_ERROR) {
  101. Serial.println("Begin Failed");
  102. } else if (error == OTA_CONNECT_ERROR) {
  103. Serial.println("Connect Failed");
  104. } else if (error == OTA_RECEIVE_ERROR) {
  105. Serial.println("Receive Failed");
  106. } else if (error == OTA_END_ERROR) {
  107. Serial.println("End Failed");
  108. }
  109. });
  110. ArduinoOTA.begin();
  111. }
  112. void setup_pwm_pca9685() {
  113. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  114. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  115. pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
  116. pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land
  117. Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
  118. }
  119. void setup() {
  120. Serial.begin(115200);
  121. Serial.println(F("Booting ...."));
  122. Wire.begin(); // Wire must be started first
  123. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  124. WiFi.mode(WIFI_STA);
  125. WiFi.begin(ssid, password);
  126. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  127. Serial.println("Connection Failed! Rebooting...");
  128. delay(5000);
  129. ESP.restart();
  130. }
  131. Serial.println("Ready");
  132. Serial.print("IP address: ");
  133. Serial.println(WiFi.localIP());
  134. setup_ota();
  135. setup_webserver();
  136. setup_pwm_pca9685();
  137. }
  138. uint32_t t;
  139. #define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("µs")
  140. #define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); }
  141. uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor){
  142. static uint8_t lastled = 255;
  143. static uint8_t current_pwm = 0;
  144. if(led != lastled){
  145. pwmController.setChannelPWM(led, startval);
  146. lastled = led;
  147. current_pwm = startval;
  148. return 1;
  149. }
  150. if(current_pwm == stopval){
  151. return 0;
  152. }
  153. else if(startval > stopval){
  154. current_pwm -= 1;
  155. }
  156. else {
  157. current_pwm += 1;
  158. }
  159. pwmController.setChannelPWM(led, current_pwm*factor);
  160. return 1;
  161. }
  162. #define LEDCOUNT 13
  163. void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){
  164. static int8_t led = 0;
  165. static uint8_t brightness = 0;
  166. static uint8_t lastbrightness = 0;
  167. static uint8_t finish = 1;
  168. static uint32_t status = 0;
  169. uint32_t status_build = 0;
  170. status_build |= direction << 16;
  171. status_build |= onoff << 8;
  172. status_build |= factor;
  173. if(status_build != status){ // check if any parameter changed
  174. finish = 0; // set state unfinished -> start action
  175. if(direction) led = 0; // reset led counter depending of direction
  176. else led = LEDCOUNT-1;
  177. if(onoff){
  178. brightness = 127; // set brightness value depending of on/off
  179. lastbrightness = 0;
  180. }
  181. else{
  182. brightness = 0;
  183. lastbrightness = 127;
  184. }
  185. status = status_build; // set parameter memory
  186. Serial.print("----Status Changed! onoff: ");
  187. Serial.print(onoff);
  188. Serial.print(" dir: ");
  189. Serial.println(direction);
  190. }
  191. if(!finish){ // finish == 0 -> action pending
  192. if(!softstart_led(led,lastbrightness, brightness, factor)){
  193. Serial.print("one LED finished, new set led: ");
  194. Serial.print(led);
  195. Serial.print(" last: ");
  196. Serial.print(lastbrightness);
  197. Serial.print(" curr: ");
  198. Serial.println(brightness);
  199. if(direction){
  200. led++;
  201. if(led >= LEDCOUNT) {
  202. finish = 1;
  203. //lastbrightness = brightness;
  204. }
  205. }
  206. else{
  207. led--;
  208. if(led < 0){
  209. //lastbrightness = brightness;
  210. finish = 1;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. void loop() {
  217. static uint32_t dimmtimer = 0;
  218. static uint8_t direction = 1;
  219. static uint8_t onoff = 1;
  220. if(millis() - dimmtimer > 2){
  221. ledsequence(direction, onoff, 4);
  222. dimmtimer = millis();
  223. }
  224. if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
  225. if(millis() > 35000 && direction == 1){
  226. onoff = 1;
  227. direction = 0;
  228. }
  229. TIMEIF_US("OTA", ArduinoOTA.handle(), 20000);
  230. TIMEIF_US("HTTP", server.handleClient(), 20000);
  231. TIMEIF_US("OTA", ArduinoOTA.handle(), 1000);
  232. TIMEIF_US("HTTP", server.handleClient(), 1000);
  233. }