Step by step guide
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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiUdp.h>
  4. #include <ArduinoOTA.h>
  5. #include "eboot_command.h"
  6. #include "creds.h"
  7. const char* ssid = STASSID;
  8. const char* password = STAPSK;
  9. WiFiServer server(80);
  10. void ota_setup() {
  11. ArduinoOTA.onStart([]() {
  12. Serial.flush();
  13. Serial.begin(74880);
  14. Serial.flush();
  15. String type;
  16. if (ArduinoOTA.getCommand() == U_FLASH) {
  17. type = "sketch";
  18. } else { // U_FS
  19. type = "filesystem";
  20. }
  21. // NOTE: if updating FS this would be the place to unmount FS using FS.end()
  22. Serial.println("[OTA] Start updating " + type);
  23. });
  24. ArduinoOTA.onEnd([]() {
  25. Serial.println("\n[OTA] End");
  26. });
  27. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  28. Serial.printf("[OTA] Progress: %u%%\r", (progress / (total / 100)));
  29. });
  30. ArduinoOTA.onError([](ota_error_t error) {
  31. Serial.printf("[OTA] Error[%u]: ", error);
  32. if (error == OTA_AUTH_ERROR) {
  33. Serial.println("[OTA] Auth Failed");
  34. } else if (error == OTA_BEGIN_ERROR) {
  35. Serial.println("[OTA] Begin Failed");
  36. } else if (error == OTA_CONNECT_ERROR) {
  37. Serial.println("[OTA] Connect Failed");
  38. } else if (error == OTA_RECEIVE_ERROR) {
  39. Serial.println("[OTA] Receive Failed");
  40. } else if (error == OTA_END_ERROR) {
  41. Serial.println("[OTA] End Failed");
  42. }
  43. });
  44. }
  45. void setup() {
  46. Serial.begin(74880);
  47. Serial.println("\n[Program] Booting");
  48. WiFi.mode(WIFI_STA);
  49. WiFi.begin(ssid, password);
  50. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  51. Serial.println("[Program] Connection Failed! Rebooting...");
  52. delay(5000);
  53. ESP.restart();
  54. }
  55. ota_setup();
  56. ArduinoOTA.begin();
  57. server.begin();
  58. Serial.printf("[Program] OTA and HTTPServer ready !\r\n");
  59. Serial.printf("[Program] IP address: ");
  60. Serial.println(WiFi.localIP());
  61. Serial.flush();
  62. Serial.begin(9600);
  63. Serial.flush();
  64. }
  65. int value = 0;
  66. void client_handler(WiFiClient* cl) {
  67. String currentLine = "";
  68. while (cl->connected()) {
  69. if (cl->available()) {
  70. char c = cl->read();
  71. Serial.write(c);
  72. if (c == '\n') {
  73. if (currentLine.length() == 0) {
  74. cl->println("HTTP/1.1 200 OK");
  75. cl->println("Content-type:text/html");
  76. cl->println();
  77. cl->print("Relais <a href=\"/H\">Einschalten</a><br>");
  78. cl->print("Relais <a href=\"/L\">Ausschalten</a><br>");
  79. cl->println();
  80. break;
  81. } else {
  82. currentLine = "";
  83. }
  84. } else if (c != '\r') {
  85. currentLine += c;
  86. }
  87. if (currentLine.endsWith("GET /H")) {
  88. Serial.println("EIN");
  89. delay(10);
  90. byte close[] = {0xA0, 0x01, 0x01, 0xA2};
  91. Serial.write(close, sizeof(close));
  92. }
  93. if (currentLine.endsWith("GET /L")) {
  94. Serial.println("AUS");
  95. delay(10);
  96. byte open[] = {0xA0, 0x01, 0x00, 0xA1};
  97. Serial.write(open, sizeof(open));
  98. }
  99. }
  100. }
  101. cl->stop();
  102. }
  103. void loop() {
  104. ArduinoOTA.handle();
  105. WiFiClient client = server.available();
  106. if (client) {
  107. client_handler(&client);
  108. }
  109. }