#include <WiFi.h>
#include <PubSubClient.h>
const char *ssid = "mySSID";
const char *password = "myPassword";
const char *mqttbroker = "broker.hivemq.com";
const char *clientID = "blk5#05-100";
int portno = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
int light = 0; // Lighting: 0 = OFF; 1 = ON
int prese = 0; // Presence: 0 = ABSENT; 1 = PRESENT
void callback(const char *topic, byte* payload, unsigned int length) {
// No subscription handling needed
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
client.setServer(mqttbroker, portno);
client.setCallback(callback);
while (!client.connected()) {
if (client.connect(clientID)) {
Serial.println("Connected to MQTT broker");
} else {
delay(1000);
}
}
// Assume GPIOs for light and prese are configured elsewhere
}
void loop() {
client.loop();
// Simulated sensor data updates (replace with actual digitalRead in real case)
light = digitalRead(12); // e.g., D12 for light status
prese = digitalRead(13); // e.g., D13 for presence status
// Publish light status
client.publish("estate/blk5/flr05/#05-100/living/light", String(light).c_str());
// Publish presence status
client.publish("estate/blk5/flr05/#05-100/living/prese", String(prese).c_str());
delay(2000); // Publish every 2 seconds
}