[LINUX] Control power on / off of USB port of Raspberry Pi


I tried to turn on / off the power of the USB port of Raspberry Pi.

Since I connected the Raspberry Pi and the smartphone via the Blynk server in previous article, I am operating the power supply from the smartphone side.

Things used

--Android smartphone

Overview of the equipment used

The whole picture of the above equipment is like this. 全体像.jpg It is an image that the smartphone and Raspberry Pi are connected in the same local area, and the Blynk server is connected to it. The Raspberry Pi and the USB-HUB / USB-FAN to be controlled this time are connected.

Advance preparation

1. 1. Introducing and connecting to Blynk

Since the following method uses Blynk, you need to install and connect Blynk on both your smartphone and Raspberry Pi.

Click here for how to do it. ↓↓↓ Connect your Raspberry Pi to your smartphone using Blynk

2. Blynk program fix

Since the script is difficult to handle in the initial setting, it is changed by referring to the following site.


As a result, the relationship between the widget settings and the script on the Raspberry Pi side is as shown in the figure below (* The image is quoted from the same site).



Work on the smartphone side

On the smartphone side, select the pin associated with the widget and script for the Blynk project. blynk.jpg

[Left] Tap the widget to open the widget settings [Middle] Since this is a button, tap it to select the pin to be linked when the button is pressed [Right] Select the pin type and pin number

Work on the Raspberry Pi side

1. 1. Introduced a tool (hub-ctrl) for controlling the USB port

pi@raspberrypi:~ $ sudo apt-get install libusb-dev
pi@raspberrypi:~ $ git clone https://github.com/codazoda/hub-ctrl.c
pi@raspberrypi:~ $ cd hub-ctrl.c
pi@raspberrypi:~/hub-ctrl.c $ gcc -O2 hub-ctrl.c -o hub-ctrl-armhf-static -lusb –static
pi@raspberrypi:~/hub-ctrl.c $ sudo cp hub-ctrl-armhf-static /usr/local/bin/hub-ctrl

2. Check the connection status of the USB port

The "lsusb" command returns the device connected to the USB port and its Bus and Device numbers. Since the USB-HUB is the operation target this time, check that the device name is "Genesys Logic".

pi@raspberrypi:~ $ lsusb

Bus 001 Device 005: ID 046d:c016 Logitech, Inc. Optical Wheel Mouse
Bus 001 Device 004: ID 05e3:0608 Genesys Logic, Inc. Hub
Bus 001 Device 006: ID 0424:7800 Standard Microsystems Corp. 
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

3. 3. Create a USB-HUB power ON / OFF shell script

Since the pin number was set to "V10" in the widget settings, the file name of the script is "V10".

pi@raspberrypi:~ $ sudo vi /home/pi/blynk/BLYNK_WRITE_V10.sh

The content of the script is like this.



HUB=`/usr/bin/lsusb -v 2>/dev/null | grep ^Bus | grep "Genesys Logic, Inc. Hub" | head -1`
BUS=`echo $HUB | awk '{print $2}'`
DEV=`echo $HUB | awk '{print $4}' | sed -e "s/\(.*\)\:/\1/p;d"`

for i in 1 2 3 4
     /usr/local/bin/hub-ctrl -b $BUS -d $DEV -P $i -p 0
Script supplement
Device information setting (3rd line) The result of the
lsusb instruction is extracted by the device name and set in the variable HUB.
Bus number setting (4th line)
Set the second field set in HUB to the variable BUS.
Device number setting (5th line)
Set the 4th field set in HUB to the variable DEV by replacing the characters.
for statement (7th line)
Since there are 4 USB-HUB ports, it is looped 4 times.
hub-ctrl command (9th line)
"-b" specifies the target Bus number, "-d" specifies the target Device number, and "-P" specifies the port number.
The last "-p" is the power supply On / Off specification, and either "0" (Off) / "1" (On) is specified.

Now that you have a script that corresponds to the pin number specified in the widget, pressing a button on your smartphone will launch this script. As a result, you can turn on / off the power of the USB fan.

Supplement to power operation of USB port

In the old version of Raspberry Pi, it was possible to control the power supply of the USB port of the main unit individually, but it became impossible because the version was upgraded.

It was said that a USB-HUB with "Per-port power switching" specifications could be controlled, but a HUB that supports it is not currently on sale. If it is ** BUFFALO BSH4AE12BK ** used this time, it is possible to control the power supply in units of USB-HUB **, so I decided to use this.


By creating the pin specified in the widget and the script associated with it, the script could be executed from the smartphone side. First of all, I understand the basics of how to move it, so next I would like to move it using a different widget.

Reference link

――Part 3 Let's see the temperature / frequency / load status of the CPU ――Series Let's control the Raspberry Pi from a smartphone using the IoT service "Blynk" (Raspberry Pi blog) - http://blog.livedoor.jp/victory7com/archives/48432885.html --How to connect a USB fan to Raspberry Pi and control the USB port ON / OFF (Mushroom Times) - https://kinokotimes.com/2017/03/07/usb-control-method-by-raspberry-pi/ --Try controlling the power ON / OFF of the USB HUB from Linux (memo memo) - http://atotto.hatenadiary.jp/entry/linux-sugoi-usb-hub-power-ctrl --Turn on / off the power of the USB port on RaspberryPI 3 Model B (PC bird blog) - https://iot-plus.net/make/raspi/rpi3-blynk-shutdown/ --I installed a back camera and LCD monitor on the car: using GL-AR150 / OpenWRT, Amazon Dash Button, and a USB hub. - https://qiita.com/somainit/items/db4d8fe760e3fb88905f

Recommended Posts

Control power on / off of USB port of Raspberry Pi
Power on / off Raspberry pi on Arduino
Power on / off your PC with raspberry pi
USB boot on Raspberry Pi 4 Model B
pigpio on Raspberry pi
Cython on Raspberry Pi
Retry to turn on/off the power of the USB port on RaspberryPi 4 Model B
Control brushless motors with GPIOs on Raspberry Pi Zero
Implementation of personally optimized photo frames on Raspberry Pi
Power on/off the USB port on the RaspberryPi 4 Model B
Installation of Docker on Raspberry Pi and L Chika
Install pyenv on Raspberry Pi and version control Python
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Port FreeRTOS to Raspberry Pi 4B
[Raspberry Pi] Stepping motor control with Raspberry Pi
Testing uart communication on Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
Servo motor control with Raspberry Pi
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Install ghoto2 on Raspberry Pi (memo)
Try using ArUco on Raspberry Pi
OpenCV installation procedure on Raspberry Pi
Graph display of household power consumption with 3GPI and Raspberry Pi
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Use python on Raspberry Pi 3 to light the LED with switch control!
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device
Connect two USB cameras to Raspberry Pi 4
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Detect "brightness" using python on Raspberry Pi 3!
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Enable UART + serial communication on Raspberry Pi
Adafruit Python BluefruitLE works on Raspberry Pi.
Accelerate Deep Learning on Raspberry Pi 4 CPU
Set swap space on Ubuntu on Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Use the Grove sensor on the Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS
Run servomotor on Raspberry Pi 3 using python
"Honwaka Notification Lamp" on Raspberry Pi Part 3
Power SG-90 servo motor with raspberry pi
Working with sensors on Mathematica on Raspberry Pi
Detect temperature using python on Raspberry Pi 3!
Mount Windows shared folder on Raspberry Pi
Matrix multiplication on Raspberry Pi GPU (Part 2)
How to install NumPy on Raspberry Pi
I installed OpenCV-Python on my Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
I played RPG Maker 2000 Ruina ~ The Story of the Abandoned City ~ on Raspberry Pi
Control Akizuki's I2C-connected OLED display from Raspberry Pi
Detect slide switches using python on Raspberry Pi 3!
Build a Django environment on Raspberry Pi (MySQL)