Android IP Webcam as a camera plus sensors!

tl:dr

Here’s an example HA configuration for using the Android IP Webcam app as an IP camera, a switch for the device’s flash, and an example to poll the motion sensor data which is continually generated and published to the app’s HTTP interface. On the Android app side, download and install the app, configure the video settings and sensor data and start the server. HA config:

IP Camera

camera:
- platform: mjpeg
  mjpeg_url: http://IP.ADDRESS:8080/video
  name: My Android Camera

Switch to toggle the camera flash

Needed to use Command line switch rather than RESTful switch because I could call three different endpoints in the Android IP Webcam app

switch:
  platform: command_line
  switches:
    camera_flash:
      command_on: 'curl -s "http://IP.ADDRESS:8080/enabletorch" > /dev/null'
      command_off: 'curl -s "http://IP.ADDRESS:8080/disabletorch" > /dev/null'
      command_state: 'curl "http://IP.ADDRESS:8080/status.json"'
      value_template: '{{ value_json.curvals.torch == "on" }}'
      friendly_name: Camera Flash

Sensor for motion detection

binary_sensor:
- platform: rest
  name: Camera Motion
  sensor_class: motion
  resource: http://IP.ADDRESS:8080/sensors.json?sense=motion_active
  value_template: '{{ value_json.motion_active.data[0][1][0] | round(0) }}'

Full details

I’ve got quite a few old Android devices sitting in junk drawers around the house which I wanted to make use of in connection with HA. Having used Android IP Webcam app in the past, I configured a few devices as IP cameras and connected to HA. You can use a device with only a front-facing (such as an old Nexus 7 tablet) or a device with an outward facing camera which are typically higher quality and usually include a flash.

Once you’ve installed the app onto your device, configure it to use the video settings you’d like and enable sensor recording with whatever sensors you want to be able to monitor. Start the camera server and then you can access the dedicated HTTP interface for the camera from your computer’s web browser. From here I used Chrome’s inspector to “reverse engineer” the requests that are sent to the camera’s web server when interacting with the various buttons. You can see sensors.json in action by clicking “Open sensor graph” from the app’s web interface. When inspecting this page you can see how when you check/uncheck the sensors it updates the URL to include/exclude each sensor in an array ?sense=SENSOR1,SENSOR2,ETC. This can help you when configuring HA for any sensors you want to use. I was focused on the “motion” sensors, but you could experiment with any sensors your device supports which the app exposes.

Motion detection

Provided in three different “sensors” from /sensors.json. I’m not 100% sure how each is calculated.

  1. motion (Motion amount)
    In my testing this one can range from 0 - 1000+, depending on motion and is inconsistent between devices (Nexus 7 front-facing camera vs. Droid Razr MAXX outward facing camera) when no motion is present. On Nexus 7 the value hovers around 110 in a dark environment and around 275 in a lighter environment whereas on the MAXX the value hovers around 290 in a dark environment and 47 in a bright environment. This sensor might work better for more accurate motion detection (on a device by device basis) than motion_active from my example configuration above, just not sure how best to interpret or calculate the data for the HA sensor. Output has the following structure always with 76 timestamp+values pairs (truncated for brevity):
    {"motion":{"unit":"","data":[[1485492206293,[72.0]],[1485492206299,[72.0]],[1485492206341,[72.0]],[1485492206403,[72.0]],[1485492206460,[72.0]],[1485492206522,[72.0]],[1485492206585,[72.0]],[1485492206643,[72.0]],[1485492206701,[72.0]],[1485492206761,[72.0]],[1485492206828,[72.0]]...]]}}
  2. motion_event (Motion reported)
    Can output multiple timestamp+value pairs (counted up to 15), with the most current timestamp last. Only outputs multiple when the value has changed. Output structure:
    {"motion_event":{"unit":"","data":[[1485493018681,[1.0]],[1485493019807,[0.0]],[1485493021730,[1.0]],[1485493021891,[0.0]]]}}
  3. motion_active (Motion active)
    Can output multiple timestamp+value pairs (I’ve seen up to three), with most current timestamp last. I think it only outputs multiple when the value has changed. Output has this structure:
    {"motion_active":{"unit":"","data":[[1485492065318,[0.0]],[1485492065482,[1.0]]]}}

status.json

Provides status of various settings and toggles as currently set. Which is how I detect the state of the flash (aka torch) in the switch config. Output looks like this:

{"video_connections":0,"audio_connections":0,"video_status":{"result": "status", "mode": "none"},"curvals":{"scenemode":"auto","torch":"off","coloreffect":"none","focusmode":"auto","antibanding":"auto","whitebalance":"auto","zoom":"100","exposure":"0","flashmode":"off","focus":"off","exposure_lock":"off","whitebalance_lock":"off","focus_region":"-1001,-1001","photo_size":"3264x2448","orientation":"upsidedown","idle":"off","audio_only":"off","overlay":"on","quality":"51","focus_homing":"off","ip_address":"IP.ADDRESS","motion_limit":"250","adet_limit":"200","night_vision":"off","night_vision_average":"2","night_vision_gain":"1.0","video_recording":"off","motion_detect":"on","video_chunk_len":"60","gps_active":"off","video_size":"1280x720","mirror_flip":"none","ffc":"off","ivideon_streaming":"off"}}

There are other various calls/endpoints for enabling/disabling settings and functionality/features. The app can also do audio, but I haven’t tried to integrate that with HA. Not sure how that could be used or output on HA dashboard.

Endpoints

These are most of the endpoints/urls I’ve discovered inspecting the IP Webcam UI.

/disabletorch (application/xml)
/enabletorch (application/xml)
/toggletorch (application/xml)
/focus (application/xml)
/nofocus (application/xml)
/settings/ffc?set=on/off (application/xml)
/settings/night_vision?set=on/off (application/xml)
/settings/overlay?set=on/off (application/xml)
/settings/orientation?set=landscape/upsidedown/portrait/upsidedown_portrait
/settings/quality?set=XX (where XX = quality %, application/xml)
/settings/gps_active?set=on/off
/settings/motion_limit?set=XXX (Motion trigger amount, application/xml)
/settings/adet_limit?set=XXX (Audio trigger amount, application/xml)
/video (video stream)
/audio.wav (audio wav stream)
/audio.opus (audio opus stream)
/ptz?zoom=X (where X = zoom level 0-15, application/xml)
/startvideo?force=1 (?)
/status.json?show_avail=1 (optional show_avail=1 shows all available options, application/json)
/sensor.json?from=TIMESTAMP&sense=SENSOR1,SENSOR2 (application/json)

I’m not a programmer, so this is the best I’ve been able to do with what the IP Webcam app and HA provide out of the box. I hope to create some automations for turning on/off lights based on motion + time.

Note

I used the free version of the Android IP Webcam app but plan on purchasing the pro version, to support the developer and remove the ads, provided I stick with these Android devices as cameras.

References

49 Likes

Awesome!

I have tablets in each room and using this app to use the cam. I was wondering how to use the motion feature and this was exactly what I was trying to do.

Testing this weekend!

Thanks for sharing!

You are very welcome. Please post back if you determine a better way to more accurately detect motion.

Awesome work! I think the option to use night mode is huge for HA, basically toggle for sunrise and sunset with time adjustments.

I find that no matter what rom for Android, using Android IP Webcam app only runs for a couple days then either get a network disconnect or the app stops streaming. I have tried a few android phones we have here and same issue. I have yet to try at another location to rule out WiFI AP, but wondered if anyone had issues.

1 Like

Found the same issue @HickHackerz. I have eventually created a tasker task that runs every night 3am to kill the app and restart it.

It’s about 2-5 mins without it and couple of errors in the log but besides that solves the issue. (Horrible solution… But works).

2 Likes

I haven’t noticed any issues with my Nexus 7 or Droid Razr MAXX, yet. With an HTC Rezound, VZW OG Droid 1 and VZW Droid 2 the app hasn’t been very stable.

Are either of you using the Pro version of the app?

Also, great idea to toggle the night mode based on sunrise/sunset + time adjustment!

I certainly paid for the pro version. I tried so many IP streaming solutions that IP Webcam was just easy and works. I like the idea for the tasker task, I wouldn’t say the solution is for security based on the stability.

1 Like

Naaaa that’s too cool

1 Like

Thanks for sharing this! I had been playing with this app and some of it’s setting and I’m affirmed to find that it’s what others are using!

Nice work!

I do something similar with the same app. I’ve found having Tasker reboot the tablet once a day does a good job of keeping it running smoothly. I run HADashboard full-screen on most of our devices that run IP Webcam. I found that IP Webcam has a bad habit of stealing focus from time to time. (You would walk up to the tablet to press a button, and find yourself looking back). It seems to happen more frequently on older hardware. Have you noticed this behavior? I ended up using Tasker to watch for IP Webcam coming into focus and having it switch back to the browser.

Otherwise, it’s been a pretty solid setup.

If you haven’t seen it yet, the app supports some extra hidden features (“cheats”) documented here: http://ip-webcam.appspot.com/cheats.html

1 Like

Thanks, @Zen. As I don’t use these devices for anything but the camera (for now) I haven’t encountered the issue with IP Webcam stealing the focus.

I came across that cheats page, but when navigating up to http://ip-webcam.appspot.com/ it looked like it was for some other software with the same name. Upon closer looking at the cheats page I see it is referencing the Android IP Webcam app. I’ll add this to the references in my post.

I have multiple switches (switch 1: switch 2: and so on)

In switch 3 I have

switch 3:
  platform: command_line
  switches:
    office_camera_presets_1_2:
      #presets1 office outside
      command_on: "curl -X GET 'http://192.168.1.45:80/decoder_control.cgi?command=31&user=admin&pwd=x"
      #presets2 office inside
      command_off: "curl -X GET 'http://192.168.1.45:80/decoder_control.cgi?command=33&user=admin&pwd=x'"

hot to put more then one smartphone flash??

also these commands how to put more then one smartphone and its motion?

You could change to the other yaml notation which allows for multiple switches without adding a number after each, especially if you are using the same platform (command_line). Here is an example. Notice I’ve given unique, room-based names to each switch. I’ve included your switch from the code above.

switch:
  platform: command_line
  switches:
    kitchen_camera_flash:
      command_on: 'curl -s "http://IP.ADDRESS:8080/enabletorch" > /dev/null'
      command_off: 'curl -s "http://IP.ADDRESS:8080/disabletorch" > /dev/null'
      command_state: 'curl "http://IP.ADDRESS:8080/status.json"'
      value_template: '{{ value_json.curvals.torch == "on" }}'
      friendly_name: Kitchen Camera Flash
    living_room_camera_flash:
      command_on: 'curl -s "http://IP.ADDRESS:8080/enabletorch" > /dev/null'
      command_off: 'curl -s "http://IP.ADDRESS:8080/disabletorch" > /dev/null'
      command_state: 'curl "http://IP.ADDRESS:8080/status.json"'
      value_template: '{{ value_json.curvals.torch == "on" }}'
      friendly_name: Living Room Camera Flash
    office_camera_presets_1_2:
      #presets1 office outside
      command_on: "curl -X GET 'http://192.168.1.45:80/decoder_control.cgi?command=31&user=admin&pwd=x"
      #presets2 office inside
      command_off: "curl -X GET 'http://192.168.1.45:80/decoder_control.cgi?command=33&user=admin&pwd=x'"

thanks, I manage.

For multiple binary_sensor?

It works great, although the motion is not triggered on HASS, how to troubleshoot?

I use command_line only in “switch 3” so its fine

what do you think of keeping all night long the flash of the phone on? I will put little used spare phones, but …

Leaving the flash on too long might be taxing to the flash or device hardware. Also, I’ve found the flashes on my old devices are not very powerful and doesn’t improve the video quality/visibility. Maybe combined with night mode it would be better.

Try this for multiple binary_sensor:

binary_sensor:
- platform: rest
  - name: Kitchen Motion
    sensor_class: motion
    resource: http://IP.ADDRESS:8080/sensors.json?sense=motion_active
    value_template: '{{ value_json.motion_active.data[0][1][0] | round(0) }}'
  - name: Living Room Motion
    sensor_class: motion
    resource: http://IP.ADDRESS:8080/sensors.json?sense=motion_active
    value_template: '{{ value_json.motion_active.data[0][1][0] | round(0) }}'

It works great, although the motion is not triggered on HASS, how to troubleshoot?

What isn’t triggered on HASS? You can use HASS’ Developer Tools > States to confirm the value of the motion sensor is changing from 0 to 1 when detecting motion.