Merge branch 'use-eeprom' into temp-control

This commit is contained in:
Chris Giacofei 2022-01-20 10:57:01 -05:00
commit a1eb11b81a
4 changed files with 112 additions and 53 deletions

View File

@ -2,10 +2,11 @@
#include <Arduino.h> #include <Arduino.h>
#include <SPI.h> #include <SPI.h>
#include <Ethernet.h> #include <Ethernet.h>
#include <EEPROM.h>
// Additional Libraries // Additoinal Libraries
#include <ArduinoJson.h> #include <cJSON.h>
#include <MQTT.h> #include <PubSubClient.h>
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
#include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C. #include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C.
#include <MD_REncoder.h> #include <MD_REncoder.h>
@ -37,8 +38,10 @@ Adafruit_MAX31865 KettleThermo = Adafruit_MAX31865(kettleRTDCS);
slowPWM boilPWM; slowPWM boilPWM;
thermoControl KettleController = thermoControl(&KettleTemp, &KettleSetpoint, &KettleDuty, &ThreshPWR, &Hysteresis); thermoControl KettleController = thermoControl(&KettleTemp, &KettleSetpoint, &KettleDuty, &ThreshPWR, &Hysteresis);
void MessageReceived(char*, byte*, unsigned int);
EthernetClient net; EthernetClient net;
MQTTClient mqtt_client; PubSubClient mqtt_client(MQTT_BROKER, 1883, MessageReceived, net);
unsigned long lastRun = 0; unsigned long lastRun = 0;
@ -114,7 +117,7 @@ void setup() {
// if you get a connection, report back via serial: // if you get a connection, report back via serial:
if (Ethernet.linkStatus() == LinkON) { if (Ethernet.linkStatus() == LinkON) {
SetupMQTT(MQTT_BROKER); ConnectMQTT();
} else { } else {
// if you didn't get a connection to the server: // if you didn't get a connection to the server:
Serial.println("connection failed"); Serial.println("connection failed");

View File

@ -1,72 +1,76 @@
void ConnectMQTT() { void ConnectMQTT() {
static const char *password = MQTT_PASSWORD; ConfigData config;
static const char *user = MQTT_USER; EEPROM.get(ConfAddress, config);
Serial.println("connecting MQTT...");
static const char *user = config.mqtt.user;
static const char *password = config.mqtt.password;
//config.mqtt.topic.root
//config.mqtt.broker
while (!mqtt_client.connect("brewhouse", user, password)) { while (!mqtt_client.connect("brewhouse", user, password)) {
Serial.print("."); Serial.print(".");
delay(1000); delay(1000);
} }
Serial.println("\nconnected!"); char topic[30];
mqtt_client.subscribe("brewery/setpoint/bk"); strcpy(topic,config.mqtt.topic.root);
strcat(topic,config.mqtt.topic.boil.setpoint);
mqtt_client.subscribe(topic);
strcpy(topic,config.mqtt.topic.root);
strcat(topic,config.mqtt.topic.mash.setpoint);
mqtt_client.subscribe(topic);
} }
void MessageReceived(String &topic, String &payload) { void MessageReceived(char* topic, byte* payload, unsigned int length) {
Serial.println("incoming: " + topic + " - " + payload); ConfigData config;
EEPROM.get(ConfAddress, config);
/** JSON Parser Setup */
StaticJsonDocument<200> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, payload);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
char buf[30]; char buf[30];
strcpy(buf,TOPIC_PREFIX); strcpy(buf,config.mqtt.topic.root);
strcat(buf,BOIL_SETPOINT_TOPIC); strcat(buf,config.mqtt.topic.boil.setpoint);
if (topic == buf) {
// Update PWM setpoint.
String name = doc["entity"];
String setting = doc["setpoint"];
KettleDuty = setting.toInt(); char msg[length+1];
String unit = doc["units"];
Serial.println("Updating setpoint for " + name + " to " + setting + " " + unit); for (int i=0;i<length;i++) {
msg[i] = (char)payload[i];
} }
} msg[length] = 0;
if (strcmp(topic, buf) == 0) {
cJSON *monitor_json = cJSON_Parse(msg);
const cJSON *name = NULL;
const cJSON *setting = NULL;
const cJSON *unit = NULL;
void SetupMQTT(const char *broker) { // Update PWM setpoint.
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported setting = cJSON_GetObjectItemCaseSensitive(monitor_json, "setpoint");
// by Arduino. You need to set the IP address directly. KettleDuty = setting->valueint;
Serial.println("Setup MQTT client."); cJSON_Delete(monitor_json);
mqtt_client.begin(broker, net); }
mqtt_client.onMessage(MessageReceived);
ConnectMQTT();
} }
static void SendSensorData() { static void SendSensorData() {
Serial.println("Sending data..."); ConfigData config;
EEPROM.get(ConfAddress, config);
// NOTE: max message length is 250 bytes. char *string = NULL;
StaticJsonDocument<200> doc; cJSON *entity = NULL;
cJSON *setpoint = NULL;
cJSON *units = NULL;
doc["entity"] = "boil_kettle"; cJSON *monitor = cJSON_CreateObject();
doc["setpoint"] = KettleDuty; cJSON_AddStringToObject(monitor, "entity", config.mqtt.topic.boil.name);
doc["units"] = "%"; cJSON_AddNumberToObject(monitor, "setpoint", KettleDuty);
cJSON_AddStringToObject(monitor, "units", "%");
char *msg = cJSON_Print(monitor);
String jstr; char topic[30];
serializeJson(doc, jstr); strcpy(topic,config.mqtt.topic.root);
strcat(topic,config.mqtt.topic.boil.sensor);
String topic = TOPIC_PREFIX;
topic += "sensor/boil_kettle";
mqtt_client.publish(topic, jstr); mqtt_client.publish(topic, msg);
cJSON_Delete(monitor);
} }

1
eeprom_setup/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.h

View File

@ -0,0 +1,51 @@
#include <EEPROM.h>
#include "config.h"
void StoreEEPROM() {
unsigned char b, e = -1, *p = (uint8_t*)CompileTimeP - 1;
while( b = pgm_read_byte( ++p ) ){
if( b != EEPROM[++e] ){
Vessel BoilKettle;
strcpy(BoilKettle.name, "boil_kettle");
strcpy(BoilKettle.setpoint, BOIL_SETPOINT_TOPIC);
strcpy(BoilKettle.sensor, BOIL_ACTUAL_TOPIC);
BoilKettle.Rref = RREF_KETTLE;
BoilKettle.RNominal = RNOMINAL_KETTLE;
// Vessel MashTun {mashname, MASH_SETPOINT_TOPIC, MASH_ACTUAL_TOPIC, RREF_MASH, RNOMINAL_MASH};
Vessel MashTun;
strcpy(MashTun.name, "mash_tun");
strcpy(MashTun.setpoint, MASH_SETPOINT_TOPIC);
strcpy(MashTun.sensor, MASH_ACTUAL_TOPIC);
MashTun.Rref = RREF_MASH;
MashTun.RNominal = RNOMINAL_MASH;
Topic mqtt_topics;
strcpy(mqtt_topics.root,TOPIC_ROOT);
mqtt_topics.mash = MashTun;
mqtt_topics.boil = BoilKettle;
Mqtt mqtt_data;
mqtt_data.broker = MQTT_BROKER;
strcpy(mqtt_data.user, MQTT_USER);
strcpy(mqtt_data.password, MQTT_PASSWORD);
mqtt_data.topic = mqtt_topics;
ConfigData conf {
mqtt_data,
UpdateInterval,
PeriodPWM,
ThreshPWR,
Hysteresis
};
EEPROM.put(ConfAddress, conf);
Serial.println(conf.mqtt.broker);
while( b = pgm_read_byte( p++ ) ) EEPROM[e++] = b;
break;
}
}
}