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

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