Using a python script for Motephat on reboot problem


#1

I want my pi zero to run a script to light up two Mote strips which are driven by a MotePhat. When I run the script from Python 3 they all light up as designed and STAY ON.
I have used crontab to get the script to run at boot up and that works BUT when the script has finished the 2 strips go out immediately.
I have tried the crontab with and without the “&” at the end of the line with no change.
Can anyone assist?


#2

Posting your python code might help. I’m no expert but somebody else might spot something than might help sort it out.


#3

In crontab, did you use python or python3 in your @reboot statement? Just using python will run your script in python 2, not python3.


#4

I got the idea from pimoroni’s learn section and their code used python. But its not the fact that the script isn’t run properly, which it is, its the fact that after the script has finished all the lights go off, where as if the script is run from within python3 all the lights stay on


#5

Ah but are you running it in python 2 or python 3 from crontab? It makes a difference.


#6

I don’t know. How can I tell ?


#7

Are you using set_clear_on_exit? http://docs.pimoroni.com/motephat/#set-clear-on-exit


#8

No, but I will try the false option later. Snow is the priority this morning! I will report back later


#9

@reboot python3 /home/pi/myfile.py & < runs it in python 3
@reboot python /home/pi/myfile.py & < runs it in python 2

I mentioned it because a lot of “how to” examples just use python. It’s only an issue if your code won’t run in both. I have some files that error out in python 2 but run fine in python 3. I wrote and tested them in idle3. The first couple of times I tried using crontab it caught me out. My files errored out.


#10

I have tried alphanumeric’s suggestion of using python3 in the reboot line…but it has not made any difference.
But placing setclearonexit(false) at the end of my code as suggested by gadgetoid has done the trick. Many thanks. Perhaps you can explain why? Also, do you know if the micro usb Mote cables are the same as the Flotilla cables? They are just the right length


#11

Glad you got it sorted. Can’t help you with the cables. I haven’t used and don’t own either.


#12

By default the LEDs are cleared when the script exits, this is for two reasons:

  • It prevents them being left on when a script is exited, since this is usually undesired
  • Without a script actively setting the LEDs they are quite sensitive to interpreting noise as signal, and unexpectedly displaying random garbage

set_clear_on_exit() prevents the exit handler from clearing the LEDs, so you can “set and forget” a colour of pattern.

Your mileage may vary using this with Mote pHAT and by far the better way to ensure the LEDs stay on having something like:

while True:
    mote.show()
    time.sleep(0.1)

at the bottom of your script.


#13

Many thanks again. Will set that up later. Had an issue last night trying to setup my printer which resulted in pi booting up in “emergency mode” which I could not exit. So I had to use “shift” at startup to reinstall the system and I need to reinstall all the drivers. Thank goodness for your dashboard
Such is life!.