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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. unsigned long last_led_blink_time = 0;
  33. ConfigMemory config;
  34. void setup() {
  35. pinMode(BUILTIN_LED_PIN, OUTPUT);
  36. Serial.begin(115200);
  37. // Blink LED for init
  38. for (int i = 0; i < 2; i++) {
  39. digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
  40. delay(LED_INIT_BLINK_INTERVAL);
  41. digitalWrite(BUILTIN_LED_PIN, HIGH); // LED off
  42. delay(LED_INIT_BLINK_INTERVAL);
  43. }
  44. config = mem_read();
  45. debug.println(F("Relais"));
  46. relais_init();
  47. debug.println(F("Moisture"));
  48. moisture_init();
  49. initSensors();
  50. // Build hostname string
  51. String hostname = SENSOR_HOSTNAME_PREFIX;
  52. hostname += SENSOR_ID;
  53. #if defined(ARDUINO_ARCH_ESP8266)
  54. // Connect to WiFi AP
  55. debug.print(F("Connecting WiFi"));
  56. WiFi.hostname(hostname);
  57. WiFi.mode(WIFI_STA);
  58. WiFi.begin(WIFI_SSID, WIFI_PASS);
  59. while (WiFi.status() != WL_CONNECTED) {
  60. delay(LED_CONNECT_BLINK_INTERVAL);
  61. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  62. debug.print(F("."));
  63. }
  64. debug.println(F("\nWiFi connected!"));
  65. #elif defined(ARDUINO_ARCH_ESP32)
  66. // Set hostname workaround
  67. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  68. WiFi.setHostname(hostname.c_str());
  69. // Workaround for WiFi connecting only every 2nd reset
  70. // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
  71. WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
  72. if (info.disconnected.reason == 202) {
  73. esp_sleep_enable_timer_wakeup(10);
  74. esp_deep_sleep_start();
  75. delay(100);
  76. }
  77. }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  78. // Connect to WiFi AP
  79. debug.print(F("Connecting WiFi"));
  80. WiFi.mode(WIFI_STA);
  81. WiFi.begin(WIFI_SSID, WIFI_PASS);
  82. while (WiFi.status() != WL_CONNECTED) {
  83. delay(LED_CONNECT_BLINK_INTERVAL);
  84. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  85. debug.print(F("."));
  86. }
  87. debug.println(F("\nWiFi connected!"));
  88. // Set hostname workaround
  89. WiFi.setHostname(hostname.c_str());
  90. #elif defined(ARDUINO_ARCH_AVR)
  91. Serial1.begin(115200);
  92. WiFi.init(&Serial1);
  93. debug.print(F("Connecting WiFi"));
  94. WiFi.begin(WIFI_SSID, WIFI_PASS);
  95. while (WiFi.status() != WL_CONNECTED) {
  96. delay(LED_CONNECT_BLINK_INTERVAL);
  97. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  98. debug.print(F("."));
  99. }
  100. debug.println(F("\nWiFi connected!"));
  101. #endif
  102. debug.println(F("Seeding"));
  103. randomSeed(micros());
  104. initMQTT();
  105. initInflux();
  106. initServers(hostname);
  107. }
  108. void loop() {
  109. unsigned long time = millis();
  110. runServers();
  111. runSensors();
  112. runMQTT();
  113. runInflux();
  114. // blink heartbeat LED
  115. if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {
  116. last_led_blink_time = time;
  117. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  118. }
  119. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  120. // reset ESP every 3d to be safe
  121. if (time >= (3UL * 24UL * 60UL * 60UL * 1000UL)) {
  122. ESP.restart();
  123. }
  124. #endif
  125. }