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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include "lora.h"
  34. unsigned long last_led_blink_time = 0;
  35. ConfigMemory config;
  36. #if defined(ARDUINO_ARCH_ESP8266)
  37. WiFiEventHandler disconnectHandler;
  38. void onDisconnected(const WiFiEventStationModeDisconnected& event) {
  39. /*
  40. * simply restart in case we lose wifi connection
  41. * we can't do anything useful without wifi anyway!
  42. */
  43. ESP.restart();
  44. }
  45. #endif // ARDUINO_ARCH_ESP8266
  46. void setup() {
  47. pinMode(BUILTIN_LED_PIN, OUTPUT);
  48. Serial.begin(115200);
  49. debug.println(F("Initializing..."));
  50. // Blink LED for init
  51. for (int i = 0; i < 2; i++) {
  52. digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
  53. delay(LED_INIT_BLINK_INTERVAL);
  54. digitalWrite(BUILTIN_LED_PIN, HIGH); // LED off
  55. delay(LED_INIT_BLINK_INTERVAL);
  56. }
  57. #ifdef FEATURE_UI
  58. debug.println(F("UI"));
  59. ui_init();
  60. #endif // FEATURE_UI
  61. config = mem_read();
  62. #ifdef FEATURE_UI
  63. ui_progress(UI_MEMORY_READY);
  64. #endif // FEATURE_UI
  65. #ifdef FEATURE_RELAIS
  66. debug.println(F("Relais"));
  67. relais_init();
  68. #endif // FEATURE_RELAIS
  69. #ifdef FEATURE_MOISTURE
  70. debug.println(F("Moisture"));
  71. moisture_init();
  72. #endif // FEATURE_MOISTURE
  73. debug.println(F("Sensors"));
  74. initSensors();
  75. #ifdef FEATURE_LORA
  76. debug.println(F("Lora"));
  77. lora_init();
  78. #endif // FEATURE_LORA
  79. #ifndef FEATURE_DISABLE_WIFI
  80. // Build hostname string
  81. String hostname = SENSOR_HOSTNAME_PREFIX;
  82. hostname += SENSOR_ID;
  83. #if defined(ARDUINO_ARCH_ESP8266)
  84. // Connect to WiFi AP
  85. debug.print(F("Connecting WiFi"));
  86. #ifdef FEATURE_UI
  87. ui_progress(UI_WIFI_CONNECT);
  88. #endif // FEATURE_UI
  89. WiFi.hostname(hostname);
  90. WiFi.mode(WIFI_STA);
  91. WiFi.hostname(hostname);
  92. WiFi.begin(WIFI_SSID, WIFI_PASS);
  93. while (WiFi.status() != WL_CONNECTED) {
  94. delay(LED_CONNECT_BLINK_INTERVAL);
  95. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  96. debug.print(F("."));
  97. #ifdef FEATURE_UI
  98. ui_progress(UI_WIFI_CONNECTING);
  99. #endif // FEATURE_UI
  100. }
  101. debug.println(F("\nWiFi connected!"));
  102. #ifdef FEATURE_UI
  103. ui_progress(UI_WIFI_CONNECTED);
  104. #endif // FEATURE_UI
  105. disconnectHandler = WiFi.onStationModeDisconnected(onDisconnected);
  106. // Set hostname workaround
  107. WiFi.hostname(hostname);
  108. #elif defined(ARDUINO_ARCH_ESP32)
  109. // Set hostname workaround
  110. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  111. WiFi.setHostname(hostname.c_str());
  112. WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
  113. /*
  114. * was initially: workaround for WiFi connecting only every 2nd reset
  115. * https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
  116. *
  117. * now simply reset on every disconnect reason - we can't do anything
  118. * useful without wifi anyway!
  119. */
  120. esp_sleep_enable_timer_wakeup(10);
  121. esp_deep_sleep_start();
  122. delay(100);
  123. ESP.restart();
  124. #ifdef NEW_ESP32_LIB
  125. }, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
  126. #else
  127. }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  128. #endif
  129. // Connect to WiFi AP
  130. debug.print(F("Connecting WiFi"));
  131. #ifdef FEATURE_UI
  132. ui_progress(UI_WIFI_CONNECT);
  133. #endif // FEATURE_UI
  134. WiFi.mode(WIFI_STA);
  135. WiFi.setHostname(hostname.c_str());
  136. WiFi.begin(WIFI_SSID, WIFI_PASS);
  137. while (WiFi.status() != WL_CONNECTED) {
  138. delay(LED_CONNECT_BLINK_INTERVAL);
  139. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  140. debug.print(F("."));
  141. #ifdef FEATURE_UI
  142. ui_progress(UI_WIFI_CONNECTING);
  143. #endif // FEATURE_UI
  144. }
  145. debug.println(F("\nWiFi connected!"));
  146. #ifdef FEATURE_UI
  147. ui_progress(UI_WIFI_CONNECTED);
  148. #endif // FEATURE_UI
  149. // Set hostname workaround
  150. WiFi.setHostname(hostname.c_str());
  151. #elif defined(ARDUINO_ARCH_AVR)
  152. Serial1.begin(115200);
  153. WiFi.init(&Serial1);
  154. debug.print(F("Connecting WiFi"));
  155. #ifdef FEATURE_UI
  156. ui_progress(UI_WIFI_CONNECT);
  157. #endif // FEATURE_UI
  158. WiFi.begin(WIFI_SSID, WIFI_PASS);
  159. while (WiFi.status() != WL_CONNECTED) {
  160. delay(LED_CONNECT_BLINK_INTERVAL);
  161. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  162. debug.print(F("."));
  163. #ifdef FEATURE_UI
  164. ui_progress(UI_WIFI_CONNECTING);
  165. #endif // FEATURE_UI
  166. }
  167. debug.println(F("\nWiFi connected!"));
  168. #ifdef FEATURE_UI
  169. ui_progress(UI_WIFI_CONNECTED);
  170. #endif // FEATURE_UI
  171. #endif // ARCH
  172. debug.println(F("Seeding"));
  173. randomSeed(micros());
  174. debug.println(F("MQTT"));
  175. initMQTT();
  176. debug.println(F("Influx"));
  177. initInflux();
  178. debug.println(F("Servers"));
  179. initServers(hostname);
  180. #endif // FEATURE_DISABLE_WIFI
  181. debug.println(F("Ready! Starting..."));
  182. #ifdef FEATURE_UI
  183. debug.println(F("UI Go"));
  184. ui_progress(UI_READY);
  185. #endif // FEATURE_UI
  186. }
  187. void loop() {
  188. runSensors();
  189. #ifndef FEATURE_DISABLE_WIFI
  190. runServers();
  191. runMQTT();
  192. runInflux();
  193. #endif // FEATURE_DISABLE_WIFI
  194. #ifdef FEATURE_UI
  195. ui_run();
  196. #endif
  197. #ifdef FEATURE_LORA
  198. lora_run();
  199. #endif // FEATURE_LORA
  200. // blink heartbeat LED
  201. unsigned long time = millis();
  202. if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {
  203. last_led_blink_time = time;
  204. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  205. }
  206. }