Don't start mqtt unless network is connected.

Also comments.
This commit is contained in:
Chris Giacofei 2022-01-24 11:41:52 -05:00
parent 8565c87ce4
commit 216e80e642

View File

@ -22,35 +22,51 @@
#include "src/functions.h" #include "src/functions.h"
void setup() { 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; ConfigData config;
EEPROM.get(ConfAddress, config); EEPROM.get(ConfAddress, config);
unsigned long lastRun = millis() - UpdateInterval; unsigned long lastRun = millis() - UpdateInterval;
Serial.begin(9600); Serial.begin(9600);
rotary.begin();
Ethernet.begin(config.mac, config.ip); Ethernet.begin(config.mac, config.ip);
/**
* Setup pins and interrupts/
*/
attachInterrupt(digitalPinToInterrupt(I_CLK), doEncoder, CHANGE); attachInterrupt(digitalPinToInterrupt(I_CLK), doEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(I_DT), doEncoder, CHANGE); attachInterrupt(digitalPinToInterrupt(I_DT), doEncoder, CHANGE);
pinMode(I_CLK, INPUT_PULLUP); rotary.begin();
pinMode(I_DT, INPUT_PULLUP);
Enter.begin(I_BTN); Enter.begin(I_BTN);
boilPWM.begin(O_PWM, config.period); boilPWM.begin(O_PWM, config.period);
mqtt_client.setServer(config.mqtt.broker, 1883); /**
mqtt_client.setClient(net); * Kettle temp control
mqtt_client.setCallback(MessageReceived); */
KettleController.begin(I_CS1); KettleController.begin(I_CS1);
KettleController.Hysteresis(config.hysteresis); KettleController.Hysteresis(config.hysteresis);
KettleController.ThreshPWR(config.threshold); KettleController.ThreshPWR(config.threshold);
// if you get a connection, report back via serial: /**
* No sense messing with MQTT if we're not on the network.
*/
if (Ethernet.linkStatus() == LinkON) { if (Ethernet.linkStatus() == LinkON) {
mqtt_client.setServer(config.mqtt.broker, 1883);
mqtt_client.setClient(net);
mqtt_client.setCallback(MessageReceived);
ConnectMQTT(); ConnectMQTT();
} }
/**
* Fire up the LCD.
*/
lcd.init(); lcd.init();
lcd.backlight(); lcd.backlight();