Send http request if alarm [Dahua IPC-HFW5442E-ZE]

Joined
Mar 7, 2022
Messages
13
Reaction score
4
Location
Germany
Hi guys,

I was wondering whether it is possible to automatically send an web request, if a motion was detected in the camera. I only have a LAN cable available (no extra cable for alarm out) and I would like to use the alarm to control my smart home system.

What I found out so far: I can use another machine to perfomr web request TO the camera (http://<ip>/cgi-bin/alarm.cgi?action=getOutStates):

Screenshot 2022-03-07 100302.png

This works pretty nice. But from architecture and network performance, it does not really make sense to "spam" the camera with web requests every second to check the status. It would be much nicer if the camera itself could perform a web request to my server whenever an alarm was detected OR when alram status changes.

Thanks for any thoughts!
 

MyDaHua

Getting the hang of it
Joined
Jul 10, 2020
Messages
91
Reaction score
37
Location
Poland
I don't think it is possible.
You can set camera to send email to your personal email server and from there you can have a script to do what you want more details here:
 
Joined
Mar 7, 2022
Messages
13
Reaction score
4
Location
Germany
Thanks @MyDaHua .
However this is too slow for my use case. For example, I want to switch the light in the driveway on (use it as a motion detector). The mail will take some seconds and the lag will be to large.
 

MyDaHua

Getting the hang of it
Joined
Jul 10, 2020
Messages
91
Reaction score
37
Location
Poland
Sorry but you are wrong.
I use this in my security system exactly like you want and it is almost instant, I use my mqtt & postfix server running on rpi-3.
I use tasmotized devices - sonoff basic R2 subscribed to topic where script publish mqtt message, no HomeAssistant involved any more.
 
Last edited:
Joined
Mar 7, 2022
Messages
13
Reaction score
4
Location
Germany
Okay, I did not expect that at all - thank you, I will give it a try. However a web request would still be my ideal solution :)
 

MyDaHua

Getting the hang of it
Joined
Jul 10, 2020
Messages
91
Reaction score
37
Location
Poland
you can modify the script so it will do the web command to tasmota device if you want so no more mqtt server involved, just postfix.

edit:
This is a sonoff device - web interface:
CCTV and PIR are web switches I use to enable/disable turning on light from PIR (inside home) and CCTV (outside home) detection if I don't need.
I also use independent timer for PIR and CCTV so it will turn light only in night time...

Screenshot_2022-03-07_10-45-25.pngScreenshot_2022-03-07_10-50-06.pngScreenshot_2022-03-07_11-04-17.png
 
Last edited:
Joined
Mar 7, 2022
Messages
13
Reaction score
4
Location
Germany
I checked your Git Repository and it looks like an excellent idea. However, I am not sure how to config postfix. If I understand it right, it will only run as a local mail server. Do you have a manual for that?
 

The Automation Guy

Known around here
Joined
Feb 7, 2019
Messages
1,422
Reaction score
2,827
Location
USA
If you have a NVR or BI in the system, it might be able to use it to send out the desired commands based on the camera's activity. This way you are pushing out the command rather than having to constantly poll the camera's alarm status. It's super easy to do in BI, but I don't know what you might be using in your system.

I also use BI and MQTT this way: The camera senses motion and sends BI a trigger --> BI upon being triggered sends out a MQTT command (you could easily substitute a HTTP command) --> My MQTT server picks up on the command and sends it to the proper device --> the proper device receives the command and acts on it (ie turning on the light). I haven't measured how long this sequence takes, but it is generally a fraction of a second in total start to finish.
 
Joined
Mar 7, 2022
Messages
13
Reaction score
4
Location
Germany
Hi everyone,

unfortunately I do not have a NVR or BI, I just have a NAS to store it on. The logic stays and should stay in my case on the cameras itself.
@MyDaHua I did not manage to get postfix running. However, I found a much easier way if you use python anyways. One script including the mail server, processing the mail and making the API Request.
The Dahua camera did not send the mail right for some reason, but it was okay for me as I just need the trigger and the IP. If anyone reuses it and their camera is sending mails right, use the handle_data function instead of handle_quit for more information:

Python:
import asyncio
import logging
import requests

from aiosmtpd.controller import Controller
from aiosmtpd.handlers import AsyncMessage, Debugging

TOKEN = "abcdefghi"
BASE_URL = "https://192.168.178.86/api/values/"
URL_CAM1 = "url1_cam"
URL_CAM2 = "url2_cam"
IP_CAM1 = '192.168.100.89'
IP_CAM2 = '192.168.100.99'

class MyMessageHandler(AsyncMessage):

    async def handle_QUIT(self, server, session, envelope):
        if session.peer[0] == IP_CAM1:
            url = BASE_URL + URL_CAM1
        elif session.peer[0] == IP_CAM2:
            url = BASE_URL + URL_CAM2

        requests.put(url=url, json={"value": 1}, params={"token": TOKEN}, verify=False)

        print("Ready to Quit..")


    # async def handle_DATA(self, server, session, envelope):
    #     # use this if your camera sends the mail right...
 
async def amain(loop):
    cont = Controller(MyMessageHandler(), hostname='', port=8025)
    cont.start()


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    loop = asyncio.get_event_loop()
    loop.create_task(amain(loop=loop))
    print(".....Starting Job.....")
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
 

MyDaHua

Getting the hang of it
Joined
Jul 10, 2020
Messages
91
Reaction score
37
Location
Poland
what settings are required in camera to be done to use this script ?
 
Joined
Mar 7, 2022
Messages
13
Reaction score
4
Location
Germany
SMTP Server is the IP of my Raspberry, Port as Set in the Python Script (cont = Controller(MyMessageHandler(), hostname='', port=8025).

The Rest is just any dummy data, any mail will be handled by the Raspberry. If your mail works right, you will receive the information in Python and can handle it there if needed.

1646922211247.png
 
Last edited:

cctv-dave

Getting the hang of it
Joined
Mar 25, 2021
Messages
128
Reaction score
87
Location
yes
I was wondering whether it is possible to automatically send an web request,
Not a web request per say you are going to have to play with python to get functionality out of it, but alarm center function will send out a message and you can turn it on/off in most if not all events.
Lack of documentation means your first python program is just going to listen on a port to capture everything and you can then decode it at will.
if you make a library, please share on github !
 

cctv-dave

Getting the hang of it
Joined
Mar 25, 2021
Messages
128
Reaction score
87
Location
yes
Oh I like that, you aren't even bothering to read the email just the act of sending it.
Hmm. You could possibly be even more efficient by just looking for the connection attempt from the IPC and dropping it than going through SMTP motions.
 

MyDaHua

Getting the hang of it
Joined
Jul 10, 2020
Messages
91
Reaction score
37
Location
Poland
One problem for me if email it is not processed properly: camera send emails at event start and at event stop so with this approach it will trigger twice and also you lose all other notification on email: Camera blind, Tamper, SD card problems... but it is a start.
 
Joined
Mar 7, 2022
Messages
13
Reaction score
4
Location
Germany
Yes, you are right. At the moment, it is a disadvantage. Nevertheless I am sure that it can be fixed.
The problem is that the camera performs a quit before transferring the message. I do not know why and I did not find a solution so far to debug into the camera or to see a log/error message. The camera just says "Failed" when I press the test button.

Do you have any idea how to debug and solve the problem?
 

MyDaHua

Getting the hang of it
Joined
Jul 10, 2020
Messages
91
Reaction score
37
Location
Poland
Maybe it is a problem of TLS ?
On my setup with postfix+dovecot I have no problem.
Can you try to send to a gmail account ( you need to enable with less secure... ) to send email and see if there sending email work ?
 
Joined
Mar 7, 2022
Messages
13
Reaction score
4
Location
Germany
Hi again,
thanks @MyDaHua again. It was a problem with TLS which the camera did not accept as it was not implemented by me. I added it and it now works. You can receive the whole message + attributes and process it as you want.
As I have not installed the camera yet and to save some time on debugging myself:
Do you have example messages to handle (e.g. sd card removed, IVS alarm, scene changing, ...). Then I could implement dummys for them as well.
 

MyDaHua

Getting the hang of it
Joined
Jul 10, 2020
Messages
91
Reaction score
37
Location
Poland
I posted some email messages I have on github:

Good job, can't wait to test your version soon !
 
Top