Load config to EEPROM
This commit is contained in:
parent
bc93148db6
commit
8750bceead
@ -2,6 +2,7 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
|
#include <EEPROM.h>
|
||||||
|
|
||||||
// Additoinal Libraries
|
// Additoinal Libraries
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
@ -13,6 +14,7 @@
|
|||||||
|
|
||||||
// My Includes
|
// My Includes
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "eeprom_init.h"
|
||||||
#include "button.h"
|
#include "button.h"
|
||||||
#include "slowPWM.h"
|
#include "slowPWM.h"
|
||||||
|
|
||||||
@ -70,6 +72,7 @@ LiquidScreen home_screen(KettleState_line, kettle_power_line);
|
|||||||
LiquidMenu menu(lcd);
|
LiquidMenu menu(lcd);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
StoreEEPROM();
|
||||||
|
|
||||||
unsigned long lastRun = millis() - UpdateInterval;
|
unsigned long lastRun = millis() - UpdateInterval;
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
66
boil_kettle/eeprom_init.h
Normal file
66
boil_kettle/eeprom_init.h
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#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 {
|
||||||
|
String name;
|
||||||
|
String setpoint;
|
||||||
|
String sensor;
|
||||||
|
double Rref;
|
||||||
|
double RNominal;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Topic {
|
||||||
|
String root;
|
||||||
|
Vessel mash;
|
||||||
|
Vessel boil;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Mqtt {
|
||||||
|
char broker[14];
|
||||||
|
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 {"boil_kettle", BOIL_SETPOINT_TOPIC, BOIL_ACTUAL_TOPIC, RREF_KETTLE, RNOMINAL_KETTLE};
|
||||||
|
Vessel MashTun {"mash_tun", MASH_SETPOINT_TOPIC, MASH_ACTUAL_TOPIC, RREF_MASH, RNOMINAL_MASH};
|
||||||
|
Topic mqtt_topics {TOPIC_ROOT, MashTun, BoilKettle};
|
||||||
|
Mqtt mqtt_data;
|
||||||
|
strcpy(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,12 @@
|
|||||||
void ConnectMQTT() {
|
void ConnectMQTT() {
|
||||||
static const char *password = MQTT_PASSWORD;
|
ConfigData config;
|
||||||
static const char *user = MQTT_USER;
|
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
|
||||||
|
|
||||||
Serial.println("connecting MQTT...");
|
Serial.println("connecting MQTT...");
|
||||||
while (!mqtt_client.connect("brewhouse", user, password)) {
|
while (!mqtt_client.connect("brewhouse", user, password)) {
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
@ -8,11 +14,14 @@ void ConnectMQTT() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("\nconnected!");
|
Serial.println("\nconnected!");
|
||||||
mqtt_client.subscribe("brewery/setpoint/bk");
|
|
||||||
|
mqtt_client.subscribe(config.mqtt.topic.root + config.mqtt.topic.boil.setpoint);
|
||||||
|
mqtt_client.subscribe(config.mqtt.topic.root + config.mqtt.topic.mash.setpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MessageReceived(String &topic, String &payload) {
|
void MessageReceived(String &topic, String &payload) {
|
||||||
Serial.println("incoming: " + topic + " - " + payload);
|
ConfigData config;
|
||||||
|
EEPROM.get(ConfAddress, config);
|
||||||
|
|
||||||
/** JSON Parser Setup */
|
/** JSON Parser Setup */
|
||||||
StaticJsonDocument<200> doc;
|
StaticJsonDocument<200> doc;
|
||||||
@ -22,29 +31,20 @@ void MessageReceived(String &topic, String &payload) {
|
|||||||
|
|
||||||
// Test if parsing succeeds.
|
// Test if parsing succeeds.
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.print(F("deserializeJson() failed: "));
|
|
||||||
Serial.println(error.f_str());
|
Serial.println(error.f_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
char buf[30];
|
|
||||||
strcpy(buf,TOPIC_PREFIX);
|
if (topic == config.mqtt.topic.root + config.mqtt.topic.boil.setpoint) {
|
||||||
strcat(buf,BOIL_SETPOINT_TOPIC);
|
|
||||||
if (topic == buf) {
|
|
||||||
// Update PWM setpoint.
|
// Update PWM setpoint.
|
||||||
String name = doc["entity"];
|
|
||||||
String setting = doc["setpoint"];
|
String setting = doc["setpoint"];
|
||||||
|
|
||||||
KettleDuty = setting.toInt();
|
KettleDuty = setting.toInt();
|
||||||
String unit = doc["units"];
|
|
||||||
|
|
||||||
Serial.println("Updating setpoint for " + name + " to " + setting + " " + unit);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetupMQTT(const char *broker) {
|
void SetupMQTT(const char *broker) {
|
||||||
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
|
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
|
||||||
// by Arduino. You need to set the IP address directly.
|
// by Arduino. You need to set the IP address directly.
|
||||||
Serial.println("Setup MQTT client.");
|
|
||||||
mqtt_client.begin(broker, net);
|
mqtt_client.begin(broker, net);
|
||||||
mqtt_client.onMessage(MessageReceived);
|
mqtt_client.onMessage(MessageReceived);
|
||||||
|
|
||||||
@ -52,20 +52,20 @@ void SetupMQTT(const char *broker) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void SendSensorData() {
|
static void SendSensorData() {
|
||||||
Serial.println("Sending data...");
|
ConfigData config;
|
||||||
|
EEPROM.get(ConfAddress, config);
|
||||||
// NOTE: max message length is 250 bytes.
|
// NOTE: max message length is 250 bytes.
|
||||||
StaticJsonDocument<200> doc;
|
StaticJsonDocument<200> doc;
|
||||||
|
|
||||||
doc["entity"] = "boil_kettle";
|
doc["entity"] = config.mqtt.topic.boil.name;
|
||||||
doc["setpoint"] = KettleDuty;
|
doc["setpoint"] = KettleDuty;
|
||||||
doc["units"] = "%";
|
doc["units"] = "%";
|
||||||
|
|
||||||
String jstr;
|
String jstr;
|
||||||
serializeJson(doc, jstr);
|
serializeJson(doc, jstr);
|
||||||
|
|
||||||
String topic = TOPIC_PREFIX;
|
String topic = config.mqtt.topic.root;
|
||||||
topic += "sensor/boil_kettle";
|
topic += config.mqtt.topic.boil.sensor;
|
||||||
|
|
||||||
mqtt_client.publish(topic, jstr);
|
mqtt_client.publish(topic, jstr);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user