xoseperez / espurna

Home automation firmware for ESP8266-based devices
http://tinkerman.cat
GNU General Public License v3.0
2.98k stars 635 forks source link

HC-SR501 PIR MOTION DETECTOR #503

Closed MariusLaurinaitis closed 6 years ago

MariusLaurinaitis commented 6 years ago

Hello Xose, You are growing fast! It would be very cool to add PIR sensor (HC-SR501) support too like [https://www.mpja.com/download/31227sc.pdf] It would be possible to see status through mqtt. Then one Wemos D1 or NodeMCU could work with a whole set of sensors.

And if we could even plug an OLED screen (popular monochrome 128x64, SPI SD1306 driver or SPI SH1106 driver) to output mqtt values :)

Best regards Marius

gretel commented 6 years ago

@MariusLaurinaitis have four sonoff basic running with HC-SR501 connected to GPIO.

the according part of hardware.h looks like this:

    #define BUTTON2_PIN         14
    #define BUTTON2_RELAY       0
    #define BUTTON2_MODE        BUTTON_PUSHBUTTON
    #define BUTTON2_PRESS       BUTTON_MODE_ON
    #define BUTTON2_CLICK       BUTTON_MODE_OFF
    #define BUTTON2_DBLCLICK    BUTTON_MODE_NONE
    #define BUTTON2_LNGCLICK    BUTTON_MODE_NONE
    #define BUTTON2_LNGLNGCLICK BUTTON_MODE_NONE

works fine and gets published via mqtt.

@xoseperez btw - i'd love to have a way to get this discovered by home-assistant

MariusLaurinaitis commented 6 years ago

@gretel thank you very much! @xoseperez btw can be included as a sensor?

xoseperez commented 6 years ago

You have the digital sensor that does just that: report the status of a digital GPIO.

cookier0218 commented 6 years ago

Hi Xose, I wanna use HC-SR501 connected to GPIO 3 of sonoff basic. The part of setting hardware.h is

#define MANUFACTURER        "ITEAD"
#define DEVICE              "SONOFF_BASIC"

// Buttons
#define BUTTON1_PIN         0
#define BUTTON1_MODE        BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON1_RELAY       1
#define BUTTON2_PIN         14
#define BUTTON2_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH
#define BUTTON2_RELAY       1
#define BUTTON3_PIN         3
#define BUTTON3_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH
#define BUTTON3_RELAY       2
// Relays
#define RELAY1_PIN          12
#define RELAY1_TYPE         RELAY_TYPE_NORMAL
#define RELAY2_PIN          4
#define RELAY2_TYPE         RELAY_TYPE_NORMAL

But it can't work, is there any other setting or problem with my settings? regards, cookier0218

xoseperez commented 6 years ago

GPIO3 is RX in the Sonoff Basic, you are probably producing noise there. Why don't you use GPIO14 which is in the header?

xoseperez commented 6 years ago

Closing this since there has been no feedback in 3 months. Feel free to reopen if necessary.

nqdanyb commented 6 years ago

As I think, it is possible to add a PIR sensor (HC-SR501) as a sensor like the DHT sensor in arduino.h so that ESPURNA can display the state of the PIR sensor on the WEB interface and publish. via MQTT. I tried attaching a PIR sensor to the GPIO pin that supports DIGITAL_SUPPORT and enabling the "#define DIGITAL_SUPPORT" sensor in the SONOFF_BASIC platform. Then the data from the PIR sensor is sent to the home-assistant, which then activates the light when the sensor senses motion. But the high latency caused by the time from detecting motion to sending home-assistant information took more than 6 seconds. What can I do to reduce this latency? Here are some screenshots: screenshot 2018-07-13 00 17 07 screenshot 2018-07-13 00 17 16

xoseperez commented 6 years ago

The EventSensor now supports events. Check fba8a9f. Setting EVENTS_TRIGGER to 1 (default) it will report events immediately via MQTT, Domoticz, Thingspeak,...

nqdanyb commented 5 years ago

Thanks @xoseperez. I tried the latest version of the espurna-sensors, but the delivery time of the PIR status (HC-SR501) from detecting motion to the Home Assistant through the MQTT is still very slow. I then modified the sensors.h file as shown below: screenshot 2018-07-17 23 43 07 After correction, the time to send the PIR status (HC-SR501) from the motion detector to the Home Assistant through the MQTT has been improved (about 1 second)

gretel commented 5 years ago

@nqdanyb my "old" approach works without any noticeable latency.

nqdanyb commented 5 years ago

Thanks @gretel . I have edited the file hardware.h as your guide. It is true that the PIR state (HC-SR501) reads very fast. But I do not know how to send it via MQTT to Home Assistant screenshot 2018-07-18 23 29 54

xoseperez commented 5 years ago

There is no support to send button event to HA at the moment. You have to configure the events in the EventSensor config section setting EVENTS_TRIGGER to 1.