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

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