Merge remote-tracking branch 'origin/use-eeprom' into no-more-strings

This commit is contained in:
Chris Giacofei 2022-01-20 09:33:19 -05:00
commit fb0afa7e43
3 changed files with 111 additions and 9 deletions

View File

@ -2,6 +2,7 @@
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>
// Additoinal Libraries
#include <cJSON.h>
@ -13,6 +14,7 @@
// My Includes
#include "config.h"
#include "eeprom_init.h"
#include "button.h"
#include "slowPWM.h"
@ -72,6 +74,7 @@ LiquidScreen home_screen(KettleState_line, kettle_power_line);
LiquidMenu menu(lcd);
void setup() {
StoreEEPROM();
unsigned long lastRun = millis() - UpdateInterval;
Serial.begin(9600);

83
boil_kettle/eeprom_init.h Normal file
View File

@ -0,0 +1,83 @@
#include <EEPROM.h>
const uint8_t CompileTimeP[] PROGMEM = __DATE__ " " __TIME__;
const size_t ConfAddress = sizeof(CompileTimeP);
unsigned char b, e = -1, *p = (uint8_t*)CompileTimeP - 1;
struct Vessel {
char name[16];
char setpoint[20];
char sensor[20];
double Rref;
double RNominal;
};
struct Topic {
char root[10];
Vessel mash;
Vessel boil;
};
struct Mqtt {
IPAddress broker;
char user[10];
char password[21];
Topic topic;
};
struct ConfigData {
Mqtt mqtt;
int interval;
int period;
uint8_t threshold;
uint8_t hysteresis;
};
void StoreEEPROM() {
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;
}
}
}

View File

@ -1,21 +1,34 @@
void ConnectMQTT() {
static const char *password = MQTT_PASSWORD;
static const char *user = MQTT_USER;
ConfigData config;
EEPROM.get(ConfAddress, config);
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)) {
Serial.print(".");
delay(1000);
}
char topic[30];
strcpy(topic,TOPIC_ROOT);
strcat(topic,BOIL_SETPOINT_TOPIC);
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(char* topic, byte* payload, unsigned int length) {
ConfigData config;
EEPROM.get(ConfAddress, config);
char buf[30];
strcpy(buf,TOPIC_ROOT);
strcat(buf,BOIL_SETPOINT_TOPIC);
strcpy(buf,config.mqtt.topic.root);
strcat(buf,config.mqtt.topic.boil.setpoint);
char msg[length+1];
@ -37,6 +50,8 @@ void MessageReceived(char* topic, byte* payload, unsigned int length) {
}
static void SendSensorData() {
ConfigData config;
EEPROM.get(ConfAddress, config);
char *string = NULL;
cJSON *entity = NULL;
@ -44,14 +59,15 @@ static void SendSensorData() {
cJSON *units = NULL;
cJSON *monitor = cJSON_CreateObject();
cJSON_AddStringToObject(monitor, "entity", "boil_kettle");
cJSON_AddStringToObject(monitor, "entity", config.mqtt.topic.boil.name);
cJSON_AddNumberToObject(monitor, "setpoint", KettleDuty);
cJSON_AddStringToObject(monitor, "units", "%");
char *msg = cJSON_Print(monitor);
char topic[30];
strcpy(topic,TOPIC_ROOT);
strcat(topic,BOIL_ACTUAL_TOPIC);
strcpy(topic,config.mqtt.topic.root);
strcat(topic,config.mqtt.topic.boil.sensor);
mqtt_client.publish(topic, msg);