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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <ESP8266mDNS.h>
  11. #include "httpserver.h"
  12. #include "ota.h"
  13. #include "wifi_credentials.h"
  14. #include <EEPROM.h>
  15. // BOARD
  16. #define ESP12_LED 2
  17. // #define NODEMCU_LED 16
  18. // PWM
  19. #include "treppe.h"
  20. os_timer_t timer1;
  21. uint8_t timer_flag = 0;
  22. Treppe stairs(12);
  23. // WIFI
  24. const char *ssid = STASSID;
  25. const char *password = STAPSK;
  26. // port 80, root directory of server '/'
  27. HTTPServer httpServer(80, "/", &stairs);
  28. uint32_t _t = 0;
  29. #define SP_US(_str, _a) \
  30. Serial.print(_str); \
  31. Serial.print(" took: "); \
  32. Serial.print(_a); \
  33. Serial.println("us")
  34. #define TIMEIF_US(_f, _l, _str) \
  35. _t = micros(); \
  36. _f; \
  37. _t = micros() - _t; \
  38. if (_t > (_l)) { \
  39. SP_US(_str, _t); \
  40. }
  41. struct { // Struct for wifi-data, 40 Bytes each SSID and PW
  42. char SSID[40] = "";
  43. char PW[40] = "";
  44. } wifi_data;
  45. void timerCallback(void *pArg) {
  46. *(static_cast<int *>(pArg)) += 1;
  47. stairs.task();
  48. }
  49. // ===============================================
  50. uint8_t inter = 0;
  51. ICACHE_RAM_ATTR void int_test() { inter = 1; }
  52. void setup() {
  53. #ifdef WITH_DEBUGGING_ON
  54. Serial.begin(460800);
  55. gdbstub_init();
  56. #else
  57. Serial.begin(76800);
  58. #endif
  59. Serial.println("Booting ....");
  60. pinMode(ESP12_LED, OUTPUT);
  61. EEPROM.begin(512); // init virtual "EEPROM" with size 512 Bytes
  62. // strncpy(wifi_data.SSID, "donotconnect", 40);
  63. // strncpy(wifi_data.PW, "unsafe lol", 40);
  64. EEPROM.get(0, wifi_data);
  65. // EEPROM.commit();
  66. Wire.begin(); // Wire must be started first
  67. Wire.setClock(
  68. 1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  69. stairs.setup();
  70. Serial.println("PCA9685 connected !");
  71. // attachInterrupt(digitalPinToInterrupt(2), int_test, RISING);
  72. // attachInterrupt(digitalPinToInterrupt(12), int_test, RISING);
  73. WiFi.mode(WIFI_STA);
  74. WiFi.begin(ssid, password);
  75. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  76. Serial.println("Connection Failed! Rebooting...");
  77. delay(5000);
  78. ESP.restart();
  79. }
  80. Serial.println("");
  81. Serial.print("Connected to ");
  82. Serial.println(ssid);
  83. Serial.print("IP address: ");
  84. Serial.println(WiFi.localIP());
  85. ota_setup();
  86. httpServer.start();
  87. httpServer.start_apps();
  88. Serial.println("HTTP server started !");
  89. if (MDNS.begin("treppenlicht")) {
  90. Serial.println("MDNS for treppenlicht started");
  91. }
  92. os_timer_setfn(&timer1, timerCallback, &timer_flag);
  93. os_timer_arm(&timer1, 20, true);
  94. Serial.println("SSID: " + String(wifi_data.SSID) +
  95. " PW: " + String(wifi_data.PW));
  96. }
  97. void loop() {
  98. if (inter != 0u) {
  99. Serial.printf("interrupt\n");
  100. inter = 0;
  101. }
  102. TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA");
  103. TIMEIF_US(httpServer.handleClient(), 10000, "HTTP");
  104. }