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

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