MQTT subscription help

Hi,

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

#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

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

image

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

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

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'

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,

@maru - you should look at ESPEasy from 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.

2 Likes

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

1 Like

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,