I’ve setup a simple magnetic door sensor via MQTT and using a NodeMCU board but for some reason my sketch will not update the MQTT Broker as the magnetic switch changes. The NodeMCU lights up with each change so I know the actual physical device is working.
Anyone keen to have a quick look through my sketch and tell me where it is amiss? It compiles and loads alright but I’m probably staring at the issue and keep overlooking it.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
// Wifi: SSID and password
const PROGMEM char* WIFI_SSID = "******";
const PROGMEM char* WIFI_PASSWORD = "*******";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "lounge_door";
const PROGMEM char* MQTT_SERVER_IP = "192.168.1.***";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "*******";
const PROGMEM char* MQTT_PASSWORD = "******";
// MQTT: topic
const PROGMEM char* MQTT_DOOR_STATUS_TOPIC = "lounge/door/status";
// default payload
const PROGMEM char* OPENING_ON = "OPEN";
const PROGMEM char* OPENING_OFF = "CLOSED";
// DOOR : D1/GPIO5
const PROGMEM uint8_t DOOR_PIN = 5;
uint8_t m_door_state = LOW; // door closed
uint8_t m_door_value = 0;
// macros for debugging
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to publish the state of the door sensor
void publishDoorSensorState() {
if (m_door_state) {
client.publish(MQTT_DOOR_STATUS_TOPIC, OPENING_OFF, true);
} else {
client.publish(MQTT_DOOR_STATUS_TOPIC, OPENING_ON, true);
}
}
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("INFO: connected");
} 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 WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: 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("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();
// read the DOOR sensor
m_door_value = digitalRead(DOOR_PIN);
if (m_door_value == HIGH) {
if (m_door_state == LOW) {
// door is opened
Serial.println("INFO: door opened");
publishDoorSensorState();
m_door_state = HIGH;
}
} else {
if (m_door_state == HIGH) {
publishDoorSensorState();
Serial.println("INFO: Door closed");
m_door_state = LOW;
}
}
}