Hi all, any help with this would be much appreciated!
I’m working on a project using the ESP-01s module and would like to publish some sensor readings from the ESP to Home assistant, Using the SubPubClient library on the ESP and the example code provided i have established a connection to the broker which i can confirm from Mosquitto broker logs where the ESP comes up as connected but the values published by ESP are not arriving to Home Assitant, I can see that by subscribing to ‘#’ in Integrations > MQTT broker.
The strangest thing is i was struggling to get it working where at one point it started working and day later stopped working again without any changes made, please see below my ESP code where Im essentially trying to publish the incremented value as a test.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "xx";
const char* password = "xx";
const char* mqtt_server = "xx";
#define temp_topic "outTopic/temp1"
#define in_topic "inTopic/setTemp1"
WiFiClient espClient;
PubSubClient client(espClient);
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int val = 0;
long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 0.5;
float set_temp = 23.0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
// Serial.print("Message arrived [");
// Serial.print(topic);
// Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
// Serial.println();
//Serial.print(set_temp);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(temp_topic, "Connected");
// ... and resubscribe
client.subscribe(in_topic);
} 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() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
client.subscribe(in_topic);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000){
++val;
temp = val;
client.publish(temp_topic, String(temp).c_str(), true);
lastMsg = now;
}
}