Brewery-Controller/boil_kettle/boil_kettle.ino
2022-01-24 11:41:52 -05:00

93 lines
2.0 KiB
C++

//Built-in
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>
// Additoinal Libraries
#include <PubSubClient.h>
#include <cJSON.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C.
#include <MD_REncoder.h>
// My Includes
#include "config.h"
#include "globals.h"
#include "src/datatypes.h"
#include "src/button.h"
#include "src/slowPWM.h"
#include "src/thermoControl.h"
#include "src/mqtt.h"
#include "src/functions.h"
void setup() {
/**
* Any state information that needs to be kept between reboots
* will be stored in EEPROM.
*
* This will allow modifying these through MQTT communication
* later without needing to recompile the code.
*/
ConfigData config;
EEPROM.get(ConfAddress, config);
unsigned long lastRun = millis() - UpdateInterval;
Serial.begin(9600);
Ethernet.begin(config.mac, config.ip);
/**
* Setup pins and interrupts/
*/
attachInterrupt(digitalPinToInterrupt(I_CLK), doEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(I_DT), doEncoder, CHANGE);
rotary.begin();
Enter.begin(I_BTN);
boilPWM.begin(O_PWM, config.period);
/**
* Kettle temp control
*/
KettleController.begin(I_CS1);
KettleController.Hysteresis(config.hysteresis);
KettleController.ThreshPWR(config.threshold);
/**
* No sense messing with MQTT if we're not on the network.
*/
if (Ethernet.linkStatus() == LinkON) {
mqtt_client.setServer(config.mqtt.broker, 1883);
mqtt_client.setClient(net);
mqtt_client.setCallback(MessageReceived);
ConnectMQTT();
}
/**
* Fire up the LCD.
*/
lcd.init();
lcd.backlight();
menu.init();
menu.add_screen(home_screen);
menu.update();
};
void loop() {
UpdateBoilKettle();
unsigned long elapsedTime = (millis() - lastRun);
if (Ethernet.linkStatus() == LinkON && elapsedTime >= UpdateInterval) {
mqtt_client.loop();
//if (!mqtt_client.connected()) ConnectMQTT();
SendSensorData();
lastRun = millis();
}
}