Getting WiFiClient from ArduinoCloud object

Hello,

Is there a way to get the WiFiClient object that ArduinoCloud creates when connecting to WiFi?

I am trying to use MqttClient with Arduino Cloud,
image

and I need the WiFiClient object that gets initialized when the Arduino gets connected to the wifi, and since that procedure is handled automatically by WiFiConnectionHandler when using the Arduino "thing" Editor, i have no idea how to get that object.

Is there any ArduinoCloud docs I can check, or does anyone know any function that should return said object?

I am using an Arduino Nano 33 IoT w/ headers.

Best regards!

1 Like

Solved here:

Solution:

#include <ArduinoMqttClient.h>
#include "thingProperties.h";

const char brokerMosq[] = "test.mosquitto.org";
int        portMosq     = 1883;
const char topicMosq[]  = "SE/practicaUA2022/murcia";

MqttClient mqttClient = NULL;

void setup() {
  Serial.begin(9600);
  delay(1500); 

  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  mqttClient = new MqttClient(ArduinoIoTPreferredConnection.getClient());
  
  ...

}
1 Like

Hi,

Related question on this note. I'm trying to use the Nano RP 2040 Connect as a Modbus Client.
I want to:

  1. read from the modbus server over TCP via WiFi
  2. publish result to IoT Cloud

I can get the ArduinoModbus library to work without ArduinoCloud connection. I can also get the ArduinoCloud Connection up and running. But, when I combine the two the device crashes when trying to open the connection to the Modbus Client.

Would be most grateful of some help! Thanks!

MWE:

#include "thingProperties.h"
#include "ArduinoRS485_RP2040.h"
#include "ArduinoModbus_RP2040.h"
#include "WiFiNINA.h"

WiFiClient wifiClient;
ModbusTCPClient modbusTCPClient(wifiClient);

IPAddress server(192, 168, 1, 10); // ip address of modbus server

bool iotCloudConnected = false;

void setup() {
  Serial.begin(9600);
  delay(1500);


  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, onArduinoIOTCloudConnect);


}

void loop() {
  // Update cloud with new values
  ArduinoCloud.update();

  // Modbus connection only if connected to IOT cloud
  if (iotCloudConnected) {
    // DOESN'T CRASH HERE
    if (!modbusTCPClient.begin(server)) {
    // CRASHES AS SOON AS modbusTCPClient.begin(server) called
      Serial.println("Modbus TCP Client failed to connect!");
    } else {
     ...
    }
  }
  
}

void onArduinoIOTCloudConnect() {
  Serial.println("Board successfully connected to Arduino IoT Cloud");
  // Reassign modbus client to use arduino IOT connection
  modbusTCPClient = ModbusTCPClient(ArduinoIoTPreferredConnection.getClient());
  iotCloudConnected = true;
}

Hello!

From my experience, i can say the behaviour can change depending on the library used with ArduinoIoTCloud.

I can suggest you to try 3 things:

  1. Use the ArduinoIoTPreferredConnection.getClient() client instead of WiFiNINA, to connect your library to the internet ( This rarely works, most of the time libraries are compatible with different internet client connections, but just a few are compatible with ArduinoIoTCloud's client)
...
ModbusTCPClient modBus = NULL;
...

void setup(){
  ...
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  ...
  modBus = new ModbusTCPClient(ArduinoIoTPreferredConnection.getClient());
  ...
}
  1. Use WiFiNINA, and link wifinina's client to arduinoCloud's connection.
#include "WiFiNINA.h"
...
WiFiClient wifiClient;
ModbusTCPClient modbusTCPClient(wifiClient);
...

void setup(){
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(SSID);
  while (WiFi.begin(SSID, PASS) != WL_CONNECTED) {
    // failed, retry
    Serial.print("Failed connection to WiFi, retrying...\n");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();
  ...
  ArduinoCloud.begin(wifiClient);
  ...
}
  1. This usually works most of the time, and it's combining both, but for some reason it's not documented ( just like most of ArduinoIoTCloud's library )

    ArduinoCloud automatically links its connection with WiFiNINA's client if a WiFiNINA client is present.
#include "WiFiNINA.h"
...
WiFiClient wifiClient;
ModbusTCPClient modbusTCPClient(wifiClient);
...

void setup(){
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(SSID);
  while (WiFi.begin(SSID, PASS) != WL_CONNECTED) {
    // failed, retry
    Serial.print("Failed connection to WiFi, retrying...\n");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();
  ...
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  ...
}

Good luck, and best regards!

1 Like

I agree this is very unfortunate. Arduino actually did recently (I guess better late than never, right?) publish some pretty nice documentation:

https://docs.arduino.cc/cloud/iot-cloud/tutorials/technical-reference#things

however, it is clearly not comprehensive because the functions mentioned by the developer in that issue are not mentioned there or in any other official documentation I have seen.

If you feel that this is something Arduino needs to resolve, I would recommend you to reopen https://github.com/arduino-libraries/ArduinoIoTCloud/issues/314 so that they can effectively track the task. Closing the issue indicates that the problem is resolved and I don't feel that is so.

1 Like

Hoah! This is awesome, didn't know about this cheatsheet, it really must be new since the last revision was today.

I've been struggling hard for the past 3 months on finding docs about the arduinoCloud library, and most of the time I just found solutions on projects that were similar to my aim, so this cheatsheet is a lifesaver :smiley:

I agree, I closed the issue because mine was just a question, and I had it solved, but considering that I'll reopen it so they can add those methods to the documentation.

Thanks :slight_smile:

1 Like

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