Unlike other tutorials this one could seem a little more advanced. You will need basic knowledge of linux (in this case CentOS) but there is nothing you should be scared of.
You will need:
- Hikvision camera
- Raspberry PI / Orange PI / VPS (we will use this option in the tutorial) / PC
I will show you how to get the events from Hikvision using a VPS. If you create an account with DigitalOcean using this link you will receive $100 in credit for 60 days.
What is a VPS or Virtual Private Server?
A virtual private server is an environment that mimics a dedicated server within a shared hosting environment. A VPS runs its own operating system and you will have superuser-level access.
Setting-up your VPS
Create and activate your account with DigitalOcean (you can use any other hosting provider, it doesn’t matter).
Create a new “Droplet” using CentOS 7.5, select then the smallest size ($5/month is ok). Choose a datacenter region closest to you and click “Create”. In a few moments you will receive an email with the IP address and login details. You will need Putty to get access to the VPS console.
You can monitor multiple cameras using a single VPS.
Once you managed to login the first step is to update the software packages, install NodeJS, Git, Nano and PM2.
yum update -y curl --silent --location https://rpm.nodesource.com/setup_8.x | bash - yum install gcc-c++ make -y yum install nodejs -y yum install git -y yum install nano -y npm install pm2@latest -g pm2 startup
First we need to download the script that we will use to monitor events on cameras.
git clone https://github.com/eusfelix/hikvision-to-mqtt.git cd hikvision-to-mqtt npm install
Now we’ll have a new folder named “hikvision-to-mqtt” with the script and all the needed libraries.
You will need to configure a “port forward” on your router in order for the script to connect to your camera. Only port 80 is needed for this to work (port example: exterior – 8181; interior – 80) . Security tip: if your router has a “source” option on the port forward rule, enter your VPS IP. That way only requests from your vps are forwarded to the camera.
Let’s configure the monitor for the first camera:
cp main.js camera1.js nano camera1.js
Edit the “options” and “mqttoptions” to match your setup.
- Host: your public ip (if static) or hostname
- Port: from the port forwarding rule, the external port.
- User/Password – the same that you use when you connect to the camera
- Mqtt host: mqtt://mqtt.iotwithus.com
- username: -none-
- password: -none-
- topic: something unique for your camera like davidshikvision01-jyiYDrP
Save the file using Ctrl + x
Running the script
First, let’s test. Run “node camera1.js” to start the script. It will try to connect to the camera and then send the events to the mqtt you specified. Use a client to connect to the mqtt server and check.
If all is ok, run “pm2 start camera1.js” to start the script using pm2. This will keep your script running even after reboot.
To add a new camera just copy the main.js to another file (cp main.js camera2.js) and repeat the steps above.