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> #include <Ethernet.h>
// Additoinal Libraries // Additoinal Libraries
#include <ArduinoJson.h> #include <cJSON.h>
#include <MQTT.h> #include <PubSubClient.h>
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
#include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C. #include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C.
#include <MD_REncoder.h> #include <MD_REncoder.h>
@ -26,8 +26,10 @@ slowPWM boilPWM;
MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK); MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK);
LiquidCrystal_I2C lcd(0x27,20,4); LiquidCrystal_I2C lcd(0x27,20,4);
void MessageReceived(char*, byte*, unsigned int);
EthernetClient net; EthernetClient net;
MQTTClient mqtt_client; PubSubClient mqtt_client(MQTT_BROKER, 1883, MessageReceived, net);
unsigned long lastRun = 0; unsigned long lastRun = 0;
@ -87,7 +89,7 @@ void setup() {
// if you get a connection, report back via serial: // if you get a connection, report back via serial:
if (Ethernet.linkStatus() == LinkON) { if (Ethernet.linkStatus() == LinkON) {
SetupMQTT(MQTT_BROKER); ConnectMQTT();
} else { } else {
// if you didn't get a connection to the server: // if you didn't get a connection to the server:
Serial.println("connection failed"); Serial.println("connection failed");

View File

@ -1,72 +1,58 @@
void ConnectMQTT() { void ConnectMQTT() {
static const char *password = MQTT_PASSWORD; static const char *password = MQTT_PASSWORD;
static const char *user = MQTT_USER; static const char *user = MQTT_USER;
Serial.println("connecting MQTT...");
while (!mqtt_client.connect("brewhouse", user, password)) { while (!mqtt_client.connect("brewhouse", user, password)) {
Serial.print("."); Serial.print(".");
delay(1000); delay(1000);
} }
Serial.println("\nconnected!"); char topic[30];
mqtt_client.subscribe("brewery/setpoint/bk"); strcpy(topic,TOPIC_ROOT);
strcat(topic,BOIL_SETPOINT_TOPIC);
mqtt_client.subscribe(topic);
} }
void MessageReceived(String &topic, String &payload) { void MessageReceived(char* topic, byte* payload, unsigned int length) {
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;
}
char buf[30]; char buf[30];
strcpy(buf,TOPIC_PREFIX); strcpy(buf,TOPIC_ROOT);
strcat(buf,BOIL_SETPOINT_TOPIC); strcat(buf,BOIL_SETPOINT_TOPIC);
if (topic == buf) {
// Update PWM setpoint.
String name = doc["entity"];
String setting = doc["setpoint"];
KettleDuty = setting.toInt(); char msg[length+1];
String unit = doc["units"];
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) { // Update PWM setpoint.
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported setting = cJSON_GetObjectItemCaseSensitive(monitor_json, "setpoint");
// by Arduino. You need to set the IP address directly. KettleDuty = setting->valueint;
Serial.println("Setup MQTT client."); }
mqtt_client.begin(broker, net);
mqtt_client.onMessage(MessageReceived);
ConnectMQTT();
} }
static void SendSensorData() { static void SendSensorData() {
Serial.println("Sending data...");
// NOTE: max message length is 250 bytes. char *string = NULL;
StaticJsonDocument<200> doc; cJSON *entity = NULL;
cJSON *setpoint = NULL;
cJSON *units = NULL;
doc["entity"] = "boil_kettle"; cJSON *monitor = cJSON_CreateObject();
doc["setpoint"] = KettleDuty; cJSON_AddStringToObject(monitor, "entity", "boil_kettle");
doc["units"] = "%"; cJSON_AddNumberToObject(monitor, "setpoint", KettleDuty);
cJSON_AddStringToObject(monitor, "units", "%");
char *msg = cJSON_Print(monitor);
String jstr; char topic[30];
serializeJson(doc, jstr); strcpy(topic,TOPIC_ROOT);
strcat(topic,BOIL_ACTUAL_TOPIC);
String topic = TOPIC_PREFIX; mqtt_client.publish(topic, msg);
topic += "sensor/boil_kettle";
mqtt_client.publish(topic, jstr);
} }