Node-RED stops publishing and Subsribing

I am controlling an led with a push button. It works fine manually. Then i controlled it by using node-RED . It works also fine for 40 seconds, after it Node-RED becomes unfunctional. I am running mosquitto and Node-RED on my laptop. I am new to Node-RED. I need your help

It sounds like you have a loop. Are you feeding back from mqtt into the switch some his?

Hi,

Thanks.

Here is my code and simple node-RED flow. Please help me.

I think i am using function client.loop inside the arduino ide looping.
Now i am using only led for simplification. You can check the code and node red flow. I have removed the push button for simplicity sake but i am getting the same problem.

code.txt (762 Bytes)

Node RED.PNG

Could you check my code please !

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

int ledPin = 8;

byte mac = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 1);
IPAddress server(192, 168, 0, 229);

void callback(char* topic, byte* payload, unsigned int length) {
if (payload[0] == '1')
{
digitalWrite(ledPin, HIGH);
}
if (payload[0] == '0')
{
digitalWrite(ledPin, LOW);
}
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{
pinMode(ledPin , OUTPUT);
digitalWrite(ledPin, LOW);
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient", "testuser", "testpass")) {
client.subscribe("led");
}
}

void loop()
{
client.loop();
}

you can attach code inline directly using the " symbol in the toolbar above (when editing your note)

Couple of questions:
1 - Where is the call tocallback()
2 - there seems to be one too many } in the part I copied.

  1. That is a very good question. Since it's arduino code it requires a void setup() to set up the code, then a void loop() that gets executed in a loop, exactly like it says on the tin. I was confused for a moment and thought maybe callback() was used there but it isn't (it's 5 years since I last wrote Arduino-C). It is however set as callback handler in the PubSubClient constructor, although I have not used the PubSubClient library before in Arduino and I'm very unsure if that's a valid way to call the constructor. Someone else needs to check that.

  2. No, that's just the bad indenting through lack of formatting, the last } is the closing for the function callback().