ESP8266 disconnected

Please tell me how to solve this problem.
Thank you.
Code:
#include <AsyncMqttClient.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>

#define WIFI_SSID " ******** "
#define WIFI_PASSWORD " *********** "

// Raspberri Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, 100, ***)
#define MQTT_PORT 1883

//Temperature MQTT Topics
#define MQTT_PUB_TEMP "mqtt/lm35"

const int LM35sensorPin = A0;
float LM35sensorValue;
float LM35voltageOut;
float LM35temperature;

// Temperature value
float temp;

AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;

WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;

unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // Interval at which to publish sensor readings

void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Serial.println("Connected to Wi-Fi.");
connectToMqtt();
}

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi);
}

void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}

void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");

if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt);
}
}

void onMqttPublish(uint16_t packetId) {
Serial.print("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}

void setup() {
pinMode(LM35sensorPin, INPUT);
Serial.begin(115200);
Serial.println();

wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);

mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
//mqttClient.onSubscribe(onMqttSubscribe);
//mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// If your broker requires authentication (username and password), set them below
mqttClient.setCredentials("REPLACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");

connectToWifi();
}

void loop() {
//LM35 SENSOR
LM35sensorValue = analogRead(LM35sensorPin);
LM35voltageOut = (LM35sensorValue * 3300) / 1024;

// calculate temperature for LM35 (LM35DZ)
LM35temperature = LM35voltageOut / 10;
//LM35temperatureF = (LM35temperatureC*1.8)+32;
unsigned long currentMillis = millis();
// Every X number of seconds (interval = 10 seconds)
// it publishes a new MQTT message
if (currentMillis - previousMillis >= interval) {
// Save the last time a new reading was published
previousMillis = currentMillis;
// Temperature in Celsius degrees
temp = LM35temperature;

// Publish an MQTT message on topic mqtt/lm35
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());                            
Serial.printf("Publishing on topic %s at QoS 0, packetId: %i ", MQTT_PUB_TEMP, packetIdPub1);
Serial.printf("Message: %.2f \n", temp);

}
}

Please post your code following the advice given in the link below

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

you should add information where does the code you have posted come from?
Is it a 1 to 1 copy & paste from somewhere?
does the original source use the same Raspberry Pi configuration?
Did you do some basic tests?
could it be normal that the connection is established and disconnected?
I haven't done much with webservers but I remember that for at least a part of "webserving" stuff it is normal to disconnect if the actual dataexchange is done.

What do you see on your raspberry Pi which is running the MQTT-server?
I guess the MQTTserver has some logfiles too. What do you see in these logfiles?

How about using this code-example? It should work with an ESP8266 too

best regards Stefan

Look similar to the problem I have been having and it was down to power. The USB cant deliver enough current when connecting to WiFi.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.