Could you please help me and how to set a retain flag with pubsubclient library? and QoS? in Arduino sketch. Thanks in advance
Thank you for your Kindly answer @gdsports, but I was looking for this official docs and still no understand how to set this flag on Arduino sketch...
My code is:
"client.publish(MQTT_DOOR_STATE_TOPIC, DOOR_OPEN, true);"
and Im using MQTT Box on Mac OS but it still saying retain flag is false
could you please give an example about how to set it up properly?
Thanks Again
almarcano:
Thank you for your Kindly answer @gdsports, but I was looking for this official docs and still no understand how to set this flag on Arduino sketch...My code is:
"client.publish(MQTT_DOOR_STATE_TOPIC, DOOR_OPEN, true);"
and Im using MQTT Box on Mac OS but it still saying retain flag is false
could you please give an example about how to set it up properly?
Thanks Again
How do you know if the value of the flag? I don't see in the API you can get that value.
Thank you for answer @arduino_new, I check the value of the flag in MQTT Box on Mac OS and can't get the value to true...
I need an example how to use the retain and QoS flags on message for a door sensor on my home assistant sever, because when the server restart it miss the status of sensor and it becomes to unknown status
Have you looked at this method from the link above?
boolean connect (clientID, willTopic, willQoS, willRetain, willMessage)
Connects the client with a Will message specified.
Parameters
clientID : the client ID to use when connecting to the server.
willTopic : the topic to be used by the will message (const char[])
willQoS : the quality of service to be used by the will message (int : 0,1 or 2)
willRetain : whether the will should be published with the retain flag (boolean)
willMessage : the payload of the will message (const char[])
Returns
false - connection failed.
true - connection succeeded.
I am just guessing here since I don't see your code and I don't know what you want to achieve.
Thanks, ok my intention is to control a light and report a door status, here is the code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
// *** Set Door Pin
#define DOOR_PIN 4
// Wifi: SSID and password
const char* WIFI_SSID = "SSID";
const char* WIFI_PASSWORD = "PASSWORD";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "office_light";
const PROGMEM char* MQTT_SERVER_IP = "IP";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "USER";
const PROGMEM char* MQTT_PASSWORD = "PASSWORD";
// MQTT: topics
// Light topic
const char* MQTT_LIGHT_STATE_TOPIC = "office/light/status";
const char* MQTT_LIGHT_COMMAND_TOPIC = "office/light/action";
// *** Door topic
const char* MQTT_DOOR_STATE_TOPIC = "maindoor/status";
// payloads by default (on/off)
const char* LIGHT_ON = "ON";
const char* LIGHT_OFF = "OFF";
// *** Door payloads
const char* DOOR_CLOSED = "HIGH";
const char* DOOR_OPEN = "LOW";
const PROGMEM uint8_t LED_PIN = 5;
boolean m_light_state = false; // light is turned off by default
// *** Door Settings
boolean m_door_state = false; // door is turned off by default
int door_lastStatusValue = 2;
unsigned long door_lastSwitchTime = 0;
int debounceTime = 2000;
char* door_state = "UNDEFINED";
char* last_state = "";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to publish the state of the light (on/off)
void publishLightState() {
if (m_light_state) {
client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
} else {
client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
}
}
// *** function called to publish the state of door (LOW/HIGH)
void publishDoorState() {
if (m_door_state) {
client.publish(MQTT_DOOR_STATE_TOPIC, DOOR_CLOSED, true);
} else {
client.publish(MQTT_DOOR_STATE_TOPIC, DOOR_OPEN, true);
}
}
// function called to turn on/off the light
void setLightState() {
if (m_light_state) {
digitalWrite(LED_PIN, LOW);
Serial.println("INFO: Turn light on...");
} else {
digitalWrite(LED_PIN, HIGH);
Serial.println("INFO: Turn light off...");
}
}
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
// concat the payload into a string
String payload;
for (uint8_t i = 0; i < p_length; i++) {
payload.concat((char)p_payload[i]);
}
// handle message topic
if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
// test if the payload is equal to "ON" or "OFF"
if (payload.equals(String(LIGHT_ON))) {
if (m_light_state != true) {
m_light_state = true;
setLightState();
publishLightState();
}
} else if (payload.equals(String(LIGHT_OFF))) {
if (m_light_state != false) {
m_light_state = false;
setLightState();
publishLightState();
}
}
}
}
// *** Check Door State
void checkDoorState() {
//Checks if the door state has changed, and MQTT pub the change
last_state = door_state; //get previous state of door
if (digitalRead(DOOR_PIN) == HIGH) // get new state of door
door_state = "Open";
else if (digitalRead(DOOR_PIN) == LOW)
door_state = "Closed";
if (last_state != door_state) { // if the state has changed then publish the change
client.publish(MQTT_DOOR_STATE_TOPIC, door_state, true);
Serial.println(door_state);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("INFO: connected");
// Once connected, publish an announcement...
publishLightState();
// ... and resubscribe
client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
} else {
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// init the serial
Serial.begin(115200);
// init the led
pinMode(LED_PIN, OUTPUT);
analogWriteRange(255);
setLightState();
// init Door Sensor
pinMode(DOOR_PIN, INPUT_PULLUP);
door_lastStatusValue = digitalRead(DOOR_PIN);
// init the WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("INFO: WiFi connected");
Serial.print("INFO: IP address: ");
Serial.println(WiFi.localIP());
// init the MQTT connection
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
checkDoorState();
client.loop();
}