Added basic device control.

This commit is contained in:
Chris Giacofei 2023-01-27 10:58:31 -05:00
parent fc80a43331
commit a9788e82a6
2 changed files with 118 additions and 41 deletions

View File

@ -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;
@ -113,10 +113,71 @@ float UpdateTemperature(DallasTemperature *sensors, DeviceAddress glycol_tank) {
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

View File

@ -88,7 +88,7 @@ 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,15 +161,14 @@ 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<TOTAL_DEVICES;i++) {
device_temp = UpdateTemperature(&sensors, DEVICE_LIST[i].temp_sensor);
hass_comm.publish_data(DEVICE_LIST[i].current_temp_topic, String(device_temp));
hass_comm.publish_data(DEVICE_LIST[i].mode.StateTopic, DEVICE_LIST[i].mode.Setting);
hass_comm.publish_data(DEVICE_LIST[i].action_topic, ACTION_COOLING);
bool dual_mode = DEVICE_LIST[i].dual;
device_temp = UpdateTemperature(&sensors, device.temp_sensor);
hass_comm.publish_data(device.current_temp_topic, String(device_temp));
hass_comm.publish_data(device.mode.StateTopic, device.mode.Setting);
hass_comm.publish_data(device.action_topic, device.action_state);
bool dual_mode = device.dual;
if (dual_mode) {
setpoint_count = 3;
@ -177,9 +176,8 @@ void SendUpdate() {
setpoint_count = 1;
}
for (int j=0;j<setpoint_count;j++) {
hass_comm.publish_data(DEVICE_LIST[i].setpoints[j].StateTopic, String(DEVICE_LIST[i].setpoints[j].Setting));
}
for (int i=0;i<setpoint_count;i++) {
hass_comm.publish_data(device.setpoints[i].StateTopic, String(device.setpoints[i].Setting));
}
}
@ -206,16 +204,34 @@ void setup() {
climateDevice("Fermenter 1", fermA_ds18b20,true);
climateDevice("Fermenter 2", fermB_ds18b20,true);
SendUpdate();
SendUpdate(DEVICE_LIST[0]);
SendUpdate(DEVICE_LIST[1]);
SendUpdate(DEVICE_LIST[2]);
lastMillis = millis();
}
void loop() {
currentMillis = millis();
if (currentMillis - lastMillis >= 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;
}