Been trying on and off for sometime to get the sample program that you posted in your tutorial for interfacing the ESP32 to UbiDots via MQTT using PubSubClient. Tried on two diff boards (DFRobot and SparkFun) but no luck. If I use a different MQTT broker (“broker.mqtt-dashboard.com”), no problems connecting to that broker. The following is the code snippet that I have been using:
/****************************************
* Include Libraries
****************************************/
#include <WiFi.h>
#include <PubSubClient.h>
/****************************************
* Define Constants
****************************************/
#define WIFISSID "myWIFISSID" // Put your WifiSSID here
#define PASSWORD "myPASSWORD" // Put your wifi password here
#define TOKEN "myTOKEN" // Put your Ubidots' TOKEN
#define MQTT_CLIENT_NAME "MqTtClIeNt" // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string;
//it should be a random and unique ascii string and different from all other devices
#define VARIABLE_LABEL "sensor" // Assing the variable label
#define DEVICE_LABEL "esp32" // Assig the device label
#define SENSOR 12 // Set the GPIO12 as SENSOR
char mqttBroker[] = "things.ubidots.com";
//char mqttBroker[] = "broker.mqtt-dashboard.com";
char payload[100];
char topic[150];
// Space to store values to send
char str_sensor[10];
char str_lat[6];
char str_lng[6];
/****************************************
* Auxiliar Functions
****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
Serial.write(payload, length);
Serial.println(topic);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attemp to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("Connected");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
WiFi.begin(WIFISSID, PASSWORD);
// Assign the pin as INPUT
pinMode(SENSOR, INPUT);
Serial.println();
Serial.print("Wait for WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label
float sensor = analogRead(SENSOR);
float lat = 6.101;
float lng= -1.293;
/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(sensor, 4, 2, str_sensor);
dtostrf(lat, 4, 2, str_lat);
dtostrf(lng, 4, 2, str_lng);
sprintf(payload, "%s {\"value\": %s", payload, str_sensor); // Adds the value
sprintf(payload, "%s, \"context\":{\"lat\": %s, \"lng\": %s}", payload, str_lat, str_lng); // Adds coordinates
sprintf(payload, "%s } }", payload); // Closes the dictionary brackets
Serial.println("Publishing data to Ubidots Cloud");
client.publish(topic, payload);
client.loop();
delay(1000);
}
And the errors I get are:
MQTT_CONNECTION_TIMEOUT -4
MQTT_CONNECT_FAILED -2
From the Arduino window, I see the following:
which I believe others have commented about.
Note 1: From UbiDot’s Devices webpage, I see that it tried to communicate because the “Last Activity” was 5 minutes ago.
Note 2: I have checked the token a couple of times and it is good.
Note 3: For PubSubClient I have used versions 2.5 and 2.6. Version 2.6 uses
MQTT_VERSION MQTT_VERSION_3_1_1
Any suggestions how I can get the ESP32’s to communicate with UbiDots?