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

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