Using PubSubClient on the IoT Cloud

I have a number of ZigBee sensors on an existing IoT that I'd like to connect with the Arduino IoT Cloud. A couple of possibilities are 1) using the API to upload them directly from my existing local IoT hub residing on a Raspberry Pi or 2) to use a Thing on the Arduino Cloud to gather the sensor data via Raspberry Pi MQTT broker and write it to IoT Cloud variables.

Since I was already up to speed with MQTT I decide to try that first.

I got the commands from an existing Arduino sketch that successfully gathers the sensor data when running on a Nano RP2040 Connect when programmed using the traditional IDE. It was not connected to the Arduino IoT Cloud at that point.

The next step was to use the same commands on an RP2040 Connect configured as a Thing.

However, in the Thing environment the same commands won't compile. The error messages indicate that the MQTT client is not a class, which it certainly was when I compiled it in the IDE.

#include "thingProperties.h"
#include <PubSubClient.h>
#include <ArduinoJson.h>

#define MQTT_USER "XXXXXXXXXX"        // MQTT broker username 
#define MQTT_PASS "XXXXXXXXXX"        // MQTT broker password
#define MQTT_IP "XXX.XXX.XXX.XXX"     // MQTT server IP address on Cobalt
#define MQTT_PORT 1883              // MQTT port
#define THING_NAME "RP2040-001"     // Unique name of this thing
int status = WL_IDLE_STATUS;

PubSubClient mqttClient(WiFiClient);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  // Initialise MQTT
  mqttClient.setServer(MQTT_IP, MQTT_PORT);
  mqttClient.setCallback(callback_function);
  if (mqttClient.connect("RP2040-002", MQTT_USER, MQTT_PASS)){
    Serial.println("Connected to MQTT");
  }
  else{
    Serial.println("Not connected to MQTT");
  }

  mqttClient.subscribe("test_topic");

}

void loop() {
  ArduinoCloud.update();
  // Your code here
  
  mqttClient.loop();
}

//--------------------------------------------------------------------------------------
// Service MQTT messages received
//--------------------------------------------------------------------------------------
void callback_function(char* topic, byte* message, unsigned int length) {
  // Message decode goes here
}

When I do a verify, the line "mqttClient.setServer(MQTT_IP, MQTT_PORT);" is highlighted with the error message "request for member 'setServer' in 'mqqtClient' which is of non-class type 'PubSubClient(WiFiClient)'".

The full verify message is:

/tmp/499798283/MQTT2Cloud_jul23a/MQTT2Cloud_jul23a.ino: In function 'void setup()': /tmp/499798283/MQTT2Cloud_jul23a/MQTT2Cloud_jul23a.ino:55:14: error: request for member 'setServer' in 'mqttClient', which is of non-class type 'PubSubClient(WiFiClient)' mqttClient.setServer(MQTT_IP, MQTT_PORT); ^ /tmp/499798283/MQTT2Cloud_jul23a/MQTT2Cloud_jul23a.ino:56:14: error: request for member 'setCallback' in 'mqttClient', which is of non-class type 'PubSubClient(WiFiClient)' mqttClient.setCallback(callback_function); ^ /tmp/499798283/MQTT2Cloud_jul23a/MQTT2Cloud_jul23a.ino:57:18: error: request for member 'connect' in 'mqttClient', which is of non-class type 'PubSubClient(WiFiClient)' if (mqttClient.connect("RP2040-002", MQTT_USER, MQTT_PASS)){ ^ /tmp/499798283/MQTT2Cloud_jul23a/MQTT2Cloud_jul23a.ino:64:14: error: request for member 'subscribe' in 'mqttClient', which is of non-class type 'PubSubClient(WiFiClient)' mqttClient.subscribe("test_topic"); ^ /tmp/499798283/MQTT2Cloud_jul23a/MQTT2Cloud_jul23a.ino: In function 'void loop()': /tmp/499798283/MQTT2Cloud_jul23a/MQTT2Cloud_jul23a.ino:72:14: error: request for member 'loop' in 'mqttClient', which is of non-class type 'PubSubClient(WiFiClient)' mqttClient.loop(); ^ Error during build: exit status 1

Firstly, I'd like to confirm that I'm using the correct name for the Wi-Fi client. I.e. WiFiClient. It seems to pass the verify ok, but I can't find any documentation on it in the context of a Thing.

Any idea as to what I am doing wrong? Is there as fundamental reason why I can't use PubSubClient() in a Thing?

It seems you can't get at the Wi-Fi client by name but instead need to reference it via this

ArduinoIoTPreferredConnection.getClient()

Doing so allowed my sketch using PubSubClient to compile correctly, but it kept crashing. So this would not appear to be the way to head.

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