Skip to content

Latest commit

 

History

History

flask_app

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Flask Application

Author: Kyungrae Kim


Turn On a Wake-on-LAN enabled Computer

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:


Example

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"}

Enable Wake-on-LAN (WoL)

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.

Getting Started

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

Development Mode

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.

Open API

Production

Run the application in production using uWSGI with the following command:

uwsgi --ini app.ini --need-app

Turn On the Computer with Google Assistant

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.

Expose the Local Server with ngrok

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  

Send a POST request with Google Assistant with IFTTT

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,

  1. Select "Say a simple phrase" as trigger
  2. Select "Webhooks" as action
  3. Select "POST" as method for the Webhooks
  4. Use {"mac_address": "XX:XX:XX:XX:XX:XX"} as Body for the Webhooks
  5. Use the ngrok URL + /api/v1/wol as URL for the Webhooks
  6. Select "application/json" as Content Type for the Webhooks

IFTTT Applet


Credits


Change Log

  • /wol v1 Completed - 17 September 2020