Hi !
I am srtuggeling with the same issue. I am not sure, where to insert the client.loop.
Can you please help?
Here is my code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define LISTEN_PORT 80
//Pins
int relay_pin1 = 12;
int temp_pin1 = A0;
// WIFI
const char* ssid = “SSID”;
const char* password = “WIFI password”;
//MQTT
const char* mqtt_server = “servers IP”;
const int mqttPort = 1883;
const char* mqttUser = “user”;
const char* mqttPassword = “password”;
WiFiClient espClient;
PubSubClient client(espClient);
WiFiServer server(LISTEN_PORT);
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++) {
char receivedChar = (char)payload[i];
Serial.print(receivedChar);
}
Serial.println();
}
//RECONNECT
void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Attempt to connect
if (client.connect(“AKAC_ESP8266_1”, mqttUser, mqttPassword, “AKAC/ESP8266_1/Status”, 2, 1,“OFFLINE” )) {
Serial.println(“connected”);
// … and subscribe to topic
client.subscribe(“X”);
} else {
//RC descriptions
//-4 : MQTT_CONNECTION_TIMEOUT - the server didn’t respond within the keepalive time
//-3 : MQTT_CONNECTION_LOST - the network connection was broken
//-2 : MQTT_CONNECT_FAILED - the network connection failed
//-1 : MQTT_DISCONNECTED - the client is disconnected cleanly
//0 : MQTT_CONNECTED - the client is connected
//1 : MQTT_CONNECT_BAD_PROTOCOL - the server doesn’t support the requested version of MQTT
//2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier
//3 : MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection
//4 : MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected
//5 : MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect
Serial.print(“failed, rc=”);
Serial.print(client.state());
if (client.state() == -4){Serial.print (“MQTT_CONNECTION_TIMEOUT - the server didn’t respond within the keepalive time”);}
if (client.state() == -3){Serial.print (“MQTT_CONNECTION_LOST - the network connection was broken”);}
if (client.state() == -2){Serial.print (“MQTT_CONNECT_FAILED - the network connection failed”);}
if (client.state() == -1){Serial.print (“MQTT_DISCONNECTED - the client is disconnected cleanly”);}
if (client.state() == 0){Serial.print (“MQTT_CONNECTED - the client is connected”);}
if (client.state() == 1){Serial.print (“MQTT_CONNECT_BAD_PROTOCOL - the server doesn’t support the requested version of MQTT”);}
if (client.state() == 2){Serial.print (“MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier”);}
if (client.state() == 3){Serial.print (“MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection”);}
if (client.state() == 4){Serial.print (“MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected”);}
if (client.state() == 4){Serial.print (“MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect”);}
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//SETUP
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
client.setServer(mqtt_server, mqttPort);
client.setCallback(callback);
//PINMODES
pinMode(relay_pin1, OUTPUT);
pinMode(temp_pin1, INPUT);
}
//LOOP
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
reconnect();
}
client.loop();
client.publish(“X”, “online”);
delay(1000);
}