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

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