I use the PiFace2 relays outputs to switch on/off two webcams.
With python2 and an older version of the library, I could use this:
import pifacedigitalio as pfio pfio.init(false)
With this init, the PiFace kept it's last states of the outputs, especially the relays kept "on", if thy where switched on.
With the new version, the relais always switch shortly off, when I start the script again.
But they should keep the last state until I set them to a new state.
The script is called by a cron job periodically to check, whether cams should switched on or off.
Within the script I check for the sunrise/sunset and set the relays accorgingly.
How can I prevent the resetting of the output state of the relays, when I start the script again?
#!/usr/bin/env python3
import ephem
import datetime
import pifacedigitalio
pfio = pifacedigitalio.PiFaceDigital()
m_latitude = '49.565389'
m_longitude = '11.232158'
m_now = datetime.date.today()
location = ephem.Observer()
location.pressure = 0
location.horizon = '-6'
location.lat, location.long = m_latitude, m_longitude
location.date = m_now
m_utc_twilight_begin = ephem.Date(location.next_rising(ephem.Sun(), use_center=True))
m_utc_twilight_end = ephem.Date(location.next_setting(ephem.Sun(), use_center=True))
m_loc_twilight_begin = ephem.localtime(m_utc_twilight_begin)
m_loc_twilight_end = ephem.localtime(m_utc_twilight_end)
m_time_now = datetime.datetime.now()
if m_time_now.time() >= m_loc_twilight_begin.time() and m_time_now.time() <= m_loc_twilight_end.time():
if pfio.relays[0].value == 0:
pfio.relays[0].turn_on()
if pfio.relays[1].value == 0:
pfio.relays[1].turn_on()
else:
if pfio.relays[0].value == 1:
pfio.relays[0].turn_off()
if pfio.relays[1].value == 1:
pfio.relays[1].turn_off()
pfio.deinit_board()
exit()




