Brewery-Controller/boil_kettle/boil_kettle.ino

148 lines
3.1 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 <Button.h>
#include <MD_REncoder.h>
// Secret stuff
#include "config.h"
// Pin definitions
#define encoderCLK 2
#define encoderDT 3
#define encoderBTN 4
#define kettlePWM 5
#define TOPIC_PREFIX "brewery/"
#define BKTOPIC "setpoint/bk"
// Global variables.
byte KettleDuty = 0;
bool KettleOn = false;
// User I/O objects.
Button Enter(encoderBTN);
MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK);
LiquidCrystal_I2C lcd(0x27,20,4);
float output = 0;
int updateInterval = 1000;
EthernetClient net;
MQTTClient mqtt_client;
// Return a character array to represent the
// On/Off state of the kettle.
char* KettleState() {
if (KettleOn) {
return (char*)"On";
} else {
return (char*)"Off";
}
}
// Interrupt function to run when encoder is turned.
//
// Increases/decreases the kettle output to a max
// of 100% and minimum of 0%.
void doEncoder()
{
uint8_t result = rotary.read();
if (result == DIR_CW && KettleDuty < 100) {
KettleDuty++;
} else if (result == DIR_CCW && KettleDuty > 0) {
KettleDuty--;
}
}
void UpdateBoilKettle(){
static byte last_kettle_duty = 0;
if (Enter.pressed()) {
KettleOn = !KettleOn;
menu.update();
}
if (last_kettle_duty != KettleDuty) {
last_kettle_duty = KettleDuty;
menu.update();
}
if (KettleOn) {
slowPWM(kettlePWM, KettleDuty, 1000);
} else {
slowPWM(kettlePWM, 0, 1000);
}
}
// LCD menu setup.
LiquidLine KettleState_line(0, 0, "Boil Kettle ", KettleState);
LiquidLine kettle_power_line(0, 1, "Kettle Power % ", KettleDuty);
LiquidScreen home_screen(KettleState_line, kettle_power_line);
LiquidMenu menu(lcd);
void setup() {
Serial.begin(9600);
rotary.begin();
Ethernet.begin(mac, ip);
Serial.println("Setting up...");
attachInterrupt(digitalPinToInterrupt(encoderCLK), doEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderDT), doEncoder, CHANGE);
pinMode(encoderCLK, INPUT_PULLUP);
pinMode(encoderDT, INPUT_PULLUP);
pinMode(encoderBTN, INPUT_PULLUP);
// 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");
}
lcd.init();
lcd.backlight();
menu.init();
menu.add_screen(home_screen);
menu.update();
};
void loop() {
UpdateBoilKettle();
static unsigned long lastRun = millis() - updateInterval;
unsigned long elapsedTime = (millis() - lastRun);
if (elapsedTime >= updateInterval) {
mqtt_client.loop();
//if (!mqtt_client.connected()) ConnectMQTT();
SendSensorData();
lastRun = millis();
}
}