I’m searching for a working arduino sketch to use a HC-SR501/505 PIR sensor on an esp8266 with MQTT and can handle to login with usr/pass to my MQTT broker.
If someone can share, it would be amazing thanks
I use the following code … for the PIR sensor, if reads GPIO 14 on a SonOff(which is ESP8266 based) which is connected to the PIR, as for the MQTT just use and modify the example code, it’s the same for most use cases. A good example would be KmanOzs SonOff code on this forum (https://github.com/KmanOz/Sonoff-HomeAssistant)
void pir_readPIR()
{
char log[LOGSZ];
pirval = digitalRead(PIR_PIN); // read input value
if (pirval == HIGH)
{ // check if the input is HIGH
if (pirState == LOW)
{
digitalWrite(LED, LOW); // turn LED ON
// we have just turned on
addLog("Motion detected!");
digitalWrite(RELAY,HIGH); // turn relay on
sprintf(log, "on");
addLog(log);
// Publish the Relay state to HA
mqttClient.publish(MQTT::Publish(PUB_PREFIX"/"MQTT_TOPIC,log).set_retain().set_qos(1));
ESP.wdtFeed();
// We only want to turn on light on the output change, not state
pirState = HIGH;
}
}
else
{
if (pirState == HIGH)
{
digitalWrite(LED, HIGH); // turn LED OFF
// we have just turned off
addLog("Motion detected Ended!");
digitalWrite(RELAY,LOW); // turn relay off
sprintf(log, "off");
addLog(log);
// Publish the Relay state to HA
mqttClient.publish(MQTT::Publish(PUB_PREFIX"/"MQTT_TOPIC,log).set_retain().set_qos(1));
ESP.wdtFeed();
// We only want to turn on the light on the output change, not state
pirState = LOW;
}
}
}
My sketch is the SonOff sketch but modified to call the function above, so download that and check it out. All I did was add a pinout as an input and a call to the above function.
I have created a new automation.yaml file and placed it in the same directory as my configuration.yaml file. I have copied the automation code and changed some bits as shown below:
I would try subscribing to mydevices/irtransmitter/sender/NEC/# in a putty terrminal to see what if anything is being sent out to your transmitter. Also what does HA say in the log file, is it seeing the controlon payload you can also subscribe to sensor/motion1 to check that it’s being sent out to HA.
I’m not familiar with your lights, but the payload for “on” and “off” in your config are both the same, all mine have a different payload for each command.
@bachoo786 First make sure to use an ESP12 based board it doesn’t worked for me on any of my ESP01 maybe because of the type of the input.(pull up?)
Edit: I’ve tried with the github link you’ve posted before and it works well.
I think you should proceed step by step and make sure you can first receive the MQTT message from your device (esp).
You can track it using the “MQTT Dashboard app” on Android https://play.google.com/store/apps/details?id=com.thn.iotmqttdashboard&hl=fr or another one like for IOS or even a chrome plugin.
First configure your Mqtt server ip, login, password into the app then add you device with a friendly name and the topic you have to subscribe to like this:
Once its done and you are sure that it’s ok add this to your sensor.yaml file or in configuration.yaml with sensor before the code.the restart “Home Assistant”
@keithh666 I have subscribed to my topic as set on my nodemcu and motion is been detected but HA doesn’t detect anything regarding payload.
@wkdwill I have am now using the bedroom lights which have different payloads for on and off but this does not matter as for the motion you need to subscribe to the topic set on the nodemcu. It all matters in the automation.yaml which in my case is as follows:
@Kareem I do not need to monitor the state of my sensor as I am subscribing to it via mqtt and can confirm the payloads are being read when motion is detected. My issue is with the automation because when motion is detected ‘controlOn’ payload is published by no “action” is taking place as per the automation.yaml
@bachoo786 I’ve tried on my side and it work flawlesslly for me. Make sure the service you call for the light is the right one “switch.turn_on” I think that it’s fine, but you don’t need to specify the entity_id : switch.bedroom_lights this is maybe the problem…