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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <Arduino.h>
  2. #ifdef WITH_DEBUGGING_ON
  3. #include <GDBStub.h> // debugging support via GDBStub over UART
  4. #endif
  5. #include "PCA9685.h"
  6. extern "C" {
  7. #include "user_interface.h"
  8. }
  9. // OTA & WEB
  10. #include <ESP8266WiFi.h>
  11. #include <WiFiUdp.h>
  12. #include <ArduinoOTA.h>
  13. #include "ota.h"
  14. #include "wifi_credentials.h"
  15. #include "httpserver.h"
  16. // BOARD
  17. #define ESP12_LED 2
  18. #define NODEMCU_LED 16
  19. // PWM
  20. os_timer_t timer1;
  21. uint8_t timer_flag = 0;
  22. PCA9685 pwmController;
  23. void setup_pwm_pca9685();
  24. // WIFI
  25. const char* ssid = STASSID;
  26. const char* password = STAPSK;
  27. // PWM
  28. uint32_t dimmtimer = 0;
  29. uint16_t time_per_stair = 500; // global parameter: dimmtime per stair [ms]
  30. uint8_t direction = 1;
  31. uint8_t onoff = 1;
  32. void ledsequence(uint8_t direction, uint8_t onoff);
  33. uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);
  34. void timerCallback(void *pArg)
  35. {
  36. *((int *) pArg) += 1;
  37. ledsequence(direction, onoff);
  38. // Serial.print("[");
  39. // Serial.print(micros()-m);
  40. // Serial.print("] timerCallback\n");
  41. // m = micros();
  42. }
  43. // HTTP
  44. void handleNotFound();
  45. HTTPServer httpServer(80, "/");
  46. uint32_t _t=0;
  47. #define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us")
  48. #define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); }
  49. // ===============================================
  50. void handleNotFound() {
  51. String message = "File Not Found\n\n";
  52. message += "URI: ";
  53. message += httpServer.uri();
  54. message += "\nMethod: ";
  55. message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
  56. message += "\nArguments: ";
  57. message += httpServer.args();
  58. message += "\n";
  59. for (uint8_t i = 0; i < httpServer.args(); i++) {
  60. message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
  61. }
  62. httpServer.send(404, "text/plain", message);
  63. }
  64. void setup_pwm_pca9685() {
  65. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  66. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  67. pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
  68. Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
  69. }
  70. uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){
  71. /*
  72. softstart task
  73. - get's called at regular intervals (1ms at the moment)
  74. - dimms single led (0 - 15, PCA9685 outputs) with linear intervals vom startval to stopval
  75. - calculates pwm steps depending on startval, stopval and timeinterval
  76. - -> results in constanst speed
  77. - returns 1 if led dimming is running
  78. - returns 0 if led dimming is finished
  79. */
  80. static uint8_t lastled = 255;
  81. static float current_pwm = 0;
  82. static float stepsize = 1.0;
  83. if(led != lastled){
  84. pwmController.setChannelPWM(led, (uint16_t)startval);
  85. lastled = led;
  86. current_pwm = startval;
  87. stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
  88. return 1;
  89. }
  90. if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize) return 0;
  91. // todo: duty cycle zero!
  92. if(startval > stopval){
  93. current_pwm -= stepsize;
  94. }
  95. else {
  96. current_pwm += stepsize;
  97. }
  98. Serial.println((uint16_t)current_pwm);
  99. pwmController.setChannelPWM(led, (uint16_t)current_pwm);
  100. return 1;
  101. }
  102. #define LEDCOUNT 16
  103. void ledsequence(uint8_t direction, uint8_t onoff){
  104. static int8_t led = 0;
  105. static uint16_t brightness = 0;
  106. static uint16_t lastbrightness = 0;
  107. static uint8_t finish = 1;
  108. static uint16_t status = 0;
  109. uint16_t status_build = 0;
  110. status_build |= direction << 8;
  111. status_build |= onoff;
  112. if(status_build != status){ // check if any parameter changed
  113. finish = 0; // set state unfinished -> start action
  114. if(direction) led = 0; // reset led counter depending of direction
  115. else led = LEDCOUNT-1;
  116. if(onoff){
  117. brightness = 2048; // set brightness value depending of on/off
  118. lastbrightness = 200;
  119. }
  120. else{
  121. brightness = 200;
  122. lastbrightness = 2048;
  123. }
  124. status = status_build; // set parameter memory
  125. Serial.print("----Status Changed! onoff: ");
  126. Serial.print(onoff);
  127. Serial.print(" dir: ");
  128. Serial.println(direction);
  129. }
  130. if(!finish){ // finish == 0 -> action pending
  131. if(!softstart_led(led,lastbrightness, brightness)){
  132. Serial.print("one LED finished: ");
  133. Serial.print(led);
  134. Serial.print(" last: ");
  135. Serial.print(lastbrightness);
  136. Serial.print(" curr: ");
  137. Serial.println(brightness);
  138. if(direction){
  139. led++;
  140. if(led >= LEDCOUNT) {
  141. finish = 1;
  142. //lastbrightness = brightness;
  143. }
  144. }
  145. else{
  146. led--;
  147. if(led < 0){
  148. //lastbrightness = brightness;
  149. finish = 1;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. void setup() {
  156. #ifdef WITH_DEBUGGING_ON
  157. Serial.begin(460800);
  158. gdbstub_init();
  159. #else
  160. Serial.begin(115200);
  161. #endif
  162. Serial.println(F("Booting ...."));
  163. pinMode(NODEMCU_LED, OUTPUT);
  164. pinMode(ESP12_LED, OUTPUT);
  165. Wire.begin(); // Wire must be started first
  166. Wire.setClock(1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  167. WiFi.mode(WIFI_STA);
  168. WiFi.begin(ssid, password);
  169. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  170. Serial.println("Connection Failed! Rebooting...");
  171. delay(5000);
  172. ESP.restart();
  173. }
  174. Serial.println("");
  175. Serial.print("Connected to ");
  176. Serial.println(ssid);
  177. Serial.print("IP address: ");
  178. Serial.println(WiFi.localIP());
  179. ota_setup();
  180. httpServer.start();
  181. httpServer.onNotFound(handleNotFound);
  182. Serial.println("HTTP server started !");
  183. setup_pwm_pca9685();
  184. Serial.println("PCA9685 connected !");
  185. os_timer_setfn(&timer1, timerCallback, &timer_flag);
  186. os_timer_arm(&timer1, 20, true);
  187. }
  188. void loop() {
  189. if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
  190. if(millis() > 45000 && direction == 1){
  191. onoff = 1;
  192. direction = 0;
  193. }
  194. TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
  195. TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
  196. }