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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "wifi_credentials.h"
  11. #include "ota.h"
  12. #include "httpserver.h"
  13. // BOARD
  14. #define ESP12_LED 2
  15. #define NODEMCU_LED 16
  16. // PWM
  17. #include "treppe.h"
  18. os_timer_t timer1;
  19. uint8_t timer_flag = 0;
  20. Treppe stairs(16);
  21. // WIFI
  22. const char* ssid = STASSID;
  23. const char* password = STAPSK;
  24. void timerCallback(void *pArg)
  25. {
  26. *((int *) pArg) += 1;
  27. stairs.task();
  28. }
  29. // port 80, root directory of server '/'
  30. HTTPServer httpServer(80, "/");
  31. uint32_t _t=0;
  32. #define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us")
  33. #define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); }
  34. // ===============================================
  35. void setup() {
  36. #ifdef WITH_DEBUGGING_ON
  37. Serial.begin(460800);
  38. gdbstub_init();
  39. #else
  40. Serial.begin(115200);
  41. #endif
  42. Serial.println(F("Booting ...."));
  43. pinMode(NODEMCU_LED, OUTPUT);
  44. pinMode(ESP12_LED, OUTPUT);
  45. Wire.begin(); // Wire must be started first
  46. Wire.setClock(1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  47. WiFi.mode(WIFI_STA);
  48. WiFi.begin(ssid, password);
  49. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  50. Serial.println("Connection Failed! Rebooting...");
  51. delay(5000);
  52. ESP.restart();
  53. }
  54. Serial.println("");
  55. Serial.print("Connected to ");
  56. Serial.println(ssid);
  57. Serial.print("IP address: ");
  58. Serial.println(WiFi.localIP());
  59. ota_setup();
  60. httpServer.start();
  61. Serial.println("HTTP server started !");
  62. stairs.setup();
  63. Serial.println("PCA9685 connected !");
  64. os_timer_setfn(&timer1, timerCallback, &timer_flag);
  65. os_timer_arm(&timer1, 20, true);
  66. }
  67. #include <random>
  68. void loop() {
  69. // if(stairs.getState() == 0) {
  70. // delay(1000);
  71. // // uint32_t t = rand() % stairs.getTicks();
  72. // // uint32_t d = rand() % 2;
  73. // // stairs.setTick(t);
  74. // // stairs.setDirection(d);
  75. // stairs.setDirection(!stairs.getDirection());
  76. // stairs.setState(1);
  77. // }
  78. TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA");
  79. TIMEIF_US(httpServer.handleClient(), 10000, "HTTP");
  80. }