restructure logic to work on groups of valves and sensors instead of being 1:1:1
This commit is contained in:
parent
794c17052f
commit
7a25e4b46e
1 changed files with 337 additions and 223 deletions
288
drop.ino
288
drop.ino
|
|
@ -1,43 +1,179 @@
|
|||
#include "config.hpp"
|
||||
|
||||
/// a connected pot
|
||||
typedef struct {
|
||||
PotConfig *config;
|
||||
// TODO: configuration validation subroutine
|
||||
// - sensors belong to an existing group
|
||||
// - valves belong to an existing group
|
||||
// - all groups have a sensor
|
||||
|
||||
constexpr unsigned int VALVE_COUNT = sizeof(VALVES) / sizeof(VALVES[0]);
|
||||
constexpr unsigned int SENSOR_COUNT = sizeof(SENSORS) / sizeof(SENSORS[0]);
|
||||
constexpr unsigned int GROUP_COUNT = sizeof(GROUPS) / sizeof(GROUPS[0]);
|
||||
|
||||
struct GroupState {
|
||||
const Group *group_config;
|
||||
/// pointer to first element of sensors array
|
||||
const Sensor **sensors;
|
||||
/// element count of sensors array
|
||||
unsigned int sensor_count;
|
||||
/// pointer to first element of valves array
|
||||
const Valve **valves;
|
||||
/// element count of valves array
|
||||
unsigned int valve_count;
|
||||
|
||||
unsigned long last_watering;
|
||||
// anything changing per pot goes here
|
||||
} Pot;
|
||||
};
|
||||
|
||||
/// all connected pots
|
||||
Pot pots[POT_COUNT];
|
||||
GroupState group_states[GROUP_COUNT];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("");
|
||||
Serial.println("Initializing...");
|
||||
void turn_everything_off() {
|
||||
set_pump(false);
|
||||
|
||||
for (int i = 0; i < VALVE_COUNT; i++) {
|
||||
set_valve(&VALVES[i], false);
|
||||
}
|
||||
}
|
||||
|
||||
/// (╯°□°)╯︵ ┻━┻
|
||||
///
|
||||
/// Will never return
|
||||
void panic(const __FlashStringHelper *message) {
|
||||
turn_everything_off();
|
||||
|
||||
// keep doing this until someone notices the blinking lights and looks at the serial output
|
||||
while (true) {
|
||||
Serial.print(F("PANIC: "));
|
||||
Serial.println(message);
|
||||
|
||||
digitalWrite(PUMP_LED_PIN, HIGH);
|
||||
digitalWrite(OK_LED_PIN, LOW);
|
||||
delay(300);
|
||||
digitalWrite(PUMP_LED_PIN, LOW);
|
||||
digitalWrite(OK_LED_PIN, HIGH);
|
||||
delay(300);
|
||||
}
|
||||
}
|
||||
|
||||
void setup_pinmodes(void) {
|
||||
Serial.print(F("Setting pin modes..."));
|
||||
|
||||
pinMode(PUMP_LED_PIN, OUTPUT);
|
||||
pinMode(PUMP_PIN, OUTPUT);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(OK_LED_PIN, OUTPUT);
|
||||
|
||||
set_pump(false);
|
||||
for (unsigned int i = 0; i < VALVE_COUNT; i++)
|
||||
pinMode(VALVES[i].pin, OUTPUT);
|
||||
for (unsigned int i = 0; i < SENSOR_COUNT; i++)
|
||||
pinMode(SENSORS[i].pin, INPUT);
|
||||
for (unsigned int i = 0; i < GROUP_COUNT; i++)
|
||||
pinMode(GROUPS[i].led_pin, OUTPUT);
|
||||
|
||||
// link pots to their config
|
||||
for (unsigned int i = 0; i < POT_COUNT; i++) {
|
||||
Pot *pot = &pots[i];
|
||||
|
||||
pot->config = &POT_CONFIGS[i];
|
||||
pot->last_watering = 0;
|
||||
|
||||
pinMode(pot->config->valve_pin, OUTPUT);
|
||||
pinMode(pot->config->led_pin, OUTPUT);
|
||||
|
||||
set_valve(pot->config->valve_pin, false);
|
||||
}
|
||||
Serial.println(F(" Done"));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
void setup_config(void) {
|
||||
Serial.print(F("Loading configuration..."));
|
||||
|
||||
bool found_enabled = false;
|
||||
|
||||
for (unsigned int group_index = 0; group_index < GROUP_COUNT; group_index++) {
|
||||
struct GroupState *group = &group_states[group_index];
|
||||
*group = {
|
||||
.group_config = &GROUPS[group_index],
|
||||
.sensors = NULL,
|
||||
.sensor_count = 0,
|
||||
.valves = NULL,
|
||||
.valve_count = 0,
|
||||
.last_watering = 0,
|
||||
};
|
||||
|
||||
digitalWrite(group->group_config->led_pin, HIGH);
|
||||
|
||||
// link valves to group
|
||||
for (unsigned int valve_index = 0; valve_index < VALVE_COUNT; valve_index++) {
|
||||
if (VALVES[valve_index].pot_group != group_index)
|
||||
continue;
|
||||
|
||||
group->valve_count++;
|
||||
group->valves = (const Valve **) realloc(group->valves, group->valve_count * sizeof(Valve *));
|
||||
if (group->valves == NULL)
|
||||
panic(F("valve realloc failed"));
|
||||
group->valves[group->valve_count - 1] = &VALVES[valve_index];
|
||||
}
|
||||
|
||||
// link sensors to group
|
||||
for (unsigned int sensor_index = 0; sensor_index < SENSOR_COUNT; sensor_index++) {
|
||||
if (SENSORS[sensor_index].pot_group != group_index)
|
||||
continue;
|
||||
|
||||
group->sensor_count++;
|
||||
group->sensors = (const Sensor **) realloc(group->sensors, group->sensor_count * sizeof(Sensor *));
|
||||
if (group->sensors == NULL)
|
||||
panic(F("sensor realloc failed"));
|
||||
group->sensors[group->sensor_count - 1] = &SENSORS[sensor_index];
|
||||
}
|
||||
|
||||
// check that all enabled groups have valid configuration
|
||||
if (group->group_config->enabled) {
|
||||
found_enabled = true;
|
||||
|
||||
if (group->valve_count == 0)
|
||||
panic(F("invalid configuration - no valves in enabled group!"));
|
||||
if (group->sensor_count == 0)
|
||||
panic(F("invalid configuration - no sensors in enabled group!"));
|
||||
}
|
||||
|
||||
digitalWrite(group->group_config->led_pin, LOW);
|
||||
}
|
||||
|
||||
if (!found_enabled)
|
||||
panic(F("invalid configuration - all groups are disabled"));
|
||||
|
||||
for (unsigned int sensor_index = 0; sensor_index < SENSOR_COUNT; sensor_index++) {
|
||||
if (SENSORS[sensor_index].pot_group >= GROUP_COUNT)
|
||||
panic(F("sensor is mapped to group that does not exist"));
|
||||
}
|
||||
|
||||
for (unsigned int valve_index = 0; valve_index < VALVE_COUNT; valve_index++) {
|
||||
if (VALVES[valve_index].pot_group >= GROUP_COUNT)
|
||||
panic(F("valve is mapped to group that does not exist"));
|
||||
}
|
||||
|
||||
Serial.println(F(" Done"));
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(9600);
|
||||
Serial.println("");
|
||||
Serial.println(F("Initializing..."));
|
||||
setup_pinmodes();
|
||||
Serial.print(F("Turning everything off in case of power cycle..."));
|
||||
turn_everything_off();
|
||||
setup_config();
|
||||
Serial.println(F("Initialization completed."));
|
||||
}
|
||||
|
||||
void water_group(GroupState *state) {
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
for (unsigned int valve_index = 0; valve_index < VALVE_COUNT; valve_index++) {
|
||||
const Valve *valve = state->valves[valve_index];
|
||||
|
||||
set_valve(valve, HIGH);
|
||||
set_pump(HIGH);
|
||||
|
||||
delay(WATER_TIME_MS);
|
||||
state->last_watering = millis();
|
||||
|
||||
set_pump(LOW);
|
||||
set_valve(valve, LOW);
|
||||
}
|
||||
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
Serial.println("");
|
||||
Serial.println("LOOP");
|
||||
|
||||
|
|
@ -45,66 +181,46 @@ void loop() {
|
|||
delay(100);
|
||||
digitalWrite(OK_LED_PIN, LOW);
|
||||
|
||||
for (unsigned int g = 0; g < GROUP_COUNT; g++) {
|
||||
GroupState *state = &group_states[g];
|
||||
if (!state->group_config->enabled)
|
||||
continue;
|
||||
|
||||
for (unsigned int i = 0; i < POT_COUNT; i++) {
|
||||
Serial.print("Pot ");
|
||||
Serial.println(i);
|
||||
per_pot(pots[i]);
|
||||
Serial.println("");
|
||||
digitalWrite(state->group_config->led_pin, HIGH);
|
||||
|
||||
Serial.print(F("Group "));
|
||||
Serial.print(g);
|
||||
Serial.print(F(": "));
|
||||
|
||||
int humidity = get_group_humidity(state->sensors, state->sensor_count);
|
||||
Serial.println(humidity);
|
||||
|
||||
if (state->last_watering + MIN_WATERING_INTERVAL_MS > millis()) {
|
||||
Serial.println(F("watered recently -> not watering"));
|
||||
} else if (humidity > MAX_HUMIDITY_PERCENT) {
|
||||
Serial.println(F("too wet -> not watering"));
|
||||
} else if (humidity < MIN_HUMIDITY_PERCENT) {
|
||||
Serial.println(F("too dry -> watering"));
|
||||
water_group(state);
|
||||
} else if (state->last_watering + MAX_WATERING_INTERVAL_MS < millis()) {
|
||||
Serial.println(F("not been watered for a long time -> watering"));
|
||||
water_group(state);
|
||||
} else {
|
||||
Serial.println(F("happy plant"));
|
||||
}
|
||||
|
||||
digitalWrite(state->group_config->led_pin, LOW);
|
||||
}
|
||||
|
||||
|
||||
delay(LOOP_DELAY_MS);
|
||||
}
|
||||
|
||||
void per_pot(Pot &pot) {
|
||||
if (pot.last_watering + MIN_WATERING_INTERVAL_MS > millis()) {
|
||||
Serial.println(F("watered recently -> not watering"));
|
||||
return;
|
||||
}
|
||||
|
||||
int percentage = get_humidity(pot.config->sensor);
|
||||
if (percentage > MAX_HUMIDITY_PERCENT) {
|
||||
Serial.println(F("too wet -> not watering"));
|
||||
} else if (percentage < MIN_HUMIDITY_PERCENT) {
|
||||
Serial.println(F("too dry -> watering"));
|
||||
water_pot(pot);
|
||||
} else if (pot.last_watering + MAX_WATERING_INTERVAL_MS < millis()) {
|
||||
Serial.println(F("not been watered for a long time -> watering"));
|
||||
water_pot(pot);
|
||||
} else {
|
||||
Serial.println(F("happy plant"));
|
||||
}
|
||||
}
|
||||
|
||||
void water_pot(Pot &pot) {
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
digitalWrite(pot.config->led_pin, HIGH);
|
||||
|
||||
set_valve(pot.config->valve_pin, HIGH);
|
||||
set_pump(HIGH);
|
||||
|
||||
delay(WATER_TIME_MS);
|
||||
pot.last_watering = millis();
|
||||
|
||||
set_pump(LOW);
|
||||
set_valve(pot.config->valve_pin, LOW);
|
||||
|
||||
digitalWrite(pot.config->led_pin, LOW);
|
||||
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
|
||||
/// get humidity sensor value as a percentage
|
||||
int get_humidity(Sensor &sensor) {
|
||||
int get_humidity(const Sensor *&sensor) {
|
||||
// take average of multiple measurements
|
||||
int sensorVal {0};
|
||||
for (unsigned int i = 0; i < MEASUREMENT_COUNT; i++) {
|
||||
sensorVal += analogRead(sensor.pin);
|
||||
Serial.print(F("Measurement "));
|
||||
Serial.print(i);
|
||||
Serial.print(F(" = "));
|
||||
Serial.println(sensorVal);
|
||||
sensorVal += analogRead(sensor->pin);
|
||||
delay(MEASUREMENT_DELAY);
|
||||
}
|
||||
|
||||
|
|
@ -115,11 +231,15 @@ int get_humidity(Sensor &sensor) {
|
|||
// Sensor has a range of e.g. 236 to 520
|
||||
// We want to translate this to a scale or 0% to 100%
|
||||
// More info: https://www.arduino.cc/reference/en/language/functions/math/map/
|
||||
sensorVal = map(sensorVal, sensor.calibration_wet, sensor.calibration_dry, 100, 0);
|
||||
Serial.print(F("Humidity: "));
|
||||
Serial.print(sensorVal);
|
||||
Serial.println(F("%"));
|
||||
return sensorVal;
|
||||
return map(sensorVal, sensor->calibration_wet, sensor->calibration_dry, 100, 0);
|
||||
}
|
||||
|
||||
int get_group_humidity(const Sensor **sensors, unsigned int sensor_count) {
|
||||
int sum = 0;
|
||||
for (unsigned int s = 0; s < sensor_count; s++) {
|
||||
sum += get_humidity(sensors[s]);
|
||||
}
|
||||
return sum / sensor_count;
|
||||
}
|
||||
|
||||
void set_pump(bool on) {
|
||||
|
|
@ -130,11 +250,11 @@ void set_pump(bool on) {
|
|||
delay(PUMP_DELAY_MS);
|
||||
}
|
||||
|
||||
void set_valve(uint8_t valve, bool open) {
|
||||
void set_valve(const Valve *valve, bool open) {
|
||||
Serial.print("setting valve on pin ");
|
||||
Serial.print(valve);
|
||||
Serial.print(valve->pin);
|
||||
Serial.print(" to state ");
|
||||
Serial.println(open);
|
||||
digitalWrite(valve, open ? LOW : HIGH);
|
||||
digitalWrite(valve->pin, open ? LOW : HIGH);
|
||||
delay(VALVE_DELAY_MS);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue