Overview
There are many articles and blogs out there that show how to use a PiGlow LED GPIO board with a Raspberry Pi but most do not cover everything. Some only describe how to set it up with hardware before the B+ and others get you going but fail on permissions.My hope is to show all the steps needed to let you run Python code with a PiGlow and the PyGlow.py module.
Security
I am not a security genius, nor do I view this project as security driven. You will need to decide what changes below are reasonable for you. For me, I want the PiGlow to work and be easy to work with. This is the process I used to get there.What you need
- Raspberry Pi B+
- Rasparian Wheezy
- PiGlow module
kernel setup
For this to work, there will need to be some kernel level changes. This simply tells the kernel to load some required modules./boot/config.txt
In order for the i2c module to work, it needs to be enabled. Add or uncomment the following to your config.txt file.dtparam=i2c1=on
/etc/modules
Ensure that the following two lines are in your modules file.i2c-dev
i2c-bcm2708
/etc/modprobe.d/raspi-blacklist.conf
Ensure that the following modules are not blacklisted by ensuring these lines are commented out.# blacklist spi-bcm2708
# blacklist i2c-bcm2708
Reboot
At this point, the kernel will have what it needs to allow you to work with your PiGlow. To activate the new configuration, you need to restart your Raspberry Pi.Setting up PyGlow Python Module
With the kernel prepped and ready, the next phase is to install the python library needed to interact with your PiGlow. For a Raspberry Pi, I suggest creating a library location where this and other common libraries can be stored for shared use across users and applications. On my RPi, I have them in
/opt/lib/python
If this path does not exist, create it or something like it. Ensure you can get to it using a normal users and chmod as needed.Setup a Library area in /etc/bash.bashrc
To ensure all users on the platform see this new library location, it will need to be added to the PYTHONPATH. That can be done either in each users .bashrc file or globally in the /etc/bash.bashrc. Just add the following line.export PYTHONPATH=/opt/lib/python
For the full approach to work, it will need to be added globally. Within a user’s .bashrc, they can append to this path with other personal lib or module path.Install PyGlow Module from GIT
To get the lastest PyGlow.py module, run the following commands.cd /opt/lib/python/
git clone https://github.com/benleb/PyGlow.git
Git should already be installed on your RPi. If not or you don’t like git. You can find a ZIP of the same thing using a few Google searches.Once completed you should have a PyGlow sub directory within /opt/lib/python/. If you read other documentation, you will be told that you can simply run (or at least attempt to run)
cd PyGlow/examples/
sudo python test.py
But when you do, you get an error that PyGlow cannot be found. This is beause by default, PYTHONPATH is not seen by sudo. This takes us to our last phase of setting this up.sudo Setup for Python Users
Add PYTHONPATH to /etc/sudoers
To tell sudo that you want to keep PYTHONPATH as an environment variable. Simply edit /etc/sudoers and add (or uncomment) the following line.Defaults env_keep += "PYTHONPATH"
Now anything in the global Python path will be available to sudo. I would assume you can have multiple lines for each ENV you want to keep.PyGlow Error for RPi B+
So, all excited, you re-run the test scriptcd $PYTHONPATH
cd PyGlow/examples/
sudo python test.py
Now a new error appears complaining about a revision issue or something like that. The fix for the PyGlow.py Error
The code for the PyGlow (at the time this was written) was not updated for the B+ (Revision #3) GRUMBLE, but the fix is easy. Edit /opt/lib/python/PyGlow/PyGlow.py and search for the following code….
# Check what Raspberry Pi version we got
if rpi.RPI_REVISION == 1:
i2c_bus = 0
elif rpi.RPI_REVISION == 2:
i2c_bus = 1
else:
raise PyGlowException(
self, "Unknown Raspberry Pi hardware revision: %s" %
(rpi.RPI_REVISION))
After the above elif, add the following elif that. It should be before the else
elif rpi.RPI_REVISION >= 3:
i2c_bus = 1
It basically says that any revision after and including 3 should use the #1 bus for ic2. Or at least I suppose.
Testing
Now you re-run, more causiously, the following command.cd $PYTHONPATH
cd PyGlow/examples/
sudo python test.py
Based on my experience, you should have success. In fact all the example scripts should now work. Next Steps
There are a lot of groups in linux and it is possible that vs using sudo to run your code you can simply add your user to a group that allows i2c and other modules to work. I’ll have to research that next to see if I can give a user permissions.This was not much of a goal for me since this device is experimental so sudo is not a worry for me to run. For eample, I like to run the sudo python cpu.py to let the lights show me how much CPU I am using. python-psutil and psutils are needed for that to work so sudo apt-get install python-psutil psutils before running that comand.
No comments:
Post a Comment