Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Mosquitto » Project not reconnecting(Not auto reconnecting)
Project not reconnecting [message #1831889] Tue, 01 September 2020 20:48
Eclipse UserFriend
Hello All,

I have a SCD-30 CO2 sensor a Miflora plant sensor a DS18B20 and a SSR-40A-relay.
All connected to my mosquitto.
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, when the connection has been lost.
I use the same mosquitto user for al my sensors.

This is my code for the 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);


int period = 5000;
unsigned long time_now = 0;


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() {
  time_now = millis();
  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");
      if (reconnect()) {
        lastReconnectAttempt = 0;
      }
    }
  } else {
    // Client connected

    while (millis() < time_now + period) {
      //wait approx. [period] ms
    }

    {
      Serial.print(temperatureC);
      Serial.println("ºC");
      client.publish("DS18B20/Temp",  String(temperatureC, 2).c_str());

    }

  }
  client.loop();
}


This is my relay code:
#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();
}
}


This is my mosquitto log:
1598871070: Socket error on client ESP12-00313D81, disconnecting.
1598871070: New connection from 192.168.178.227 on port 1883.
1598871070: New client connected from 192.168.178.227 as ESP12-00313D81 (p2, c1, k15, u'openhab').
1598871070: No will message specified.
1598871070: Sending CONNACK to ESP12-00313D81 (0, 0)
1598871074: Socket error on client ESP12-00313D81, disconnecting.
1598871074: New connection from 192.168.178.227 on port 1883.
1598871074: New client connected from 192.168.178.227 as ESP12-00313D81 (p2, c1, k15, u'openhab').
1598871074: No will message specified.
1598871074: Sending CONNACK to ESP12-00313D81 (0, 0)
1598871075: New connection from 192.168.178.206 on port 1883.
1598871075: Client miflora-client already connected, closing old connection.
1598871075: New client connected from 192.168.178.206 as miflora-client (p2, c1, k15, u'openhab2').
1598871075: No will message specified.
1598871075: Sending CONNACK to miflora-client (0, 0)
1598871077: New connection from 192.168.178.227 on port 1883.
1598871077: Client ESP12-00313D81 already connected, closing old connection.
1598871077: New client connected from 192.168.178.227 as ESP12-00313D81 (p2, c1, k15, u'openhab').
1598871077: No will message specified.
1598871077: Sending CONNACK to ESP12-00313D81 (0, 0)


Greetings.
Previous Topic:QoS2 Publishes reordered
Next Topic:Using MQTT with HTTP serving stream
Goto Forum:
  


Current Time: Fri Apr 26 12:04:01 GMT 2024

Powered by FUDForum. Page generated in 0.02957 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top