MqttSub only for ESP-Link firmware


Description

In this tutorial is shown how to subscribe a Mqtt Topic and to read published data, using an Arduino UNO WiFi.

Warning:
Please, it's important to upgrade at the latest ESP firmware, using the Arduino UNO firmware updater tool, before to continue this tutorial.

Hardware 

Circuit

Connect the board to PC using an USB cable.
To realize this tutorial you need of a  MQTT Server  and of a MQTT client.
As Mqtt client it is used mqttfx.

Set the MQTT

Connect at the Arduino UNO Web panel, using the ip address of the board or the hostname.local/.
If you haven't configured your board then click here.
Access to the Connectivity menu:
- enable MQTT Client;
- type the ip address or the hostname of your MQTT server;
- select the port;
- click on Update.
Wait that the MQTT client status becomes "connected", eventually reload the page:
 
MQTT

After launch the MQTT client, in this tutorial is used mqttfx, and set it:
- click on the icon for to access in the settings menu;

 Mqtt set

- type your Mqtt server address on the Broker Address field;
Apply the changes and after click OK
Mqttfx
- Select the profile created;
- Click on Connect;
- Wait that appears the green icon in the right top of the page;

Code

Now upload the code below, using the Arduino IDE:

Warning:
This tutorial is available only using the Arduino IDE 1.8.x and downloading the UNOWiFiDev.Edition library from Library Manager.

/*
File: MqttPub.ino

This example subscribes a MQTT channel and reads published data, then prints it
into the serial monitor. Please configure the MQTT server address via Uno WiFi
Web Panel. Topics are automatically created (or subscribed) via api calls into
the sketch. You can use a maximum of 3 topics.

Note: works only with Arduino Uno WiFi Developer Edition.
*/

#include <Wire.h>
#include <UnoWiFiDevEd.h>

#define CONNECTOR "mqtt"
#define TOPIC "arduino/data"

void setup() {
Ciao.begin();
Serial.begin(9600);
}

void loop(){
CiaoData data = Ciao.read(CONNECTOR, TOPIC);
if (!data.isEmpty()){
const char* value = data.get(2);
Serial.println(value);
}
}
Note:
You can use a maximum of 3 Topic.

OUTPUT

Uploaded the sketch, follow these steps:
- open the Serial Monitor by the Arduino IDE;
- set the baudrate to 9600;
serial monitor
- select Publish by your MQTT client;
- Write the correct Topic (in this example arduino/data);
- Write your message and click on the Publish button;

write on Mqttfx


- Now check on the Serial Monitor if the message is been published:
Read on the serial monitor