ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_RELAIS
  62. debug.println(F("Relais"));
  63. relais_init();
  64. #endif // FEATURE_RELAIS
  65. #ifdef FEATURE_MOISTURE
  66. debug.println(F("Moisture"));
  67. moisture_init();
  68. #endif // FEATURE_MOISTURE
  69. debug.println(F("Sensors"));
  70. initSensors();
  71. // Build hostname string
  72. String hostname = SENSOR_HOSTNAME_PREFIX;
  73. hostname += SENSOR_ID;
  74. #if defined(ARDUINO_ARCH_ESP8266)
  75. // Connect to WiFi AP
  76. debug.print(F("Connecting WiFi"));
  77. #ifdef FEATURE_UI
  78. ui_progress(UI_WIFI_CONNECT);
  79. #endif // FEATURE_UI
  80. WiFi.hostname(hostname);
  81. WiFi.mode(WIFI_STA);
  82. WiFi.hostname(hostname);
  83. WiFi.begin(WIFI_SSID, WIFI_PASS);
  84. while (WiFi.status() != WL_CONNECTED) {
  85. delay(LED_CONNECT_BLINK_INTERVAL);
  86. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  87. debug.print(F("."));
  88. #ifdef FEATURE_UI
  89. ui_progress(UI_WIFI_CONNECTING);
  90. #endif // FEATURE_UI
  91. }
  92. debug.println(F("\nWiFi connected!"));
  93. #ifdef FEATURE_UI
  94. ui_progress(UI_WIFI_CONNECTED);
  95. #endif // FEATURE_UI
  96. disconnectHandler = WiFi.onStationModeDisconnected(onDisconnected);
  97. // Set hostname workaround
  98. WiFi.hostname(hostname);
  99. #elif defined(ARDUINO_ARCH_ESP32)
  100. // Set hostname workaround
  101. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  102. WiFi.setHostname(hostname.c_str());
  103. WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
  104. /*
  105. * was initially: workaround for WiFi connecting only every 2nd reset
  106. * https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
  107. *
  108. * now simply reset on every disconnect reason - we can't do anything
  109. * useful without wifi anyway!
  110. */
  111. esp_sleep_enable_timer_wakeup(10);
  112. esp_deep_sleep_start();
  113. delay(100);
  114. ESP.restart();
  115. #ifdef NEW_ESP32_LIB
  116. }, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
  117. #else
  118. }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  119. #endif
  120. // Connect to WiFi AP
  121. debug.print(F("Connecting WiFi"));
  122. #ifdef FEATURE_UI
  123. ui_progress(UI_WIFI_CONNECT);
  124. #endif // FEATURE_UI
  125. WiFi.mode(WIFI_STA);
  126. WiFi.setHostname(hostname.c_str());
  127. WiFi.begin(WIFI_SSID, WIFI_PASS);
  128. while (WiFi.status() != WL_CONNECTED) {
  129. delay(LED_CONNECT_BLINK_INTERVAL);
  130. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  131. debug.print(F("."));
  132. #ifdef FEATURE_UI
  133. ui_progress(UI_WIFI_CONNECTING);
  134. #endif // FEATURE_UI
  135. }
  136. debug.println(F("\nWiFi connected!"));
  137. #ifdef FEATURE_UI
  138. ui_progress(UI_WIFI_CONNECTED);
  139. #endif // FEATURE_UI
  140. // Set hostname workaround
  141. WiFi.setHostname(hostname.c_str());
  142. #elif defined(ARDUINO_ARCH_AVR)
  143. Serial1.begin(115200);
  144. WiFi.init(&Serial1);
  145. debug.print(F("Connecting WiFi"));
  146. #ifdef FEATURE_UI
  147. ui_progress(UI_WIFI_CONNECT);
  148. #endif // FEATURE_UI
  149. WiFi.begin(WIFI_SSID, WIFI_PASS);
  150. while (WiFi.status() != WL_CONNECTED) {
  151. delay(LED_CONNECT_BLINK_INTERVAL);
  152. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  153. debug.print(F("."));
  154. #ifdef FEATURE_UI
  155. ui_progress(UI_WIFI_CONNECTING);
  156. #endif // FEATURE_UI
  157. }
  158. debug.println(F("\nWiFi connected!"));
  159. #ifdef FEATURE_UI
  160. ui_progress(UI_WIFI_CONNECTED);
  161. #endif // FEATURE_UI
  162. #endif
  163. debug.println(F("Seeding"));
  164. randomSeed(micros());
  165. debug.println(F("MQTT"));
  166. initMQTT();
  167. debug.println(F("Influx"));
  168. initInflux();
  169. debug.println(F("Servers"));
  170. initServers(hostname);
  171. debug.println(F("Ready! Starting..."));
  172. #ifdef FEATURE_UI
  173. debug.println(F("UI Go"));
  174. ui_progress(UI_READY);
  175. #endif // FEATURE_UI
  176. }
  177. void loop() {
  178. runServers();
  179. runSensors();
  180. runMQTT();
  181. runInflux();
  182. #ifdef FEATURE_UI
  183. ui_run();
  184. #endif
  185. // blink heartbeat LED
  186. unsigned long time = millis();
  187. if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {
  188. last_led_blink_time = time;
  189. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  190. }
  191. }