diff --git a/boil_kettle/boil_kettle.ino b/boil_kettle/boil_kettle.ino index 7a16a55..d0e9bfd 100644 --- a/boil_kettle/boil_kettle.ino +++ b/boil_kettle/boil_kettle.ino @@ -2,10 +2,11 @@ #include #include #include +#include -// Additional Libraries -#include -#include +// Additoinal Libraries +#include +#include #include #include // LiquidMenu_config.h needs to be modified to use I2C. #include @@ -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"); diff --git a/boil_kettle/mqtt.ino b/boil_kettle/mqtt.ino index f3aeafd..66e94ba 100644 --- a/boil_kettle/mqtt.ino +++ b/boil_kettle/mqtt.ino @@ -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;ivalueint; + 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); } diff --git a/eeprom_setup/.gitignore b/eeprom_setup/.gitignore new file mode 100644 index 0000000..299bb98 --- /dev/null +++ b/eeprom_setup/.gitignore @@ -0,0 +1 @@ +config.h \ No newline at end of file diff --git a/eeprom_setup/eeprom_init.h b/eeprom_setup/eeprom_init.h new file mode 100644 index 0000000..a699228 --- /dev/null +++ b/eeprom_setup/eeprom_init.h @@ -0,0 +1,51 @@ +#include +#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; + } + } +}