When I reboot the zero the LED does nothing. I can see “Blink script started” in logfile.log so I know the script ran and there are no errors in the log file.
Copilot wants to take me down a investigative rabbit hole (as usual) and suggests a GPIO conflict that I can’t pin down.
I’m starting with crontab as its supposed to be the simpler option. Once I can get that to work I’ll do the same in systemd.
Insights welcome!
from gpiozero import LED
from time import sleep
led = LED(25)
with open("/home/morchard/logfile.log", "a") as f:
f.write("Blink script started\n")
sleep(30)
while True:
led.on()
print("On")
sleep(1)
led.off()
print("Off")
sleep(1)
Not sure how it will handle the same file being open as the “redirect” AND in the script itself. I assume the one in the script will fail as its already open from the command line in a redirect (maybe ?).
Can you try not opening it in the script and just let the redirect log the prints.
I have a few suggestions. Firstly, you will need to make sure crontab runs your code as a root user or at least a user that has GPIO permissions. I would try adding your line to the root crontab using sudo crontab -e.
If this doesn’t work, it’s also possible that your code is running on startup before the system has time to bring the GPIO online. Adding a small delay before your code runs normally gets around this @reboot sleep 10 && /usr/bin/python3 /home/morchard/Testblink.py >> /home/morchard/logfile.log 2>&1
That shouldn’t really make a difference in this case. Your boot time may be a little longer as you’re loading a desktop environment as well, but this shouldn’t affect your script or crontab usage.
Turns out the ribbon cable is intermittently faulty on the end plugged to the Zero.
The LED would blink when I manually run it, jumpered direct to the Zero.
I’d fit the ribbon cable, manually run and all’s well. Then I’d muck around setting up the auto start files and it wouldn’t work. No doubt I’ve moved the ribbon.
Anyway, jumpered direct to the Zero and it worked first time as coded above, ex an delays or log file writes.
I thought that perhaps the intermittent fault was the solderless header not making a solid connection to the Zero’s PCB but I’ve tested again and I’m sure its the ribbon cable where it clamps into the 40 pin connector.