Uno R4 Wifi says MQTT "tested" but no example

The official Arduino website mentions:

The Arduino UNO R4 WiFi has a built in ESP32-S3 module that enables you to connect to Wi-Fi® networks, and perform network operations. Protocols including HTTPS, MQTT, UDP are tested and supported, and in this article, you will find a number of examples that will get you started<

Yet, there are no examples of MQTT. Many of the older libraries for MQTT that worked on the rev2 wifi no longer work with the Renesas architecture. Has anyone managed to get MQTT working with this board?

For reference... https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples

I did create a fork for pubsubclient.h which when used can be compiled, but I have not had success connecting to my local mqtt (I did connect to test.mosquitto.org). You can find the fork at https://github.com/tony2feathers/pubsubclient_UnoR4

Hi @tony2feathers. I recommend giving the "ArduinoMqttClient" library examples a try. You'll find them in the File > Examples > ArduinoMqttClient menu in Arduino IDE after you install "ArduinoMqttClient" via Library Manager.

This library is a dependency of the "ArduinoIoTCloud" library, which allows the UNO R4 WiFi board to be used as an Arduino IoT Cloud Thing. So this library definitely works with the board and is indeed tested in that application.

I thought I had tried that library before, but I'll try again from scratch and post my results. It might be a day or two. Thanks for the speedy reply. Not sure if it's your lane, but if this is the case, might want a link on the Uno R4 Wifi pages in reference to MQTT that links or refers to that library/those examples.

Did you have any luck? I tried this example and it wouldn't compile. It hangs on this:

'WiFiClient' does not name a type; did you mean 'MqttClient'?

Hi @mizzoudavis. Change these lines in the sketch:

#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
  #include <WiFiNINA.h>
#elif defined(ARDUINO_SAMD_MKR1000)
  #include <WiFi101.h>
#elif defined(ARDUINO_ARCH_ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_GIGA)
  #include <WiFi.h>
#endif

to this:

#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
  #include <WiFiNINA.h>
#elif defined(ARDUINO_SAMD_MKR1000)
  #include <WiFi101.h>
#elif defined(ARDUINO_ARCH_ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_GIGA)
  #include <WiFi.h>
#elif defined(ARDUINO_UNOR4_WIFI)
  #include <WiFiS3.h>
#endif

I did end up getting it to work with a lot of tweaks. Most of those were because of the specific nature of my program, but I can confirm ArduinoMqttClient and WiFiS3 work for a local MQTT connection. I'll try to upload an example later this weekend and post a link here. ptillisch's changes will work, or you can just remove all the if statements and add #include <WiFiS3.h> along with #include . You can also include your secrets file like the example shows. I also went the extra step and created separate wifisetup and mqttsetup functions which I called during setup. This just made it easier for me to read when I was troubleshooting, but is unnecessary.

Here is an example I created and can confirm this works.

I think I made it public, but if you can't access it let me know. I'll double check the settings.

Thanks for posting this. I get connected to wifi and an error code of '2 when attempting to connect to test.mosquitto.org. I'm wondering if there's something goofy with my board?

/*
  ArduinoMqttClient - WiFi Simple Receive

  This example connects to a MQTT broker and subscribes to a single topic.
  When a message is received it prints the message to the Serial Monitor.

  The circuit:
  - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board

  This example code is in the public domain.
*/

#include <ArduinoMqttClient.h>
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
  #include <WiFiNINA.h>
#elif defined(ARDUINO_SAMD_MKR1000)
  #include <WiFi101.h>
#elif defined(ARDUINO_ARCH_ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_GIGA)
  #include <WiFi.h>
#elif defined(ARDUINO_UNOR4_WIFI)
  #include <WiFiS3.h>
#endif

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;    // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)

// To connect with SSL/TLS:
// 1) Change WiFiClient to WiFiSSLClient.
// 2) Change port value from 1883 to 8883.
// 3) Change broker value to a server with a known SSL/TLS root certificate 
//    flashed in the WiFi module.

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "test.mosquitto.org";
int        port     = 1883;
const char topic[]  = "arduino/simple";

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // attempt to connect to WiFi network:
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();

  // You can provide a unique client ID, if not set the library uses Arduino-millis()
  // Each client must have a unique client ID
  // mqttClient.setId("clientId");

  // You can provide a username and password for authentication
  // mqttClient.setUsernamePassword("username", "password");

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  Serial.print("Subscribing to topic: ");
  Serial.println(topic);
  Serial.println();

  // subscribe to a topic
  mqttClient.subscribe(topic);

  // topics can be unsubscribed using:
  // mqttClient.unsubscribe(topic);

  Serial.print("Waiting for messages on topic: ");
  Serial.println(topic);
  Serial.println();
}

void loop() {
  int messageSize = mqttClient.parseMessage();
  if (messageSize) {
    // we received a message, print out the topic and contents
    Serial.print("Received a message with topic '");
    Serial.print(mqttClient.messageTopic());
    Serial.print("', length ");
    Serial.print(messageSize);
    Serial.println(" bytes:");

    // use the Stream interface to print the contents
    while (mqttClient.available()) {
      Serial.print((char)mqttClient.read());
    }
    Serial.println();

    Serial.println();
  }
}

Thank you so much for posting this. I think something is off with my board. I can't get this to connect to my wifi network just says

"15:35:40.851 -> Attempting to connect to WPA SSID: networkname"

I'm stumped.

The same error occurs here too whether I try to connect to a local broker or mosquitto

I will check my local broker setup tomorrow as it may need tweaking

Ok, I think I've found an issue. This line reads:

    status = WiFi.begin(ssid);

And I got some progress when I changed it to:

    status = WiFi.begin(ssid, pass);

Yeah, sorry my code was designed to connect to an open ssid. Had to remove the pass, because using "" wasn't good enough.

I had already fixed the problem with WiFi.begin() last night and this morning I reinstalled mosquitto on a Raspberry Pi 4 but still get the same error when trying to connect to it with the Nano R4

Attempting to connect to WPA SSID:  mySSID
You're connected to the network

Attempting to connect to the MQTT broker: 192.168.1.112
MQTT connection failed! Error code = -2

The broker is definitely running as I can connect to it using MQTT Explorer

Have you updated the firmware on the radio module/"bridge" chip of your UNO R4 WiFi board @UKHeliBob?

I was not aware that there was an an update. I assume that you mean this https://support.arduino.cc/hc/en-us/articles/9670986058780-Update-the-wireless-connectivity-firmware-on-UNO-R4-WiFi

If so, then updating the firmware has not solved the problem

Attempting to connect to the MQTT broker: 192.168.1.112
MQTT connection failed! Error code = -2

That is correct.

It is not connecting to mosquitto either

Attempting to connect to the MQTT broker: test.mosquitto.org
MQTT connection failed! Error code = -2

When attempting to connect to the local broker mosquitto does not log any activity

I have experimented a bit more with this and started by printing the value returned by mqttClient.connect(), like this

    Serial.print("Attempting to connect to the MQTT broker: ");
    Serial.println(broker);

    Serial.println(mqttClient.connect(broker, port));
    if (!mqttClient.connect(broker, port))
    {
        Serial.print("MQTT connection failed! Error code = ");
        Serial.println(mqttClient.connectError());
        while (1)
            ;
    }

    Serial.println("You're connected to the MQTT broker!");
    Serial.println();

Much to my surprise the connection to the broker succeeded, the print having shown a value of zero, ie failed. It looks like the first attempt to connect to the broker fails then the subsequent one succeeds

Buoyed with this success I changed the code to

    Serial.print("Attempting to connect to the MQTT broker: ");
    Serial.println(broker);

    while (!mqttClient.connect(broker, port))
    {
        Serial.print(".");
        delay(100);
    }

    Serial.println("\nYou're connected to the MQTT broker!");
    Serial.println();

This connects to the broker on the second attempt every time that I have tried it, both the local broker and test.mosquitto.org

The problem is that the original code only works if a connection is made to the broker on the first attempt which it often (always ?) does

EDIT :

The problem is that the original code only works if a connection is made to the broker on the first attempt which it often (always ?) fails to do

I have a verified, working code snippet now and I'll try to post it today.

NOTE - I have changed the last sentence of my post #18 so that it makes sense