diff --git a/boil_kettle/boil_kettle.ino b/boil_kettle/boil_kettle.ino index 269a964..e50261a 100644 --- a/boil_kettle/boil_kettle.ino +++ b/boil_kettle/boil_kettle.ino @@ -4,8 +4,8 @@ #include // Additoinal Libraries -#include -#include +#include +#include #include #include // LiquidMenu_config.h needs to be modified to use I2C. #include @@ -26,8 +26,10 @@ slowPWM boilPWM; MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK); LiquidCrystal_I2C lcd(0x27,20,4); +void MessageReceived(char*, byte*, unsigned int); + EthernetClient net; -MQTTClient mqtt_client; +PubSubClient mqtt_client(MQTT_BROKER, 1883, MessageReceived, net); unsigned long lastRun = 0; @@ -87,7 +89,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..1f195a0 100644 --- a/boil_kettle/mqtt.ino +++ b/boil_kettle/mqtt.ino @@ -1,72 +1,58 @@ void ConnectMQTT() { static const char *password = MQTT_PASSWORD; static const char *user = MQTT_USER; - Serial.println("connecting MQTT..."); 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,TOPIC_ROOT); + strcat(topic,BOIL_SETPOINT_TOPIC); + mqtt_client.subscribe(topic); } -void MessageReceived(String &topic, String &payload) { - Serial.println("incoming: " + topic + " - " + payload); - - /** 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; - } +void MessageReceived(char* topic, byte* payload, unsigned int length) { char buf[30]; - strcpy(buf,TOPIC_PREFIX); + strcpy(buf,TOPIC_ROOT); strcat(buf,BOIL_SETPOINT_TOPIC); - if (topic == buf) { - // Update PWM setpoint. - String name = doc["entity"]; - String setting = doc["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; + } } static void SendSensorData() { - Serial.println("Sending data..."); - // 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", "boil_kettle"); + cJSON_AddNumberToObject(monitor, "setpoint", KettleDuty); + cJSON_AddStringToObject(monitor, "units", "%"); + char *msg = cJSON_Print(monitor); - String jstr; - serializeJson(doc, jstr); + char topic[30]; + strcpy(topic,TOPIC_ROOT); + strcat(topic,BOIL_ACTUAL_TOPIC); - String topic = TOPIC_PREFIX; - topic += "sensor/boil_kettle"; - - mqtt_client.publish(topic, jstr); + mqtt_client.publish(topic, msg); }