Python Code to change text overlay & shutter

toomscj7

n3wb
Joined
Jul 2, 2015
Messages
25
Reaction score
19
Sharing this in case some or all of this is useful for others. I have a $35 Raspberry Pi2 linux system that Im using at a camera constroller. I plan to do more with this in the future, but for now this is where I'm at with it. I use the same Raspberry Pi2 to control my lawn irrigation sprinklers too so its convenient to use it for this also.

Goals:
1) When sun is up use 1/1000 shutter speeds, when sun is down use 1/30 or 1/25
2) Include a custom text overlay. In this case I am using wind & temperature data from weather underground.
3) Run every 10 minutes for weather data, only adjust shutter speed twice a day

Some day when I get around to it I may tie the cameras and lawn sprinklers together. I could have them shut off it its detects motion while they are running. Or vice versa have them turn on when they detect motion during the day (Solicitor and rabbits eating the garden defense turrets).


PYTHON CODE:
===============================================================
import datetime
import requests
import time
import urllib2
import json


#Camera Shutter Strings
cam1 = 'http://CAMERA1IPHERE/Image/channels/1/Shutter'
cam2 = 'http://CAMERA2IPHERE/Image/channels/1/Shutter'
cam3 = 'http://CAMERA3IPHERE/Image/channels/1/Shutter'


#Camera Overlay Strings
cam1overlay = 'http://CAMERA1IPHERE/Video/inputs/channels/1/overlays/text/1'
cam2overlay = 'http://CAMERA2IPHERE/Video/inputs/channels/1/overlays/text/1'
cam3overlay = 'http://CAMERA3IPHERE/Video/inputs/channels/1/overlays/text/1'


#Timestamp for last time the shutter setting was changed
lastchange = ''
#List of the status codes of each camera so you can see which ones were changed successfully
statuscodes = []
#Weather Unground API Key (get this from their site)
key = "APIKEYHERE"


while True:
try:
#Splits out the datatime value
now = str(datetime.datetime.now()).split(' ')[1].split(':')[0]
#Builds API URL for WUnderground to get temp, wind data. I have it polling data from a specific weather station. "KINGREEN15"
url = 'http://api.wunderground.com/api/' + key + '/geolookup/conditions/q/pws:KINGREEN15.json'
f = urllib2.urlopen(url)
json_string = f.read()
parsed_json = json.loads(json_string)
temperature_string = parsed_json['current_observation']['temp_f']
wind_dir_string = parsed_json['current_observation']['wind_dir']
wind_speed_string = parsed_json['current_observation']['wind_mph']

#Builds url to get sunrise/sunset data for my zip code
url = 'http://api.wunderground.com/api/' + key + '/astronomy/q/IN/46143.json'
f = urllib2.urlopen(url)
json_string = f.read()
parsed_json = json.loads(json_string)
sunrise_string = parsed_json['sun_phase']['sunrise']['hour']
sunset_string = parsed_json['sun_phase']['sunset']['hour']

#Builds a temporary variable with the XML data for the overlay
tempdata = '<?xml version="1.0" encoding="UTF-8"?>\n'
tempdata = tempdata + '<TextOverlay version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">\n'
tempdata = tempdata + '<id>1</id>\n'
tempdata = tempdata + '<enabled>true</enabled>\n'
tempdata = tempdata + '<posX>16</posX>\n'
tempdata = tempdata + '<posY>0</posY>\n'
tempdata = tempdata + '<message>' + str(temperature_string) + 'F - Wind: '+ str(wind_speed_string) + '(' + str(wind_dir_string) + ')</message>\n'
tempdata = tempdata + '</TextOverlay>'

#Checks to see if the sun is up, if up. If so changes cameras to 1/1000 shutter speed
if int(now) >= int(sunrise_string) and int(now) < int(sunset_string):
shutter = 'shutterup1000.xml'
with open(shutter) as fh:
data = fh.read()
#Uses lastchange to only do this once a day before the sun goes down.
if lastchange != 'up':
statuscodes = []
r = requests.put(cam1, data=data,auth = ('admin', 'password here'))
statuscodes.append(r.status_code)
r = requests.put(cam2, data=data,auth = ('admin', 'password here'))
statuscodes.append(r.status_code)
r = requests.put(cam3, data=data,auth = ('admin', 'password here'))
statuscodes.append(r.status_code)
lastchange = 'up'
print str(datetime.datetime.now()) + ' sun is up, changed setting to 1/1000 - Status Code: ' + str(statuscodes)

#If the sun is down, changes 2 cameras to 1/30 and one to 1/25 shutter
else:
shutter = 'shutterdown30.xml'
shutter2 = 'shutterdown25.xml'
with open(shutter) as fh:
data = fh.read()
with open(shutter2) as fh:
data2 = fh.read()
if lastchange != 'down':
statuscodes = []
r = requests.put(cam1, data=data,auth = ('admin', 'password here'))
statuscodes.append(r.status_code)
r = requests.put(cam2, data=data,auth = ('admin', 'password here'))
statuscodes.append(r.status_code)
r = requests.put(cam3, data=data2,auth = ('admin', 'password here'))
statuscodes.append(r.status_code)
lastchange = 'down'
print str(datetime.datetime.now()) + ' sun is down, changed setting to 1/30 or 1/25 - Status Code: ' + str(statuscodes)


#Pushes out the overlay data to the cameras
r = requests.put(cam1overlay, data=tempdata,auth = ('admin', 'password here'))
r = requests.put(cam2overlay, data=tempdata,auth = ('admin', 'password here'))
r = requests.put(cam3overlay, data=tempdata,auth = ('admin', 'password here'))






time.sleep(600)
except:
print 'crashed'
time.sleep(600)
==============================================================
XML Flat Files Data:

shutterdown30.xml
<?xml version="1.0" encoding="UTF-8"?>
<Shutter version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
<ShutterLevel>1/30</ShutterLevel>
</Shutter>


shutterdown25.xml
<?xml version="1.0" encoding="UTF-8"?>
<Shutter version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
<ShutterLevel>1/25</ShutterLevel>
</Shutter>

shutterup1000.xml
<?xml version="1.0" encoding="UTF-8"?>
<Shutter version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
<ShutterLevel>1/1000</ShutterLevel>
</Shutter>







================================
Console Log:

C:\cams>python HouseSun.py
2015-07-05 23:01:37.239000 sun is down, changed setting to 1/30 or 1/25 - Status Code: [200, 200, 200]
2015-07-06 06:02:10.364000 sun is up, changed setting to 1/1000 - Status Code: [200, 200, 200]

 

Attachments

toomscj7

n3wb
Joined
Jul 2, 2015
Messages
25
Reaction score
19
The getpass Python module is probably the more secure way to capture the password than what I did. Call me lazy :)
 
Top