diff --git a/boil_kettle/boil_kettle.ino b/boil_kettle/boil_kettle.ino index 7895f38..a73bdef 100644 --- a/boil_kettle/boil_kettle.ino +++ b/boil_kettle/boil_kettle.ino @@ -3,28 +3,21 @@ #include #include #include +#include +#include // Additoinal Libraries -#include -#include #include #include // LiquidMenu_config.h needs to be modified to use I2C. #include -#include // My Includes #include "config.h" +#include "globals.h" #include "button.h" #include "slowPWM.h" #include "thermoControl.h" -// Global variables. -uint8_t KettleDuty = 0; -uint8_t KettleTemp; -uint8_t KettleSetpoint; -modes KettleMode = OFF; -const int UpdateInterval = 5000; - /* Defined in config.h for now */ // uint8_t ThreshPWR = 5; // double Hysteresis = 1; @@ -34,24 +27,20 @@ Button Enter; MD_REncoder rotary = MD_REncoder(I_DT, I_CLK); LiquidCrystal_I2C lcd(0x27,20,4); -// Internal I/O -Adafruit_MAX31865 KettleThermo = Adafruit_MAX31865(kettleRTDCS); +// Internal I/O objects. slowPWM boilPWM; thermoControl KettleController; -void MessageReceived(char*, byte*, unsigned int); - +// Network objects. EthernetClient net; PubSubClient mqtt_client; -unsigned long lastRun = 0; - // Return a character array to represent the -// On/Off state of the kettle. +// state of the kettle. char* ShowKettleState() { - if (KettleMode == MANUAL) { + if (KettleController.Mode() == MANUAL) { return (char*)F("Kettle: Manual"); - } else if (KettleMode == AUTOMATIC) { + } else if (KettleController.Mode() == AUTOMATIC) { return (char*)F("Kettle: Auto"); } else { return (char*)F("Kettle: Off"); @@ -61,18 +50,18 @@ char* ShowKettleState() { char* ShowKettleSetting() { static char LCD_Line[21]; char setting[4]; - if (KettleMode == MANUAL) { + if (KettleController.Mode() == MANUAL) { strcpy(LCD_Line, (char*)F("Kettle Power: ")); - itoa(KettleDuty / 10, setting, 10); + itoa(KettleController.Power(), setting, 10); strcat(LCD_Line, setting); - strcat(LCD_Line, "%"); + strcat(LCD_Line, (char*)F("%")); return LCD_Line; - } else if (KettleMode == AUTOMATIC) { + } else if (KettleController.Mode() == AUTOMATIC) { strcpy(LCD_Line, (char*)F("Kettle Temp: ")); - itoa(KettleSetpoint / 10, setting, 10); + itoa(KettleController.Setpoint(), setting, 10); strcat(LCD_Line, setting); - strcat(LCD_Line, "F"); + strcat(LCD_Line, (char*)F("F")); return LCD_Line; } else { return (char*)""; @@ -87,18 +76,20 @@ void doEncoder() { uint8_t result = rotary.read(); uint8_t inc; + uint8_t KettleDuty = (uint8_t)KettleController.Power(); if (result) { uint8_t speed = rotary.speed(); - speed >= 10 ? inc = 50 : inc = 10; + speed >= 10 ? inc = 5 : inc = 1; } - if (result == DIR_CW && KettleDuty < 1000) { + if (result == DIR_CW && KettleDuty < 100) { KettleDuty = (KettleDuty / inc) * inc + inc; } else if (result == DIR_CCW && KettleDuty > 0) { KettleDuty = (KettleDuty / inc) * inc - inc; } - + KettleController.Power((double)KettleDuty); + SettingChanged = true; } // LCD menu setup. @@ -146,23 +137,16 @@ void setup() { }; void UpdateBoilKettle(){ - static uint8_t last_KettleDuty = 0; - static uint8_t last_KettleTemp = 0; + if (Enter.pressed()) { - - KettleMode = (modes)(KettleMode + 1); - menu.update(); + KettleController.CycleMode(); + SettingChanged = true; } - - if (last_KettleDuty != KettleDuty) { - last_KettleDuty = KettleDuty; - menu.update(); - } - - if (last_KettleTemp != KettleTemp) { - last_KettleTemp = KettleTemp; + + if (SettingChanged) { menu.update(); + SettingChanged = false; } if (KettleController.Mode() != OFF) { @@ -174,7 +158,6 @@ void UpdateBoilKettle(){ } void loop() { - KettleTemp = (uint8_t)(KettleThermo.readRTD() * 10); UpdateBoilKettle(); unsigned long elapsedTime = (millis() - lastRun); diff --git a/boil_kettle/mqtt.ino b/boil_kettle/mqtt.ino index 66e94ba..a965304 100644 --- a/boil_kettle/mqtt.ino +++ b/boil_kettle/mqtt.ino @@ -1,3 +1,4 @@ + void ConnectMQTT() { ConfigData config; EEPROM.get(ConfAddress, config); diff --git a/boil_kettle/slowPWM.h b/boil_kettle/slowPWM.h index 7355f38..94b9a38 100644 --- a/boil_kettle/slowPWM.h +++ b/boil_kettle/slowPWM.h @@ -17,7 +17,6 @@ class slowPWM { lastSwitchTime = 0; outputState = LOW; pinMode(pin, OUTPUT); - Serial.println("Setup PWM"); } byte compute(uint8_t duty) { diff --git a/boil_kettle/thermoControl.cpp b/boil_kettle/thermoControl.cpp index cda0931..4f20ccf 100644 --- a/boil_kettle/thermoControl.cpp +++ b/boil_kettle/thermoControl.cpp @@ -3,14 +3,25 @@ #include #include "thermoControl.h" -void thermoControl::begin(uint8_t* input_temp, uint8_t* setpoint_temp, uint8_t* output_pwm, uint8_t max_threshold, uint8_t hysteresis) { + +void thermoControl::begin(int8_t pinRTD) { + RTD = new Adafruit_MAX31865(pinRTD); + outMax = 100; + outMin = 10; + OpMode = OFF; + SampleTime = 100; + lastTime = millis()-SampleTime; +} + +void thermoControl::begin(int8_t pinRTD, double temp, double output_pwm, double max_pwr_threshold, double hysteresis) { output_pwm = output_pwm; - input_temp = input_temp; - setpoint_temp = setpoint_temp; - - outMax = 1000; - outMin = 100; - + + RTD = new Adafruit_MAX31865(pinRTD); + control_temp = temp; + + outMax = 100; + outMin = 10; + OpMode = OFF; SampleTime = 100; lastTime = millis()-SampleTime; @@ -20,20 +31,22 @@ bool thermoControl::Compute() { unsigned long now = millis(); unsigned long timeChange = (now - lastTime); - if(timeChange>=SampleTime && OpMode == AUTOMATIC) { - uint8_t output; - uint8_t error = *setpoint_temp - *input_temp; + double temp = RTD->readRTD(); - if (error >= *max_threshold) { + if(timeChange>=SampleTime && OpMode == AUTOMATIC) { + double output; + double error = control_temp - temp; + + if (error >= max_pwr_threshold) { output = outMax; - } else if (error > *hysteresis) { - output = 100 * error / *max_threshold; + } else if (error > hysteresis) { + output = 100 * error / max_pwr_threshold; output = max(output, outMin); output = min(output, outMax); } else { output = 0; } - *output_pwm = output; + output_pwm = output; lastTime = now; return true; @@ -42,28 +55,48 @@ bool thermoControl::Compute() { } } +double thermoControl::Power() { + return output_pwm; +} + +void thermoControl::Power(double pwr) { + output_pwm = pwr; +} + +double thermoControl::Setpoint() { + return control_temp; +} + +void thermoControl::Setpoint(double temp) { + control_temp = temp; +} + void thermoControl::SetSampleTime(int NewSampleTime) { if (NewSampleTime > 0) { SampleTime = (unsigned long)NewSampleTime; } } -void thermoControl::SetPowerLimits(uint8_t Max, uint8_t Min) { +void thermoControl::SetPowerLimits(double Max, double Min) { if(Min >= Max) return; outMax = Max; outMin = Min; } -void thermoControl::SetMode(modes newMode) { +void thermoControl::Mode(modes newMode) { OpMode = newMode; } -modes thermoControl::GetMode() { +modes thermoControl::Mode() { return OpMode; } modes thermoControl::CycleMode() { - OpMode = (modes)(OpMode + 1); + if (OpMode + 1 == OVERFLOW) { + OpMode = (modes)(0); + } else { + OpMode = (modes)(OpMode + 1); + } return OpMode; } - + diff --git a/boil_kettle/thermoControl.h b/boil_kettle/thermoControl.h index f980506..ca0f2af 100644 --- a/boil_kettle/thermoControl.h +++ b/boil_kettle/thermoControl.h @@ -1,32 +1,39 @@ #ifndef THERMOCONTROL_h #define THERMOCONTROL_h -enum modes : uint8_t {OFF, AUTOMATIC, MANUAL}; +#include + +enum modes : uint8_t {OFF, AUTOMATIC, MANUAL, OVERFLOW}; class thermoControl { private: - double *input_temp; - double *output_pwm; - double *setpoint_temp; - double *hysteresis; - double *max_threshold; - double outMax; - double outMin; - + Adafruit_MAX31865* RTD; + double output_pwm; + double control_temp; + double hysteresis; + double max_pwr_threshold; + int outMax; + int outMin; + modes OpMode; unsigned long SampleTime; unsigned long lastTime; public: - void begin(uint8_t*, uint8_t*, uint8_t*, uint8_t, uint8_t); + void begin(int8_t, double, double, double, double); + void begin(int8_t); bool Compute(); + double Power(); + void Power(double); + double Setpoint(); + void Setpoint(double); void SetSampleTime(int); - void SetPowerLimits(uint8_t, uint8_t); - void SetHysteresis(uint8_t); - void SetThreshPWR(uint8_t); - void SetMode(modes); - modes GetMode(); + void SetPowerLimits(double, double); + void SetHysteresis(double); + void SetThreshPWR(double); + void Mode(modes); + modes Mode(); modes CycleMode(); };