I am using mosquitto broker. Pubsubclient v-2.8. I cannot reduce the latency of received messeges and the latency is almost 20 seconds.
When I check the received messeges with my phone they are being received almost instantly
Any help is greatly appreciated
Here is my Subscribe code (I am running the subscribing code on an ESP8266) :
#include <ESP8266WiFi.h>
#include <Servo.h>
char ssid[] = "INFOLEX";
const char* password = "infofuture";
#include <PubSubClient.h>
#define mqtt_server "192.168.1.5"
WiFiClient espClient;
PubSubClient client(espClient);
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
float servoval1, servoval2, servoval3;
float servoval4, servoval5, servoval6;
void setup() {
Serial.begin(9600);
// pinMode(D0, OUTPUT);
// pinMode(D1, OUTPUT);
// pinMode(D2, OUTPUT);
// pinMode(D3, OUTPUT);
// pinMode(D4, OUTPUT);
// pinMode(D5, OUTPUT);
//
// servo1.attach(D0);
// servo2.attach(D1);
// servo3.attach(D2);
// servo4.attach(D3);
// servo5.attach(D4);
// servo6.attach(D5);
Serial.println();
// begin Wifi connect
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//end Wifi connect
client.setServer(mqtt_server, 1883);
client.setCallback(mqttCallback);
}
void topicsSubscribe() {
client.subscribe("receive1");
client.subscribe("receive2");
client.subscribe("receive3");
client.subscribe("receive4");
client.subscribe("receive5");
client.subscribe("receive6");
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
String strTopic = String((char*)topic);
if (strTopic == "receive1") {
char buffer[6];
memcpy(buffer, payload, length);
buffer[length] = '\0';
servoval1 = String((char*)payload).toFloat();
Serial.print("1. ");
Serial.println(servoval1);
servo1.write(servoval1);
}
if (strTopic == "receive2") {
char buffer[6];
memcpy(buffer, payload, length);
buffer[length] = '\0';
servoval2 = String((char*)payload).toFloat();
Serial.print("2. ");
Serial.println(servoval2);
servo2.write(servoval2);
}
if (strTopic == "receive3") {
char buffer[6];
memcpy(buffer, payload, length);
buffer[length] = '\0';
servoval3 = String((char*)payload).toFloat();
Serial.print("3. ");
Serial.println(servoval3);
servo3.write(servoval3);
}
if (strTopic == "receive4") {
char buffer[6];
memcpy(buffer, payload, length);
buffer[length] = '\0';
servoval4 = String((char*)payload).toFloat();
Serial.print("4. ");
Serial.println(servoval4);
servo4.write(servoval4);
}
if (strTopic == "receive5") {
char buffer[6];
memcpy(buffer, payload, length);
buffer[length] = '\0';
servoval5 = String((char*)payload).toFloat();
Serial.print("5. ");
Serial.println(servoval5);
servo5.write(servoval5);
}
if (strTopic == "receive6") {
char buffer[6];
memcpy(buffer, payload, length);
buffer[length] = '\0';
servoval6 = String((char*)payload).toFloat();
Serial.print("6. ");
Serial.println(servoval6);
servo6.write(servoval6);
}
}
void reconnect() {
// Loop until we're reconnected
int counter = 0;
while (!client.connected()) {
if (counter == 5) {
ESP.restart();
}
counter += 1;
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("growtController")) {
Serial.println("connected");
topicsSubscribe();
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
My publishing code which I am running on an ESP32 is:
#include <EEPROM.h>
#include <WiFi.h>
const char* ssid = "INFOLEX";
const char* password = "infofuture";
TaskHandle_t Task1;
int av;
byte test = 55;
float aux1 = 2000;
float aux2 = 1000;
float aux3 = 1500;
float dual1 = 1000;
float dual2 = 2000;
float channel_1, channel_2, channel_3, channel_4, channel_5, channel_6, channel_7, servo1, servo2, servo3, servo4;
float get1 = 0;
float get2 = 0;
float get3 = 0;
float get4 = 0;
float value = 1500;
float value1 = 1500;
float value2 = 1500;
float value3 = 1500;
#include <PubSubClient.h>
#define mqtt_server "192.168.1.5"
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9200);
EEPROM.begin(500);
if (EEPROM.read(0) != 55) {
EEPROM.put(100, 1500);
EEPROM.put(200, 1500);
EEPROM.put(300, 1500);
EEPROM.put(400, 1500);
EEPROM.write(0, test);
}
pinMode(36, INPUT);
pinMode(39, INPUT);
pinMode(34, INPUT);
pinMode(35, INPUT);
pinMode(32, INPUT);
pinMode(21, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
//.println();
//.print("Connecting to ");
//.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
//.print(".");
}
//.println("");
//.println("WiFi connected");
//.println("IP address: ");
//.println(WiFi.localIP());
client.setServer(mqtt_server, 1883);
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
}
void Task1code( void * pvParameters ){
for(;;){
if (!client.connected()) {
reconnect();
}
EEPROM.get(100, get1);
EEPROM.get(200, get2);
EEPROM.get(300, get3);
EEPROM.get(400, get4);
if (get1 > 2200 || get1 < 950) {
get1 = 1500;
EEPROM.put(100, get1);
}
if (get2 > 2200 || get2 < 950) {
get2 = 1500;
EEPROM.put(200, get2);
}
if (get3 > 2200 || get3 < 950) {
get3 = 1500;
EEPROM.put(300, get3);
}
if (get4 > 2200 || get4 < 950) {
get4 = 1500;
EEPROM.put(400, get4);
}
if (av > 2900 && av < 3080) {
value = value + 1;
EEPROM.put(100, value);
}
if (av > 2645 && av < 2845) {
value = value - 1;
EEPROM.put(100, value);
}
if (av > 2261 && av < 2561) {
value1 = value1 + 1;
EEPROM.put(200, value1);
}
if (av > 1800 && av < 2200) {
value1 = value1 - 1;
EEPROM.put(200, value1);
}
if (av > 1480 && av < 1700) {
value2 = value2 + 1;
EEPROM.put(300, value2);
}
if (av > 1107 && av < 1307) {
value2 = value2 - 1;
EEPROM.put(300, value2);
}
if (av > 652 && av < 852) {
value3 = value3 + 1;
EEPROM.put(400, value3);
}
if (av > 105 && av < 305) {
value3 = value3 - 1;
EEPROM.put(400, value3);
}
EEPROM.commit();
}
}
void getValues() {
av = analogRead(32);
channel_1 = analogRead(36);
servo1 = map(channel_1, 336, 3450, 1000, 2000);
servo1 = servo1 + get1 - 1500;
servo1 = constrain(servo1, 1000, 2000);
channel_2 = analogRead(39);
servo2 = map(channel_2, 3281, 640, 1000, 2000);
servo2 = servo2 + get2 - 1500;
servo2 = constrain(servo2, 1000, 2000);
channel_3 = analogRead(34);
servo3 = map(channel_3, 3135, 704, 1000, 2000);
servo3 = servo3 + get3 - 1500;
servo3 = constrain(servo3, 1000, 2000);
channel_4 = analogRead(35);
servo4 = map(channel_4, 432, 3830, 2000, 1000);
servo4 = servo4 + get4 - 1500;
servo4 = constrain(servo4, 1000, 2000);
channel_5 = digitalRead(21);
channel_6 = digitalRead(19);
channel_7 = digitalRead(23);
}
void reconnect() {
int counter = 0;
while (!client.connected()) {
if (counter == 5) {
ESP.restart();
}
counter += 1;
//.print("Attempting MQTT connection...");
if (client.connect("growTentControl")) {
//.println("connected");
} else {
//.print("failed, rc=");
//.print(client.state());
//.println(" try again in 5 seconds");
}
}
}
void loop() {
getValues();
client.publish("bridge1", String(servo1, 2).c_str());
//(50);
//.print("1. ");
//.println(servo1);
//.print("_____________EE1::");
//.println(get1);
client.publish("bridge2", String(servo2, 2).c_str());
//(50);
//.print("2. ");
//.println(servo2);
//.print("_____________EE2::");
//.println(get2);
client.publish("bridge3", String(servo3, 2).c_str());
//(50);
//.print("3. ");
//.println(servo3);
//.print("_____________EE3::");
//.println(get3);
client.publish("bridge4", String(servo4, 2).c_str());
//(50);
;
//.print("4. ");
//.println(servo4);
//.print("_____________EE4::");
//.println(get4);
if ((channel_5 == LOW) && (channel_6 == HIGH)) {
client.publish("bridge5", String(aux1, 2).c_str());
//(50);
//.print("5. ");
//.println("2000");
} else if ((channel_5 == HIGH) && (channel_6 == LOW)) {
client.publish("bridge5", String(aux2, 2).c_str());
//(50);
//.print("5. ");
//.println("1000");
} else {
client.publish("bridge5", String(aux3, 2).c_str());
//(50);
//.print("5. ");
//.println("1500");
}
if (channel_7 == LOW) {
client.publish("bridge6", String(dual1, 2).c_str());
//(50);
//.print("6. ");
//.println("2000");
} else {
client.publish("bridge6", String(dual2, 2).c_str());
//(50);
//.print("6. ");
//.println("1000");
}
//.print("_______________________________________________");
//.println(av);
client.loop();
}