260 lines
		
	
	
	
		
			7.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			260 lines
		
	
	
	
		
			7.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "config.hpp"
 | |
| 
 | |
| // 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;
 | |
| };
 | |
| 
 | |
| /// all connected pots
 | |
| GroupState group_states[GROUP_COUNT];
 | |
| 
 | |
| 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);
 | |
| 
 | |
|   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);
 | |
| 
 | |
|   Serial.println(F(" Done"));
 | |
| }
 | |
| 
 | |
| 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");
 | |
| 
 | |
|   digitalWrite(OK_LED_PIN, HIGH);
 | |
|   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;
 | |
| 
 | |
|     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);
 | |
| }
 | |
| 
 | |
| /// get humidity sensor value as a percentage
 | |
| 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);
 | |
|     delay(MEASUREMENT_DELAY);
 | |
|   }
 | |
| 
 | |
|   sensorVal /= MEASUREMENT_COUNT;
 | |
|   Serial.print(F("Average: "));
 | |
|   Serial.println(sensorVal);
 | |
| 
 | |
|   // 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/
 | |
|   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) {
 | |
|   Serial.print("setting pump to state ");
 | |
|   Serial.println(on);
 | |
|   digitalWrite(PUMP_LED_PIN, on ? HIGH : LOW);
 | |
|   digitalWrite(PUMP_PIN, on ? LOW : HIGH);
 | |
|   delay(PUMP_DELAY_MS);
 | |
| }
 | |
| 
 | |
| void set_valve(const Valve *valve, bool open) {
 | |
|   Serial.print("setting valve on pin ");
 | |
|   Serial.print(valve->pin);
 | |
|   Serial.print(" to state ");
 | |
|   Serial.println(open);
 | |
|   digitalWrite(valve->pin, open ? LOW : HIGH);
 | |
|   delay(VALVE_DELAY_MS);
 | |
| }
 | 
