Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Foscam IP Cameras

Markus Storm edited this page Jun 28, 2016 · 14 revisions

Although there is no openHAB binding for Foscam IP cameras, there are many ways to interact with them from an openHAB server. This wiki page describes some of the techniques shared by openHAB users (edited).

Contents:


Displaying Streaming Video in SiteMap

One of the camera substreams can be configured to to stream MJPEG (replaces existing substream video).

From Kimberly Fischer:

I was originally told that the H.264 cameras cannot be show live in the UI. Not true. My solution was to turn one of the streams to mjpeg and then the video widget works great. I don't know if its a good as the HD stream but it does work flawlessly in the UI without any plugins. Make sure the firmware is up to date and you use the mjpeg option in your sitemap item.

http://ip address:port/cgi-bin/CGIProxy.fcgi?cmd=setSubStreamFormat&format=1&usr=xxxx&pwd=xxxxx</strong>

will set the substream to mjpeg. I have 5 foscam FI9853EP HD POE cameras that I can view in the openhab ui. Works from the internet too. Also works in HABDroid as well. [I can also] view my h.264 cams in chrome from the address bar.

From Nicholas Waterton:

I have 4 foscams displayed in 4 quadrants using the webview sitemap item. You make a simple web page with four image items. I have individual web pages for each camera, and made each image a link to/from the main 4up page.

This now means that you can display all 4 cameras at once (in quadrants), then touching (or clicking) on an image zooms it up to full screen. Touching it again goes back to quadrants again.

You have to have an http server (Apache2 I'm using) you can serve the web pages from. The pages are very simple, just a few lines each. This works independently of how many people are using openhab, and in a browser it's just a web page.

Here is my web page set up:

<!DOCTYPE html>
<html>
<meta http-equiv="refresh" content="60">
<body bgcolor="black">
<a href="http://ip/Porch_Camera_Motion.html"><img id=cam1 src=http://1p:8082/ width=49.7% border="0"></a>
<a href="http://ip/Hallway_Camera_Motion.html"><img id=cam2 src=http://ip:8083/ width=49.7% border="0"></a><br>
<a href="http://ip/Back_Garden_Camera_Motion.html"><img id=cam3 src=http://ip:8084/ width=49.7% border="0"></a>
<a href="http://ip/Side_Garden_Camera_Motion.html"><img id=cam4 src=http://ip:8081/ width=49.7% border="0"></a>
</body>
</html>

Each web page looks like this (this would be "Porch_Camera_Motion.html"):

<!DOCTYPE html>
<html>
<meta http-equiv="refresh" content="60">
<link href="style.css" rel="stylesheet" type="text/css">
<body>
<a href="http://ip/All_Cameras_Motion.html"><img src="http://ip:8082" alt="Porch WD Live Feed"></a>
</body>
</html>

Stylesheet is:

body {
      background-color: black;
      color: white;
      margin: 0;
      padding: 0;
      }
img,video {
    width: auto;
    width: 100%;
    height: auto;
    }

One for each camera, where ip is your ip address of the web server. I'm using the program motion, which gives an mjpeg stream on ports (you define) I'm using 8081-8084, but you could substitute any ip:port combination that gets you an mjpeg stream as Kimberly points out above.

The refresh line is optional.

in your sitemap, the item looks like this:

Webview url="http://ip/All_Cameras_Motion.html" height=14

The height setting is important! it's height*22 lines (not lines), it has to be big enough to display on your device, but not so big that the screen would scroll, or the screen will continually refresh.

This is what it looks like (at night hence B/W):

Openhab Screenshot


Motion and Sound Alarms

Some versions of Foscam cameras provide a motion detection callback (models?), but the newer HD cameras do not. Users have proposed several techniques for obtaining Foscam camera motion alarms in openHAB.

Poll Camera Device State (H.264 camera API)

The Foscam HD camera API supports retrieval of the device state which includes the motion alarm status. This status is changed for a brief time after motion is detected and then it resets itself. Polling is required to detect the motion alarms.

From Markus Storm:

I'm polling [with]:

String Foscam_Motion "Motion detection [MAP(foscam.map):%s]" <camera> (Status,Test) { http="<[http://foscam:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=USER&pwd=PASSWORD:4000:REGEX(.*?<motionDetectAlarm>(.*?)</motionDetectAlarm>.*)]" }
String Foscam_Sound "Sound detection [MAP(foscam.map):%s]" <camera> (Status,Test) { http="<[http://foscam:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=USER&pwd=PASSWORD:4000:REGEX(.*?<soundAlarm>(.*?)</soundAlarm>.*)]" }

Maybe not the most elegant solution, and specific to Foscam, but it works and it's fast to react, almost as good as a PIR sensor.

Poll Camera Log (H.264 camera API)

From Steve Bate:

The camera log can be polled to retrieve motion alarms events. This has the advantage that no events will be lost and analysis can be performed on event timing and frequency and so on. The log access would be difficult to do directly with a http binding, so I recommend using a script to retrieve and process the log. I use the foscam-python-library and JSR223 Jython rules and scripts. I also use the log analysis to determine who is logging into my camera (user names and IP addresses) for security monitoring.

See also: General API Access, foscam_log

Use Motion Alarm Callback (older camera API)

From watou:

Here is some sample code adapted from one of my rule files that demonstrates instructing two different Foscam model cameras to enable or disable motion detection, based on the switch state of the Present item. When the switch changes to OFF, it sends HTTP commands to each camera (IP addresses ending in .14 and .15) to enable motion detection, and in the case of the model that supports callbacks on motion, tells it to callback to the openHAB server (IP address ending in .10) to flip a Switch item. When the Present switch is set to ON, it disables motion detection on all cameras.

import org.openhab.core.library.types.*
import java.util.HashMap
import java.util.LinkedHashMap

val HashMap<String, LinkedHashMap<String, Object>> cameras =
    newLinkedHashMap(
    "entrance-cam" -> (newLinkedHashMap(
		"snap_url" -> "http://192.168.1.14/snapshot.cgi?user=admin&pwd=password",
		"enable_motion_url" -> "http://192.168.1.14/set_alarm.cgi?motion_armed=1&motion_sensitivity=5&motion_compensation=1&mail=1&upload_interval=34463&http=1&http_url=http%3A%2F%2F192.168.1.10%3A8080%2FCMD%3FEntranceMotion%3DON&schedule_enable=0&user=admin&pwd=password",
  	"disable_motion_url" -> "http://192.168.1.14/set_alarm.cgi?motion_armed=0&user=admin&pwd=password")
           as LinkedHashMap<String, Object>),
  "kitchen-cam" -> (newLinkedHashMap(
  	"snap_url" -> "http://192.168.1.15:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=admin&pwd=password",
  	"enable_motion_url" -> "http://192.168.1.15:88/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&linkage=7&snapInterval=2&sensitivity=1&triggerInterval=5&schedule0=1023&schedule1=1023&schedule2=1023&schedule3=1023&schedule4=1023&schedule5=1023&schedule6=1023&area0=1023&area1=1023&area2=1023&area3=1023&area4=1023&area5=1023&area6=1023&area7=1023&area8=1023&area9=1023&usr=admin&pwd=password",
  	"disable_motion_url" -> "http://192.168.1.15:88/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=0&usr=admin&pwd=password")
      as LinkedHashMap<String, Object>)
   )

rule PresenceChanged
when
	Item Present changed
then
	switch Present.state {
		case OFF : cameras.values.forEach [ camera | sendHttpGetRequest(camera.get("enable_motion_url")) ]
		case ON : cameras.values.forEach [ camera | sendHttpGetRequest(camera.get("disable_motion_url")) ]
  }
end

Use inotify to monitor new recordings or snapshots (Linux-specific)

The Foscam cameras can be configured to transmit recordings and snapshots to an FTP server. The Linux inotify tools can be used to monitor for new files and then inject an item state change into openHAB using the REST API. The advantage is that openHAB polling for motion alarms is not required.

From marco_anderheyden:

I've created a bash-script which detects a motion and send it to openHAB. This is done by inotify-tools. Inotify "subscribes" a record folder, and when the camera is storing a video or image, i invokes another bash-script which is doing a curl http request to openHAB's rest service.

I've also completed a short bash script, which can extract a snapshot for example every minute, and save it to the harddisk and could also upload it to an ftp server. This can also placed into the sitemap, of course.

Resources

Use Motion to process video streams

Motion is a program that monitors the video signal from cameras. It is able to detect if a significant part of the picture has changed; in other words, it can detect motion. Some openHAB users stream video to the Motion application and then inject motion triggers into openHAB using itsp ReST API.

From Ben Jones:

This is very easy to setup and works well with all sorts of different IP/USB cameras. There is a nice simple REST API for enabling/disabling motion detection and it provides a mechanism for calling out to custom scripts and programs on various events.

For example I have 4 cameras (all different types) dotted around the house all being monitoring by one instance of motion on my home server. I can view the camera streams via the motion web server, arm/disarm motion detection via a simple HTTP binding in openHAB, and then call out to a python script which updates another Camera_MotionDetection item in openHAB via the REST API.

I also have another script which posts the motion snapshot to a private Slack channel so I can quickly monitor from my phone when away from home.

... [T]he only thing it can handle is the arm/disarm logic.

From Nicholas Waterton:

I use the HD low level protocol for Foscam, to get the video and motion triggers (you can get audio also, but it's a pain). I built the low level protocol into a modified version of motion (plus HD motion tracking for the pan/tilt cameras), and have a python program to trap low level motion triggers. The low level protocol is much faster than using the URL interface (but it's not published anywhere, so it's deciphered by trial and error).

Normally you would use rtsp to get the camera image, but it has horrible lag on it, and distorts frequently. The low level protocol pulls the H.264 feed directly from the camera, with little delay, and I decode it using the ffmpeg decoder libraries (libavdecode). You can do this in motion or python, it's pushing it to try decoding mainstream HD video at 30 fps in python though. substream video or snapshots work OK.

You need the latest version of "motion" the earlier ones did not support RTSP, and you need FFMPEG as well (motion uses FFMPEG libraries to decode the H.264 video stream).

The Python script in the below "resources" does not need motion (or any other program), it works out of the box (low level HD comms protocol is built in). It only works with HD cameras (SD uses a different protocol).

The program is a command line program that takes various options - see -h for help as shown below:

usage: Camera_Motion_Detect.py [-h] [-c CAMERAIP] [-p OHPORT] [-P CAMERAPORT]
                               [-k KEY] [-v VALUE] [-n NAME] [-D]
                               [openhabhost]

Camera Motion Detector for Openhab

positional arguments:
  openhabhost           ip adress of Openhab host (default: localhost)

optional arguments:
  -h, --help            show this help message and exit
  -c CAMERAIP, --cameraip CAMERAIP
                        ip adress of camera host(default: 192.168.100.110)
  -p OHPORT, --ohport OHPORT
                        openhab REST port number (default: 8009)
  -P CAMERAPORT, --cameraport CAMERAPORT
                        Camera port to connect to (default: 34101)
  -k KEY, --key KEY     openhab item (Switch) to activate (default:
                        HallwayMotion)
  -v VALUE, --value VALUE
                        value to send to openhab item key (default: ON)
  -n NAME, --name NAME  name of the camera -u sed for log file (default:
                        Hallway)
  -D, --debug           debug mode

You may want to edit the script to change the defaults, camera login, password, default log file location etc. There is a lot of extraneous stuff in there - unused modules/functions and so on, as it's hacked out of a larger server program (sorry I wrote this for myself). It connects to the HD FosCam, and basically ignores all the data (video, audio etc) until it sees a motion detect code (code 111). It then sends a defined value (ON default) to a defined key (switch item) via the openhab REST interface. It has to be a Switch item, as you can't sent a value to a Contact item via the REST interface (Openhab quirk?). The program runs until you ^C out of it. I leave multiple instances of it (one for each camera) running for days (weeks even) with no problems.

Resources

Use Apache FTP Server FTPlet

From Martin Raepple:

The Apache FTP Server can be used to capture the motion detection event by implementing an FTPlet. An FTPlet can hook into the common FTP commands such as login. If the Foscam HD IP camera is configured for FTP in the capture storage location settings, a motion detection alarm triggers the camera to call the FTPlet's onLogin method.

The sample FTPlet below takes the camera's name from the authenticated user in the onLogin method. Therefore, each camera must have its own user in the Apache FTP Server (configured with res/conf/users.properties in the server's installation directory). Next, it calls openHAB's REST API to change the state of the camera's assigned motion detection switch item, which may cause other rules in openHAB to trigger. On my openHAB installation, the FTPlet also checks for the current state of a presence item. If nobody is at home, the login operation will continue and the captured video is stored on the FTP Server. Otherwise, there is no need to record the video, and the session will be disconnected.

Similar to some of the other solutions provided above, this approach does not require to openHAB to poll for a motion alarm. It only requires the Apache FTP Server to run on a host which can be reached by the camera(s) and openHAB. The Apache FTP Server is a lightweight process with very little resource consumption and can be deployed on the same host where openHAB is running.

Resources


General API Access

The Foscam HD API is exposed as an HTTP API. This can be accessed directly using the openHAB HTTP binding but it can also be accessed using wrapper libraries for various programming languages.

JSR223 and Jython

From Steve Bate:

The foscam-python-lib (see link below) is compatible with Jython and can be used with the openHAB JSR223 binding and Jython rules to access Foscam HD cameras. The library supports most of the Foscam CGI API. The library is useful because some of the CGI functionality is not practical to use with just an HTTP binding. For example, to enable or disable motion control, the current motion detect config must be retrieved and merged with the enabled/disable value. Otherwise, all the other motion detection configurations will be reset.

The library can also be used with CPython outside of openHAB and then use the openhab ReST API to inject state changes and the exec binding to invoke external CPython scripts.

API Wrapper Libraries

Language Library Notes
Python foscam-python-lib Jython compatible, H.264
Javascript foscam-javascript-lib H.264
Javascript foscamhd-client Node.js, H.264
Python ipcam
.NET Foscon

Resources


Related Discussions

Installation


User Interfaces


Community

(link to openHAB forum)

Development



Misc


Samples

A good source of inspiration and tips from users gathered over the years. Be aware that things may have changed since they were written and some examples might not work correctly.

Please update the wiki if you do come across any out of date information.

Use case examples

Collections of Rules on a single page

Single Rules

Scripts


Release Notes

Clone this wiki locally