element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum PiFace 2 with python3 - pifacedigitalio resets always on init()
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 1 reply
  • Subscribers 705 subscribers
  • Views 556 views
  • Users 0 members are here
  • piface 2
  • raspberry_pi
  • pifacedigital2
Related

PiFace 2 with python3 - pifacedigitalio resets always on init()

gumbofroehn
gumbofroehn over 9 years ago

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()

  • Sign in to reply
  • Cancel
Parents
  • clem57
    0 clem57 over 9 years ago

    If you want to save the states, you can write hem to a file when changed. Then on initialization, you reread the state to know what to set/reset states. Check https://docs.python.org/2/tutorial/inputoutput.html  which explains I/O under python.

    C

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • clem57
    0 clem57 over 9 years ago

    If you want to save the states, you can write hem to a file when changed. Then on initialization, you reread the state to know what to set/reset states. Check https://docs.python.org/2/tutorial/inputoutput.html  which explains I/O under python.

    C

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube