Just rearranging crap to my liking.

This commit is contained in:
Chris Giacofei 2022-01-13 09:55:17 -05:00
parent 5af0919960
commit e85a8d1704
2 changed files with 48 additions and 51 deletions

View File

@ -16,27 +16,24 @@
#include "config.h"
#include "menu.h"
enum kettle_mode : uint8_t {OFF = 0, PWM = 1, OVERFLOW};
enum encoder_state : uint8_t {
ENC_ST_LINE = 0, // in this state it cycles the focus through the lines
ENC_ST_SETTING = 1, // in this state it "increases or decreases" the value of a setting
ENC_ST_OUT_OF_BOUNDS
};
enum function_types : uint8_t {increase = 1, decrease = 2};
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(encoderDT, encoderCLK);
LiquidCrystal_I2C lcd(0x27,20,4);
Adafruit_MAX31865 kettleRTD(kettleRTDCS);
Adafruit_MAX31865 mashRTD(mashRTDCS);
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;
@ -52,45 +49,49 @@ LiquidMenu menu(lcd);
void setup() {
Serial.begin(9600);
RotaryEncoder.begin();
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);
Serial.println("Setting up...");
// Attach functions menu lines
kettle_mode_line.attach_function(increase, kettle_mode_up);
kettle_mode_line.attach_function(decrease, kettle_mode_down);
kettle_setpoint_line.attach_function(increase, increase_setpoint);
kettle_setpoint_line.attach_function(decrease, decrease_setpoint);
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);
pinMode(encoderCLK, INPUT_PULLUP);
pinMode(encoderDT, INPUT_PULLUP);
pinMode(kettlePWM, OUTPUT);
// if you get a connection, report back via serial:
if (net.connect("www.google.com", 80)) {
Serial.print("you connected to ");
Serial.println(net.remoteIP());
// Make a HTTP request:
net.println("GET /search?q=arduino HTTP/1.1");
net.println("Host: www.google.com");
net.println("Connection: close");
net.println();
SetupMQTT("192.168.1.198");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// 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();
@ -109,9 +110,10 @@ void loop() {
static unsigned long lastRun = millis() - UpdateInterval;
unsigned long elapsedTime = (millis() - lastRun);
if (elapsedTime >= UpdateInterval) {
//
if (NetworkOn && elapsedTime >= UpdateInterval) {
mqtt_client.loop();
//if (!mqtt_client.connected()) ConnectMQTT();
if (!mqtt_client.connected()) ConnectMQTT();
SendSensorData();
lastRun = millis();
@ -154,7 +156,7 @@ void ButtonCheck(){
if (!EncoderButton.pressed()) { return; }
if (EncoderState + 1 < ENC_ST_OUT_OF_BOUNDS) { // check if the next state is valid
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
@ -163,12 +165,11 @@ void ButtonCheck(){
}
/**
* Return a character array to represent the
* On/Off state of the kettle.
* Return a character array to represent the state of the kettle.
*/
char* KettleState() {
if (KettleMode == OFF) {
return (char*)"Off";
return (char*)"OFF";
} else if (KettleMode == PWM) {
return (char*)"PWM";
} else {
@ -223,7 +224,7 @@ char* SetpointUnit(){
/**
* Cycle kettle mode forward.
*/
void kettle_mode_up() {
void kettle_mode_next() {
if (KettleMode + 1 < OVERFLOW) {
KettleMode = (kettle_mode)(KettleMode + 1);
} else {
@ -234,7 +235,7 @@ void kettle_mode_up() {
/**
* Cycle kettle mode backward.
*/
void kettle_mode_down() {
void kettle_mode_previous() {
if (KettleMode - 1 >= 0) {
KettleMode = (kettle_mode)(KettleMode - 1);
} else {

View File

@ -6,14 +6,10 @@ char * Concatenate(char *first, char *second) {
}
void ConnectMQTT() {
Serial.println("connecting MQTT...");
while (!mqtt_client.connect("brewhouse", MQTT_USER, MQTT_PASSWORD)) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
mqtt_client.subscribe(Concatenate(TOPIC_PREFIX, BK_SETPOINT_TOPIC));
}