outdoor-weather-station.ino 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //#define TEST_MODE
  2. #define USE_SERIAL
  3. #define LIGHT_PIN A3
  4. #define ERR reboot()
  5. #ifndef TEST_MODE
  6. #define SLEEP_TIME_MIN 5
  7. #define SLEEP_TIME_SEC 60 * SLEEP_TIME_MIN
  8. #define SLEEP_TIME_MS 1000 * SLEEP_TIME_SEC
  9. #else
  10. #define SLEEP_TIME_MS 10 * 1000 // 10 second sleep in test mode
  11. #endif
  12. #define WIFI_TIMEOUT_SECONDS 30 // How long to wait before giving up on wifi connection
  13. #ifdef USE_SERIAL
  14. char log_buff[200];
  15. #define debug(...) if(Serial) { sprintf(log_buff, __VA_ARGS__); Serial.println(log_buff); Serial.flush(); }
  16. #define debugln(X, Y) if(Serial) { Serial.print(X); Serial.println(Y); } // For things that can't be represented as a normal % formatter
  17. #define debugNoLn(...) if(Serial) { sprintf(log_buff, __VA_ARGS__); Serial.print(log_buff); }
  18. #else
  19. #define debug(...)
  20. #define debugln(X, Y)
  21. #define debugNoLn(...)
  22. #endif
  23. #define RTDB_AUTH_TOKEN "abc123"
  24. #define RTDB_PROJECT "weather-project"
  25. #define RTDB_PATH "outdoor"
  26. const String RTDB_HOST = String(RTDB_PROJECT) + "-default-rtdb.firebaseio.com";
  27. const String RTDB_URL = String("https://") + RTDB_HOST + "/" + RTDB_PATH + ".json?auth=" + RTDB_AUTH_TOKEN;
  28. // Used in various places to get the sleep time to be as close to SLEEP_TIME_MIN as possible.
  29. int64_t wakeTime;
  30. // Defined later, but declare it now.
  31. void showError();
  32. // Turn LEDs on to red, then go into deep sleep, restarting after 5 seconds.
  33. // Effectively doing a reboot, while visually displaying an error state.
  34. void reboot() {
  35. showError();
  36. const int64_t reboot_sleep_time = 5 * 1000 * 1000;
  37. debug("Rebooting by going to sleep for: %d", reboot_sleep_time);
  38. esp_sleep_enable_timer_wakeup(reboot_sleep_time);
  39. Serial.flush();
  40. esp_deep_sleep_start();
  41. }