siblues
Posts: 26
Joined: Sun Oct 22, 2017 1:26 am

Turning usb ports on and off

Fri Nov 03, 2017 4:43 pm

I am currently getting used to using Octoprint on my RPi3.I am going to wire a relay to turn the printer on and off but as long as the usb is plugged into the RPi the screen on the printer stays on.I am wondering if there is a way to turn the usb port off when triggering the relay to turn the printer screen off?

siblues
Posts: 26
Joined: Sun Oct 22, 2017 1:26 am

Re: Turning usb ports on and off

Sun Nov 05, 2017 5:01 am

So I came across a couple of threads on how to turn the usb ports off and the code they have in a few threads is

Code: Select all

   sudo apt-get update
   sudo apt-get install libusb-dev
   git clone https://github.com/codazoda/hub-ctrl.c
   cd hub-ctrl.c
   gcc -o hub-ctrl hub-ctrl.c -lusb
   cp hub-ctrl ..
   cd ..
And here is the explanation in the thread
At this point you're back where you started in the root but hub-ctrl will now function and can turn off your USB and Ethernet port. For a detailed walk through of what the commands above do
Update the list of what Linux updates/versions exist and where they are located
Install the current version of libusb-dev
Copy the source code of hub-ctrl.c to your machine
Change into the directory where the hub-ctrl.c code was copied
Compile it into an executable named hub-ctrl (note the lack of a .c)
Copy the compiled exectuable to the root directory
Change back into the root directory
Now it all comes down to running commands against your newly compiled hub-ctrl command.

To jump right into it, here are some examples:
Turn off Ethernet Port: sudo ./hub-ctrl -h 0 -P 1 -p 0
Turn on Ethernet Port: sudo ./hub-ctrl -h 0 -P 1 -p 1
Turn off all USB Ports: sudo ./hub-ctrl -h 0 -P 2 -p 0
Turn on all USB Ports: sudo ./hub-ctrl -h 0 -P 2 -p 1
Essentially the number after the -P determines the port while the number after the -p (case matters in Linux don't you know) determines the state (0 = off, 1 = on).

Now, the information elsewhere refers to shutting down individual ports and using values of -P to do it. They claim a -P 3 would shut down an individual port. My own experience is...no. While it did not throw an error, no USB devices shut down when attempting individual control. It only worked when I shut all USB ports down.

So to do a full USB reset via power-off/on you could munge together a command like this:

Code: Select all

 sudo ./hub-ctrl -h 0 -P 2 -p 0 ; sleep 5; sudo ./hub-ctrl -h 0 -P 2 -p 1;
This would bounce all USB devices via a power off, wait 5 seconds, then power them all back on.
The printer is being turned off by a relay board triggered by the GPIO pins so how would I be able to tell the raspberry to run this code to turn off the usb at the same time?

Would just interrupting the power wire on the usb itself with a relay work without harming the gpio's or RPi or even needing this script to do it?Octoprint was kind enough to have that included in the software already.?I have a 4 relay board with two open relays so I can use one of the available relays to open and close the circuit on the +5v usb wire?

User avatar
davidcoton
Posts: 5083
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Turning usb ports on and off

Sun Nov 05, 2017 9:23 am

IIRC (I may have some details wrong):
Pi models with more than one USB connection have a hub chip that can be controlled by hub-ctrl. They are wired so that the wired network port is on one channel, and all the USB ports on another. There is no way to control all USB ports individually.
Pi models with a single USB port (excluding the power socket) do not have a hub chip, power cannot be controlled in software.
You can use one of your relays to switch the 5V associated with a USB port, either by hacking the Pi's board (risky) or by external wiring. A MOSFET could do the same job, but needs an extra transistor to drive it to switch 5V from a 3V3 GPIO.
Signature retired

siblues
Posts: 26
Joined: Sun Oct 22, 2017 1:26 am

Re: Turning usb ports on and off

Sun Nov 05, 2017 11:35 am

I was thinking the external wiring option would be much simpler for me. I was thinking of just using the 5v wire in the USB cable itself and using one of the relays to switch it on and off. I know that is a crude way of doing it but it seems possible to me I am just unsure if doing it that way can harm any of the electronics.
Last edited by siblues on Sun Nov 05, 2017 12:22 pm, edited 1 time in total.

User avatar
davidcoton
Posts: 5083
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Turning usb ports on and off

Sun Nov 05, 2017 11:55 am

I don't think it will cause any harm. There might be software (driver) issues if you cut power while the USB peripheral is trying to do something, but USB is designed to be hot pluggable. The hardware should tolerate the 5V being turned off. I would not expect a problem from the power being disconnected while the data lines remain connected. Obviously you need to flush buffers and unmount any storage devices first.
Signature retired

Return to “Beginners”