Hi all,
I am having an issue getting HA to send a mqtt payload to a nodemcu.
The mcu is connecting to Mosquito but I not command it turn the Led on or off. I tried keeping simple to start with.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
int lightPin = 2;
// Wifi: SSID and password
const char* WIFI_SSID = "BT**********";
const char* WIFI_PASSWORD = "R**************";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "stepper";
const PROGMEM char* MQTT_SERVER_IP = "192*********";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "****";
const PROGMEM char* MQTT_PASSWORD = "*****";
// MQTT: topics
const PROGMEM char* MQTT_SWITCH_STATUS_TOPIC = "stepper/switch1/stat";
const PROGMEM char* MQTT_SWITCH_COMMAND_TOPIC = "stepper/switch1/cmd";
// default payload
const PROGMEM char* SWITCH_ON = "ON";
const PROGMEM char* SWITCH_OFF = "OFF";
// store the state of the switch
boolean m_switch_state = false;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to publish the state of the switch (on/off)
void publishSwitchState() {
if (m_switch_state) {
client.publish(MQTT_SWITCH_STATUS_TOPIC, SWITCH_ON, true);
} else {
client.publish(MQTT_SWITCH_STATUS_TOPIC, SWITCH_OFF, true);
}
}
// 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_SWITCH_COMMAND_TOPIC).equals(p_topic)) {
// test if the payload is equal to "ON" or "OFF"
if (payload.equals(String(SWITCH_ON))) {
if (m_switch_state != true) {
m_switch_state = true;
publishSwitchState();
digitalWrite(lightPin, LOW);
}
} else if (payload.equals(String(SWITCH_OFF))) {
if (m_switch_state != false) {
m_switch_state = false;
publishSwitchState();
digitalWrite(lightPin, HIGH);
}
}
}
}
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...
// publish the initial values
publishSwitchState();
// ... and resubscribe
client.subscribe(MQTT_SWITCH_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);
digitalWrite(lightPin, HIGH);
// 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.println("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();
}
client.loop();
}
The above code is what I have on the NodeMCU
and below the switch in my config. yaml
- platform: mqtt
name: "stepper"
command_topic: "stepper/switch1"
state_topic: "stepper/switch1"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
I’m sure Its something to do with the Topic but I can’t work out what.
Any help would be greatly appreciated