D1 Mini suddendly stop to send messages to Mosquitto.

Hello colleagues!

i have an D1 mini with 2 sensors (BME280 connected via I2C) and other sensor (18b20) connected to pin 2 (i dont wanna publish this data to MQTT) . on the other hand i have oled display connected via i2c too, every works perfect!.

The information from BME280 is published to mosquitto locally on PC (Mosquitto works fine too, i have other device connected and it works fine.)

The problem is that suddendly the D1 mini stops to send MQTT data, but the meter on the display continues showing correctly. the D1 mini no hangs, show the data on serial monitor.

Im already changed the D1 Mini for Lolin node MCU v3, and the problem persists. could you see my code and help me what is my mistakes? (Im newbie on programming :confused: ).

Thanks in advance!!!

#include <SPI.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <Adafruit_GFX.h>
#include <DallasTemperature.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128    
#define SCREEN_HEIGHT 64   
#define OLED_RESET -1


// sensor temperatura ambiente:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1023.25)
Adafruit_BME280 bme; // I2C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

 
const char* ssid = "LOSLOPEZ";
const char* password = "iddqdidkfa";
const char* mqtt_server = "192.168.1.56";
const char* mqtt_user = "";
const char* mqtt_pass = "";
int mqtt_port = 1883;
const char * root_topic_subscribe ="feeds/casa/temp_sub";
const char * root_topic_publish ="feeds/casa/temp_pub";

float ultimatemp = 0;
float temperaturaambiente = 0;
float presion = 0;
float humedad = 0;
float ultimatempamb =0;

const int oneWireBus = 2;     

OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);


WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
long count=0;
char msg[5];
char msg_on[5];
char msg_hum [5]; 
int value = 0;




void setup() {
  setup_wifi();
  bme.begin(0x76);
  Serial.begin(115200);
  sensors.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
  client.setServer(mqtt_server, mqtt_port);
  delay(10);
  Serial.println(WiFi.localIP());

  reconnect();


}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESTACION_METEO")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      if(client.subscribe(root_topic_subscribe)){
        Serial.println("Suscripcion ok");
      }else{
        Serial.println("fallo Suscripcion");
      }
    } else {
      Serial.print("Fallo :( con error ->");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
      }
  }
 }


void setup_wifi() {

    while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  
}


void  pantalla1(){
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);  //oled display
  display.print("Pantalla1");

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,20);  //oled display
  display.print("TS:");
  display.print(ultimatemp);
  
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,40);  
  display.print("Pa.");
  display.print(bme.readPressure() / 100.0F);
  delay(1000);
 }

void pantalla2() {

  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);  //oled display
  display.print("Pantalla2");
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,20);  //oled display
  display.print("T:");
  display.print(bme.readTemperature());
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,40);
  display.print("H.:");
  display.print(bme.readHumidity());
  display.display();
  delay (1000);

 
 
  }
void loop() {

  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);

  temperaturaambiente = bme.readTemperature();
  presion = (bme.readPressure() / 100.0F);
  humedad = bme.readHumidity();

  if (temperaturaambiente != ultimatempamb) {
   ultimatempamb = temperaturaambiente;
   client.subscribe(root_topic_subscribe);
   delay(2000);
   sprintf("Ambiente", " %0.2f " , temperaturaambiente );
   client.publish("feeds/casa/tempambiente", "Ambiente");
   delay(2000);
   sprintf("Humedad", " %0.2f " , humedad);
   client.publish("feeds/casa/humambiente", "Humedad");
   delay(2000);
   sprintf("Presion", "%0.2f" , presion );
   client.publish("feeds/casa/presion", "Presion");
  }
   
  Serial.print("Temp.Sensor: ");
  Serial.print(temperatureC);
  Serial.println("ºC");
 
pantalla1();
delay (3000);
pantalla2();

// sensor ambiente:
  Serial.print("Temp. ambiente = ");
  Serial.print(bme.readTemperature());
  Serial.println(" *C");

  Serial.print("Presion = ");
  Serial.print(bme.readPressure() / 100.0F);
  Serial.println(" hPa");

  Serial.print("Altitud = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");

  Serial.print("Humedad= ");
  Serial.print(bme.readHumidity());
  Serial.println(" %");



  delay(1000);
  yield();

 
}

suddendly the D1 mini stops to send MQTT data,

Do you mean that at the beginning, the MQTT data is sent successfully, but stops later? Reading your code, I cannot see how it can ever send any real data. It only sends fixed text messages like "Ambiente" and "Humedad".

  sprintf("Humedad", " %0.2f " , humedad);
   client.publish("feeds/casa/humambiente", "Humedad");

The first parameter of sprintf() should be a char array. The function converts the value using the requested format into a string and puts the result into the char array. But you give a constant string instead of the char array. I am surprised that this compiles. Then you would give the char array to the .publish() method.

client.subscribe(root_topic_subscribe);

Why is your code subscribing to topics?

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