Adding Reboot and Shutdown Commands to the Raspberry Pi AIY Projects Google Assistant
|UPDATE! The Google AIY Project have now pulled my commands to do this into the official repository https://github.com/google/aiyprojects-raspbian so if you use the most up to date version from there it’s no longer necessary to add this yourself. The box should now respond to “pi reboot” and “pi power off”
The MagPi magazine came with a fantastic kit to add create a voice interactive Google Assistant with a Raspberry Pi.
As with all headless pi projects shutting down or rebooting can be a pain, I usually end up using ssh, so my first simple project is to add shutdown and reboot as voice commands. It turned out to be pretty easy!
The file you need to edit is ~/voice-recognizer-raspi/src/action.py
find the section that looks like this:
# =========================================
# Makers! Implement your own actions here.
# =========================================
def make_actor(say):
"""Create an actor to carry out the user's commands."""
actor = actionbase.Actor()
actor.add_keyword(
_('ip address'), SpeakShellCommandOutput(
say, "ip -4 route get 1 | head -1 | cut -d' ' -f8",
_('I do not have an ip address assigned to me.')))
actor.add_keyword(_('volume up'), VolumeControl(say, 10))
actor.add_keyword(_('volume down'), VolumeControl(say, -10))
actor.add_keyword(_('max volume'), VolumeControl(say, 100))
actor.add_keyword(_('repeat after me'),
RepeatAfterMe(say, _('repeat after me')))
add to the bottom of that section:
actor.add_keyword(_('power off'), SpeakShellCommandOutput(say, "sudo shutdown now",_('failed to shutdown')))
actor.add_keyword(_('reboot'), SpeakShellCommandOutput(say, "sudo shutdown -r now", _('failed to reboot')))
I tried using the command “shutdown” at first but that seemed to be conflicting with a Chromecast command.
Save the file and restart the voice recognizer service sudo systemctl restart voice-recognizer.service
Now your pi will shutdown when you say “power off” and reboot when you say “reboot”!
UPDATE:
I’ve made an updated version of this which responds to the command before it reboots / shuts down. I’ve put it on Github and you can find it here: https://github.com/ktinkerer/aiyprojects-raspbian/blob/master/src/action.py
Brilliant, you beat me to it, I had the same idea.
Code worked perfectly, once I realised I had used a { and not a ( in one of the statements!
Small point, but if you’re running from a battery, shutdown won’t actually cut the power. Instead do a “sudo poweroff”. I tend to do a sync first.
Excellent point and something I hadn’t realised!
Thanks a lot, Ian for mentioning the poweroff command, I had no idea you can do that.
I used the updated version from your github page, but I also get the conflict with a Chromecast command, like what you had with your original version.
What are you using as your voice command? I found shut down conflicted but power off seemed OK.
Thank you so much for adding these commands. I was getting ready 2 days ago to give it a try and add a shutdown command but when I updated the code, it was already there. Awesome!
This is really useful, thank you. Any idea how to implement this without using the AIY Kit or image?
Shutdown and reboot are standard linux commands so it would depend what you were trying to run it on. Raspbian doesn’t ask for a password when you use sudo, this wouldn’t work if it did because it would wait for a password.
Sorry for the delay in replying, I’ve been on holiday!
والپروئیک اسید، والپروات سدیم، و فرمهای والپروات سٍمیسدیم، داروهایی هستند که در درجه اول برای درمان صرع و اختلال دو قطبی و جلوگیری از سردردهای میگرنی مورد استفاده قرار میگیرند.my web blog: رهاکین