Merge branch 'use-eeprom' into temp-control
This commit is contained in:
commit
a1eb11b81a
@ -2,10 +2,11 @@
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
// Additional Libraries
|
||||
#include <ArduinoJson.h>
|
||||
#include <MQTT.h>
|
||||
// Additoinal Libraries
|
||||
#include <cJSON.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <LiquidCrystal_I2C.h>
|
||||
#include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C.
|
||||
#include <MD_REncoder.h>
|
||||
@ -37,8 +38,10 @@ Adafruit_MAX31865 KettleThermo = Adafruit_MAX31865(kettleRTDCS);
|
||||
slowPWM boilPWM;
|
||||
thermoControl KettleController = thermoControl(&KettleTemp, &KettleSetpoint, &KettleDuty, &ThreshPWR, &Hysteresis);
|
||||
|
||||
void MessageReceived(char*, byte*, unsigned int);
|
||||
|
||||
EthernetClient net;
|
||||
MQTTClient mqtt_client;
|
||||
PubSubClient mqtt_client(MQTT_BROKER, 1883, MessageReceived, net);
|
||||
|
||||
unsigned long lastRun = 0;
|
||||
|
||||
@ -114,7 +117,7 @@ void setup() {
|
||||
|
||||
// if you get a connection, report back via serial:
|
||||
if (Ethernet.linkStatus() == LinkON) {
|
||||
SetupMQTT(MQTT_BROKER);
|
||||
ConnectMQTT();
|
||||
} else {
|
||||
// if you didn't get a connection to the server:
|
||||
Serial.println("connection failed");
|
||||
|
@ -1,72 +1,76 @@
|
||||
void ConnectMQTT() {
|
||||
static const char *password = MQTT_PASSWORD;
|
||||
static const char *user = MQTT_USER;
|
||||
Serial.println("connecting MQTT...");
|
||||
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);
|
||||
}
|
||||
|
||||
Serial.println("\nconnected!");
|
||||
mqtt_client.subscribe("brewery/setpoint/bk");
|
||||
char topic[30];
|
||||
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) {
|
||||
Serial.println("incoming: " + topic + " - " + payload);
|
||||
void MessageReceived(char* topic, byte* payload, unsigned int length) {
|
||||
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];
|
||||
strcpy(buf,TOPIC_PREFIX);
|
||||
strcat(buf,BOIL_SETPOINT_TOPIC);
|
||||
if (topic == buf) {
|
||||
// Update PWM setpoint.
|
||||
String name = doc["entity"];
|
||||
String setting = doc["setpoint"];
|
||||
strcpy(buf,config.mqtt.topic.root);
|
||||
strcat(buf,config.mqtt.topic.boil.setpoint);
|
||||
|
||||
KettleDuty = setting.toInt();
|
||||
String unit = doc["units"];
|
||||
char msg[length+1];
|
||||
|
||||
Serial.println("Updating setpoint for " + name + " to " + setting + " " + unit);
|
||||
for (int i=0;i<length;i++) {
|
||||
msg[i] = (char)payload[i];
|
||||
}
|
||||
}
|
||||
msg[length] = 0;
|
||||
|
||||
void SetupMQTT(const char *broker) {
|
||||
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
|
||||
// by Arduino. You need to set the IP address directly.
|
||||
Serial.println("Setup MQTT client.");
|
||||
mqtt_client.begin(broker, net);
|
||||
mqtt_client.onMessage(MessageReceived);
|
||||
if (strcmp(topic, buf) == 0) {
|
||||
cJSON *monitor_json = cJSON_Parse(msg);
|
||||
const cJSON *name = NULL;
|
||||
const cJSON *setting = NULL;
|
||||
const cJSON *unit = NULL;
|
||||
|
||||
ConnectMQTT();
|
||||
// Update PWM setpoint.
|
||||
setting = cJSON_GetObjectItemCaseSensitive(monitor_json, "setpoint");
|
||||
KettleDuty = setting->valueint;
|
||||
cJSON_Delete(monitor_json);
|
||||
}
|
||||
}
|
||||
|
||||
static void SendSensorData() {
|
||||
Serial.println("Sending data...");
|
||||
ConfigData config;
|
||||
EEPROM.get(ConfAddress, config);
|
||||
|
||||
// NOTE: max message length is 250 bytes.
|
||||
StaticJsonDocument<200> doc;
|
||||
char *string = NULL;
|
||||
cJSON *entity = NULL;
|
||||
cJSON *setpoint = NULL;
|
||||
cJSON *units = NULL;
|
||||
|
||||
doc["entity"] = "boil_kettle";
|
||||
doc["setpoint"] = KettleDuty;
|
||||
doc["units"] = "%";
|
||||
cJSON *monitor = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(monitor, "entity", config.mqtt.topic.boil.name);
|
||||
cJSON_AddNumberToObject(monitor, "setpoint", KettleDuty);
|
||||
cJSON_AddStringToObject(monitor, "units", "%");
|
||||
char *msg = cJSON_Print(monitor);
|
||||
|
||||
String jstr;
|
||||
serializeJson(doc, jstr);
|
||||
char topic[30];
|
||||
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
1
eeprom_setup/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
config.h
|
51
eeprom_setup/eeprom_init.h
Normal file
51
eeprom_setup/eeprom_init.h
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user