Pi with USBLamp


So lets start building something with the Pi.

My first idea is to use the Pi as a device that highlights an issue to you that you can just leave running.

So this example is setting up a USB Lamp and calling an API which will return a state which is good or bad, and the lamp showing a colour that relates to this.

So after a quick google I found this post http://cweiske.de/tagebuch/usblamp-monitoring.htm which does something similar.

What I liked about this post is that he found a cheap USB lamp (19.95 euros) that has a linux driver.
So I ordered from getDigital an received my USBLamp (though originally supposed to be used for checking if you have any emails).

Also to make life simplier you should download Daniels git repo as I called one of his functions.
Details to be found at https://github.com/daniel-git/usblamp

To do that you log into your Pi and type in the following commands:

• apt-get install libusb-dev
• wget https://github.com/daniel-git/usblamp/tarball/master --no-check-certificate
• tar xzf master
• cd daniel-git-usblamp*/src
• make
• sudo make install

After following those commands you now should be able to attach your USBLamp to your Pi and type in the following command to switch it on:

usblamp red

If the light does not switch on then try running as root to see if it is a permission issue.

Now download my code into the same location you are currently in under ‘scripts’
https://github.com/glynrob/Python-pi-usblamp

colourloop.py This script runs through all colours available at 1 second intervals then switches off the USBLamp at the end.

This is a simple loop through an array of colours calling upon Davids function switchTo in the script usbfunction.py.
Try this to check that everything is working for you.
Run as root if you are getting permission issues.

apicheck.py This script calls an API and sets the colour of the USBLamp dependant on what is returned.

  • The  def signal_handler is our exit code which turns the USBLamp off when you exit the script
  • checkTime is the number of seconds to wait before attempting to call the API again
  • http://glynrob.com/api/rand3.php is the API I setup which returns 0, 1 or 2 randomly to prove this concept
  • If status checks the response of the API and calls the colour that matches the status
  • time.sleep is the python call to wait for the amount of time set on checkTime

Hopefully this is clear on what the code is doing and you can see how to change the API to your own and set the interval time to your requirements.

This seems very usful as it has a wide range of uses for any API that you wish to build.

Leave a Reply

Your email address will not be published. Required fields are marked *