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

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