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

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