Pubsub client and dashboard button problem

#1

Hi, i have a problem with simple flow with mqtt and two buttons (on and off)
I use pubsub client library and arduino.
My goal is to turn on/off led connected to esp8266 via nod red dashboard.
The problem is that in serial monitor allways have (msg onn) that turns always led on. If i press (off) button in dashboard the led turns off for a second or two and again turns on. (in serial monitor when peress off button it sends msg off but after that automatically recieves on msgs)

I paste the flows and the arduino code:

[{"id":"3440dee8.160c22","type":"mqtt out","z":"c5653a05.c28fb8","name":"","topic":"ledcontrol","qos":"","retain":"","broker":"661629f4.451408","x":440,"y":200,"wires":[]},{"id":"661629f4.451408","type":"mqtt-broker","z":"","name":"","broker":"test.mosquitto.org","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

the arduino code is:

#include <ESP8266WiFi.h>
#include<PubSubClient.h>

const char* mqtt_server="test.mosquitto.org";
WiFiClient espclient;
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  
   pinMode(2,OUTPUT);
  Serial.begin(115200);
  Serial.print("connecting");
  WiFi.begin("alicska","cska1948");         //SSID,PASSWORD 
  while(WiFi.status()!=WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  
  reconnect();

}


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

for(int i=0;i<length1;i++){
  
  Serial.print(payload[i]); 
  
}
if(payload[0]==49) digitalWrite(2,HIGH);    //ASCII VALUE OF '1' IS 49
else if(payload[0]==50)digitalWrite(2,LOW);//ASCII VALUE OF '2' IS 50
Serial.println();
}

PubSubClient client(mqtt_server,1883,callback,espclient);


void reconnect(){
  while(WiFi.status()!=WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  while(!client.connected()){
  if(client.connect("ESP8266Client123456789")){
    Serial.println("connected");
    client.subscribe("ledcontrol");
  }
    else{
      Serial.print("failed,rc=");
      Serial.println(client.state());
      delay(500);
    }
  } 
}
void loop() {
    if(!client.connected()){
      reconnect();
    }
    
    client.loop();
long now = millis();
  if (now - lastMsg > 5000) {
    lastMsg = now;
  }
}

Thank you for your help

#2

Did you realise that the flow you posted only contains the mqtt out node?

1 Like
#3

I'am sorry this is the flow:

[{"id":"bee035a9.904be8","type":"ui_button","z":"29c99426.3ac6bc","name":"","group":"611a8f0a.c74a8","order":0,"width":0,"height":0,"passthru":false,"label":"onn","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"str","topic":"ledcontrol","x":270,"y":200,"wires":[["a5bfa47d.6b0698"]]},{"id":"25d7bf66.d6d2e","type":"ui_button","z":"29c99426.3ac6bc","name":"","group":"611a8f0a.c74a8","order":1,"width":0,"height":0,"passthru":false,"label":"off","tooltip":"","color":"","bgcolor":"","icon":"","payload":"2","payloadType":"str","topic":"ledcontrol","x":270,"y":300,"wires":[["a5bfa47d.6b0698"]]},{"id":"a5bfa47d.6b0698","type":"mqtt out","z":"29c99426.3ac6bc","name":"","topic":"ledcontrol","qos":"","retain":"","broker":"75f4026e.bff7ac","x":480,"y":260,"wires":[]},{"id":"c8791890.5a0e08","type":"mqtt in","z":"29c99426.3ac6bc","name":"","topic":"ledcontrol","qos":"0","broker":"11103eb9.229941","x":250,"y":100,"wires":[["2c8fc683.6609aa"]]},{"id":"2c8fc683.6609aa","type":"debug","z":"29c99426.3ac6bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":480,"y":100,"wires":[]},{"id":"611a8f0a.c74a8","type":"ui_group","z":"","name":"control","tab":"ce3f48b7.84b1f8","order":1,"disp":true,"width":"6","collapse":false},{"id":"75f4026e.bff7ac","type":"mqtt-broker","z":"","name":"","broker":"test.mosquitto.org/","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willRetain":null,"willPayload":""},{"id":"11103eb9.229941","type":"mqtt-broker","z":"","name":"","broker":"test.mosquitto.org/","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"ce3f48b7.84b1f8","type":"ui_tab","name":"Tab 1","icon":"dashboard","order":1}]
#4

You should be able to see that the Node-RED part is working fine. In that the MQTT-OUT node sends data to the broker you have selected and the MQTT-IN node receives that data

The issues probably lies within your Arduino code but this isn't an Arduino forum.

Also by using a test server you are likely to be seeing other peoples data as well as your own. Take the time to set up a mosquito broker that you control