# Switching Power on Raspberry Pi USB Ports

I want to create a stop motion movie of sourdough growing over night. Don't ask. Since I'm very comfortable with the OS, I am using Raspberry Pi as the hardware platform. And since everyone will tell you that _Lighting_ is important for any kind of filmmaking I played around with ways to toggle power on the USB ports to control USB-powered lamps. The package libusb-dev is only one
sudo apt install libusb-dev

away. Luckily, Joel Dare (aka "codazoda") already wrote a handy program to access those C-bindings1:
pi@sauercam:~ $git clone https://github.com/codazoda/hub-ctrl.c Cloning into 'hub-ctrl.c'... remote: Enumerating objects: 13, done. remote: Counting objects: 100% (13/13), done. remote: Compressing objects: 100% (12/12), done. remote: Total 62 (delta 3), reused 5 (delta 1), pack-reused 49 Unpacking objects: 100% (62/62), done. pi@sauercam:~$ cd hub-ctrl.c/
pi@sauercam:~/hub-ctrl.c $gcc hub-ctrl.c -l usb -o hub-ctrl pi@sauercam:~/hub-ctrl.c$ sudo mv hub-ctrl /usr/bin/

This will produce an ELF binary named hub-ctrl and place it in your path. Now let's figure out how the Raspberry Pi Model 1 B 2.0 (CPU Revision 000e) and 2 Model B 1.1 (CPU Revision a01041) are structured wrt USB:
pi@sauercam-000e:~ $lsusb -t /: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=dwc_otg/1p, 480M |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/5p, 480M |__ Port 1: Dev 3, If 0, Class=Vendor Specific Class, Driver=smsc95xx, 480M pi@sauercam-a01041:~$ lsusb -t
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=dwc_otg/1p, 480M
|__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/5p, 480M
|__ Port 1: Dev 3, If 0, Class=Vendor Specific Class, Driver=smsc95xx, 480M
|__ Port 2: Dev 5, If 0, Class=Hub, Driver=hub/4p, 480M

I know for a fact that the Ethernet card is connected via USB on the Raspberry Pi so for the B 2.0 we are probably out of luck: it only has one bus with one hub with one port. If we disable it to switch USB lamps off, we will also disable the Ethernet connection. For the 2 Model B 1.1 it looks a bit differently though: there are two ports available. Let's try the second one:
sudo hub-ctrl -b 001 -d 002 -P 2 -p 0 # re-enable with: -p 1

And now it's disco time:
awk -v n=10 -v seed="$RANDOM" 'BEGIN { srand(seed); for (i=0; i<n; ++i) printf("%.4f\n", rand() / 3) }' | while read amount; do sleep$amount; hub-ctrl -b 001 -d 002 -P 2 -p 1; hub-ctrl -b 001 -d 002 -P 2 -p 0; done


1. https://github.com/codazoda/hub-ctrl.c/blob/master/hub-ctrl.c []