Subscribed information with delay - NodeMCU/MQTT


#1

Hey there! I’m using NodeMCU 1.0 and Arduino IDE for my project, sending data with MQTT protocol.

I’m publishing 3 variables: temperature measured by DS18B20 and two feedback from different LEDs (that way i’m able to know if the led went off or on, its a response from the board).

And i’m subscribing other 3 variables: the turn off/on button of LEDs and a reset button. I left the board to send data all night long. The temperature sent data perfectly during all night, but the feedback variables from LEDs stopped working after a feel hours. So I sent the reset variable to reboot the board to try to solve the problem, and apparently the reset bit got there hours after I sent it.

Can anyone help me to understand why that lag to sent and receive data from the board? I think I’m doing something wrong. The code is on the post

Thanks you all!

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"
#include "OneWire.h"
#include "DallasTemperature.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "***" // Your Ubidots TOKEN
#define WIFINAME "***" //Your SSID
#define WIFIPASS "***" // Your Wifi Pass
#define DEVICE_LABEL  "disp01"  // Put here your Ubidots device label
#define VARIABLE_LABEL1  "firesled1"  // Put here your Ubidots variable label
#define VARIABLE_LABEL2  "firesled2"
#define VARIABLE_LABEL3  "reset"

/****************************************
 * Define Pins
 ****************************************/
#define ledEx 14
#define ledIn 16

Ubidots client(TOKEN);

/****************************************
 * Configuration DS18B20
 ****************************************/
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempDeviceAddress;
int resolution = 12;
int delayInMillis = 0;
float ds18b20_C = 0.0;

/****************************************
 * Misc
 ****************************************/

char*  topico;
char*  topico1 = "/v1.6/devices/disp01/firesled1/lv";
char*  topico2 = "/v1.6/devices/disp01/firesled2/lv";
char*  topico3 = "/v1.6/devices/disp01/reset/lv";
int statusLedIn, statusLedEx, statusReset;

/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  topico = topic;
  
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  
  //Topic 1
  if (strcmp(topico,topico1) == 0){
    
    if ((char)payload[0]=='1'){
      digitalWrite(ledIn, LOW);
    }
    else{
      digitalWrite(ledIn, HIGH);
    }
  }
  
 //Topic 2
  if (strcmp(topico,topico2) == 0){

    if ((char)payload[0]=='1'){
      digitalWrite(ledEx, HIGH);
    }
    else{
      digitalWrite(ledEx, LOW);
    }
  }
  
  //Topic 3
  if (strcmp(topico,topico3) == 0){
    
    if ((char)payload[0]=='1'){
      functionReset();
    }
  }
  Serial.println();
}

void readDs18b20(){
  sensors.requestTemperatures();
  ds18b20_C = sensors.getTempCByIndex(0);
  //Serial.print("DS18B20 = "); 
  //Serial.println(ds18b20_C);
  //Serial.println("    ");
}

void readLeds(){
  statusLedIn = digitalRead(ledIn);
  statusLedEx = digitalRead(ledEx);
}

void functionReset(){
  Serial.println("Resetting...");
  ESP.restart();
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  Serial.begin(115200);
  
  client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(false); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  
  pinMode(ledExterno, OUTPUT);
  pinMode(ledInterno, OUTPUT);
  pinMode(botaoExterno, INPUT);
  pinMode(botaoInterno, INPUT);
  
  // Init DS18B20
  sensors.getAddress(tempDeviceAddress, 0);
  sensors.setResolution(tempDeviceAddress, resolution);
  sensors.setWaitForConversion(false);
  }

void loop() {
  
  if(!client.connected()){
      client.reconnect();
      client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL1); //Insert the dataSource and Variable's Labels
      client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL2);
      client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL3);
  }
  
  readDs18b20();
  readLeds();
  
  client.add("temperature", ds18b20_C);
   client.ubidotsPublish("disp01");
  client.add("statusled1", statusLedIn);
  client.ubidotsPublish("disp01");
  client.add("statusled2", statusLedEx);
  client.ubidotsPublish("disp01");
  
  client.loop();
}

#2

Greetings, probably you would need to modify your reconnect routine, my advise is to replace this piece of code:

if(!client.connected()){
      client.reconnect();
      client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL1); //Insert the dataSource and Variable's Labels
      client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL2);
      client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL3);
  }

Instead of using the connected() method, you may implement a reconnect routine every hour, in that way you can be sure that your device will be always online.

Hope it helps you.


#3

Hey Jota! Thanks very much! I will try that right now and test it!