# Node-RED stops publishing and Subsribing

**URL:** <https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540>\
**Category:** General\
**Created:** [24 December 2019 08:12 UTC](https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540 "2019-12-24T08:12:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![saeed1](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@saeed1](https://discourse.nodered.org/u/saeed1)\
**Post date:** [24 December 2019 08:12 UTC](https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540/1 "2019-12-24T08:12:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 December 2019 09:20 UTC](https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540/2 "2019-12-24T09:20:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saeed1](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@saeed1](https://discourse.nodered.org/u/saeed1)\
**Post date:** [24 December 2019 09:37 UTC](https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540/3 "2019-12-24T09:37:51Z")

</div>

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](https://discourse.nodered.org/uploads/short-url/6Yus9M0hyjVNvvnP9oiVIxrWcX2.txt) (762 Bytes)

![Node RED.PNG](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/ba59791cf42850220183b4a60b1bd541b224d87c.png)

---

<div class="post-metadata">

**Author:** ![saeed1](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@saeed1](https://discourse.nodered.org/u/saeed1)\
**Post date:** [24 December 2019 09:58 UTC](https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540/4 "2019-12-24T09:58:49Z")

</div>

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();  
}

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 December 2019 10:02 UTC](https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540/5 "2019-12-24T10:02:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 December 2019 20:34 UTC](https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540/6 "2019-12-24T20:34:36Z")

</div>

> [@saeed1](#):
>
> ```auto
> void callback(char* topic, byte* payload, unsigned int length) {
> if (payload[0] == '1')
> {
> digitalWrite(ledPin, HIGH);
> }
> if (payload[0] == '0')
> {
> digitalWrite(ledPin, LOW);
> }
> }
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [24 December 2019 20:59 UTC](https://discourse.nodered.org/t/node-red-stops-publishing-and-subsribing/19540/7 "2019-12-24T20:59:49Z")

</div>

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()`.
