Hello All,
I have a SCD-30 CO2 sensor a Miflora plant sensor a DS18B20 and a SSR-40A-relay.
The SCD-30 and the Miflora plant sensors are reconnecting when the connection is lost.
The Relay and DS18B20 are nor reconnecting.
I think the problem is within mosquitto, but i need to be sure of this.
I do see in the serial monitor “Reconnecting” every 5 seconds.
This is my code for the rely and DS18B20.
Relay:
#include <WiFi.h>
#include <PubSubClient.h>
//Wifi Settings
const char* ssid = "MySSID";
const char* password = "MyPass";
//MQTT Settings
const char* mqttServer = "IP";
const int mqttPort = 1883;
const char* mqttUser = "USer";
const char* mqttPassword = "Passwd";
WiFiClient espClient;
PubSubClient client(espClient);
long lastReconnectAttempt = 0;
uint64_t chipid = ESP.getEfuseMac(); // MAC address of ESP32
uint16_t chip = (uint16_t)(chipid>>32);
char clientid[25];
boolean reconnect() {
if (client.connect(clientid, mqttUser, mqttPassword )) {
// Once connected, publish an announcement...
client.publish("relay/Info","Reconnected", true);
}
return client.connected();
}
void setup() {
Serial.begin(115200);
pinMode(4, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
snprintf(clientid,25,"ESP32-%08X",chip);
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect(clientid, mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
lastReconnectAttempt = 0;
}
}
client.subscribe("relay");
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
if (payload[0] == '1') {
digitalWrite(4, HIGH);
}
else if (payload[0] == '0') {
digitalWrite(4, LOW);
}
Serial.println(digitalRead(4) ? "HIGH" : "LOW"); // <<<<<<< NEW
}
void loop(){
if (!client.connected()) {
unsigned long now = millis();
if (now - lastReconnectAttempt > 5000UL) {
lastReconnectAttempt = now;
// Attempt to reconnect
Serial.print("Reconnecting");
client.publish("relay/Info1","Reconnecting", true);
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
} else {
// Client connected
client.loop();
}
}
DS18B20:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <PubSubClient.h>
//Wifi Settings
const char* ssid = "MySSID";
const char* password = "MyPass";
//MQTT Settings
const char* mqttServer = "IP";
const int mqttPort = 1883;
const char* mqttUser = "USer";
const char* mqttPassword = "Passwd";
WiFiClient espClient;
PubSubClient client(espClient);
long lastReconnectAttempt = 0;
uint64_t chipid = ESP.getEfuseMac(); // MAC address of ESP32
uint16_t chip = (uint16_t)(chipid>>32);
char clientid[25];
boolean reconnect() {
if (client.connect(clientid, mqttUser, mqttPassword )) {
// Once connected, publish an announcement...
client.publish("DS18B20/Info","Reconnected", true);
}
return client.connected();
}
// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
// Start the Serial Monitor
Serial.begin(115200);
// Start the DS18B20 sensor
sensors.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
snprintf(clientid,25,"ESP32-%08X",chip);
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect(clientid, mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
lastReconnectAttempt = 0;
}
}
}
void loop() {
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
if (!client.connected()) {
unsigned long now = millis();
if (now - lastReconnectAttempt > 5000UL) {
lastReconnectAttempt = now;
// Attempt to reconnect
Serial.print("Reconnecting");
client.publish("DS18B20/Info1","Reconnecting", true);
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
} else {
// Client connected
client.loop();
Serial.print(temperatureC);
Serial.println("ºC");
client.publish("DS18B20/Temp", String(temperatureC, 2).c_str());
delay(5000);
}
}
When i only have the Relay and DS18B20 connected and they lose connecting i see the following in my mosquitto logs
1596047074: Sending PINGRESP to ESP32-0000F0DB
1596047078: Received PUBLISH from ESP32-0000CCEE (d0, q0, r0, m0, 'DS18B20/Temp', ... (5 bytes))
1596047078: Sending PUBLISH to 3828c3ea-6cfc-46a6-95e5-3a56116b2930 (d0, q0, r0, m0, 'DS18B20/Temp', ... (5 bytes))
1596047078: Received PINGREQ from ESP32-0000CCEE
1596047078: Sending PINGRESP to ESP32-0000CCEE
1596047084: Received PUBLISH from ESP32-0000CCEE (d0, q0, r0, m0, 'DS18B20/Temp', ... (5 bytes))
1596047084: Sending PUBLISH to 3828c3ea-6cfc-46a6-95e5-3a56116b2930 (d0, q0, r0, m0, 'DS18B20/Temp', ... (5 bytes))
1596047089: Received PINGREQ from ESP32-0000F0DB
1596047089: Sending PINGRESP to ESP32-0000F0DB
1596047090: Received PUBLISH from ESP32-0000CCEE (d0, q0, r0, m0, 'DS18B20/Temp', ... (5 bytes))
1596047090: Sending PUBLISH to 3828c3ea-6cfc-46a6-95e5-3a56116b2930 (d0, q0, r0, m0, 'DS18B20/Temp', ... (5 bytes))
1596047105: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047105: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047112: Client ESP32-0000F0DB has exceeded timeout, disconnecting.
1596047112: Client ESP32-0000CCEE has exceeded timeout, disconnecting.
1596047164: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047164: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047220: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1596047224: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047224: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047284: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047284: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047344: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047344: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047404: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047404: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047464: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047464: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047524: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047524: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047584: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047584: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047644: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047644: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047704: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047704: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047764: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047764: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047824: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047824: Sending PINGRESP to 3828c3ea-6cfc-46a6-95e5-3a56116b2930
1596047884: Received PINGREQ from 3828c3ea-6cfc-46a6-95e5-3a56116b2930
So is this a Mosquitto problem ore a problem with my code.
Greetings