Change libraries used for mqtt and json parsing.

Not using strings for data any more.
This commit is contained in:
Chris Giacofei 2022-01-20 08:40:32 -05:00
parent bc93148db6
commit 421a0c6580
2 changed files with 39 additions and 51 deletions

View File

@ -4,8 +4,8 @@
#include <Ethernet.h>
// Additoinal Libraries
#include <ArduinoJson.h>
#include <MQTT.h>
#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>
@ -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");

View File

@ -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;i<length;i++) {
msg[i] = (char)payload[i];
}
}
msg[length] = 0;
if (strcmp(topic, buf) == 0) {
cJSON *monitor_json = cJSON_Parse(msg);
const cJSON *name = NULL;
const cJSON *setting = NULL;
const cJSON *unit = NULL;
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);
ConnectMQTT();
// Update PWM setpoint.
setting = cJSON_GetObjectItemCaseSensitive(monitor_json, "setpoint");
KettleDuty = setting->valueint;
}
}
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);
}