ESP32 and NODE RED MQTT

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


#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

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.

THANK FOR YOU REQUEST

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

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

As Paul said, go to 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.

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

And many times we learn from that "overreach" :wink:

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