EliasKotlyar / Xiaomi-Dafang-Hacks

4.19k stars 1k forks source link

Motion Detection - MQTT Not Publishing to myhome/device/motion #552

Closed belch85 closed 5 years ago

belch85 commented 6 years ago

Successfully hacked DaFang, integrated into HASS and MQTT (video feed & control switches). Followed the motion setup guide here.

I've configured the motion area (see here), watching the live-stream on TinyCam, I make movement in the watch area and there are no mqtt payloads in myhome/device/motion.

Initially I thought that the LED indicator wasn't working because the light on the front of camera stays solid blue - however I now see while watching the live feed that it is in fact an overlayed led at top right corner of video feed (and is working) so the problem is just with MQTT motion alerts.

I can see alive-messages every 30 seconds in MQTT home/device topic so I know that connection is working.

I've subscribed to myhome/# to check that it isn't posting to another topic - there are no motion messages anywhere.

Subscribed to myhome/device/motion/detection - I can see the ON status every 30 seconds.

I've just tried a different microSD card with a fresh install - same results.

Any clues? I think this issue might be related?

olskar commented 6 years ago

You have publish_mqtt_message=true set to true in motion.conf?

What happens if you try to run detectionOn.sh manually?

mprajsler commented 6 years ago

Hi, I got exactly same issue no motion in MQTT myhome/device/motion. LED in videofeed once motion detected is working. Also all other MQTT control/status topics are working fine... I got Xiaofang 1S

gerard33 commented 6 years ago

First of all, great piece of software! Incredible what a nice features are available this way for this cheap cam.

I have a Dafang and exactly the same issue as @belch85 describes above.

When running ./detectionOn.sh (in folder /system/sdcard/scripts/) the sensor in Home Assistant switches to motion detected. And with ./detectionOff.sh it switches off again. So the communication with Home Assistant is working fine.

goermezer commented 6 years ago

This seems to be a memory problem. After creating a swap file like described in https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks/issues/178, the motion publishing to MQTT works for me. Memory usage with swap looks then like this:


             total       used       free     shared    buffers     cached
Mem:            34         31          3          0          0          7
-/+ buffers/cache:         23         11
Swap:           63          1         62
robbinjanssen commented 6 years ago

@goermezer you're referring to this comment? https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks/issues/178#issuecomment-379290462 ?

goermezer commented 6 years ago

@robbinjanssen:

For example:

dd if=/dev/zero of=/system/sdcard/swapfile bs=1M count=64 mkswap /system/sdcard/swapfile swapon /system/sdcard/swapfile

After reboot it isn`t mounted. So I added this to run.sh:

swapon /system/sdcard/swapfile

dreed47 commented 5 years ago

I've tried to create this swap file but I'm still not getting motion events in MQTT. Also, the latest version of the code looks like it has code to create a swap file but it's disabled by default. I've tried both manually creating the swap via @goermezer message above and I've tried to enable the code in the run.sh. What's the best way for me to debug this and see whats going on?

UPDATE: The run.sh is now creating the swapfile but I think it was still running out of memory. I say this because motion detection events would get created if I scaled the video down to 960x540. I changed the swapfile size from 256 to 512 and it seems to be working now.

UPDATE2: I can't get it to work consistently @512M swap size. Sometimes forcing the video back to 960x540 gets it to work (but now always). I tried a 1024M swap file size but that seem to cause other issues with the camera not actually recognizing the motion at all. I'm convinced this is a memory issue but not sure how to resolve it.

TacoScheltema commented 5 years ago

Hi, I had the same issue, getting status messages but not getting any motion messages. I enabled the swap file in run.sh but that didn't change anything.

Then I had a read here: https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks/blob/master/integration/homeassistant/homeassistant.md, specifically the part about setting up the automations.yaml file in homeassistant. It mentions to set the topic to myhome/mycamera/motion. Since in the default mqtt.conf.dist file the topic is set to TOPIC="$LOCATION/$DEVICE_NAME" I tried subscribing to $LOCATION/$DEVICE_NAME/motion which in my setup is OFFICE/dafang/motion, now I'm getting ON and OFF events.

dreed47 commented 5 years ago

@ TacoScheltema How big is your memory card? What model is your camera? I can't get motion detection to work consistently. My configuration on the camera side on the HA side are correct because, when it works, I can generate notices to HA's UI on a motion detection.

TacoScheltema commented 5 years ago

@dreed47 I used a 2GB memory card, formatted the full 2GB with FAT32 on a mac with diskutil. My camera is a Xiaomi Dafang. I've had the camera for a few months now so I assume the firmware on the device is not one of the latest ones. I did pull the latest modded firmware from this git repo earlier this week and am running from that now. before I used the same 2GB sd card, formatted with a 512mb and a 1.5gb partition which never properly worked. So far I haven't had many issues with this latest version and motion detection is working. I'm even getting images sent on the myhome/mycamera/motion/snapshot topic which I'm writing to disk using Node Red.

dreed47 commented 5 years ago

Thx @TacoScheltema I have a Wyzecam V2 and a Xiaomi Xiaofang 1S and both are using an 8GB card. It seems that motion events to mqtt work fine on the Wyze with a 512M swap file. But the video hangs on the Xiaofang when using 512M swap file. When I make it 384M it seems to work. I'm going to let them both go for a while and see how stable it is.

I'm curious how you are doing the file output in Node Red. I assume you're using the file storage node? How do you have it setup to create the image file?

TacoScheltema commented 5 years ago

Hi @dreed47, Here's an export of my node setup:

[
  {
    "id": "ba857021.888c2",
    "type": "mqtt in",
    "z": "7eb8122a.9577cc",
    "name": "Snapshots",
    "topic": "OFFICE/dafang/motion/snapshot",
    "qos": "2",
    "broker": "223e7fde.3a35f",
    "x": 250,
    "y": 300,
    "wires": [
      [
        "6579c494.f587cc"
      ]
    ]
  },
  {
    "id": "6579c494.f587cc",
    "type": "file",
    "z": "7eb8122a.9577cc",
    "name": "Office snapshot",
    "filename": "/home/nodered/www/images/office.jpg",
    "appendNewline": false,
    "createDir": false,
    "overwriteFile": "true",
    "x": 460,
    "y": 300,
    "wires": [
      []
    ]
  },
  {
    "id": "223e7fde.3a35f",
    "type": "mqtt-broker",
    "z": "",
    "name": "Local MQTT",
    "broker": "127.0.0.1",
    "port": "1883",
    "clientid": "",
    "usetls": false,
    "compatmode": true,
    "keepalive": "60",
    "cleansession": true,
    "birthTopic": "",
    "birthQos": "0",
    "birthPayload": "",
    "closeTopic": "",
    "closePayload": "",
    "willTopic": "",
    "willQos": "0",
    "willPayload": ""
  }
]

You'll have to change the topic and MQTT server to match your environment of course.

Basically it's a mqtt input tied to a file output. The OFFICE/dafang/motion/snapshot topic will receive the jpg image so it's just a matter of sending that to a file.

dreed47 commented 5 years ago

Awesome @TacoScheltema That helps. It was easier than I thought. I wonder if there's a way to not overwrite the images but rather create them with unique names.

TacoScheltema commented 5 years ago

@dreed47 Yes, that's something I'll be looking at as well.

dreed47 commented 5 years ago

@dreed47 Yes, that's something I'll be looking at as well. This is what I use in a node red function node to limit the number of snapshots saved.

// pull the camera name out of the topic string
var cameraName = msg.topic.split("/")[1];
var count = context.get(cameraName) || 0; 
count +=1;
// limit number of snapshots saved to 8
if (count > 8) {
count = 1;
}
context.set(cameraName, count);
msg.cameraName = cameraName;
msg.notifyTitle = "Motion Alert! " + cameraName;
msg.notifyMessage = "Motion detected -      [" + cameraName + " local snapshot](http://hassio.local:8123/local/snapshots/" + cameraName + "-" + count + '.jpg)';
msg.filename = "/config/www/snapshots/" + cameraName + "-" + count + ".jpg"
return msg;
stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Jpsy commented 4 years ago

Just for completeness: Creating a swap file meanwhile is part of Dafang Hacks. You just have to activate it. Read the FAQ: https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks/blob/master/hacks/faq.md

What if my scripts in config/userscripts/motiondetection are not executed or mqtt/telegram messages/emails are not sent on motion? Your camera probably runs out of memory when processing the motion event. This is likely in cameras with 64MB e.g. the Xiaofang 1s. Try to enable some swap memory by copying swap.conf.dist to swap.conf and setting SWAP=true.