Hacking the XiaomiFang Wifi camera

I’ve have set up the Dafang Camera. I’m saving Snapshot, when on detection is on to the /config/www/camera/ directory. I can access the directory through samba, but I would like to access them through https. Can this be done?

1 Like

I solved it.
I can send the currentpic picture with telegram now.
Here is my code:

configuration.yaml:

camera:
  - platform: generic
    name: dafang_generic
    still_image_url: https://[IP_ADDRESS]/cgi-bin/currentpic.cgi
    username: root
    password: ismart12

Automation:

- alias: notify_1_xiaomi_dafang_1_send_picture
  initial_state: True
  hide_entity: False
  trigger:
    - platform: state
      entity_id: 
        #- sensor.front_door_node_1_pir_motion_status 
        - sensor.front_door_node_1_front_door_status
        - sensor.r433_mhz_random_code
  action:
    - service: camera.snapshot
      data:
        entity_id: camera.dafang_generic
        filename: '/config/www/xiaomi_dafang_1/dafang_snapshot_1.jpg'
    - service: camera.snapshot
      data:
        entity_id: camera.dafang_generic
        filename: '/config/www/xiaomi_dafang_1/dafang_snapshot_2.jpg'
    - service: camera.snapshot
      data:
        entity_id: camera.dafang_generic
        filename: '/config/www/xiaomi_dafang_1/dafang_snapshot_3.jpg'
    - service: notify.notify_1
      data:
        title: Front Door Pictures 1-3
        message: Front Door Pictures 1-3
        data:
          photo:
            - file: '/config/www/xiaomi_dafang_1/dafang_snapshot_1.jpg'
              caption: >
                Front Door Snapshot 1
                {{ states.sensor.current_date_time.state }}
            - file: '/config/www/xiaomi_dafang_1/dafang_snapshot_2.jpg'
              caption: >
                Front Door Snapshot 2
                {{ states.sensor.current_date_time.state }}
            - file: '/config/www/xiaomi_dafang_1/dafang_snapshot_3.jpg'
              caption: >
                Front Door Snapshot 3
                {{ states.sensor.current_date_time.state }}
    - delay: '00:00:05'
    - service: camera.snapshot
      data:
        entity_id: camera.dafang_generic
        filename: '/config/www/xiaomi_dafang_1/dafang_snapshot_4.jpg'
    - service: notify.notify_1
      data:
        title: Front Door Pictures 4
        message: Front Door Pictures 4
        data:
          photo:
            - file: '/config/www/xiaomi_dafang_1/dafang_snapshot_4.jpg'
              caption: >
                Front Door Snapshot 4 
                {{ states.sensor.current_date_time.state }}
1 Like

Hello,

I installed the hack on my Wyzecam v2. I see the stream in HA, but I can’t get the webpage of the camera. So I’m not able to activate MQTT. Any idea what is wrong? I get a ERR_CONNECTION_REFUSED with I enter the IP in my browser.

I’m also able to see the feed in VLC with sound. I modified the mqtt.conf. But still, without access to the web interface, I can’t activate it.

Please help

Hi, I tried your script but i keep getting the same picture over and over. No new pictures are being generated. Also the timestamps change in the /config/www/xiaomi_dafang_1 folder so I know the script is working. The URL https://[IP_ADDRESS]/cgi-bin/currentpic.cgi shows the latest pic so no problem there. Any insights?

If anyone is interested, I have a working config with this (i posted this originally in github of dafang-hacks)

configuration.yaml

ffmpeg: ## is necessary to enable ffmpeg in home assistant 
camera:
  - platform: ffmpeg
    name: Dafang
    input: -rtsp_transport tcp -i rtsp://user:[email protected]:8554/unicast

Here is a simplified automation to request an image with a Telegram command /dafang (working telegram bot and telegram polling config is needed)

automation.yaml

- id: telegram_dafang
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/dafang'
  action:
  - service: notify.telegram_cam
    data_template:
      message: Message
      data: 
        photo:
          url: 'https://hassip.ip:port{{states.camera.dafang.attributes.entity_picture}}'
          caption: '{{now().strftime("%d.%m.%Y-%H:%M:%S")}}'

For {{states.camera.dafang.attributes.entity_picture}} please change “camera.dafang” into your own setting. You can find the correct one by checking entity list in your hass-io (https://hassio.ip:port/dev-state/).
the camera attribute entity_picture is the same as this /api/camera_proxy/camera.dafang?token=##hash## in which the token-hash changes all the time on a new image load. So in that case, the current image will be pulled and sent via Telegram.

I have a couple Camera automations running, but this might be the most useful - this tells me about a movement in our house when nobody is at home (could be either through camera sensor or any binary sensor) and additionally send me a current image.

- id: sensor-on-warn-telegram
  trigger:
  - entity_id: group.motionsensor
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: group.mobiledevices
    state: not_home
  action:
  - service: notify.telegram_warn
    data_template:
      message: Movement detected.
      data: 
        photo:
          url: 'https://hassip.ip:port{{states.camera.dafang.attributes.entity_picture}}'
          caption: '{{now().strftime("%d.%m.%Y-%H:%M:%S")}}'
  - service: notify.telegram_warn
    data_template:
      message: 'Something is moving. Could it be a raccoon? Please check your home.'

I have also created multiple commands which I can send through Telegram, for example if I enter /dafang-rtsp-restart it will stop and start rtsp again Few other commands are to start/stop/restart mqttcontrol or mqttstatus… It sends the corresponding cgi command locally through home assistant but thats another topic.

Feel free to ask, if you like to know anything else…

3 Likes

Do you have all of your dafang related stuff somewhere? Github? I have one coming in a week or two so really interested!

Thanks.

From reading the various threads it seems it isn’t possible to get decent live streams in HA from this cam, as its using RTSP?

Is it possible to use something like this https://streamedian.com/#demo with nginx on Hassio to get a live stream within an iframe in the HA interface?

I am running the hacked firmware too, but sometimes the streams of the camera simply freeze (also outside of hass) and then I need to visit the webpage of the camera first in order to get the stream running again.

Any thoughts on possible causes? WiFi connection shouldn’t be the problem as it is close to the router…

This is only true if you use a low powered device like the raspberry. I switched to a NUC and here the RTSP Stream from the camera is perfect :slight_smile:
However, you can switch to a “Snapshot” based approach that sends a JPEG every second. This worked well for me using the Pi3.

Do you have a howto for that? ffmpeg destroyed my pi3 experience.

Thanks for the info.
As far as I understand, the ffmpeg needs to be running on the HA server (so this is where the grunt is needed) and end devices displaying the stream can still be lower power ?

I was hoping that making use of the RTSP plugin I linked may allow the end device to display the RTSP stream directly (without conversion) so doesn’t have a dependency on horsepower at the HA server, or the end device.

I should still have my “custom” plugin on one the camera. Ill try to download it and post the code. It was relatively simple to achieve.

#bin/sh
PIDFILE="/var/run/snapshot-server.pid"

status()
{
  pid="$(cat "$PIDFILE" 2>/dev/null)"
  if [ "$pid" ]; then
    kill -0 "$pid" >/dev/null && echo "PID: $pid" || return 1
  fi
}

start()
{
  LOG=/dev/null
  echo "Starting Snapshot server..."
  snx_isp_ctl --mfset-mode 3
  snx_snapshot -m -q 60 -n 1 -W 1920 -H 1080 -e 0 > $LOG 2>&1 &
  echo "$!" > "$PIDFILE"
}

stop()
{
  pid="$(cat "$PIDFILE" 2>/dev/null)"
  if [ "$pid" ]; then
     kill $pid ||  rm "$PIDFILE"
  fi
}

if [ $# -eq 0 ]; then
  start
else
  case $1 in start|stop|status)
    $1
    ;;
  esac
fi

You also need to activate the snx_snapshot component by moving it into the folder with all the other components. The above script i simply named: 17-snapshot-server.sh and put it into: /media/mmcblk0p2/data/etc/scripts/

I actually believe part of it is the ffmpeg version used within hassio. If im not mistaken it doesnt use the HW encoding / decoding capabilities of the Pi. Some people had a lot of success with using the Shinobi Pro Addon for Hassio which should do what you want. I never tried it though.

@AlmostSerious hey guys has anyone managed to get the command for turning and rotating the camera with the dafang hack?

You can turn the image it self via the snx_isp_ctl module. But i guess you mean physical movement of the camera it self? Mine doesnt support this feature, but i read that there is a new version of the dafang hacks (a fork from Samtap) that supports this from within the UI.

1 Like

I couldnt find this fork. Can you please share the link?

Thanks.

1 Like

Did anyone succeed to use the camera as a device for TTS?

Lars

Hi @thawizard @AlmostSerious

Do you get a “second” lag on your stream on VLC? and alot of lag on Homeassistant via the ffmpeg component? How can I resolve this?

Do you mind sharing your camera settings?

Thanks.

Sorry to post this here, but I think this is probably the largest group around working with these cameras in HA, can someone take a look at this Dafang camera (Wyze cam v2) stops working basically my HA system stops talked to the camera after awhile and I don’t know why.