276 lines
6.4 KiB
C++
276 lines
6.4 KiB
C++
//Built-in
|
|
#include <Arduino.h>
|
|
#include <SPI.h>
|
|
#include <Ethernet.h>
|
|
|
|
// Additoinal Libraries
|
|
#include <ArduinoJson.h>
|
|
#include <MQTT.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
#include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C.
|
|
#include <MD_REncoder.h>
|
|
#include <Adafruit_MAX31865.h>
|
|
|
|
// My Includes
|
|
#include "button.h"
|
|
#include "config.h"
|
|
#include "menu.h"
|
|
|
|
enum kettle_mode {OFF=0, PWM=1, OVERFLOW};
|
|
enum encoder_state {ENC_ST_LINE=0, ENC_ST_SETTING=1, ENC_ST_OOB};
|
|
enum function_types {increase=1, decrease};
|
|
|
|
/* ---------- Global variables ---------- */
|
|
byte KettleDuty = 0;
|
|
bool NetworkOn = false;
|
|
kettle_mode KettleMode = OFF;
|
|
encoder_state EncoderState = ENC_ST_LINE;
|
|
|
|
/* ---------- User I/O objects ---------- */
|
|
Button EncoderButton;
|
|
MD_REncoder RotaryEncoder = MD_REncoder(encoderDT, encoderCLK);
|
|
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4);
|
|
|
|
/* ---------- Controller I/O objects ---------- */
|
|
Adafruit_MAX31865 kettleRTD = Adafruit_MAX31865(kettleRTDCS);
|
|
Adafruit_MAX31865 mashRTD = Adafruit_MAX31865(mashRTDCS);
|
|
|
|
/* ---------- Network bits ---------- */
|
|
EthernetClient net;
|
|
MQTTClient mqtt_client;
|
|
|
|
/* ---------- LCD menu setup ---------- */
|
|
LiquidLine kettle_mode_line(0, 0, "Output Mode ", KettleState);
|
|
LiquidLine kettle_setpoint_line(0, 1, "Kettle Power ", KettleSetpoint, SetpointUnit);
|
|
LiquidLine kettle_actual_line(0, 2, "Kettle Temp ", KettleActual, "F");
|
|
LiquidScreen home_screen(kettle_mode_line, kettle_setpoint_line, kettle_actual_line);
|
|
LiquidMenu menu(lcd);
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
|
Serial.println("Setting up...");
|
|
Ethernet.begin(mac, ip);
|
|
|
|
// Set all the I/O pin states
|
|
RotaryEncoder.begin();
|
|
EncoderButton.begin(encoderBTN);
|
|
kettleRTD.begin(MAX31865_3WIRE);
|
|
mashRTD.begin(MAX31865_3WIRE);
|
|
pinMode(encoderCLK, INPUT_PULLUP);
|
|
pinMode(encoderDT, INPUT_PULLUP);
|
|
pinMode(kettlePWM, OUTPUT);
|
|
|
|
// Attach interrupts to the rotary encoder
|
|
attachInterrupt(digitalPinToInterrupt(encoderCLK), doEncoder, CHANGE);
|
|
attachInterrupt(digitalPinToInterrupt(encoderDT), doEncoder, CHANGE);
|
|
|
|
// Attach functions to menu lines
|
|
kettle_mode_line.attach_function(increase, kettle_mode_next);
|
|
kettle_mode_line.attach_function(decrease, kettle_mode_previous);
|
|
kettle_setpoint_line.attach_function(increase, increase_setpoint);
|
|
kettle_setpoint_line.attach_function(decrease, decrease_setpoint);
|
|
|
|
lcd.init();
|
|
lcd.backlight();
|
|
|
|
// Check for network connection before attemping MQTT setup.
|
|
if (Ethernet.linkStatus() == LinkON) {
|
|
NetworkOn = true;
|
|
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("Ethernet connected.");
|
|
lcd.setCursor(1, 1);
|
|
lcd.print(net.remoteIP());
|
|
|
|
SetupMQTT(MQTT_BROKER);
|
|
} else {
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("No network connection.");
|
|
}
|
|
|
|
delay(5000);
|
|
lcd.clear();
|
|
|
|
menu.init();
|
|
menu.add_screen(home_screen);
|
|
menu.update();
|
|
|
|
};
|
|
|
|
void loop() {
|
|
ButtonCheck();
|
|
|
|
if (KettleMode == PWM) {
|
|
slowPWM(kettlePWM, KettleDuty, PeriodPWM);
|
|
} else {
|
|
slowPWM(kettlePWM, 0, PeriodPWM);
|
|
}
|
|
|
|
static unsigned long lastRun = millis() - UpdateInterval;
|
|
unsigned long elapsedTime = (millis() - lastRun);
|
|
|
|
//
|
|
if (NetworkOn && elapsedTime >= UpdateInterval) {
|
|
mqtt_client.loop();
|
|
if (!mqtt_client.connected()) ConnectMQTT();
|
|
|
|
SendSensorData();
|
|
lastRun = millis();
|
|
}
|
|
|
|
}
|
|
|
|
/* Interrupt function to run when encoder is turned.
|
|
Either switches focus of the menu lines or
|
|
changes setpoint of focused line. */
|
|
void doEncoder() {
|
|
uint8_t result = RotaryEncoder.read();
|
|
|
|
// check in which state the encoder is currently in
|
|
switch (EncoderState) {
|
|
case ENC_ST_LINE: // cycling focus through the lines
|
|
if (result == DIR_CW) {
|
|
menu.switch_focus(true);
|
|
} else {
|
|
menu.switch_focus(false);
|
|
}
|
|
break;
|
|
case ENC_ST_SETTING: // changing a setting
|
|
if (result == DIR_CW) {
|
|
menu.call_function(increase);
|
|
} else {
|
|
menu.call_function(decrease);
|
|
}
|
|
break;
|
|
default: // invalid state
|
|
EncoderState = 0; // return to a valid state
|
|
break;
|
|
}
|
|
menu.update();
|
|
}
|
|
|
|
// Cycle the rotary encoder state when button is pressed.
|
|
// Switches between menu navigation and adjusting setpoints.
|
|
void ButtonCheck(){
|
|
|
|
if (!EncoderButton.pressed()) { return; }
|
|
|
|
if (EncoderState + 1 < ENC_ST_OOB) { // check if the next state is valid
|
|
EncoderState = (encoder_state)(EncoderState + 1); // increment to the next state (ENC_ST_LINE -> ENC_ST_SETTING)
|
|
} else {
|
|
EncoderState = 0; // in case of press while changing a setting - wrap around to state ENC_ST_LINE
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Return a character array to represent the state of the kettle.
|
|
*/
|
|
char* KettleState() {
|
|
if (KettleMode == OFF) {
|
|
return (char*)"OFF";
|
|
} else if (KettleMode == PWM) {
|
|
return (char*)"PWM";
|
|
} else {
|
|
return (char*)"PID";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return the setpoint of the kettle
|
|
*/
|
|
char* KettleSetpoint() {
|
|
if (KettleMode == OFF) {
|
|
return (char*)"---";
|
|
} else if (KettleMode == PWM) {
|
|
return (char*)KettleDuty;
|
|
}
|
|
/* Eventually
|
|
else if (KettleMode == PID) {
|
|
return (char*)KettleTEMP;
|
|
}
|
|
*/
|
|
}
|
|
|
|
/**
|
|
* Return the actual temp as char.
|
|
*/
|
|
char* KettleActual() {
|
|
char buffer[6];
|
|
dtostrf(kettleRTD.temperature(RNOMINAL_KETTLE, RREF_KETTLE), 5, 1, buffer);
|
|
return buffer;
|
|
}
|
|
|
|
/**
|
|
* Return the actual temp as char.
|
|
*/
|
|
char* MashActual() {
|
|
char buffer[6];
|
|
dtostrf(mashRTD.temperature(RNOMINAL_MASH, RREF_MASH), 5, 1, buffer);
|
|
return buffer;
|
|
}
|
|
|
|
char* SetpointUnit(){
|
|
if (KettleMode == OFF) {
|
|
return (char*)" ";
|
|
} else if (KettleMode == PWM) {
|
|
return (char*)"%";
|
|
} else {
|
|
return (char*)"F";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cycle kettle mode forward.
|
|
*/
|
|
void kettle_mode_next() {
|
|
if (KettleMode + 1 < OVERFLOW) {
|
|
KettleMode = (kettle_mode)(KettleMode + 1);
|
|
} else {
|
|
KettleMode = 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cycle kettle mode backward.
|
|
*/
|
|
void kettle_mode_previous() {
|
|
if (KettleMode - 1 >= 0) {
|
|
KettleMode = (kettle_mode)(KettleMode - 1);
|
|
} else {
|
|
KettleMode = OVERFLOW - 1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Increase setpoint for line.
|
|
*/
|
|
void increase_setpoint() {
|
|
if (KettleMode == PWM && KettleDuty < 100) {
|
|
KettleDuty++;
|
|
}
|
|
|
|
/* Eventually
|
|
else if (KettleMode == PID && KettleTEMP < 220) {
|
|
KettleTEMP++;
|
|
}
|
|
*/
|
|
}
|
|
|
|
/**
|
|
* Decrease setpoint for line.
|
|
*/
|
|
void decrease_setpoint() {
|
|
if (KettleMode == PWM && KettleDuty > 0) {
|
|
KettleDuty--;
|
|
}
|
|
|
|
/* Eventually
|
|
else if (KettleMode == PID && KettleTEMP > 0) {
|
|
KettleTEMP--;
|
|
}
|
|
*/
|
|
}
|
|
|