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

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 "ota.h"
  11. #include "wifi_credentials.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. // HTTP
  30. void handleNotFound();
  31. HTTPServer httpServer(80, "/");
  32. uint32_t _t=0;
  33. #define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us")
  34. #define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); }
  35. // ===============================================
  36. void handleNotFound() {
  37. String message = "File Not Found\n\n";
  38. message += "URI: ";
  39. message += httpServer.uri();
  40. message += "\nMethod: ";
  41. message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
  42. message += "\nArguments: ";
  43. message += httpServer.args();
  44. message += "\n";
  45. for (uint8_t i = 0; i < httpServer.args(); i++) {
  46. message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
  47. }
  48. httpServer.send(404, "text/plain", message);
  49. }
  50. void setup() {
  51. #ifdef WITH_DEBUGGING_ON
  52. Serial.begin(460800);
  53. gdbstub_init();
  54. #else
  55. Serial.begin(115200);
  56. #endif
  57. Serial.println(F("Booting ...."));
  58. pinMode(NODEMCU_LED, OUTPUT);
  59. pinMode(ESP12_LED, OUTPUT);
  60. Wire.begin(); // Wire must be started first
  61. Wire.setClock(1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  62. WiFi.mode(WIFI_STA);
  63. WiFi.begin(ssid, password);
  64. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  65. Serial.println("Connection Failed! Rebooting...");
  66. delay(5000);
  67. ESP.restart();
  68. }
  69. Serial.println("");
  70. Serial.print("Connected to ");
  71. Serial.println(ssid);
  72. Serial.print("IP address: ");
  73. Serial.println(WiFi.localIP());
  74. ota_setup();
  75. httpServer.start();
  76. httpServer.onNotFound(handleNotFound);
  77. Serial.println("HTTP server started !");
  78. stairs.setup();
  79. Serial.println("PCA9685 connected !");
  80. os_timer_setfn(&timer1, timerCallback, &timer_flag);
  81. os_timer_arm(&timer1, 20, true);
  82. stairs.setState(1);
  83. stairs.setDirection(1);
  84. }
  85. #include <random>
  86. void loop() {
  87. if(stairs.getState() == 0) {
  88. delay(1000);
  89. // uint32_t t = rand() % stairs.getTicks();
  90. // uint32_t d = rand() % 2;
  91. // stairs.setTick(t);
  92. // stairs.setDirection(d);
  93. stairs.setDirection(!stairs.getDirection());
  94. stairs.setState(1);
  95. }
  96. TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA");
  97. TIMEIF_US(httpServer.handleClient(), 10000, "HTTP");
  98. }