So after some researches online I've found what seemes a intresting way to pass data to Home Assistant using Json objects via MQTT so I put all together and after some painful debugging the software compiles but there's no data coming into my broker. The Json part was in ArduinoJson 5 so I think I've migrated it to 6 correctly.
Now this is the ESP8266 code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <SerialTransfer.h>
// Update these with values suitable for your network.
const char* ssid = "MYSSID";
const char* password = "MYPASSWORD";
const char* mqtt_server = "192.168.5.213";
#define mqtt_user "MQTTUSR" //enter your MQTT username
#define mqtt_password "MQTTPW" //enter your password
WiFiClient espClient;
PubSubClient client(espClient);
//Setup Serial Communication
SerialTransfer myTransfer;
//topic for the temperature
char* tempTopic = "ha/outside_temp";
//topic for the humidity
char* humTopic = "ha/outside_hum";
//topic for the ppm
char* ppmTopic = "ha/outside_ppm";
//Setup variables
String strTopic;
String strPayload;
String tempBuffer[11];
float humVal, tempVal, ppmVal;
//publishData function gets all the variables needed to be sent on MQTT
void publishData(float p_temperature){
//create JSON object
StaticJsonDocument<200> jsonBuffer;
JsonObject root = jsonBuffer.as<JsonObject>();
root["temperature"] = (String)p_temperature;
//root.prettyPrintTo(Serial);
serializeJsonPretty(root, Serial);
Serial.println("");
char data[200];
serializeJson(root, data);
//root.printTo(data, rootmeasureLenght()+1);
client.publish(tempTopic, data, true);
}
void setup_wifi() {
Serial.begin(115200);
delay(100);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.begin(115200);
Serial1.begin(115200); //Com to PC
myTransfer.begin(Serial1);
delay(150);
}
void loop(){
//pooling data from serial
if(myTransfer.available())
{
uint8_t recSize = 0;
myTransfer.rxObj(humVal, sizeof(humVal), recSize);
recSize += sizeof(humVal);
myTransfer.rxObj(tempVal, sizeof(tempVal), recSize);
recSize += sizeof(tempVal);
myTransfer.rxObj(ppmVal, sizeof(ppmVal), recSize);
recSize += sizeof(ppmVal);
//call to publish data
publishData(tempVal);
}
else if(myTransfer.status < 0)
{
Serial.print("ERROR: ");
if(myTransfer.status == -1)
Serial.println(F("CRC_ERROR"));
else if(myTransfer.status == -2)
Serial.println(F("PAYLOAD_ERROR"));
else if(myTransfer.status == -3)
Serial.println(F("STOP_BYTE_ERROR"));
}
if (!client.connected()) {
reconnect();
}
client.loop();
}
This is the sender UNO hooked to DHT11 and MQ135:
#include <SerialTransfer.h>
#include <SoftwareSerial.h>
#include <dht.h>
#include <MQ135.h>
const uint8_t DHT11_PIN = 4;
const uint8_t ANALOGPIN = A0;
dht DHT;
MQ135 gasSensor = MQ135(ANALOGPIN);
SerialTransfer myTransfer;
SoftwareSerial mySerial (2,3); //RX,TX
float humVal, tempVal;
void setup()
{
Serial.begin(115200);
mySerial.begin(115200);
myTransfer.begin(mySerial);
float rzero = gasSensor.getRZero();
Serial.print("MQ135 RZERO Calibration Value : ");
Serial.println(rzero);
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
float ppm = gasSensor.getPPM();
humVal = DHT.humidity;
tempVal = DHT.temperature;
Serial.print(F("Humidity: ")); Serial.println(humVal);
Serial.print(F("Temperature: ")); Serial.println(tempVal);
Serial.print(F("PPM: ")); Serial.println(ppm);
Serial.println();
uint8_t sendSize = 0;
myTransfer.txObj(humVal, sizeof(humVal), sendSize);
sendSize += sizeof(humVal);
myTransfer.txObj(tempVal, sizeof(tempVal), sendSize);
sendSize += sizeof(tempVal);
myTransfer.txObj(ppm, sizeof(ppm), sendSize);
sendSize += sizeof(ppm);
myTransfer.sendData(sendSize);
delay(5000);
}
And I believe it's usuful to know how I am handling the topics into Home Assistant
#External Weather Unit
sensor 5:
- platform: mqtt
state_topic: "ha/outside_temp"
unit_of_measurement: "°C"
value_template: '{{ value_json.temperature }}'
name: "Temp2 Outside"
- platform: mqtt
state_topic: "ha/humoutside"
unit_of_measurement: "%"
name: "Hum2 Outside"
- platform: mqtt
state_topic: "ha/ppmoutside"
unit_of_measurement: "ppm"
name: "PPM Outside"
In Home Assistant I've modified just the temperature value because I wanted to test with just one simple value otherwise debugging 3 different things could've become difficult.
I look forward to hear your thoughts!