MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
main.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <Arduino.h>
16 
17 #include "WiFi.h"
18 #include "firebase_logger.h"
19 #include "ldr.h"
20 #include "light_scheduler.h"
21 #include "relay.h"
22 #include "scheduler.h"
23 #include "secrets.h"
24 #include "temperature_sensor.h"
25 #include "timer.h"
26 #include "utility.h"
27 
29 LDR ldr(10, &adc);
30 TemperatureSensor temp_sensor(pin::temp_sensor_bus);
31 RelayAH misters(pin::misters);
32 RelayAH light(pin::extra_relay); // changed role on purpose
33 LightScheduler light_scheduler(
34  &light, constants::light_start_hour, constants::light_start_min,
35  constants::light_duration);
36 RelayAH extra(pin::light);
40 
41 String wrap_date_time(const char * path)
42 {
43  auto timeinfo = timer.get_utc_time();
44  auto tim = mktime(&timeinfo);
45  char buf[12];
46  strftime(buf, sizeof(buf), "%F", &timeinfo);
47  String out(buf);
48  out += "/";
49  out += path;
50  out += "/";
51  out += tim;
52  return out;
53 }
54 
55 String wrap_date(const char * path)
56 {
57  auto timeinfo = timer.get_utc_time();
58  auto tim = mktime(&timeinfo);
59  char buf[12];
60  strftime(buf, sizeof(buf), "%F", &timeinfo);
61  String out(buf);
62  out += "/";
63  out += path;
64  return out;
65 }
66 
68 {
69  misters.toggle();
70  Serial.printf("Misters %s\n", (bool)misters.get_state() ? "on" : "off");
71  // set on DB day_ts/misters/ts : value
72  flog.set_bool(wrap_date_time("misters").c_str(), (bool)misters.get_state());
73 }
74 
76 {
77  float temp = temp_sensor.read();
78  Serial.printf("Temperature %f\n", temp);
79  // set on DB day_ts/temperature/ts : value
80  flog.set_float(wrap_date_time("temperature").c_str(), temp);
81 }
82 
84 {
85  float volt = ldr.read_voltage();
86 
87  auto time_info = timer.get_utc_time();
88 
89  light_scheduler.run(&time_info, volt >= constants::ldr_thresh_v);
90 
91  // set on DB /day_ts/ldr/ts : value
92  flog.set_float(wrap_date_time("ldr_volts").c_str(), volt);
93  // set on DB /day_ts/lights/ts : value
94  flog.set_bool(wrap_date_time("lights").c_str(), (bool)light.get_state());
95 }
96 
97 void check_wifi()
98 {
99  if (!WiFi.isConnected()) {
100  WiFi.reconnect();
101  Serial.println("Reconnecting WiFi ...");
102  }
103 }
104 
106 {
108  flog.push_time(
109  wrap_date("token_refreshed").c_str(), timer.get_epoch_time());
110  }
111 }
112 
113 void handle_wifi_connection(WiFiEvent_t event, WiFiEventInfo_t info)
114 {
115  if (event == WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED) {
116  Serial.println("WiFi Connected, initialising ...");
117  timer.begin();
118  flog.begin(
119  FIREBASE_URL, FIREBASE_TOKEN, FIREBASE_USER_EMAIL,
120  FIREBASE_USER_PASSWORD);
121  }
122 }
123 
124 void setup()
125 {
126  Serial.begin(115200);
130  adc.begin(constants::adc_bus_addr, pin::adc_sda, pin::adc_scl);
131  WiFi.mode(WIFI_STA);
132  // WiFi.onEvent(handle_wifi_connection, ARDUINO_EVENT_WIFI_STA_CONNECTED); // TODO: This does not work, wifi wont connect if set
133  // WiFi.setAutoReconnect(true); // TODO: This does not work, wifi wont connect if set
134  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
135  while (!WiFi.isConnected()) {
136  Serial.println("Connecting WiFi ...");
137  delay(2000);
138  }
139  timer.begin(); // TODO: Remove if can be set from handle wifi connection
140  flog.begin(
141  FIREBASE_URL, FIREBASE_TOKEN, FIREBASE_USER_EMAIL,
142  FIREBASE_USER_PASSWORD); // TODO: Remove if can be set from handle wifi connection
143  temp_sensor.begin();
144  delay(1000);
145  Serial.printf("Found temperature sensors: %d\n", temp_sensor.device_count());
146  scheduler.begin();
147  scheduler.create_task(check_wifi, constants::wifi_check_time);
149  check_and_refresh_firebase_token, constants::check_token_refresh_time);
150  scheduler.create_task(toggle_misters, constants::mister_toggle_time);
151  scheduler.create_task(check_temperature, constants::temperature_check_time);
152  scheduler.create_task(check_and_set_light, constants::light_check_n_set_time);
153  Serial.println("Initialisation complete");
154  flog.push_time(wrap_date("inits").c_str(), timer.get_epoch_time());
155 }
156 
157 void loop() { scheduler.run(); }
Definition: adc.h:28
void begin(int addr, int sda_pin, int scl_pin)
Definition: adc.cpp:21
Firebase RTDB logger.
void begin(const char *fire_url, const char *fire_token, const char *email, const char *pass)
initialise and authorise system to use Firebase RTDB (real time database) make sure wifi is initialis...
void set_float(const char *path, double value)
Set a float value.
void set_bool(const char *path, bool value)
Set a bool value.
bool check_and_refresh_token()
refresh token if expired
void push_time(const char *path, time_t timestamp)
Push a timestamp into a list to a relative path, useful to log times like when initialisation was com...
Light dependent resistor wrapper.
Definition: ldr.h:29
float read_voltage()
Read voltage from ADC for LDR. Make sure ADC is initialised before calling this method.
Definition: ldr.cpp:21
Schedules light switching depending on time of day and ambient light.
Active high relay.
Definition: relay.h:45
int get_state()
Get the current state.
Definition: relay.cpp:27
void begin(Switch sw)
initialise pin and set initial relay state
Definition: relay.cpp:19
void toggle()
Toggle the relay.
Definition: relay.cpp:29
A scheduler for running tasks on the controller, minimum time between events is 1 second based off th...
Definition: scheduler.h:34
void begin(long int timer_sync_interval_ms=600000)
initialise timer
Definition: scheduler.cpp:45
void run()
This needs to be run in the loop function continuously. Best practice is to have a loop function that...
Definition: scheduler.cpp:31
void create_task(void(*task_func)(), unsigned long interval_sec)
Create a task.
Definition: scheduler.cpp:21
wrapper for Dallas one-wire temperature sensor (DS18B20)
void begin()
Initialise all one wire sensors.
uint8_t device_count()
Get count of total sensors on the one wire bus.
float read()
Read temperature as float value from sensor.
Timer that syncs with NTP server.
Definition: timer.h:27
void begin(long int sync_interval_ms=600000)
initialise the timer, set a sync interval to sync with NTP server
Definition: timer.cpp:52
tm get_utc_time()
Get the utc time now.
Definition: timer.cpp:28
time_t get_epoch_time()
Get the seconds since epoch now.
Definition: timer.cpp:38
String wrap_date_time(const char *path)
Definition: main.cpp:41
RelayAH light(pin::extra_relay)
Definition: main.cpp:33
FireLogger flog
Definition: main.cpp:38
void handle_wifi_connection(WiFiEvent_t event, WiFiEventInfo_t info)
Definition: main.cpp:113
String wrap_date(const char *path)
Definition: main.cpp:55
LDR ldr(10, &adc)
void check_temperature()
Definition: main.cpp:75
void setup()
Definition: main.cpp:124
void toggle_misters()
Definition: main.cpp:67
RelayAH misters(pin::misters)
Scheduler scheduler
Definition: main.cpp:39
void check_and_refresh_firebase_token()
Definition: main.cpp:105
void check_wifi()
Definition: main.cpp:97
ADC adc
Definition: main.cpp:28
Timer timer
Definition: main.cpp:37
RelayAH extra(pin::light)
TemperatureSensor temp_sensor(pin::temp_sensor_bus)
void loop()
Definition: main.cpp:157
void check_and_set_light()
Definition: main.cpp:83