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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include "wifi_credentials.h"
  4. #include "PCA9685.h"
  5. // Änderung
  6. // OTA & WEB
  7. #include <ESP8266WiFi.h>
  8. #include <WiFiUdp.h>
  9. #include <ArduinoOTA.h>
  10. #include <ESP8266WebServer.h>
  11. #include <WiFiClient.h> // evtl weghauen ?
  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. const int led = 13;
  19. ESP8266WebServer server(80);
  20. void handleRootGz() {
  21. const char* dataType = "text/html";
  22. server.sendHeader(F("Content-Encoding"), F("gzip"));
  23. server.send(200, dataType, (const char*)index_html_gz, index_html_gz_len);
  24. }
  25. void handleCssGz() {
  26. const char* dataType = "text/css";
  27. server.sendHeader(F("Content-Encoding"), F("gzip"));
  28. server.send(200, dataType, (const char*)style_css_gz, style_css_gz_len);
  29. }
  30. void handleNotFound() {
  31. digitalWrite(led, 1);
  32. String message = "File Not Found\n\n";
  33. message += "URI: ";
  34. message += server.uri();
  35. message += "\nMethod: ";
  36. message += (server.method() == HTTP_GET) ? "GET" : "POST";
  37. message += "\nArguments: ";
  38. message += server.args();
  39. message += "\n";
  40. for (uint8_t i = 0; i < server.args(); i++) {
  41. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  42. }
  43. server.send(404, "text/plain", message);
  44. digitalWrite(led, 0);
  45. }
  46. void setup_webserver() {
  47. pinMode(led, OUTPUT);
  48. digitalWrite(led, 0);
  49. Serial.println("");
  50. // Wait for connection
  51. while (WiFi.status() != WL_CONNECTED) {
  52. delay(500);
  53. Serial.print(".");
  54. }
  55. Serial.println("");
  56. Serial.print("Connected to ");
  57. Serial.println(ssid);
  58. Serial.print("IP address: ");
  59. Serial.println(WiFi.localIP());
  60. //server.on("/", handleRoot);
  61. server.on("/", handleRootGz);
  62. server.on("/styles/style.css", handleCssGz);
  63. server.onNotFound(handleNotFound);
  64. server.begin();
  65. Serial.println("HTTP server started");
  66. }
  67. PCA9685 pwmController;
  68. void setup() {
  69. Serial.begin(115200);
  70. Serial.println("Booting");
  71. WiFi.mode(WIFI_STA);
  72. WiFi.begin(ssid, password);
  73. Wire.begin(); // Wire must be started first
  74. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  75. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  76. Serial.println("Connection Failed! Rebooting...");
  77. delay(5000);
  78. ESP.restart();
  79. }
  80. // 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. Serial.println("Ready");
  119. Serial.print("IP address: ");
  120. Serial.println(WiFi.localIP());
  121. setup_webserver();
  122. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  123. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  124. pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
  125. pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land
  126. Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
  127. }
  128. uint32_t t;
  129. #define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("µs")
  130. #define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); }
  131. uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor){
  132. static uint8_t lastled = 255;
  133. static uint8_t current_pwm = 0;
  134. if(led != lastled){
  135. pwmController.setChannelPWM(led, startval);
  136. lastled = led;
  137. current_pwm = startval;
  138. return 1;
  139. }
  140. if(current_pwm == stopval){
  141. return 0;
  142. }
  143. else if(startval > stopval){
  144. current_pwm -= 1;
  145. }
  146. else {
  147. current_pwm += 1;
  148. }
  149. pwmController.setChannelPWM(led, current_pwm*factor);
  150. return 1;
  151. }
  152. #define LEDCOUNT 13
  153. void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){
  154. static int8_t led = 0;
  155. static uint8_t brightness = 0;
  156. static uint8_t lastbrightness = 0;
  157. static uint8_t finish = 1;
  158. static uint32_t status = 0;
  159. uint32_t status_build = 0;
  160. status_build |= direction << 16;
  161. status_build |= onoff << 8;
  162. status_build |= factor;
  163. if(status_build != status){ // check if any parameter changed
  164. finish = 0; // set state unfinished -> start action
  165. if(direction) led = 0; // reset led counter depending of direction
  166. else led = LEDCOUNT-1;
  167. if(onoff) brightness = 127; // set brightness value depending of on/off
  168. else brightness = 0;
  169. status = status_build; // set parameter memory
  170. Serial.println("----Status Changed!");
  171. }
  172. if(!finish){ // finish == 0 -> action pending
  173. if(!softstart_led(led,lastbrightness, brightness, factor)){
  174. Serial.print("one LED finished, new set led: ");
  175. Serial.print(led);
  176. Serial.print(" last: ");
  177. Serial.print(lastbrightness);
  178. Serial.print(" curr: ");
  179. Serial.println(brightness);
  180. if(direction){
  181. led++;
  182. if(led >= LEDCOUNT) {
  183. finish = 1;
  184. lastbrightness = brightness;
  185. }
  186. }
  187. else{
  188. led--;
  189. if(led < 0){
  190. lastbrightness = brightness;
  191. finish = 1;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. void loop() {
  198. static uint32_t dimmtimer = 0;
  199. /* static uint8_t led = 0;
  200. static uint16_t brightness = 127;
  201. static uint16_t lastbrightness = 0;
  202. if(millis() - dimmtimer > 2){
  203. if(softstart_led(led, lastbrightness, brightness)){
  204. // do nothing
  205. }
  206. else{
  207. if(led < LEDCOUNT-1) led++;
  208. else {
  209. led = 0;
  210. uint16_t mem = lastbrightness;
  211. lastbrightness = brightness;
  212. brightness = mem;
  213. }
  214. softstart_led((led), lastbrightness, brightness);
  215. }
  216. if(led >= LEDCOUNT){
  217. //led = 0;
  218. //uint16_t mem = lastbrightness;
  219. //lastbrightness = brightness;
  220. // brightness = mem;
  221. }
  222. dimmtimer = millis();
  223. }*/
  224. static uint8_t direction = 1;
  225. static uint8_t onoff = 1;
  226. if(millis() - dimmtimer > 2){
  227. ledsequence(direction, onoff, 4);
  228. }
  229. if(millis() > 25000) onoff = 0;
  230. TIMEIF_US("OTA", ArduinoOTA.handle(), 20000);
  231. TIMEIF_US("HTTP", server.handleClient(), 20000);
  232. }