@will Yes, i built a controller for air conditioner. It uses a subscribe method to get the value of temperature of variable on Ubidots Cloud, if it is higher than 25°C the air conditioner turn on automatically.
I used the subscribe example adding 3 new lines
in setup i added:
@RWB data transfer should be similar to TCP since MQTT runs on TCP/IP, but lower than HTTP. But the main reason to use MQTT is for applications that need to send data from the cloud to the device.
Imagine a cloud-controlled device to open/close a door remotely. In the case of HTTP, the device would have to continuously make GET requests to the server to see if there’s a change in a variable, and then take an action depending on the last reading. This takes a lot of requests and it’s not entirely a real-time interaction since it depends of the polling frequency. With MQTT, the device subscribes to the variable and only gets notified when there’s a change in the variable. This way, the connection between the device and the cloud is left open but data only travels when necessary, saving battery, network bandwidth and improving the real-time experience.
Thanks @aguspg for the info, it does make perfect sense.
I have hear a lot of MQTT but now it looks like I have a reason to take a short course on it get a better understanding of how most people are using it.
Sounds like its much better for the Electron devices also.
Hi everybody. One question why after being subcrition or publish a value . something happens that is disconnected from the server MQTT and stops transmitting only seen in serial zeros, after it reconnects
Why a can I Do ? this is my code
#include "MQTT.h"
#include "UbidotsMQTT.h"
#include "ESP8266WiFi.h"
#define WIFISSID "xxx"
#define PASSWORD "xxxxx"
#define TOKEN "xxxx" // Ubidots TOKEN
#define DATA_SOURCE_IDENTIFIER "xxx" //data source ID, ir a Ubidots/source lado izquierdo esta el label del source
#define VARIABLE_IDENTIFIER "RELAY_SALA" // Add a variable identifier, it must be in lowercase
void callback(char* topic, byte* payload, unsigned int length);
Ubidots client(TOKEN, callback);
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 setup() {
Serial.begin(115200);
delay(10);
Serial.println("Connecting WiFi");
client.wifiConnection(WIFISSID,PASSWORD);
Serial.println("Connected");
pinMode(D0,OUTPUT);
while (client.connect()) {
// client.setDataSourceLabel(DATA_SOURCE_IDENTIFIER);//PUBLICAR
}
}
void loop() {
float value= client.getValueSubscribe(DATA_SOURCE_IDENTIFIER, VARIABLE_IDENTIFIER);//SUSCRIBIR
Serial.println(value);
if (p.atoi()==1.00){
digitalWrite(D0,HIGH);
}
else{
digitalWrite(D0,LOW);
}
client.loop();
}