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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. WiFi.hostname(hostname);
  78. WiFi.mode(WIFI_STA);
  79. WiFi.hostname(hostname);
  80. WiFi.begin(WIFI_SSID, WIFI_PASS);
  81. while (WiFi.status() != WL_CONNECTED) {
  82. delay(LED_CONNECT_BLINK_INTERVAL);
  83. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  84. debug.print(F("."));
  85. }
  86. debug.println(F("\nWiFi connected!"));
  87. disconnectHandler = WiFi.onStationModeDisconnected(onDisconnected);
  88. // Set hostname workaround
  89. WiFi.hostname(hostname);
  90. #elif defined(ARDUINO_ARCH_ESP32)
  91. // Set hostname workaround
  92. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  93. WiFi.setHostname(hostname.c_str());
  94. WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
  95. /*
  96. * was initially: workaround for WiFi connecting only every 2nd reset
  97. * https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
  98. *
  99. * now simply reset on every disconnect reason - we can't do anything
  100. * useful without wifi anyway!
  101. */
  102. esp_sleep_enable_timer_wakeup(10);
  103. esp_deep_sleep_start();
  104. delay(100);
  105. ESP.restart();
  106. #ifdef NEW_ESP32_LIB
  107. }, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
  108. #else
  109. }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  110. #endif
  111. // Connect to WiFi AP
  112. debug.print(F("Connecting WiFi"));
  113. WiFi.mode(WIFI_STA);
  114. WiFi.setHostname(hostname.c_str());
  115. WiFi.begin(WIFI_SSID, WIFI_PASS);
  116. while (WiFi.status() != WL_CONNECTED) {
  117. delay(LED_CONNECT_BLINK_INTERVAL);
  118. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  119. debug.print(F("."));
  120. }
  121. debug.println(F("\nWiFi connected!"));
  122. // Set hostname workaround
  123. WiFi.setHostname(hostname.c_str());
  124. #elif defined(ARDUINO_ARCH_AVR)
  125. Serial1.begin(115200);
  126. WiFi.init(&Serial1);
  127. debug.print(F("Connecting WiFi"));
  128. WiFi.begin(WIFI_SSID, WIFI_PASS);
  129. while (WiFi.status() != WL_CONNECTED) {
  130. delay(LED_CONNECT_BLINK_INTERVAL);
  131. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  132. debug.print(F("."));
  133. }
  134. debug.println(F("\nWiFi connected!"));
  135. #endif
  136. debug.println(F("Seeding"));
  137. randomSeed(micros());
  138. debug.println(F("MQTT"));
  139. initMQTT();
  140. debug.println(F("Influx"));
  141. initInflux();
  142. debug.println(F("Servers"));
  143. initServers(hostname);
  144. debug.println(F("Ready! Starting..."));
  145. }
  146. void loop() {
  147. runServers();
  148. runSensors();
  149. runMQTT();
  150. runInflux();
  151. #ifdef FEATURE_UI
  152. ui_run();
  153. #endif
  154. // blink heartbeat LED
  155. unsigned long time = millis();
  156. if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {
  157. last_led_blink_time = time;
  158. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  159. }
  160. }