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

1393 posts

Uber Geek


#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
3316 posts

Uber Geek

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
2464 posts

Uber Geek

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
45 posts

Geek

Subscriber

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

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


itxtme
1779 posts

Uber Geek

Subscriber

  #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

1393 posts

Uber Geek


  #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
4607 posts

Uber Geek

Trusted

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

Use Node-Red?

The BigTimer module is really good.

huckster
435 posts

Ultimate Geek

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





News »

Nanoleaf enhances lighting line with launch of Triangles and Mini Triangles
Posted 17-Oct-2020 20:18


Synology unveils DS16211+
Posted 17-Oct-2020 20:12


Ingram Micro introduces FootfallCam to New Zealand channel
Posted 17-Oct-2020 20:06


Dropbox adopts Virtual First working policy
Posted 17-Oct-2020 19:47


OPPO announces Reno4 Series 5G line-up in NZ
Posted 16-Oct-2020 08:52


Microsoft Highway to a Hundred expands to Asia Pacific
Posted 14-Oct-2020 09:34


Spark turns on 5G in Auckland
Posted 14-Oct-2020 09:29


AMD Launches AMD Ryzen 5000 Series Desktop Processors
Posted 9-Oct-2020 10:13


Teletrac Navman launches integrated multi-camera solution for transport and logistics industry
Posted 8-Oct-2020 10:57


Farmside hits 10,000 RBI customers
Posted 7-Oct-2020 15:32


NordVPN starts deploying colocated servers
Posted 7-Oct-2020 09:00


Google introduces Nest Wifi routers in New Zealand
Posted 7-Oct-2020 05:00


Orcon to bundle Google Nest Wifi router with new accounts
Posted 7-Oct-2020 05:00


Epay and Centrapay partner to create digital gift cards
Posted 2-Oct-2020 17:34


Inseego launches 5G MiFi M2000 mobile hotspot
Posted 2-Oct-2020 14:53









Geekzone Live »

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


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



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.

Alternatively, you can receive a daily email with Geekzone updates.