ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * main.cpp
  3. *
  4. * ESP8266 / ESP32 Environmental Sensor
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  9. * you can do whatever you want with this stuff. If we meet some day, and you
  10. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  11. * ----------------------------------------------------------------------------
  12. */
  13. #include <Arduino.h>
  14. #if defined(ARDUINO_ARCH_ESP8266)
  15. #include <ESP8266WiFi.h>
  16. #elif defined(ARDUINO_ARCH_ESP32)
  17. #include <WiFi.h>
  18. #elif defined(ARDUINO_ARCH_AVR)
  19. #include <UnoWiFiDevEdSerial1.h>
  20. #include <WiFiLink.h>
  21. #endif
  22. #include "config.h"
  23. #include "DebugLog.h"
  24. #include "moisture.h"
  25. #include "sensors.h"
  26. #include "relais.h"
  27. #include "memory.h"
  28. #include "influx.h"
  29. #include "mqtt.h"
  30. #include "html.h"
  31. #include "servers.h"
  32. #include "ui.h"
  33. unsigned long last_led_blink_time = 0;
  34. ConfigMemory config;
  35. #if defined(ARDUINO_ARCH_ESP8266)
  36. WiFiEventHandler disconnectHandler;
  37. void onDisconnected(const WiFiEventStationModeDisconnected& event) {
  38. /*
  39. * simply restart in case we lose wifi connection
  40. * we can't do anything useful without wifi anyway!
  41. */
  42. ESP.restart();
  43. }
  44. #endif // ARDUINO_ARCH_ESP8266
  45. void setup() {
  46. pinMode(BUILTIN_LED_PIN, OUTPUT);
  47. Serial.begin(115200);
  48. debug.println(F("Initializing..."));
  49. // Blink LED for init
  50. for (int i = 0; i < 2; i++) {
  51. digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
  52. delay(LED_INIT_BLINK_INTERVAL);
  53. digitalWrite(BUILTIN_LED_PIN, HIGH); // LED off
  54. delay(LED_INIT_BLINK_INTERVAL);
  55. }
  56. #ifdef FEATURE_UI
  57. debug.println(F("UI"));
  58. ui_init();
  59. #endif // FEATURE_UI
  60. config = mem_read();
  61. #ifdef FEATURE_UI
  62. ui_progress(UI_MEMORY_READY);
  63. #endif // FEATURE_UI
  64. #ifdef FEATURE_RELAIS
  65. debug.println(F("Relais"));
  66. relais_init();
  67. #endif // FEATURE_RELAIS
  68. #ifdef FEATURE_MOISTURE
  69. debug.println(F("Moisture"));
  70. moisture_init();
  71. #endif // FEATURE_MOISTURE
  72. debug.println(F("Sensors"));
  73. initSensors();
  74. // Build hostname string
  75. String hostname = SENSOR_HOSTNAME_PREFIX;
  76. hostname += SENSOR_ID;
  77. #if defined(ARDUINO_ARCH_ESP8266)
  78. // Connect to WiFi AP
  79. debug.print(F("Connecting WiFi"));
  80. #ifdef FEATURE_UI
  81. ui_progress(UI_WIFI_CONNECT);
  82. #endif // FEATURE_UI
  83. WiFi.hostname(hostname);
  84. WiFi.mode(WIFI_STA);
  85. WiFi.hostname(hostname);
  86. WiFi.begin(WIFI_SSID, WIFI_PASS);
  87. while (WiFi.status() != WL_CONNECTED) {
  88. delay(LED_CONNECT_BLINK_INTERVAL);
  89. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  90. debug.print(F("."));
  91. #ifdef FEATURE_UI
  92. ui_progress(UI_WIFI_CONNECTING);
  93. #endif // FEATURE_UI
  94. }
  95. debug.println(F("\nWiFi connected!"));
  96. #ifdef FEATURE_UI
  97. ui_progress(UI_WIFI_CONNECTED);
  98. #endif // FEATURE_UI
  99. disconnectHandler = WiFi.onStationModeDisconnected(onDisconnected);
  100. // Set hostname workaround
  101. WiFi.hostname(hostname);
  102. #elif defined(ARDUINO_ARCH_ESP32)
  103. // Set hostname workaround
  104. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  105. WiFi.setHostname(hostname.c_str());
  106. WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
  107. /*
  108. * was initially: workaround for WiFi connecting only every 2nd reset
  109. * https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
  110. *
  111. * now simply reset on every disconnect reason - we can't do anything
  112. * useful without wifi anyway!
  113. */
  114. esp_sleep_enable_timer_wakeup(10);
  115. esp_deep_sleep_start();
  116. delay(100);
  117. ESP.restart();
  118. #ifdef NEW_ESP32_LIB
  119. }, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
  120. #else
  121. }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  122. #endif
  123. // Connect to WiFi AP
  124. debug.print(F("Connecting WiFi"));
  125. #ifdef FEATURE_UI
  126. ui_progress(UI_WIFI_CONNECT);
  127. #endif // FEATURE_UI
  128. WiFi.mode(WIFI_STA);
  129. WiFi.setHostname(hostname.c_str());
  130. WiFi.begin(WIFI_SSID, WIFI_PASS);
  131. while (WiFi.status() != WL_CONNECTED) {
  132. delay(LED_CONNECT_BLINK_INTERVAL);
  133. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  134. debug.print(F("."));
  135. #ifdef FEATURE_UI
  136. ui_progress(UI_WIFI_CONNECTING);
  137. #endif // FEATURE_UI
  138. }
  139. debug.println(F("\nWiFi connected!"));
  140. #ifdef FEATURE_UI
  141. ui_progress(UI_WIFI_CONNECTED);
  142. #endif // FEATURE_UI
  143. // Set hostname workaround
  144. WiFi.setHostname(hostname.c_str());
  145. #elif defined(ARDUINO_ARCH_AVR)
  146. Serial1.begin(115200);
  147. WiFi.init(&Serial1);
  148. debug.print(F("Connecting WiFi"));
  149. #ifdef FEATURE_UI
  150. ui_progress(UI_WIFI_CONNECT);
  151. #endif // FEATURE_UI
  152. WiFi.begin(WIFI_SSID, WIFI_PASS);
  153. while (WiFi.status() != WL_CONNECTED) {
  154. delay(LED_CONNECT_BLINK_INTERVAL);
  155. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  156. debug.print(F("."));
  157. #ifdef FEATURE_UI
  158. ui_progress(UI_WIFI_CONNECTING);
  159. #endif // FEATURE_UI
  160. }
  161. debug.println(F("\nWiFi connected!"));
  162. #ifdef FEATURE_UI
  163. ui_progress(UI_WIFI_CONNECTED);
  164. #endif // FEATURE_UI
  165. #endif
  166. debug.println(F("Seeding"));
  167. randomSeed(micros());
  168. debug.println(F("MQTT"));
  169. initMQTT();
  170. debug.println(F("Influx"));
  171. initInflux();
  172. debug.println(F("Servers"));
  173. initServers(hostname);
  174. debug.println(F("Ready! Starting..."));
  175. #ifdef FEATURE_UI
  176. debug.println(F("UI Go"));
  177. ui_progress(UI_READY);
  178. #endif // FEATURE_UI
  179. }
  180. void loop() {
  181. runServers();
  182. runSensors();
  183. runMQTT();
  184. runInflux();
  185. #ifdef FEATURE_UI
  186. ui_run();
  187. #endif
  188. // blink heartbeat LED
  189. unsigned long time = millis();
  190. if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {
  191. last_led_blink_time = time;
  192. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  193. }
  194. }