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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #if defined(ARDUINO_ARCH_ESP8266)
  35. WiFiEventHandler disconnectHandler;
  36. void onDisconnected(const WiFiEventStationModeDisconnected& event) {
  37. /*
  38. * simply restart in case we lose wifi connection
  39. * we can't do anything useful without wifi anyway!
  40. */
  41. ESP.restart();
  42. }
  43. #endif // ARDUINO_ARCH_ESP8266
  44. void setup() {
  45. pinMode(BUILTIN_LED_PIN, OUTPUT);
  46. Serial.begin(115200);
  47. debug.println(F("Initializing..."));
  48. // Blink LED for init
  49. for (int i = 0; i < 2; i++) {
  50. digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
  51. delay(LED_INIT_BLINK_INTERVAL);
  52. digitalWrite(BUILTIN_LED_PIN, HIGH); // LED off
  53. delay(LED_INIT_BLINK_INTERVAL);
  54. }
  55. config = mem_read();
  56. #ifdef FEATURE_RELAIS
  57. debug.println(F("Relais"));
  58. relais_init();
  59. #endif // FEATURE_RELAIS
  60. #ifdef FEATURE_MOISTURE
  61. debug.println(F("Moisture"));
  62. moisture_init();
  63. #endif // FEATURE_MOISTURE
  64. debug.println(F("Sensors"));
  65. initSensors();
  66. // Build hostname string
  67. String hostname = SENSOR_HOSTNAME_PREFIX;
  68. hostname += SENSOR_ID;
  69. #if defined(ARDUINO_ARCH_ESP8266)
  70. // Connect to WiFi AP
  71. debug.print(F("Connecting WiFi"));
  72. WiFi.hostname(hostname);
  73. WiFi.mode(WIFI_STA);
  74. WiFi.hostname(hostname);
  75. WiFi.begin(WIFI_SSID, WIFI_PASS);
  76. while (WiFi.status() != WL_CONNECTED) {
  77. delay(LED_CONNECT_BLINK_INTERVAL);
  78. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  79. debug.print(F("."));
  80. }
  81. debug.println(F("\nWiFi connected!"));
  82. disconnectHandler = WiFi.onStationModeDisconnected(onDisconnected);
  83. // Set hostname workaround
  84. WiFi.hostname(hostname);
  85. #elif defined(ARDUINO_ARCH_ESP32)
  86. // Set hostname workaround
  87. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  88. WiFi.setHostname(hostname.c_str());
  89. WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
  90. /*
  91. * was initially: workaround for WiFi connecting only every 2nd reset
  92. * https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
  93. *
  94. * now simply reset on every disconnect reason - we can't do anything
  95. * useful without wifi anyway!
  96. */
  97. esp_sleep_enable_timer_wakeup(10);
  98. esp_deep_sleep_start();
  99. delay(100);
  100. ESP.restart();
  101. }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  102. // Connect to WiFi AP
  103. debug.print(F("Connecting WiFi"));
  104. WiFi.mode(WIFI_STA);
  105. WiFi.setHostname(hostname.c_str());
  106. WiFi.begin(WIFI_SSID, WIFI_PASS);
  107. while (WiFi.status() != WL_CONNECTED) {
  108. delay(LED_CONNECT_BLINK_INTERVAL);
  109. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  110. debug.print(F("."));
  111. }
  112. debug.println(F("\nWiFi connected!"));
  113. // Set hostname workaround
  114. WiFi.setHostname(hostname.c_str());
  115. #elif defined(ARDUINO_ARCH_AVR)
  116. Serial1.begin(115200);
  117. WiFi.init(&Serial1);
  118. debug.print(F("Connecting WiFi"));
  119. WiFi.begin(WIFI_SSID, WIFI_PASS);
  120. while (WiFi.status() != WL_CONNECTED) {
  121. delay(LED_CONNECT_BLINK_INTERVAL);
  122. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  123. debug.print(F("."));
  124. }
  125. debug.println(F("\nWiFi connected!"));
  126. #endif
  127. debug.println(F("Seeding"));
  128. randomSeed(micros());
  129. debug.println(F("MQTT"));
  130. initMQTT();
  131. debug.println(F("Influx"));
  132. initInflux();
  133. debug.println(F("Servers"));
  134. initServers(hostname);
  135. debug.println(F("Ready! Starting..."));
  136. }
  137. void loop() {
  138. unsigned long time = millis();
  139. runServers();
  140. runSensors();
  141. runMQTT();
  142. runInflux();
  143. // blink heartbeat LED
  144. if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {
  145. last_led_blink_time = time;
  146. digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
  147. }
  148. }