# MQTT subscription help

**URL:** <https://discourse.nodered.org/t/mqtt-subscription-help/18001>\
**Category:** General\
**Created:** [15 November 2019 13:42 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001 "2019-11-15T13:42:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [15 November 2019 13:42 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001/1 "2019-11-15T13:42:23Z")

</div>

Hi,

Can someone help me with this code? I'd like to trigger a Led when the value of the payload is \> 1000.

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

// Update these with values suitable for your network.

const char *ssid = "xxx";
const char *password = "xxx";
const char *mqtt_server = "xxxx";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int led = D4;

void setup_wifi()
{

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char *topic, byte *payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if value of C02 is above 1000
  if ((char)payload[0] > '1000') // This is where it goes wrong!
  {
    digitalWrite(led, HIGH); // Turn the LED on 
    
  }
  else
  {
    digitalWrite(led, LOW); // Turn the LED off 
  }
}

void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
      // Once connected, publish an announcement...
      //client.publish("outTopic", "Test");
      // ... and resubscribe
      client.subscribe("my/sensors/co2");
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
  pinMode(led, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop()
{

  if (!client.connected())
  {
    reconnect();
  }
  client.loop();

  /*
  long now = millis();
  if (now - lastMsg > 2000)
  {
    lastMsg = now;
    ++value;
    snprintf(msg, 50, "Fx235 #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
  */
}

```

Kind regards

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2019 13:55 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001/2 "2019-11-15T13:55:06Z")

</div>

I don't see what the code you posted has to do with node-red.

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [15 November 2019 15:02 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001/3 "2019-11-15T15:02:03Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f057a543d8647082c1f2ae9efe7e8efca51d4a36.png)

Check!.... I was blurry for a moment while staring at this line ... and believe me, I accidentally pressed the send button..😉

So I am curious if there are readers here who can and would like to answer my question.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [16 November 2019 07:40 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001/4 "2019-11-16T07:40:29Z")

</div>

But your code is related to arduino, not node-red.  
Are they any messages on the broker from your ESP ? Use mqtt explorer to 'explore'

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [17 November 2019 20:07 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001/5 "2019-11-17T20:07:27Z")

</div>

Hi bakman2,

Thank you for the interest and yes I'm aware that my question is not directly a node-RED question, sorry for that!  
One Wemos D1 is publishing to the broker, that works fine, I can subscribe with another Wemos D1 to that topic and I see the published CO2 value. ...so subscription is also OK.  
What I try to realize is, when the first Wemos D1 publish a value \> 1000, on the second Wemos D1 a GPIO pin become HIGH.

Kind Regards,

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 November 2019 21:30 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001/6 "2019-11-17T21:30:37Z")

</div>

@maru - you should look at ESPEasy from [http://letscontrolit.com](http://letscontrolit.com) which you flash on to the WeMos, then you can use the built wantls to setup up many sensors. Since I found it I have pretty much stopped coding any sketches on the WeMos's.

Give it a look many of us on the forum use it.

p.s. you can do inter wemos/espeasy communication too.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [18 November 2019 06:56 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001/7 "2019-11-18T06:56:28Z")

</div>

I agree with Paul @zenofmud, using ESP-Easy on a Wemos with MQTT makes capturing and moving data around your system a breeze.

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [18 November 2019 16:12 UTC](https://discourse.nodered.org/t/mqtt-subscription-help/18001/8 "2019-11-18T16:12:33Z")

</div>

Thank you @dynamicdave and @zenofmud, for the ESP-Easy hint, but I think I solved my problem now. Besides that I've worked once before with ESP-Easy and I will definitly take a look again.

Kind regards,
