Homekit / homebridge

195 views
Skip to first unread message

bluesplat...@gmail.com

unread,
Feb 17, 2021, 1:03:45 AM2/17/21
to Camect User Forum
Hi,

I use homekit/homebridge for all my home automation, including exposing the cameras via the homebridge-camera-ffmpeg plugin. I've been meaning to, for ages now, figure out how I can integrate camect events into homebridge (I like to have all of my notifications via a single mechanism, and of course I can tie it into home automation).  I have toyed with either using the camera's motion detection or motioneye's motion detection... but both are pretty basic, and we all have camect for the awesome noise filtering that it does, so I wanted to use camect.

Well this turned out to be pretty trivial. Firstly, the homebridge-camera-ffmpeg plugin supports getting motion events via MQTT (https://sunoo.github.io/homebridge-camera-ffmpeg/automation/mqtt.html). I already run an MQTT server for various other home automation devices, so that seemed like the easiest path to go, although it should be noted that you could also use a HTTP based approach.

So, I needed a way of getting events from camect into MQTT... a little bit of python hacking later and we have the following eye-sore attached below. It isn't particularly pretty, but 42 lines of code later, I get events via homekit! (Yes it needs a little work, for example it treats any event as motion, which may not be accurate, and the whole sleep() thing is horrid).

One really nice thing about this is I have multiple cameras in some areas, and homekit is smart enough to only send a single notification about motion in one area, regardless of how many cameras are pointed at it.

Thanks to the camect guys for providing camect-py, and hopefully this helps someone out that is thinking of doing something similar.

Cheers

Nick

import camect
import time
import paho.mqtt.client as mqtt


camect_host="your_ip_here:443"
camect_passwd="xxxxxx"
camect_user="admin"

mqtt_host="your_ip_here"
mqtt_port=1883
mqtt_user=None
mqtt_passwd=None
mqtt_topic="homebridge"

def post_event_to_mqtt(client, evt):
    client.publish(mqtt_topic+"/motion", evt["cam_name"])
    print(evt)

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("$SYS/#")

# Connect to mqtt
client = mqtt.Client()
client.on_connect = on_connect
client.loop_start()

client.connect(mqtt_host, mqtt_port, 60)

home = camect.Home(camect_host, camect_user, camect_passwd)
home.add_event_listener(lambda evt: post_event_to_mqtt(client,evt))

progress=1
while True:
    time.sleep(10)
    print('\r{}% done...'.format(progress), end='', flush=True)
    progress+=1



Brian Wilson

unread,
Feb 17, 2021, 8:59:56 AM2/17/21
to Camect User Forum, bluesplat...@gmail.com
Can you share how you have your cameras configured in homebridge-camera-ffmpeg? I'd like to make them available via Homekit as an easier interface for technically challenged family members. 

bluesplat...@gmail.com

unread,
Feb 18, 2021, 4:15:57 AM2/18/21
to Camect User Forum, Brian Wilson, fo...@camect.com
I'm not sure how useful my camera config is, as it will very much depend on the specific camera you have and what methods it has of exposing video and snapshots. To make things more complicated, I don't actually connect directly to my cameras using homebridge, because I found that one of them just didn't work if I tried to stream too many streams concurrently... so I use motioneye to expose the cameras to homebridge (and I was also playing around with motioneye as an alternative solution anyway).

This is all unnecessarily complex, and if your camera happily streams to multiple sources without issue, should be avoided.

A single camera config looks like this:
               {
                    "name": "Rear Side",
                    "manufacturer": "Dahua",
                    "motion": true,
                    "motionTimeout": 30,
                    "model": "IPC-HFW4831E-SE",
                    "videoConfig": {
                        "source": "-f mjpeg -i http://xxxxxxx.xxxxxx:8085",
                        "stillImageSource": "-i http:// xxxxxxx . xxxxxx /picture/5/current/",
                        "maxStreams": 2,
                        "maxWidth": 704,
                        "maxHeight": 576,
                        "maxFPS": 10,
                        "maxBitrate": 300,
                        "vcodec": "libx264",
                        "packetSize": 1316,
                        "audio": false,
                        "debug": true
                    }
                }
You will need to customise for your setup, and the source should point directly to your camera stream, not the whacky setup I have (also note that stillImageSource is not required, it will grab a frame from source if necessary). I would suggest using a lower bitrate/resolution stream, it all seemed a bit slow with 4k video (which is wasted on your phone anyway!).

Camect does not provide "motion end" events, so the motionTimeout is needed. I think it provides a nicer interface than telegram, although you don't get event classification, just a motion event (which is fine for me).

Hopefully this is useful,
Reply all
Reply to author
Forward
0 new messages