Introduction: Sonoff Camera ONVIF HACK

The Sonoff GK-200MP2-B Wi-Fi IP Security Camera is a handy tool to improve home security. It is cheap (+-$30), support pan and tilt (PTZ). It contains an on board micro SD card to record videos continuously and/or when motion is detected. It also includes an alarm when motion is detected. It is accessed via the eWeLink app (like all other sonoff devices) on iOS or android devices, which required cloud access.

This camera is however not ONVIF compliant out off the box, which make it difficult to operate it via your other NVR software and hardware from your local network

To make this camera ONVIF compliant, additions to the firmware (an embedded linux system) needs to be done. This additions is done on the onboard sd card of the GK-200MP2-B Wi-Fi IP Security Camera and can be undone by removing the files. After the firmware additions is made, the camera will be ONVIF compliant and you will be able to monitor and control the camera via an internal website hosted on the camera itself. You can access the recording on the internal micro SD card via this website

Additional optional steps:

After you made the camera ONVIF compliant, you can connect it to your existing ONVIF compliant NVR Hardware (The camera is already an NVR) or NVR software, or create a python-onvif NVR on your PC or Raspberry PI, for which I will provide instructions. This additional steps should also work on other (Out of the box) ONVIF cameras, like v380 cameras coming in all shapes in different countries like this one in South Africa https://www.communica.co.za/products/xy-wifi-cam-od4mmv380-ptz

Supplies

  1. Sonoff GK-200MP2-B Wi-Fi IP Security Camera
  2. Linux PC/Raspberry PI (preferred) or Windows PC to build your own extra NVR (Optional) with Python and VLC. Linux machines is preferred since there exist python libraries to discover ONVIF cameras which is only POSIX (read Linux) compatible.
  3. Python Skills (Optional for your own Python/VLC NVR)
  4. Micro SD Card reader (Optional for your own Python/VLC NVR)

I will test other (Out of the box) ONVIF Cameras to check how they react to the Python ONVIF module for Pan/Tilt

Step 1: Make the Sonoff WiFi Secuirity Camera ONVIF Compliant

This Step is applicable to the Sonoff cameras only.

The ONVIF hack for this camera was done by roleoroleo on github

https://github.com/roleoroleo/sonoff-hack .

All the different releases of this firmware addition is available at https://github.com/roleoroleo/sonoff-hack/release...

Steps:

1. Switch of the Sonoff cam and remove the micro SD Card

2. Insert the Micro SD Card in your Windows or Raspberry PI computer Micro SD Card reader

3. Download the firmware from https://github.com/roleoroleo/sonoff-hack/release...

4. Extract the firmware on the Micro SD Card. The file boot.sh and directory sonoff-hack should be in the root directory of the Micro SD Card, otherwise it will not work

5. Insert the Micro SD Card back in the Camera, switch the camera on and wait for it to complete the boot.

6. You can now open your browser and go to htt://camera_ip_address:8080

You will notice that a webserver was installed on port 8080 of the sonoff camera. You can play around with the settings, see the video stream (PTZ menu), pan and tilt the camera (PTZ menu) and download the videos created when motion was detected (Motion Events menu).

In the Configuration Menu you can

  1. Disable the cloud (Camera will not be available in eWeLink app)
  2. Make sure ONVIF and ONVIF WS-Discovery is on
  3. ONVIF Profile = Both
  4. ONVIF Network Interface=Wireless
  5. Do not change any port numbers
  6. You can enable services like FTP, telnet and SSH. Make sure you provide a proper password. Just remember that the services use ram on the Sonoff camera and may slow response down.

You should now be able to view and control the camera with your ONVIF compliant NVR or NVR Software

If you need to access all videos, not only videos created with motion, you should enable ftp (In configuration menu of the web) and access the Micro SD via FTP to download the videos.

Step 2: Build Your Python VLC NVR System

You now need to install VLC media player on your windows or
Linux/Raspberry PI computer. VLC is included in the latest Raspberry PI OS.

Python-onvif must also be installed.

https://github.com/quatanium/python-onvif (Python2.7)

https://github.com/rambo/onvif-py3 (Python3)

Install Python onvif for windows PC:

Download the VLC Media player https://www.videolan.org/ and install it.

Make sure Python2.7 is installed. This Python-onvif library is not compatible with Python 3 (See https://pypi.org/project/onvif/)

Install Python onvif by opening the command line (cmd.exe) and type in

pip install suds
pip install suds_passworddigest
install onvif

Go to your python installation directory (c:\Python27). You should see a directory wsdl. Copy this directory into C:\Python27\Lib\site-packages\

Install Python onvif for Linux/Raspberry PI (Python 2.7)

Make sure Python2.7 is installed. This Python onvif is not compatible with Python 3

Check if VLC is installed. Run vlc in the cmdl

If you need to install it, run

sudo apt-get install vlc

install Python onvif by running

sudo pip install suds
sudo pip install suds_passworddigest
sudo pip install onvif 

Make sure you have allocated at least 512mb GPU Memory in Preferences>Raspberry PI Configuration>Performance

Also make sure that your wsdl directory is copied to /etc/onvif/. Search for it with

sudo pcmanfm

and copy it to /etc/onvif/

Install Python onvif for Linux/Raspberry PI (Python 3)

The Python 3 Module for Python onvif is available at https://github.com/rambo/onvif-py3

Start by installing the dependencies

sudo pip3 install suds-py3
sudo pip3 install git+https://github.com/miuhaki/suds-passworddigest-py3.git

Now you are ready to install Python3 onvif

sudo pip3 install onvif-py3

Also make sure that your wsdl directory is copied to /etc/onvif/. Search for it with

sudo pcmanfm

and copy it to /etc/onvif/

Install the WS-Discovery module on Raspberry PI/Linux PC only

This WS-Discovery module is not compatible with Windows. It is POSIX (UNIX Like) compatible

Some ONVIF devices can be discovered by WS-Discovery. WS-Discovery may also pickup non ONVIF Devices

Info on WS-Discovery is available at https://pypi.org/project/WSDiscovery/

For Python 2.7

sudo pip install WSDiscovery==1.1.2

For Python 3

sudo pip3 install WSDiscovery==2.0.0

Step 3: ​Use VLC As the NVR

You should now find your RTSP (Real Time Streaming Protocol) URL for your camera.

Sonoff WiFi Camera RTSP Stream URLs:

HD Stream:

rtsp://rtsp:12345678@camera_ip_adress:554/av_stream/ch0 

SD Stream:

rtsp://rtsp:12345678@camera_ip_adress:554/av_stream/ch1

The V380 Camera RTSP Stream URL:

rtsp://username:password@camera_ip_adress:554/Live/ch00_1

Other Cameras:

To discover the RTSP URL of your camera (if it uses RTSP) may be a difficult task.

Valuable resource is https://www.getscw.com/decoding/rtsp

RTSP URLs should be published in your camera manual

Wireshark may also assist to detect RTSP Streams if you have the know how, but Network Traffic Package Analysis is not my strong point

If you discover RTSP streams of other cameras, please provide them in the comments.

Run your RTSP Stream in VLC

Open VLC, Go To Media>Open Network Stream and type the RTSP_URL or in Linux run

vlc RTSP_URL

VLC Should now display the Video Stream.

Sonoff VLC RTSP Login Problems:

If the login credentials (username:password) of rtsp:123456789 is not excepted, use the username and password you set in http://camera_ip_adress:8080 . If you still have problems, restart the sonoff camera, wait for it to boot and go to the eWeLink app, open Settings (...)>More Settings>RTSP and press Create RTSP Link.

On Windows , You can now run the attach python2 script PT_Control.py to pan and tilt the camera. Remember to provide the correct username and password in continuous_move():

On the Raspberry PI/ Linux you can use the Python2/3 script (PT_Control_WSDiscovery.py/PT_Control_WSDiscovery_python3.py) that also detects ONVIF cameras. WSDiscovery will print the IP in the form xxx.xxx.xxx.xxx if the port number is 80 and xxx.xxx.xxx.xxx:yyyy if the port is number is something else(yyyy). This IP and port number is used to log into the camera. See the attach photo for detail

.It seems like your wsdl directory should also be in the right place. For me it was copied (with pip install onvif) to /home/pi/.local/wsdl/ , but it is suppose to be in /home/pi/.local/lib/python2.7/dist-packages/wsdl/. To fix this, run File Manager as root and copy the directory wsdl where python expect it. Use the Tools>Find Files menu to find the wsdl directory

sudo pcmanfm

Use the VLC Playback>Record menu to record video

If you get an error message regarding devicemgmt.wsdl , make sure the wsdl directory is where python expects it to be.

Step 4: Future Developments

I plan to integrate opencv into the python script or even (if possible) load opencv on the camera itself to enable object identification and tracking. This is already in development. I hope the home assistant users will also find the python pan tilt scripts useful. They were looking for it.

If you want to enable ONVIF Pan / Tilt in motionEye NVR Software, check this out https://www.instructables.com/MOTIONEYE-NVR-PANTILT/

Automation Contest

Participated in the
Automation Contest