Hello, I am working on a project and I faced a problem in getting the action after done during subscribe. I have a server on ESP32 which is connected to Ubidots for publishing and subscribing. 2 clients with ESP8266 are connected with it with flame sensor and magnetic switch respectively. Now I am trying to subscribe the clients, but I am having a problem. As per me, there is time delay of around 1.5mins. So is there is any solution for this?
this is the setup and loop part:
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// Assign the pin as OUTPUT
pinMode(RELAY, OUTPUT);
Serial.println();
Serial.print("Wait for WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println(F("Server started"));
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, VARIABLE_LABEL_SUBSCRIBE); // This is the format to Subscribe Values :- /v1.6/devices/[device label]/[variable label]/lv
client.subscribe(topicSubscribe);
Serial.println();
//wifi_connect();
// Flame Sensor mode INPUT_PULLUP
pinMode(fireSensor, INPUT_PULLUP);
// Set fireSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(fireSensor), detectsFire, RISING);
pinMode(4, OUTPUT);
//wifi_connect();
}
void loop() {
if (system_enable && cmd){
//if(cmd){
//
send_command("fire");
digitalWrite(4, HIGH);
delay(5000);
digitalWrite(4, LOW);// for switching OFF the buzzer afetr 5000 ms
cmd = false;
//}
}
}
Hi @gargashi,
I hope all is well.
Can you please let us know which mqttbroker (URL) are you using to subscribe to Ubidots variables? Additionally, are you using any specific libraries?
All the best,
-Isabel
mqtt broker= industrial.api.ubidots.com
Libraries: ESP8266WiFi.h
PubSubClient.h
cmd: in the program is the command going to server(ESP32)
#include <ESP8266WiFi.h>
#include "PubSubClient.h"
#define PORT 80
char* ssid = "*******";
char* password = "******";
#define TOKEN "*********"
#define MQTT_CLIENT_NAME "*****"
#define DEVICE_LABEL "******" // Assig the device label
#define VARIABLE_LABEL_SUBSCRIBE "relay" // Assing the variable label
#define RELAY 16 // Set the GPIO16 as RELAY
bool system_enable = 1;
extern bool system_enable;
char mqttBroker[] = "industrial.api.ubidots.com";
char payload[100];
char topic[150];
char topicSubscribe[100];//To store the topic from which the message is subscribed
WiFiServer server(PORT);
WiFiClient esp_client;
WiFiClient ubidots;
PubSubClient client(ubidots);
int ex = 0;
String req;
const char* host = "***.***.**.**";
const uint16_t port = 80;
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message == "0") {
system_enable = false;
//digitalWrite(RELAY, LOW);
Serial.println("System is disabled");
} else {
system_enable = true;
//digitalWrite(RELAY, HIGH);
Serial.println("System is enabled");
}
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");
client.subscribe(topicSubscribe);
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
// Set GPIOs for fireSensor
const int fireSensor = 5;
bool cmd = false;
// Checks if motion was detected, sets LED HIGH and starts a timer
ICACHE_RAM_ATTR void detectsFire()
{
Serial.println("Fire Alarm!!!");
cmd = true;
}
Hello, thank you, I solved my problem, now I am not getting any delay. I was making some mistakes. Thanks again, for this good community.
Hi @gargashi,
Glad to hear you were able to solve it.
Have a nice day,
-Isabel
Hi
My Name is Said , could you please explain what were the mistakes that you solved the problem of delay
in receiving subscriptions
Thank you
Hello Said, actually now I didn’t remember the actual mistake. But probably there was silly mistake in code from my side. It was not any big mistake. Please check your code line by line & match with examples given on Ubidots Help Centre. You can figure it out.
Thanks