MqttPub only for ESP-Link firmware


Description

In this tutorial is shown how to publish analog data via Mqtt Topic, 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

Use an analog sensor on the pin A1 and after connect the board to PC, using an USB cable.
Furthermore you need a MQTT Server  and a MQTT client.
In this tutorial is been 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 been 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;
- Click on Subscribe;
- Write the correct Topic (in this example arduino/analog/A1) and click on the Subscribe button:

subscribeMQTT Topic

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 simply publishes analog data into a MQTT topic. Before use it,
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/analog/A1"

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


void loop(){

delay(5000);
int data_value = analogRead(A1); // data value from analog pin 1
Ciao.write(CONNECTOR, TOPIC, String(data_value)); // pushes data into a channel called PIN_A1

}
Note:
You can use a maximum of 3 Topic.

OUTPUT

Subscribe the correct Topic (arduino/analog/A1) from the Mqtt client and look the output:

output