Hacking the XiaomiFang Wifi camera

Updated two dafang via the web ui of the camera (hack). Once updated I cant access the web ui. No response. But mqtt works and also snapshots. But currentpic.cgi doesnt work. Anyone experienced this?

Well I think the problem is my MQTT password. I’ve set a password starting with the $ character and in the string $COMMAND used in the mqtt-discovery.sh script the password doesn’t appear…
I changed the password to a string not starting with $ character and now everything works well :+1:

So the problem isn’t in the script :slight_smile:

Hello, my Dafang cameras are now working in HA and I’d like to use automations to enable motion detection when no one is home and to disable it when someone comes home.
So I wrote this automation :

  • id: mode_presence
    alias: Mode présence
    trigger:
    platform: state
    entity_id: group.Famille
    from: ‘not_home’
    to: ‘home’
    action:
    #Extinction détection mouvement

    • service: camera.disable_motion_detection
      entity_id: camera.cam01
    • service: camera.disable_motion_detection
      entity_id: camera.cam02
      #Information présence
    • service: notify.telegram
      data:
      message: ‘Mode présence activé’
  • id: mode_absence
    alias: Mode absence
    trigger:
    platform: state
    entity_id: group.Famille
    from: ‘home’
    to: ‘not_home’
    action:
    #Démarrage détection mouvement

    • service: camera.enable_motion_detection
      entity_id: camera.cam01
    • service: camera.enable_motion_detection
      entity_id: camera.cam02
      #Information présence
    • service: notify.telegram
      data:
      message: ‘Mode absence activé’

It seems that the motion_detection services are not working. In my homeassistant log file I have those errors

Traceback (most recent call last):
File “/usr/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/camera/init.py”, line 433, in disable_motion_detection
raise NotImplementedError()
NotImplementedError
2020-04-10 16:03:32 ERROR (MainThread) [homeassistant.core] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/camera/init.py”, line 433, in disable_motion_detection
raise NotImplementedError()
NotImplementedError
2020-04-10 16:10:48 ERROR (MainThread) [homeassistant.core] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/camera/init.py”, line 424, in enable_motion_detection
raise NotImplementedError()
NotImplementedError

Can someone explain me why I have those errors ? Should I use MQTT messages to set the motion detection ON or OFF ?

The Telegram notification works fine.

group.Famille is incorrect. All entity ids are lower case

Thanks Nickrout, this wasn’t the problem I think but was a mistake.
I’ve change the automation ad this works

  • id: mode_presence
    alias: Mode présence
    trigger:
    platform: state
    entity_id: group.famille
    from: ‘not_home’
    to: ‘home’
    action:
    #Extinction détection mouvement
    • service: switch.turn_off
      data:
      entity_id: switch.cam01_motion_detection
    • service: switch.turn_off
      data:
      entity_id: switch.cam02_motion_detection
      #Information présence
    • service: notify.telegram
      data:
      message: ‘Mode présence activé’

Please post your code properly. See How to help us help you - or How to ask a good question

I get following error in HA logs from long time but I ignored it. Now decided to fix and I ran into this thread. I thought of applying fix (removing the icon info) but I didn’t find icon reference in motion sensor section in mqtt-autodiscovery.sh file.

@nickrout Any ideas to fix this issue?

# Motion sensor 
/system/sdcard/bin/mosquitto_pub.bin -h "$HOST" -p "$PORT" -u "$USER" -P "$PASS" -t "$AUTODISCOVERY_PREFIX/binary_sensor/$DEVICE_NAME/motion/config" ${MOSQUITTOPUBOPTS} ${MOSQUITTOOPTS} "{\"name\": \"$DEVICE_NAME motion sensor\", \"unique_id\": \"$MAC_SIMPLE-motion-sensor\", \"device\": {\"identifiers\": \"$MAC_SIMPLE\", \"connections\": [[\"mac\", \"$MAC\"]], \"manufacturer\": \"$MANUFACTURER\", \"model\": \"$MODEL\", \"name\": \"$MANUFACTURER $MODEL\", \"sw_version\": \"$VER\"}, \"state_topic\": \"$TOPIC/motion\", \"device_class\": \"motion\"}"

HA error:

2020-04-12 19:30:30 ERROR (MainThread) [homeassistant.util.logging] Exception in async_discover when dispatching 'mqtt_discovery_new_binary_sensor_mqtt': ({'name': 'garage-camera motion sensor', 'unique_id': 'a4da222fd641-motion-sensor', 'icon': 'mdi:run', 'state_topic': 'home/garage-camera/motion', 'device_class': 'motion', 'platform': 'mqtt'},)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/binary_sensor.py", line 84, in async_discover
    config = PLATFORM_SCHEMA(discovery_payload)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: extra keys not allowed @ data['icon']

Hey, I know this is an old post but has any work been done on utilising the speaker and mic?
I was going to use it as an intercom with my Xiaomi ZigBee button at the front door.

Unfortunately no.
Just as an update. The camera has been ticking fine, the only problem I had so far was a dead SD card. This was an easy fix by replacing the SD and following the instructions again as per my original post

1 Like

Hi,

Could you keep the rtsp server and have a still image? This latter is required for creating a camera. Note that I’d like to use the stream integration to be able to stream to a Lovelace card.

You can have a still image and the rtsp stream. Mine is

camera:
  - platform: generic
    still_image_url: https://user:[email protected]/cgi-bin/currentpic.cgi
    name: foyer
    verify_ssl: false
    authentication: digest
    stream_source: rtsp://dafang.lan:8554/unicast

Thanks @nickrout.
Mine does not have the currentpic.cgi.
I used ffmpeg and it’s working.

I’ve noticed the mqtt entities show unavailable after a HA reboot. Is there a group topic I can send to them to get them to update again? Only way I can get them to show in HA again is by rebooting the cameras.

Do your discovery messages have the retain flag set?

1 Like

I haven’t set this myself. I can see an option in mqtt.conf for MOSQUITTOPUBOPTS="" but this is for all messages, not sure if I want to do that?

Also I’d like HA to trigger a custom script on the camera, is this possible? I might put it in as a feature request if not.

Sure, use a commandline switch

I added “-r” in mqtt.conf but I’m still getting occasions after rebooting they go back to unavailable.

Hi i don’t have sounf in my home assistant

I used have sound in vlc.

Do you gave idea?

Hello guys.

I am running into some troubles configuring mqtt integration.
But there is no sensors discovered by mqtt integration :frowning:

I can see the moquitto logs:

1590581586: New connection from 192.168.1.126 on port 1883.
1590581586: New client connected from 192.168.1.126 as mosqpub|9227-DAFANG (p1, c1, k60, u'mqtt').
1590581586: Client mosqpub|9227-DAFANG disconnected.

Occasionally the this line is logged to:

[WARN] Not found homeassistant on local database

Looking into these treads i realize I shouldn’t use homeassistant as LOCATION. So I change it and restart the services. But it keeps being logged.

In config.yaml I have:

mqtt:
  broker: localhost
  discovery: true
  discovery_prefix: homeassistant

and my mqtt.conf is:

> ############################################################
> # 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=<home assistant dedicated user>
> PASS=<home assistant dedicated user's password>
> HOST=192.168.1.187
> PORT=1883
> 
> # Define a location
> LOCATION="garage"
> 
> # Define device name
> DEVICE_NAME="dafang"
> 
> # 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"
> 
> # Define additional options for Mosquitto here.
> # For example --cafile /system/sdcard/config/DST_Root_CA_X3.pem --tls-version tlsv1
> # or use a special id to connect to brokers like azure
> MOSQUITTOOPTS=""
> 
> # Add options for mosquitto_pub like -r for retaining messages
> MOSQUITTOPUBOPTS=""
> 
> # Send a mqtt statusupdate every n seconds
> STATUSINTERVAL=30
> 
> # Define whether you would like to have light level exposed (by hardware sensor og virtual calcuations).
> # If the device doesn't come with a hardware sensor, your only option is to use 'virtual'.
> #
> #  Options:
> #     hw = Use 'hw' if the device has build in hardware light sensor. For more details, see . See issue eg. #1120 for more details...
> #     virtual = Use 'virtual' calculations, based on the lightlevel on the image, also known as ISP exposure (from /proc/jz/isp/isp_info)
> #     false = Use 'false' (default) to DISABLE the topic (It will not publishing details about light level)
> LIGHT_SENSOR="false"

I am quite new using mqtt so it is possible I was missing something!
Any idea?

thanks a lot