Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


peejayw

1913 posts

Uber Geek
+1 received by user: 123


#260059 8-Nov-2019 14:39
Send private message

Any kind Python experts there who could produce some code to turn one GPIO pin on and off multiple times per day, 7 days a week? I need to specify the start and end times ie On at 8am off at 8:02 am for probably 6 time slots per day.

 

My knowledge of Python is pretty basic but if someone could produce the basic script I could then modify it as required.





 I'm supposed to respect my elders, but it's getting harder and harder for me to find one now.


Create new topic
bazzer
3438 posts

Uber Geek
+1 received by user: 267

Trusted

  #2350311 8-Nov-2019 17:45
Send private message

  • What's your timeframe?
  • Does it need to be doing anything else at the same time (i.e. what OS does it need running in the background, assuming Raspbian)?
  • How do you imagine specifying the times (text config file)?

I don't know how to do this, but it doesn't sound that hard so I might be willing to have a crack if no one else can help you out but I don't know when I'd be able to get to it.




afe66
3181 posts

Uber Geek
+1 received by user: 1678

Lifetime subscriber

  #2350321 8-Nov-2019 18:07
Send private message

Look at the raspberry pi foundation education documents on how to use the gpio connectors. There are tutorials from memory which teach you how to light a led so if you could start looking there.

There are lots of basic educational resources out there.

My programming skills ended with bbc basic in 1980s and I was able to write a python script converting entered text into Morse code beeps with beeper connected to gpio and a bread board.

You might surprise yourself in how easy it could be.

1yippy1
67 posts

Master Geek
+1 received by user: 1


  #2350393 8-Nov-2019 18:50
Send private message

Check this out https://www.raspberrypi.org/forums/viewtopic.php?t=195014




itxtme
2102 posts

Uber Geek
+1 received by user: 557


  #2350505 8-Nov-2019 23:48
Send private message

First, make sure you test with a voltage meter.  Also make sure your assumptions on GPIO channels match the Pi version you are attempting.  Different versions have different locations and you may be looking at the wrong map!  Make sure you are using an always off channel.

 

Your requirement for time frames isnt too clear, but would it not be easier to have two python scripts, one that turns the GPIO on and then one that turns off and have them timed on your cron job?

 

On (08:00, 10:00, 12:00)

 

0 8,10,12 * * * /usr/bin/python turnOn.py >/dev/null 2>&1

 

Off at (08:02,10:02,12:02)

 

2 8,10,12 * * * /usr/bin/python turnOff.py >/dev/null 2>&1

 

I dont code in Python, however I believe something along the lines of below would work as a python script (untested).  I have used similar in a relay I played with.

 

Turn on

 


import RPi.GPIO as GPIO
import time

 

channel = 21

 

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)

 

#Turns GPIO on
def motor_on(pin):
    GPIO.output(pin, GPIO.HIGH)  # Turn on

 

if __name__ == '__main__':
    try:
        motor_on(channel)
    except KeyboardInterrupt:
        GPIO.cleanup()

 

 

 

 

Turn Off

 


import RPi.GPIO as GPIO
import time

 

channel = 21

 

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)

 

#Turns GPIO off
def motor_off(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn off

 


if __name__ == '__main__':
    try:
        motor_off(channel)
        GPIO.cleanup()
    except KeyboardInterrupt:
        GPIO.cleanup()

 

 

 

 

 


peejayw

1913 posts

Uber Geek
+1 received by user: 123


  #2350544 9-Nov-2019 07:27
Send private message

Thanks for the ideas, will see where they lead.





 I'm supposed to respect my elders, but it's getting harder and harder for me to find one now.


chevrolux
4962 posts

Uber Geek
+1 received by user: 2638
Inactive user


  #2350565 9-Nov-2019 10:10
Send private message

Use Node-Red?

The BigTimer module is really good.

 
 
 

Support Geekzone with one-off or recurring donations Donate via PressPatron.
huckster
886 posts

Ultimate Geek
+1 received by user: 460

ID Verified
Lifetime subscriber

  #2350646 9-Nov-2019 10:50
Send private message

Cron is good and simple way to do this if the Pi remains on the network and sync's time. It has no real-time clock so it may get confused time-wise.

 

You can always buy a real-time clock add-on.


Create new topic








Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.