I’ve just started with my first openhab 2 implementation and have started with a PIR sensor using MQTT and running on a raspberry PI.
I’ve followed the instructions here and tweaked the code to use a Particle Photon as I had one to hand.
I’ve got the Photon publishing out to MQTT and can see the messages coming through, see my code below:
(Please note there will be a lot of references to a door, this is just from me adapting the code from a door sensor to a PIR sensor)
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
#include <Debounce.h>
/* MQTT Settings */
// Topic which listens for commands
char* outTopic = "MK-SmartHouse/security/MK-DoorSensor1";
//MQTT Server IP Address
byte server[] = { 192,168,1,55 };
//Unique device ID
const char* mqttDeviceID = "MK-SmartHouseDevice1";
//MQTT Client
MQTT client(server, 1883, callback);
// receive message nothing to do here - we're only sending
void callback(char* topic, byte* payload, unsigned int length) {}
Debounce motioninput;
void setup() {
Serial.begin(9600);
client.connect(mqttDeviceID);
motioninput.attach(A0, INPUT);
motioninput.interval(100); // interval in ms
}
bool resetSensor;
void loop() {
motioninput.update();
while (!client.isConnected()) {
client.connect(mqttDeviceID);
delay(100);
}
if (motioninput.read() == HIGH) {
client.publish(outTopic, "OPEN");
if (Particle.connected()) {
Particle.publish(outTopic, "HALLWAY 1", PRIVATE, WITH_ACK);
}
RGB.control(true);
while (motioninput.read() == HIGH) {
RGB.color(0,0,0);
delay(500);
RGB.color(255,0,0);
delay(100);
Particle.process();
motioninput.update();
} // hang tight here until motion stops
RGB.control(false);
delay(5000);
// Allow next close event
resetSensor = true;
}
else {
if (resetSensor) {
client.publish(outTopic, "CLOSE");
delay(5000);
// Stop next close event until it has been opened
resetSensor = false;
}
}
}
The sensor is connecting and I can see the contact and switch I’ve added in the basicUI, I also get the notifications through when the state goes high or low, again currently this says OPEN or CLOSE at the moment.
The problem I am having though is that the contact in the UI always says OPEN and never says closed, even though I can see the notifications coming through.
These are my rules
//Side Door Sensor Is Open
rule "Side Door Sensor"
when
Item MKDoorSensor1 received update OPEN
then
if(SecuritySystem.state == ON)
{
sendBroadcastNotification("SECURITY SYSTEM: SIDE DOOR OPEN")
}
end
//Side Door Sensor Is Closed
rule "Side Door Sensor Closed"
when
Item MKDoorSensor1 received update CLOSE
then
if(SecuritySystem.state == ON)
{
sendBroadcastNotification("SECURITY SYSTEM: SIDE DOOR CLOSED")
}
end
These are my items
Switch SecuritySystem "Security System"
Contact MKDoorSensor1 "Side Door [%s]" <door> { mqtt="<[broker:MK-SmartHouse/security/MK-DoorSensor1:state:default]" }
And this is my sitemap
sitemap home label="MK-SmartHouse"
{
Frame label="Security"
{
Switch item=SecuritySystem
Text item=MKDoorSensor1
}
}
Any help would be much appreciated