Need help adding retain command to MQTT Arduino switch code

Any one help me out.

#include <Servo.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiAP.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiSTA.h>
#include <ESP8266WiFiType.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>

#include <PubSubClient.h>

/************ WIFI and MQTT INFORMATION (CHANGE THESE FOR YOUR SETUP) ******************/
#define wifi_ssid “wirelessNAME” //enter your WIFI SSID
#define wifi_password “XXXXXXX” //enter your WIFI Password

#define mqtt_server “192.168.0.22” // Enter your MQTT server adderss or IP. I use my DuckDNS adddress (yourname.duckdns.org) in this field
#define mqtt_user “homeassistant” //enter your MQTT username
#define mqtt_password “Monkey55” //enter your password

WiFiClient espClient;
PubSubClient client(espClient);

Servo myservo;

int val;
int itsatrap = 0;

void setup() {
pinMode(2, OUTPUT);
myservo.write(0);

Serial.begin(115200);

setup_wifi();

client.setServer(mqtt_server, 1883); //CHANGE PORT HERE IF NEEDED
client.setCallback(callback);

}

void setup_wifi() {

delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}

Serial.println(“”);
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
String mytopic(topic);
if (itsatrap == 0 && mytopic == “home/kitchen/coffee_payload” && message.equals(“ON”)){
myservo.attach(D4);
delay(500);
myservo.write(58);
client.publish(“home/kitchen/coffee_state”, “ON”);
delay(500);
myservo.detach();
}
else if (mytopic == “home/kitchen/coffee_payload” && message.equalsIgnoreCase(“OFF”)){
myservo.attach(D4);
delay(500);
myservo.write(0);
client.publish(“home/kitchen/coffee_state”, “OFF”);
delay(1000);
myservo.detach();
}

else{
    itsatrap = 0;
}

}

void loop() {

{

if (!client.connected()) {
reconnect();
}
client.loop();
}
}

void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Attempt to connect
if (client.connect(“ESPBlindstl”, mqtt_user, mqtt_password)) {
Serial.println(“connected”);

  client.subscribe("home/kitchen/coffee_payload");
  client.subscribe("home/kitchen/coffee_brightness");
  client.publish("home/kitchen/coffee_state", "OFF");
} else {
  Serial.print("failed, rc=");
  Serial.print(client.state());
  Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
  delay(5000);
}

}
}

This isn’t really an ha issue but an Arduino/mqtt one to be honest.
Check this
https://www.google.co.uk/search?q=mqtt+client.publish+retain&oq=mqtt+client.publish+retain

1 Like

thanks for the help, I know this is really nodeMCU/Arduino but thought some brigtht/clear folks from here might be able to help.

I think I just add retain=True like this

client.publish(“home/kitchen/coffee_state”, “ON”,retain=True );

But not 100%

The documentation for the publish method is here Arduino Client for MQTT

You are mixing Python syntax with C syntax, which doesn’t have named parameters.

Try

client.publish(“home/kitchen/coffee_state”, “ON”,true );

Although I can’t remember if true is a valid boolean in the Arduino world, You may have to experiment with that.

1 Like

thanks I will try that later.

Hi @ladaowner,

Also struggling with this.
Did the solution suggested by gpbenton work?

Never bothered to follow this up.

just to confirm @gpbenton is correct, the syntax is:

client.publish("home/kitchen/coffee_state", "ON",true );

I use the same syntact in my arduino programs, I’ve just rewritten the double quotes above to plain ASCII else it’d fail

1 Like

Hi @lolouk44,

I’m having the same issue.
using:
client.publish("ha/voordeur/state", "closed", true);
…does publish my state to hassio but retain flag stays false?
Schermafdruk 2020-04-05 22.31.10

I’m trying to find a solution for a few days now. Can’t find anything usefull online.

Are there other settings to be done in the arduino IDE code? or maybe on the hassio side?

Hope some can help me out. Really need help with this issue.

tnx a lot for any help!