# ESP32 and NODE RED MQTT

**URL:** <https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178>\
**Category:** General\
**Created:** [4 May 2021 18:33 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178 "2021-05-04T18:33:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BetinEc](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@BetinEc](https://discourse.nodered.org/u/BetinEc)\
**Post date:** [4 May 2021 18:33 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178/1 "2021-05-04T18:33:04Z")

</div>

I need to read the data on my ESP32. data that I send from a local NODE-RED, to my NODE RED in IBM CLOUD, which data reaches the server and NODE RED CLOUD, now he wants to know about a program that helps me read this data in IDE ARDUINO, to be able to perform an action such as turning a led on or off

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a05bb8bc373916334e3adbffbd4af65642e5874.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e202383bcd0efc86a8fd4cd31c7763072353af44.png)

```auto
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h> 

//-------- your wifi -----------

const char* ssid = "CELERITY_MONTALVAN";
const char* password = "1722510060B.";

#define ORG "5hjcib" 
#define DEVICE_TYPE "raspberry1" 
#define DEVICE_ID "comunicacioniot" 
#define TOKEN "123456789Bf" 
//-------- Bluemix information to build up MQTT message -------

char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
// this part I need to know how to subscribe to topics that IBM WATSON sends me
char pubTopic[] = "iot-2/evt/status/fmt/json";
char subTopic[] = "iot-2/cmd/test/fmt/String";

char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

WiFiClient wifiClient;
PubSubClient client(server, 1883, NULL, wifiClient);

const char ledpin3 = 4;

void receivedCallback(char* pubTopic, byte* payload, unsigned int length) {
  Serial.print("Message received: ");
  Serial.println (pubTopic);

  Serial.print("payload: ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  /* we got '1' -> on */
  if ((char)payload[0] == '1') {
    digitalWrite(ledpin3, HIGH); 
  } else {
    /* we got '0' -> on */
    digitalWrite(ledpin3, LOW);
  }
}

void setup() {
    Serial.begin(115200);
    Serial.println();
    pinMode(ledpin3, OUTPUT);
    Serial.print("Connecting to "); 
    Serial.print(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    } 
    Serial.println("");
    
    Serial.print("WiFi connected, IP address: "); 
    Serial.println(WiFi.localIP());

    if (!client.connected()) {
        Serial.print("Reconnecting client to ");
        Serial.println(server);
        while (!client.connect(clientId, authMethod, token)) {
            Serial.print(".");
            delay(500);
        }
        client.setCallback(receivedCallback);
        if (client.subscribe(subTopic)) {
            Serial.println("subscribe to cmd OK");
        } else {
            Serial.println("subscribe to cmd FAILED");
        }
        Serial.println("Bluemix connected");
    }
}

void loop() {
    client.loop();

        if (client.publish(pubTopic, (char*) payload.c_str())) {
            Serial.println("Publish ok");
        } else {
            Serial.println("Publish failed");
        }
    }
}

```

this is a program on which I am basing myself to be able to connect to the IBM CLOUD server, but the part of reading what the server sends me is missing to perform an action

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [4 May 2021 22:42 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178/2 "2021-05-04T22:42:21Z")

</div>

If I am reading your question correctly, you want to publish MQTT messages to a PC on another network?

This will work as long as both computers are using the same Broker.

I am using a public broker so that I can send data to a remote ESP unit that is on a different LAN.

---

<div class="post-metadata">

**Author:** ![BetinEc](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@BetinEc](https://discourse.nodered.org/u/BetinEc)\
**Post date:** [4 May 2021 23:17 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178/3 "2021-05-04T23:17:55Z")

</div>

![PROBLEM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6e055d01ca9c5e7c13d5d87e260ff34dae6b0d55.png)

> [@SteveMann](#):
>
> desea publicar mensajes MQTT en un PC en otra red

THANK FOR YOU REQUEST

 ![PROBLEM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6e055d01ca9c5e7c13d5d87e260ff34dae6b0d55.png)

I need to subscribe to the IBM WATSON server in order to read the data it sends me. (I don't know what types of data it sends me either), for the IDE ARDUINO I can read them and perform an action to turn on a led.  
EVENT VALUE FORMAT  
event {"d":{"Bool":"on"}} json

These are the data that arrive at the IBM WATSON these data I need to use to perform an on-off action of a led in the ESP32 module

---

<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:** [5 May 2021 00:22 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178/4 "2021-05-05T00:22:21Z")

</div>

So your real question is "Using the Arduino IDE, how do I subscribe to a topic?"  
This is really a question for the Arduino IDE forum. You should look at the MQTT library `PubSubClient`. And since there are many examples out there about the library, do a google search for some of the existing tutorials

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [5 May 2021 01:46 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178/5 "2021-05-05T01:46:41Z")

</div>

As Paul said, go to [https://forum.arduino.cc/](https://forum.arduino.cc/)  
What you want is an example of using the PubSubClient on the ESP32. I have a lot of ESP8266 devices that use that library and from your description MQTT is the correct protocol. I am assuming that "IBM CLOUD WATSON" is running an MQTT broker.

I know that the Raspberry Pi is capable of MQTT protocol. In fact my MQTT broker in my home network is running on a Raspberry Pi 3. I have never used it to publish or subscribe to MQTT messages.

Everything you have described is possible without Node Red at all, unless you plan to control the publish from Node Red.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [5 May 2021 04:33 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178/6 "2021-05-05T04:33:39Z")

</div>

Many times we overreach things. I am using ESPHome for my ESP devices, all of them using MQTT to publish/subscribe. So simple, why make it more complicated than necessary

> **[ESPHome](https://esphome.io/index.html)**
>
> ESPHome Homepage - Reimagining DIY Home Automation. ESPHome is a framework that tries to provide the best possible use experience for using ESP8266 and ESP32 microcontrollers for Home Automation. Just write a simple YAML configuration file and get...

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [5 May 2021 05:59 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178/7 "2021-05-05T05:59:49Z")

</div>

> [@krambriw](#):
>
> Many times we overreach things.

And many times we learn from that "overreach" 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 July 2021 06:00 UTC](https://discourse.nodered.org/t/esp32-and-node-red-mqtt/45178/8 "2021-07-04T06:00:13Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
