diff --git a/lib/Device/Device.h b/lib/Device/Device.h index 444e934..4456cd3 100644 --- a/lib/Device/Device.h +++ b/lib/Device/Device.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include @@ -31,17 +31,17 @@ const char version[] = "0.0.1"; #ifndef DEVICE_NAME #define DEVICE_NAME "MQTT Thermostat" -#endif +#endif #ifndef DEVICE_MDL #define DEVICE_MDL "Thermostat" -#endif +#endif #ifndef DEVICE_MF #define DEVICE_MF "" -#endif +#endif -#ifndef ROOT +#ifndef ROOT #define ROOT "thermostat/" #endif @@ -55,24 +55,22 @@ const char version[] = "0.0.1"; #define TOPIC_LIMIT 4 -#define OFF 0 -#define COOL 1 -#define HEAT 2 -#define AUTO 3 +//~ #define OFF 0 +//~ #define COOL 1 +//~ #define HEAT 2 +//~ #define AUTO 3 + +#define TEMP 0 +#define TEMP_HI 1 +#define TEMP_LO 2 uint8_t CHILLER_SP = 70; uint8_t FERMA_SP = 70; uint8_t FERMB_SP = 70; -uint8_t CHILLER_MD = OFF; -uint8_t FERMA_MD = OFF; -uint8_t FERMB_MD = OFF; - //~ typedef void (*ptr)(uint8_t*, uint8_t*); //typedef ptr (*pm)(); - - struct CommandTopic { //~ ptr CmdFunc; byte* Setting; @@ -100,6 +98,8 @@ struct ControlDevice { String action_state; String swing_topic; String swing_state; + byte heat_pin; + byte cool_pin; Mode mode; SetPoint setpoints[3]; bool dual; @@ -108,15 +108,76 @@ struct ControlDevice { float UpdateTemperature(DallasTemperature *sensors, DeviceAddress glycol_tank) { // method 2 - faster float tempC = sensors->getTempC(glycol_tank); - if(tempC == DEVICE_DISCONNECTED_C) + if(tempC == DEVICE_DISCONNECTED_C) { Serial.println("Error: Could not read temperature data"); return 0; } - Serial.print("Temp C: "); - Serial.print(tempC); - Serial.print(" Temp F: "); + return DallasTemperature::toFahrenheit(tempC); } + +bool DeviceLoop(ControlDevice &device, DallasTemperature *sensors) { + Serial.println(device.name); + + float dsTemp = UpdateTemperature(sensors, device.temp_sensor); + String initial_action = device.action_state; + + if (device.mode.Setting == "auto") { + int setHi = device.setpoints[TEMP_HI].Setting; + int setLo = device.setpoints[TEMP_LO].Setting; + + if ((setLo <= dsTemp) && (dsTemp <= setHi)) { // Within Temp Range + device.action_state = ACTION_IDLE; + digitalWrite(device.cool_pin, LOW); + digitalWrite(device.heat_pin, LOW); + } else if (dsTemp < setLo) { // Needs Heat + device.action_state = ACTION_HEATING; + digitalWrite(device.cool_pin, LOW); + digitalWrite(device.heat_pin, HIGH); + } else if (dsTemp > setHi) { // Needs Cooling + device.action_state = ACTION_COOLING; + digitalWrite(device.cool_pin, HIGH); + digitalWrite(device.heat_pin, LOW); + } + } + + int setTemp = device.setpoints[TEMP].Setting; + + if (device.mode.Setting == "cool") { + + + if (dsTemp > setTemp) { // Needs Cooling + device.action_state = ACTION_COOLING; + digitalWrite(device.cool_pin, HIGH); + digitalWrite(device.heat_pin, LOW); + } else { + device.action_state = ACTION_IDLE; + digitalWrite(device.cool_pin, LOW); + digitalWrite(device.heat_pin, LOW); + } + } + + if (device.mode.Setting == "heat") { + if (dsTemp < setTemp) { // Needs Cooling + device.action_state = ACTION_HEATING; + digitalWrite(device.cool_pin, LOW); + digitalWrite(device.heat_pin, HIGH); + } else { + device.action_state = ACTION_IDLE; + digitalWrite(device.cool_pin, LOW); + digitalWrite(device.heat_pin, LOW); + } + } + + if (device.mode.Setting == "off") { + device.action_state = ACTION_IDLE; + } + + if (initial_action != device.action_state) { + return true; + } + return false; +} #endif diff --git a/src/FermController/FermController.ino b/src/FermController/FermController.ino index 32e16c1..63e9f11 100644 --- a/src/FermController/FermController.ino +++ b/src/FermController/FermController.ino @@ -88,8 +88,8 @@ void climateDevice(String name, byte ds18b20[], bool multimode=false) { ControlDevice device; device.dual = false; device.name = name; - device.topic_root = topic_root; - + device.topic_root = "brewhouse/" + slugify(name) + "/"; + for (int i=0;i<8;i++) { device.temp_sensor[i] = ds18b20[i]; } @@ -161,26 +161,24 @@ void climateDevice(String name, byte ds18b20[], bool multimode=false) { hass_comm.mqtt_discovery(config_topic, entity); } -void SendUpdate() { +void SendUpdate(ControlDevice device) { int setpoint_count = 0; float device_temp; - for (int i=0;i= 10000) { + if (DeviceLoop(DEVICE_LIST[0], &sensors)) { + Serial.println("Ouput state changed."); + SendUpdate(DEVICE_LIST[0]); + } + if (DeviceLoop(DEVICE_LIST[1], &sensors)) { + Serial.println("Ouput state changed."); + SendUpdate(DEVICE_LIST[1]); + } + if (DeviceLoop(DEVICE_LIST[2], &sensors)) { + Serial.println("Ouput state changed."); + SendUpdate(DEVICE_LIST[2]); + } sensors.requestTemperatures(); - SendUpdate(); + SendUpdate(DEVICE_LIST[0]); + SendUpdate(DEVICE_LIST[1]); + SendUpdate(DEVICE_LIST[2]); lastMillis = currentMillis; }