Hi, I wanna make a IoT project, but I noticed there is two methods to send variables by mqtt, What is the difference ?
The first one : use too many sprintf + client.publish functions:
/*************************************************************************************************
* This Example sends harcoded data to Ubidots and serves as example for users that have devices
* based on ESP8266 chips
*
* This example is given AS IT IS without any warranty
*
* Made by Jose García @https://github.com/jotathebest/ ,
* adapted from the original WiFiClient ESP8266 example
*************************************************************************************************/
/****************************************
* Include Libraries
****************************************/
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <stdio.h>
/****************************************
* Define Constants
****************************************/
#define WIFISSID "...." // Put your WifiSSID here
#define PASSWORD "...." // Put your wifi password here
#define TOKEN "...." // Put your Ubidots' TOKEN
#define DEVICE_LABEL "weather-station" // Put the device label
#define VARIABLE_LABEL_1 "temperature" // Put the variable label
#define VARIABLE_LABEL_2 "humidity" // Put the variable label
#define MQTT_CLIENT_NAME "...." // MQTT client Name, put a Random ASCII
char mqttBroker[] = "industrial.api.ubidots.com";
char payload[700];
char topic[150];
// Space to store values to send
char str_val_1[6];
char str_val_2[6];
/****************************************
* Initializate constructors for objects
****************************************/
ESP8266WiFiMulti WiFiMulti;
WiFiClient ubidots;
PubSubClient client(ubidots);
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt 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);
WiFiMulti.addAP(WIFISSID, PASSWORD);
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while(WiFiMulti.run() != 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();
}
// Values to send
float temperature = random(0, 50);
float humidity = random(0, 50);
/* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
dtostrf(temperature, 4, 2, str_val_1);
dtostrf(humidity, 4, 2, str_val_2);
sprintf(topic, "%s", ""); // Cleans the topic content
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload content
sprintf(payload, "{\"%s\":", VARIABLE_LABEL_1); // Adds the variable label
sprintf(payload, "%s {\"value\": %s}", payload, str_val_1); // Adds the value
sprintf(payload, "%s, \"%s\":", payload, VARIABLE_LABEL_2); // Adds the variable label
sprintf(payload, "%s {\"value\": %s}", payload, str_val_2); // Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets
Serial.println(payload);
client.publish(topic, payload);
client.loop();
delay(1000);
}
The second one use only few functions “add and ubidotsPublish” to indicate variables and device:
/******************************************
*
* This example works for both Industrial and STEM users.
* If you are using the old educational platform,
* please consider to migrate your account to a STEM plan
*
* Developed by Jose Garcia, https://github.com/jotathebest/
*
* ****************************************/
/****************************************
* Include Libraries
****************************************/
#include "UbidotsESPMQTT.h"
/****************************************
* Define Constants
****************************************/
#define TOKEN "....." // Your Ubidots TOKEN
#define WIFINAME "....." // Your SSID
#define WIFIPASS "....." // Your Wifi Pass
Ubidots client(TOKEN);
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
/****************************************
* Main Functions
****************************************/
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
client.setDebug(true); // Pass a true or false bool value to activate debug messages
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
client.reconnect();
}
// Values to send
float temperature = random(0, 50);
float humidity = random(0, 50);
// Publish values to 2 different data sources
client.add("VARIABLE_LABEL_1", temperature); // Insert your variable Labels and the value to be sent**
client.add("VARIABLE_LABEL_2", humidity);
client.ubidotsPublish("DEVICE_LABEL");**
client.loop();
delay(1000);
}