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

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