Powering the USB ports on and off using cron on the Raspberry Pi 3

Viewed 906

Thanks for looking at this post. I have set up a Raspberry Pi 3 B plus as a remote time lapse camera that is powered by a solar panel. The Pi shuts down at night to save power and the Wakeup alarm on the Pijuice board turns it on in the morning. I would like to turn the USB ports on and off once an hour so the Huawei USB dongle sim card can communicate over the mobile cellular network to upload the photos to my Google Drive account. The reason for wanting the USB ports to be off most of the time is to save battery power, especially on cloudy days.
I know that the following commands will turn off and on the USB ports;

Turn OFF USB chip echo '1-1' |sudo tee /sys/bus/usb/drivers/usb/unbind

Turn ON USB chip echo '1-1' |sudo tee /sys/bus/usb/drivers/usb/bind

What I would like to do is have these in a script that can be scheduled as a crontab. I tried the following but it did not work. The file was called usb-on.sh

#! /bin/sh 
#this is a script that I made up for turning on the USB ports
echo '1-1' |sudo tee /sys/bus/usb/drivers/usb/unbind`

When I tested the script I got the following error

pi@raspberrypi:~ $ sudo sh usb-on.sh
1-1
tee: /sys/bus/usb/drivers/usb/unbind: No such device

Any help is appreciated! Thanks

1 Answers

Worked it out by just adding the command to a crontab.

CRONTAB to turn on and off the USB ports Open terminal

crontab -e

add the following

`#Turn on USB ports at the top of the hour 0 * * * * echo '1-1' |sudo tee /sys/bus/usb/drivers/usb/bind

#Turn off USB ports 5 minutes after the hour 5 * * * * echo '1-1' |sudo tee /sys/bus/usb/drivers/usb/unbind`

Related