1

i am trying to get my WS2812 NeoPixel python sketch to run at boot, i used a tutorial https://tutorials-raspberrypi.com/connect-control-raspberry-pi-ws2812-rgb-led-strips/ it install the correct files and such and all works fine when i run the commands in terminal the lights do as they are meant to, an just trying to figure out how to make them start on boot, i am still really new to all of this

the 3 commands i use to run the python script as follow 

cd rpi_ws281x/

cd python

sudo PYTHONPATH=".:build/lib.linux-armv7l-2.7" python examples/strandtest.py

any help would be much approached

i have tried following a tutorial on hear explaining what to do and it always comes back with an error file or directory not found

2 Answers2

2

You could use cron for this. To schedule a cron job, you will use the crontab utility. Start by reading the man pages for cron and crontab. You could also search the Internet for information resources, and find something similar to this guide.

Once you have the gist (general idea) of this, try this from the command line of your RPi:

crontab -e

This should launch a text editor (nano or pico IIRC) that opens with your existing crontab in it. You will edit this file to add the line you need to start your program at boot time. At the bottom of your existing crontab file, add something like this:

@reboot ( /bin/sleep 30; /usr/bin/python /path/to/your/PythonProgram.py  > /home/pi/cronjoblog 2>&1)

For an explanation of what this command does & why we do it this way, see this answer. If you have any issues getting this to work, please edit your OP, and we'll try to help.


P.S. The crontab Guru is a useful asset to bookmark.

Seamus
  • 21,900
  • 3
  • 33
  • 70
0

You can set a command for your python code to run at every start-up. All that you need is just add a command to rc.local using this command:

sudo nano /etc/rc.local

Add the following command for python2 to the file before exit 0:

sudo python2 /path/to/file.py

And add the following command for python3:

sudo python3 /path/to/file.py

If your code is running with any endless loop like while True, then use ampersand (&) at the end of the command like this:

sudo python3 /path/to/file.py &

theashwanisingla
  • 267
  • 1
  • 11