Category Archives: Programming

Hikvision Separate Day / Night Settings with Raspberry Pi

The Hikvision cameras I use for LPR don’t have the ability to manually control the gain of the sensor when the camera is set in “Auto” mode. “Auto” mode pushes the camera from night to day mode automagically by detecting the ambient light in the scene. This is normally fine, but for LPR the gain needs to be set very low to compensate for the bright reflections that come from license plates. Manually pushing the camera into night or day mode enables the control of gain, but making the camera switch automagically becomes impossible.

To get around this issue I use a Raspberry Pi to push configuration changes to the camera manually. It finds the local sunrise/sunset times based on latitude/longitude and uses those times to schedule the change. If you are using a NVR you need to enable remote camera management in the settings so you can reach each individual camera.

Place this first block of code into a file named

import requests
import json
import time
import subprocess
from dateutil import tz
from datetime import datetime

longitude = ''
latitude = ''
timezone = ''#America/New_York
ips = [] #['', '']
user = ''
password = ''
nightOrDayPath = '/home/pi/'

r = requests.get('' + latitude + '&lng=' + longitude + '&date=today')
json = json.loads(r.content.decode('utf-8'))

from_zone = tz.gettz('UTC')
to_zone = tz.gettz(timezone)

utcSunset = datetime.strptime(time.strftime("%Y-%m-%d") + " " + json['results']['sunset'], '%Y-%m-%d %I:%M:%S %p')
utcSunset = utcSunset.replace(tzinfo=from_zone)
utcSunrise = datetime.strptime(time.strftime("%Y-%m-%d") + " " + json['results']['sunrise'], '%Y-%m-%d %I:%M:%S %p')
utcSunrise = utcSunrise.replace(tzinfo=from_zone)
sunset = utcSunset.astimezone(to_zone)
sunrise = utcSunrise.astimezone(to_zone)

print("sunrise is " + sunrise.strftime('%H:%M %p'))
print("sunset is " +  sunset.strftime('%H:%M %p'))

for ip in ips:
        dayCommand = 'python3 ' + nightOrDayPath + ' --ip ' + ip + ' --user ' + user + ' --password ' + password + ' --dayornight day'
        nightCommand = 'python3 ' + nightOrDayPath + ' --ip ' + ip + ' --user ' + user + ' --password ' + password + ' --dayornight night'

        p = subprocess.Popen('at ' + sunrise.strftime('%H:%M') , stdin=subprocess.PIPE, shell=True)
        p = subprocess.Popen('at ' + sunset.strftime('%H:%M'), stdin=subprocess.PIPE, shell=True)

print('tasks are scheduled')

Create a cron job that will execute after midnight each day.

1 0 * * * /usr/bin/python3 /home/pi/

Put the second block in

#!/usr/bin/env python
import requests
from requests.auth import HTTPBasicAuth
import sys
import argparse

parser = argparse.ArgumentParser(description='Set day and night status on Hikvision cameras.')
parser.add_argument('--ip', help='IP address and port to send command to')
parser.add_argument('--user', help='Hikvision username')
parser.add_argument('--password', help='Hikvision password')
parser.add_argument('--dayornight', help='night or day', choices=['night', 'day'])


xml = '<ImageChannel version="2.0" xmlns=""><IrcutFilter version="2.0" xmlns=""><IrcutFilterType>' + args.dayornight + '</IrcutFilterType></IrcutFilter></Im$
requests.put('http://' + args.ip + '/ISAPI/Image/channels/1', data=xml, auth=HTTPBasicAuth(args.user, args.password));

DataTables Search box blocking backspace key?

Working with DataTables for the first time I was having some issues with the UI. The search textbox was not allowing the user the backspace. They were able to select the text and use the delete key to clear it out, but it almost felt like there was a script capturing and suppressing the backspace key press. Upon closer inspection the “type” of the input was “Search”. According to w3schools (and Visual Studio’s Intellisense) this was valid HTML. The example of their site worked as well. Perhaps it had something to do with the other scripts running on the page, or possibly the Bootstrap library addon for DataTables? Changing the type to “Text” fixed the issue. I found an easy workaround here until I can fix the root of the problem.