Author: Kyungrae Kim
This is a simple RESTful API built using Flask. The application turns on a Wake-on-LAN enabled computer with the following:
- Accepts a POST request to the route "/wol", which accepts one argument "mac_address"
- Returns a JSON object with the key "message" and broadcasts a magic packet to the subnet
This README contains extra steps to use Google Assistant to communicate with the endpoint running on a local server.
This is a continued effort to complete a personal project started from the following repositories:
If you POST
{"mac_address": "XX:XX:XX:XX:XX:XX"}
It will return the following and turn on the computer:
{"message": "Starting the computer"}
Before trying out the endpoint, enable Wake-on-LAN (WoL) feature on the target computer by following a post similar to Lifewire - How to Set Up and Use Wake-on-LAN.
Clone this repository to your local machine:
git clone https://github.com/jeremymaya/raspberry-pi-os.git
Install the dependencies:
pip3 install -r requirements.txt
Start the application in development mode with the following command:
FLASK_ENV=development flask run
Test the functionality of the endpoint running at localhost:5000
with the following command:
curl -X POST http://127.0.0.1:5000/api/v1/wol --data '{"mac_address": "XX:XX:XX:XX:XX:XX"}' -H 'Content-Type: application/json'
The expected output upn suceess of the above command is:
{
"message": "Turning on the computer"
}
Alternatively, test the functionality of the endpoint running at http://127.0.0.1:5000/api/docs/ by clicking the Try it out
button.
Run the application in production using uWSGI with the following command:
uwsgi --ini app.ini --need-app
Now that the computer can be turned on with a POST request, let's integrate Google Assistant and Webhooks so it can be turned on remotely.
For Google Assistant to communicate with the endpoint running on the local server, the server nseeds to be exposed with a public URL.
There are different ways to achieve this. This example uses ngrok to expose the local server.
After following the Setup & Installation steps, enter the following command for the application running in production:
./ngrok http 8600
The above command should output the following which indicates a public URL of the local server.
Session Status online
Account Kyungrae Kim (Plan: Free)
Version 2.3.35
Region United States (us)
Web Interface http://127.0.0.1:XXXX
Forwarding http://XXXX.ngrok.io -> http://localhost:8600
Forwarding https://XXXX.ngrok.io -> http://localhost:8600
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
HTTP request can be sent using Google Assistant by pairing with Webhook on If This Than This (IFTTT).
Create a new applet following Integrating Google Home And IFTTT Webhooks.
For this example,
- Select "Say a simple phrase" as trigger
- Select "Webhooks" as action
- Select "POST" as method for the Webhooks
- Use
{"mac_address": "XX:XX:XX:XX:XX:XX"}
as Body for the Webhooks - Use the ngrok URL + /api/v1/wol as URL for the Webhooks
- Select "application/json" as Content Type for the Webhooks
- Lifewire - How to Set Up and Use Wake-on-LAN
- Data Science Blog - REST API Development with Flask
- Integrating Google Home And IFTTT Webhooks
- Reddit - [GUIDE] Switch your PC from anywhere in the world with an OK Google command (using a RaspberryPi and wake on LAN)
- How to Host a Raspberry Pi Web Server on the Internet with ngrok
- intrinsic - Why should I use a Reverse Proxy if Node.js is Production-Ready?
- How to Build a Raspberry Pi Server for Development
- How do you set up a local testing server?
- Python's http.server library “basic security checks”
- Wake On LAN Python Script
- WAKE ON LAN (PYTHON RECIPE)
- https://andreashessblog.wordpress.com/2016/12/10/python-script-wake-on-lan/
- Github - Python Wake on LAN
- /wol v1 Completed - 17 September 2020