-
Notifications
You must be signed in to change notification settings - Fork 0
/
waker.py
49 lines (40 loc) · 1.24 KB
/
waker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from paho.mqtt import client as mqtt_client
import sys
import os
mac_address = sys.argv[2]
broker = sys.argv[1]
port = 1883
topic = "wol/mqtt"
client_id = 'raspi'
username = sys.argv[3]
password = sys.argv[4]
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
# Set Connecting Client ID
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
def wake_up_gaming_pc(mac_address):
os.system(f"echo Waking up {mac_address}")
os.system(f"sudo etherwake -i eth0 {mac_address}")
return
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
print(f"Received '{msg.payload.decode()}' on {msg.topic}")
if msg.topic == topic and msg.payload.decode() == "wake up":
wake_up_gaming_pc(mac_address)
return
err = client.subscribe(topic)
if err[0] != 0:
print("Failed to connect to broker")
client.on_message = on_message
return
client = connect_mqtt()
subscribe(client)
client.loop_forever()