MQTT Error compiling for board Arduino Uno

I am trying to make Arduino as mqtt client to subscribe message from broker

https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device#programming-the-subscriber-device

#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID
char pass[] = SECRET_PASS;    // your network password

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "test.mosquitto.org";
int        port     = 1883;
const char topic[]  = "real_unique_topic";
const char topic2[]  = "real_unique_topic_2";
const char topic3[]  = "real_unique_topic_3";

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 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();

  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();

  // set the message receive callback
  mqttClient.onMessage(onMqttMessage);

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

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

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

  Serial.print("Topic: ");
  Serial.println(topic);
  Serial.print("Topic: ");
  Serial.println(topic2);
  Serial.print("Topic: ");
  Serial.println(topic3);

  Serial.println();
}

void loop() {
  // call poll() regularly to allow the library to receive MQTT messages and
  // send MQTT keep alive which avoids being disconnected by the broker
  mqttClient.poll();
}

void onMqttMessage(int messageSize) {
  // we received a message, print out the topic and contents
  Serial.println("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();
}

when I compile code It show following error

In file included from C:\Users\Documents\Arduino\libraries\WiFiNINA\src/WiFiStorage.h:23:0,
from C:\Users\Documents\Arduino\libraries\WiFiNINA\src/WiFi.h:38,
from C:\Users\Documents\Arduino\libraries\WiFiNINA\src/WiFiNINA.h:23,
C:\Users\Documents\Arduino\libraries\WiFiNINA\src/utility/wifi_drv.h:293:12: error: 'PinStatus' does not name a type
static PinStatus digitalRead(uint8_t pin);
^~~~~~~~~
Using library ArduinoMqttClient at version 0.1.6 in folder: C:\Users\Documents\Arduino\libraries\ArduinoMqttClient
Using library WiFiNINA at version 1.8.13 in folder: C:\Users\Documents\Arduino\libraries\WiFiNINA
Using library SPI at version 1.0 in folder: C:\Users\Downloads\arduino-nightly-windows\arduino-nightly\hardware\arduino\avr\libraries\SPI
exit status 1
Error compiling for board Arduino Uno.

This looks a bit like the following issue.

You could try to do the same, but then for "Arduino AVR boards".

1 Like

I have tried to update many times but I am getting error message

I found that I have to remove and install the library again.

And I think maybe I am not installing the right library can you please tell me where to find the right library for mqtt client

According to the documentation the library is compatible with all architectures.

At this point I would probably back up all projects, uninstall the IDE, (re)move the Arduino15 folder (probably in C:\Users\{username}\AppData\Local\Arduino15) and do a fresh install of the IDE.

1 Like

If I install Arduino IDE again then the following library will be installed automatically or I have to install it manually

#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>

If I need to install it manually, what name do I need to search in the menu?

What Arduino board do you have? If it is an ESP8266 or ESP32 based board or one of many other boards, it is not directly compatible with the IDE. You must install extra software to be able to program those boards. When you install the IDE there will be a list of the boards that can be programmed with the IDE under the Tools, Boards menu. If your board is not on that list you will have to install a "core" for that board. Installing additional cores in the IDE.

Those files may be installed with the core. If not, look in the IDE library manager, and find and install those libraries. Installing Arduino libraries.

I have Arduino uno board.

An Uno will not do WiFi without other, external, hardware. The WiFiNINA library is certainly not compatible with Uno. You need a board like the ESP8266 or ESP32 with built in WiFi to use that library.

1 Like

They are probably still installed, so it would be good to upgrade them via "Tools -> Manage libraries". If they are not installed, you can use the same tool to install the latest version.

If this is the Arduino UNO WiFi Rev.2, you are good to go according to the documentation. If you have external hardware, you may have to use an other library as @groundFungus mentioned. In that case you perhaps want to share which hardware you have.

1 Like

Wiznet W5100 ethernet chip

https://www.electronicscomp.com/ethernet-w5100-shield-network-expansion-board-with-micro-sd-card-slot-for-arduino?gclid=CjwKCAjw3K2XBhAzEiwAmmgrAvvbpdBOSo-XGKt_NRLl0voP-UHNLYqr_njE0pI3y2iiBbbgs2R-ZBoC40oQAvD_BwE

Ethernet is not Wifi. There are Ethernet libraries for use with that shield. Here is one such library.

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