Hi, I am in need of a library to publish a message to Mosquitto with QoS 1 from a ESP8266. I am currently using the pubsub library which is doing fine but it has one limitation as described below.
I am working on a door sensor in which the ESP8266 powers via a ATiny by pulling CH_PD HIGH. It then publishes the message and powers itself down by pulling CH_PD LOW. I am using the pubsub library to publish the message but the issue is that it seems to be working in a async QoS 0 mode to publish the message. Most of the times, the ESP powers down before the message has been published. To avoid this I put in a delay of 1 sec and it works fine but I need a better solution. Also because this sensor will be used in a security alarm it must guarantee that the message has been published which I think can happen if QoS 1 is implemented. I have seen many discussions on QoS >0 for pubsub library but none of them is for publishing , all of them talk about subscribing.
Can somebody suggest a way out here or point me to a library which can do this. Or maybe something else I can do to guarantee the publishing of the message?
Thanks
My code below:
/*
/*
* 2.0 - Initial -
*
*/
#include "DebugMacros.h"
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
ADC_MODE(ADC_VCC);
const char* ssid = "SSID";
const char* password = "PSWD";
const char* mqtt_server = "MQTT_SERVER";
const int mqtt_port = 1883;
const char *mqtt_user = "mqtt_user";
const char *mqtt_pass = "pswd";
const char *mqtt_client_name = "Main_Door_sensor"; // Client connections cant have the same connection name
const char *mqtt_topic = "home/main_door/status";
const char *mqtt__vcc_topic = "home/main_door/vcc";
IPAddress ip(192, 168, 1, 50);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiClient espClient;
PubSubClient client(espClient);
// Variables
//Hold pin will hold CH_PD HIGH till we're executing the setup, the last step would be set it LOW which will power down the ESP
int HOLD_PIN = 0; // defines GPIO 0 as the hold pin (will hold CH_PD high untill we power down).
void setup()
{
#ifdef DEBUG
Serial.begin(115200);
#endif
pinMode(HOLD_PIN, OUTPUT); // sets GPIO 0 to output
digitalWrite(HOLD_PIN, HIGH); // sets GPIO 0 to high (this holds CH_PD high even if the PIR output goes low)
DPRINTLN(millis());
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
publishMessage();
DPRINTLN(millis());
DPRINTLN("powering down");
//Allow a delay to let MQTT publish the message as the publish method is asyncronous, If I dont put this, the ESP powers down before the msg is published
delay(1000);
digitalWrite(HOLD_PIN, LOW); // set GPIO 0 low this takes CH_PD & powers down the ESP
}
void setup_wifi()
{
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(50);
}
DPRINT("WiFi connected, IP Address:");
DPRINTLN(WiFi.localIP());
}
void publishMessage() {
// Loop until we're reconnected
char i = 0;
while (!client.connected())
{
i++;
DPRINT("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_client_name,mqtt_user,mqtt_pass)) {
DPRINTLN("connected");
// Once connected, publish an announcement...
client.publish(mqtt_topic, "OPEN");
DPRINTLN("published OPEN");
//measure batery voltage and publish that too
int battery_Voltage = ESP.getVcc();
char batt_volt[6];
itoa(battery_Voltage, batt_volt, 10);
client.publish(mqtt__vcc_topic, batt_volt);
DPRINTLN("published VCC");
}
else
{
DPRINT("failed, rc=");
DPRINT(client.state());
DPRINTLN(" try again in 2 seconds");
delay(1000);
}
if(i >=3)
break;
}
}
void loop()
{
//Nothing to do here as the setup does all the work and then powers down the ESP by writing a LOW signal to CH_PD
}