Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Code notes

René Kliment edited this page Dec 18, 2016 · 6 revisions

AlexaPi restart on crashes

Warning: If you encounter crashes with AlexaPi, please report them, so we can fix them. That is the solution. This feature exists as a last resort when for example the app crashes after several weeks of operation due to some memory leak and there is no obvious way to solve it.

SystemD

This feature is controlled by a systemd unit override file /etc/systemd/system/AlexaPi.service.d/restart.conf.

If you wish to enable it: sudo cp /opt/AlexaPi/src/scripts/unit-overrides/restart.conf /etc/systemd/system/AlexaPi.service.d/

And to disable it: sudo rm /etc/systemd/system/AlexaPi.service.d/restart.conf

And sudo systemctl daemon-reload for the changes to take effect.

Classic init scripts

The feature is controlled by the presence of /etc/opt/AlexaPi/monitor_enable.

If you wish to enable it: sudo touch /etc/opt/AlexaPi/monitor_enable

And to disable it: sudo rm /etc/opt/AlexaPi/monitor_enable

Temporary setting

To prevent the re-spawn from happening, add a file called monitor_pause into the /run/AlexaPi directory. sudo touch /run/AlexaPi/monitor_pause will do the job. This will prevent the script from creating a new instance of AlexaPi, and a kill command will now truly kill the alexa program. sudo rm /run/AlexaPi/monitor_pause will return to re-spawning.

After a reboot, AlexaPi will be restarted and re-spawned as usual.

A note on Shairport-sync

By default, shairport-sync (the airplay client) uses port 5000. This is no problem if everything goes as planned, but AlexaPi's authorization on sammachin's design uses port 5000 as well, and if shairport-sync is running while that happens, everything fails.

As a result, I have changed the authorization port for AlexaPi to 5050. Note that you will have to change the settings within the developer website for this to work.

Configuration

Configuration of AlexaPi is stored in a file called config.yaml, which as the extension suggests uses the YAML format. AlexaPi looks for this file in several locations:

  • /etc/opt/AlexaPi
  • the src subdirectory of AlexaPi installation (AlexaPi/src)

The last existing file in this ordered list will be used. That means normally /etc/opt/AlexaPi/config.yml is used, but if you create config.yaml in the latter paths, it will be used instead.

How it works internally

The Amazon AVS credentials are stored in the configuration file called config.yaml which is used by auth_web.py and main.py, there is a template with blank values.

The auth_web.py is a simple web server to generate the refresh token via OAuth to the Amazon users account, it then fills this in the configuration file and displays it in the browser.

main.py is the 'main' Alexa client. It simply runs on a while True loop waiting either for the trigger word "Alexa," or for the button to be pressed. It then records audio and when the button is released it posts this to the AVS service using the requests library. When the response comes back it is played back using vlc.

The LEDs are a visual indicator of the status. Dual red/green LED can be used as well as separate LEDs. Red is connected to GPIO 24 and green to GPIO 25. When recording the red LED will be lit when the file is being posted and waiting for the response both LED's are lit (or in the case of a dual R?G LED it goes Yellow) and when the response is played only the green LED is lit. If the client gets an error back from AVS then the red LED will flash 3 times.