# Schedule driven GPIO events



## Phishfry (Jun 6, 2021)

I want to use GPIO outputs to drive an IOT Power Strip.





						IoT Power Relay - COM-14236 - SparkFun Electronics
					

Do you want to control a standard wall outlet device with your microcontroller, but don’t want to mess with the high-voltage wiring? The IoT Power Relay




					www.sparkfun.com
				




My question is about triggering scheduled events.
For instance I want to issue a GPIO command to turn a pin on and off on a schedule.

So I could use `cron` but would like to avoid system utilities.
Looking around I found sysutils/fcron and sysutils/anacron. Both can run commands on a schedule.
anacron seems to use daily jobs. I need minute precision.

Another option I have considered is deskutils/calcurse.
I like the approach of scheduling by calendar. It can run commands too.

Can you recommend any programs I may have missed.

I need to schedule a command like `gpioctl` to run 4 times a day.


----------



## obsigna (Jun 6, 2021)

Even, if I don't exactly understand, what is wrong with a cronjob -- I got a lot of it in my crontabs --, the most effective solution would be, to write your own daemon (see here: https://forums.freebsd.org/threads/polling-a-sysctl-value.63501), wich you would modify to do a sleep(3) for the required amount of seconds (21600 s), and then calls the respective gpio(3) instruction.


----------



## Phishfry (Jun 6, 2021)

Here is a sample task I need to do. I have a Siemans PLC at work that is old and getting flaky.
It keeps bad time and cannot handle DST time changes. I have to mess with it too much.

Surly I could find some timer circuit for less $ but I like to have an SSH window into the controller.
Plus I could feed cameras through it for auxiliary function.

This PLC controls a solenoid for Air Powered Horn that sounds at the beginning and end of the workday.
I would like to replace it with something else. Like a old Pi or anything with FreeBSD GPIO.

So I need to accurately trigger a solenoid 4 times a day.

I was looking at this program with interest. deskutils/remind/
This is overkill but I like the flexibility. Especially with complex scheduling.


----------



## Alain De Vos (Jun 6, 2021)

I moved my cron settings to fcron. fcron runs on seconds precision, and does cron and anacron.


----------



## evantaylor (Jun 7, 2021)

I’ve not played with GPIO under FreeBSD, but I have gotten i2c gpio pins working for my own project and found this:



			schedule — schedule 1.1.0 documentation
		


Using it for both Python and micropython options for a project this year.  May suit your needs if you can do gpio under FreeBSD/Python.


----------



## Phishfry (Jun 16, 2021)

obsigna said:


> the most effective solution would be, to write your own daemon


Thank You for mentioning that. In the end that is going to be the best solution.
But I think I want a daemon for each pin. A GPIO service for each pin. Totally independent.
Then I could make one GPIO pin service dependent on the state of another.

I really see the need the for an event driven GPIO program.
Cron is not preferable to me.

Anybody ever hear of Light-O-Rama?








						Light-O-Rama Store
					

Create something amazing where all your lights and special effects are perfectly timed to the music and video. We provide the hardware and software to make the magic that dazzles the crowds. Our controllers combined with award winning sequencing tools give you the power to synchronize lights and...



					www1.lightorama.com
				



They make Christmas Light Controllers. Mega tree. RS485 connects to the various junction boxes.
It is bad ass. Why is there no software for more 'General" I/O events?








						Download Software
					

How Do You Download Light-O-Rama Software? If you have an active license (or just purchased a new one), download the latest version of software now by clicking the big blue button below the 'Download the Latest ShowTime Sequencing Suite Here' section. After the software downloads, enter your...



					www1.lightorama.com


----------



## Alain De Vos (Jun 16, 2021)

evantaylor said:


> I’ve not played with GPIO under FreeBSD, but I have gotten i2c gpio pins working for my own project and found this:
> 
> 
> 
> ...


You could for instance use it to take every two hours an incremental zfs snapshot.


----------



## Phishfry (Jun 16, 2021)

I am looking at the deskutils/calcurse codebase.
If I changed 'Appointments' into 'Events' it might fit the bill.
Not Light-O-Rama but basic scheduled on off events.
Lots of un-needed features though..


----------



## Jose (Jun 16, 2021)

Phishfry said:


> I want to use GPIO outputs to drive an IOT Power Strip.
> 
> 
> 
> ...


That is a neat device!


----------



## mer (Jun 16, 2021)

Phishfry said:


> But I think I want a daemon for each pin. A GPIO service for each pin. Totally independent.
> Then I could make one GPIO pin service dependent on the state of another.


Take a look at the way moused and some other things work with hot pluggable devices.
You write a script once, then you have a symlink like moused.ums0 that the script looks at it's name to figure out what "port" (pin) it needs to work on.


----------



## Phishfry (Jun 16, 2021)

Jose said:


> That is a neat device!


Agreed. I bought five on ebay for a discount but the seller just shipped them today.
Nine days later and he only has one  feedback left for him. Noob needs to tighten up.


> Got here quick once sent


Hahaha. Wish I had seen that.
Well the best deals require patience.


----------



## Phishfry (Jun 16, 2021)

With regards to calcurses, The notfcation system is not very robust. So that is a bust.
This guy reverted to cron.








						I can't get notifications to work! · Issue #204 · lfos/calcurse
					

I can't get notifications to work. With the following configuration: appearance.calendarview=monthly appearance.compactpanels=no appearance.defaultpanel=calendar appearance.layout=5 appearance....




					github.com
				




So I started playing with `dialog --calendar` but I really don't have the skills to build my pipe dream.
Was thinking of hooking the fcron scheduler into a curses calendar interface. Similar to calcurses.
I was hoping to adopt some existing code but that is looking futile.


----------



## Phishfry (Jun 24, 2021)

Phishfry said:


> Well the best deals require patience.


I had patience as the seller took 10 days to ship my item.
He only shipped me one but I ordered 5.
It has been that kind of week.

Good news is I have the one IOT Relay working on my APU2.
Had to change type from Open Drain to Push Pull.
So finally I have outlet control.

Now its time to test out fcron. I will use a clock radio for testing.


----------



## astyle (Jun 26, 2021)

If you can hook up your light controller to a FreeBSD machine and control that using command line first, there's theoretically no limit to how fancy you can get afterwards. But first things first, get the controller working under FreeBSD.


----------



## Phishfry (Jun 26, 2021)

I don't have a Light-O-Rama controller. I just marvel at the off the shelf approach. Nothing new there.
RS485 daisychained controllers. Outlet boxes and some nice orchestration software.
Just application of newer electronics like the triacs for RGB LED's for spectacular effect.

I have been studying my needs and messing with cron/fcron.
It seems to me what we are missing in FreeBSD is a gpiod. On Linux this is what is used.
I looked at the Linux gpiod code and the differences are too great to port.
Scheduling is handled with the daemon as well as monitoring.
There is a OpenBSD GPIO daemon implementation that is closer to our GPIO subsystem.


----------



## Phishfry (Jun 26, 2021)

Alain De Vos said:


> fcron runs on seconds precision, and does cron and anacron.


So how are you doing this? f/a/cron all use the same time/date format?
Is it like this? Running a script?








						How to set a crontab to execute commands on a seconds time interval on GNU / Linux and FreeBSD - ☩ Walking in Light with Christ - Faith, Computing, Diary
					

Have you ever been in need to execute some commands scheduled via a crontab, every let’s say 5 seconds?, naturally this is not possible with crontab, however adding a small shell script to loop and execute a command or commands every 5 seconds and setting it up to execute once in a minute...




					www.pc-freak.net
				




I don't see second precision here:

```
root@APU2:~ # fcrondyn
fcrondyn> ls
ID   |USER     |SCHEDULE        |CMD
3    |root     |2021-06-27 12:11|/usr/sbin/gpioctl 0 1
2    |root     |2021-06-27 12:12|/usr/sbin/gpioctl 0 0
```


----------



## astyle (Jun 26, 2021)

Sounds more like a Pi application - it may be better to have a dedicated device to run the light show for a few hours. FreeBSD or Rasbian, proper command line control, and then you can get as fancy as you want.


----------



## Phishfry (Jun 26, 2021)

FreeBSD's crontab(5) does seem to support seconds in some capacity:
@every_second   Run once a second.

fcron also has the ability to use seconds for repeats.








						Cron alternative with timeouts and second resolution
					

Do you know an alternative to cron daemon that has second resolution, timeouts, and advanced system for error reporting? But it should be lightweight and should support different platforms(linux an...




					superuser.com
				




Neither of these seem to accommodate a start time/stop time in seconds like I want?


----------



## Alain De Vos (Jun 26, 2021)

E.g, one line out of my fcrontab

```
@first(2000s)  1800s  /usr/home/x/Root/bin/snap_usr_home_increment  -u root
```
It runs first 2000s after starting and following regularly at 1800s time intervals.
Note, when you write 2000 instead of 2000s it will interprete it as 2000 minutes.


----------



## astyle (Jun 26, 2021)

Phishfry said:


> FreeBSD's crontab(5) does seem to support seconds in some capacity:
> @every_second   Run once a second.
> 
> fcron also has the ability to use seconds for repeats.
> ...


In which case, it might be easier to daisy-chain a couple scripts... write a script whose total run time is an hour, and has a PID file. Then your crontab has to check for completion of the script before starting it up all over again. It shouldn't take that many processor cycles to do the checking.


----------



## Phishfry (Jun 26, 2021)

Nice to see an example of a gpio daemon.
Air Quality Daemon
set fartd_enable=yes


----------



## astyle (Jun 26, 2021)

Phishfry said:


> Nice to see an example of a gpio daemon.
> Air Quality Daemon
> set fartd_enable=yes


ROFLMAO


----------

