Hacking the XiaomiFang Wifi camera

Good spot.

I just changed it, it’s gotten even more strange now:

GUI Working

192.168.0.100 - - [16/Apr/2018:22:30:24 +0000] "GET /cgi-bin/action.cgi?cmd=motor_down&val=100 HTTP/1.1" 200 168 "http://192.168.0.112/cgi-bin/status.cgi" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
auth_check_userpass(Root,ismart12,...);

Home Assistant (Not working)

127.0.0.1 - - [16/Apr/2018:22:30:41 +0000] "GET /cgi-bin/action.cgi?cmd=motor_down&val=100 HTTP/1.1" 401 0 "-" "curl/7.59.0-DEV"

Config:

############################################################
# edit this file and move it to /system/sdcard/config/mqtt.conf #
############################################################

export LD_LIBRARY_PATH='/thirdlib:/system/lib:/system/sdcard/lib'

# Options for mosquitto_sub & mosquitto_pub
USER=
PASS=
HOST=192.168.0.106
PORT=1883

# Define a location
LOCATION="Downstairs"

# Define device name
DEVICE_NAME="HomeCamera2"

# Define the base topic used by the camera
# send a message to myhome/dafang/set with the payload help for help.
# Results will be placed in myhome/dafang/${command} or topic/dafang/error - so please subscribe topic/dafang/# for testing purposes
TOPIC="$LOCATION/$DEVICE_NAME"

# Define an autodiscovery prefix, if autodiscovery is desired:
AUTODISCOVERY_PREFIX="homeassistant"

MOSQUITTOOPTS=""
# add Options for Mosquitto here. For example --cafile /system/sdcard/config/DST_Root_CA_X3.pem --tls-version tlsv1
# or using a special id to connect to brokers like azure
MOSQUITTOPUBOPTS=""
# add options for mosquitto_pub like -r for retaining messages

CURLOPTS="–-user root:ismart12"

FIFO=/var/run/mqtt-sub
# the FIFO used. Change it if you like to
STATUSINTERVAL=30
# Send a mqtt statusupdate every 30 seconds
STATUSFORMAT=json
# Configure format of status. Standard is text, uncomment this line for json

After a reboot of HA, it’s working! Now it’s only the LED buttons that don’t work.

Edit: It was a minor config error however if I enable the blue LED it only stays on for like 3 seconds then goes back to Yellow.

I’m stuck here as well. I can switch on both LEDs and RTSP just fine, but it does not switch off (HA flips the switch slider back after a few seconds). Also LEDs go back yellow/off after a while.

@jmtatsch Is there any chance you can share your HA config for Dafang switches esp. template values? Thanks a lot!

The slider goes back if there has happened an internal state update after the update interval (usually 30s). Especially the yellow led per default lights up as long as motion is detected.

The best thing, there is no more templates needed (at least for the main functionality:

  • LEDs
  • IR-CUT
  • Motion Detection/Tracking/Sensor
  • (Auto) Night Mode
  • Light sensor

Thus I would strongly recommend to use only the autodiscovered entities.
Only if you want the Uptime, Wireless link details you will need to parse them out of the json via templates.

1 Like

Might be the motion detection lighting up your yellow led. You can set motion_trigger_led=false in motion.conf if you don’t like that.

2 Likes

You only need to reset mqtt-control to resend the discovery information. We could also resend the discovery information at a certain interval although I am not sure what a reasonable interval would be.

Hello
I have just start using the Hacks for Dafang and its works perfect.
The only thing i missing is the possibility to go to same saved positions.
Is this possible right now ?
Or is it the best to calibrate H and V and then send some mqtt to the position i want to monitor ?

BG
Andreas H

I know that motion detection overrides the current state But when i flip the switch, the yellow/blue led turns on or the rtsp server starts. So far, so good. But the HA switch immediately goes back in same state as before. In that way I cannot turn off either led or stop the rtsp server.

Besides I am using template values, because even though HA gets mqtt information without problems, I don’t have any autodiscovered entities in my entity list. Besides that, everything works just fine, except switches turning off (or staying on), which you can see on the screenshot below (blue/rtsp are on but their respective switches off)

Sure, I could create two switches (or use optimistic: true) for on and off, but that’s not the way it was intended.

The HA switch immediately going back to the same state as before means that HA never got the acknowledgement of the state change. This probably means your state topic is wrong.

Please use autodiscovery, it is so much easier. And the dafang mqtt api is still changing, so if you do it everything manually things will eventually break.

Not possible right now. I am currently reworking the motor control api to sneak it in as two ‘cover’ devices via mqtt autodiscovery. That has an internal state with ticks and endstops. Also I am not sure calibration works as it should. I would expect that after calibration the camera should be exactly in the middle of its possible movement range, but its not by far.

Hello I’ve used the hack to enable the xiaofang to be added into my HA just like in the first post. The snapshots in the overview are working great, but when I click the image it doesn’t show the stream, just a laggy/static image that get refreshed in a minute or two
I’ve been looking for a solution but I can’t find anything, anyone have solved this issue???

What is ment with “id:”? Which id?

Id is a unique id the HomeAssistant automation tool uses to track & manage automations. If you use the tool, HA will create one automatically for you. If not you will need to make one up in order to be able to use the tool again.

So how can I get “You should now be getting messages on topic myhome/mycamera/motion and images on myhome/mycamera/motion/snapshot while myhome/mycamera/motion/detection is set to ON” to work? Because Pushbullet is working if I trigger it manually but not automatically.

oh what do i do if mqtt-status shows me : NOK
reinstall all or is there something simpler?
everything else seems to work.
i want to create a motion detected automation which sends me a notify with pushbullet…

Your mqtt.conf exists and is valid?
You can also ssh on to the camera, delete the pidfile and start the service again.

Use e.g. mosquitto_sub to connect to your broker and subscribe to -t “myhome/#” and you should be able to see the communication between Dafang and HA.

1 Like

For anyone who is unable to get a feed using:
input: -rtsp_transport tcp -i rtsp://192.X.X.X/unicast

Try this instead:
input: rtsp://192.X.X.X/unicast

This is what works for me.

In the Xiaomi Mi Home app the camera is 100% stable. However if I load the custom firmware and I try to view the rtsp feed in full screen from HA the feed will crash and only a restart of rtsp feed will fix it. The video feed can last any where between 5 to 30 seconds before it crashes.

HA is a Docker running on Synology NAS. I guess that I should try using a raspberry pi build of Hassio.

Any ideas further ideas???

camera:

  • platform: ffmpeg
    name: DaFang
    input: -rtsp_transport tcp -i rtsp://192.168.0.128:8554/unicast

I use Hassio on Pi and get same result, did many tests

When video in high resolution it freeze, stutter and eventually hang

When edit rtsp script file to output video 640x360@15FPS it works good, I get stable video stream in Home Assistant but very low quality

I think the problem is FFMPEG in HA, it is not using HW acceleration

When using Zoneminder Docker or Shinobi Docker I can view stream in high resolution on my Pi but with FFMPEG camera it works good only on very low quality