bk1285 / rpi_wordclock

Software to create a Raspberry Pi based wordclock
GNU General Public License v3.0
214 stars 107 forks source link

Set plugin from script #116

Closed MarioVicc closed 4 years ago

MarioVicc commented 5 years ago

Hi

First of all, let me thank you all for this great project!

I’m looking for the best way to switch to a specific plugin from a (python) script and hope you guys can help me out.

(I already have a script that determines if I’m present by checking whether my phone is in Bluetooth range and I’d like to turn the wordclock on and off accordingly)

Thanks Mario

MarioVicc commented 5 years ago

To answer my own question for future user:

You can use the included RESTful API to interact with the wordclock. Issuing requests to a web server seems a bit cumbersome to me, but it works:

import requests

headers = {
    'accept': 'application/json',
    'Content-Type': 'application/json',
}

led_off = '{  "name": "leds_off"}'

response = requests.post('http://127.0.0.1/api/plugin', headers=headers, data=led_off)

you'll have to install requests first:

pip install requests

MarioVicc commented 5 years ago

And here the actual script I’m running based off prb3333’s work over at https://www.instructables.com/id/Raspberry-Pi-Bluetooth-InOut-Board-or-Whos-Hom/ Check out that post to see how to set it up!

import bluetooth
import time
import os
import requests

headers = {
    'accept': 'application/json',
    'Content-Type': 'application/json',
}

time_default = '{  "name": "time_default"}'
led_off = '{  "name": "leds_off"}'

time.sleep(120)

count = 0

result = bluetooth.lookup_name('AA:BB:CC:DD:EE:FF', timeout=5)
if (result != None):
    response = requests.post('http://127.0.0.1/api/plugin', headers=headers, data=time_default)
    user_is_present = 1
else:
    response = requests.post('http://127.0.0.1/api/plugin', headers=headers, data=led_off)
    user_is_present = 0

while True:
    result = bluetooth.lookup_name('AA:BB:CC:DD:EE:FF', timeout=5)
    if (result != None):
        count = 0
        if (user_is_present == 0):
            response = requests.post('http://127.0.0.1/api/plugin', headers=headers, data=time_default)
            user_is_present = 1
    else:
        if (user_is_present == 1):
            count = count + 1
            if (count > 5):
                response = requests.post('http://127.0.0.1/api/plugin', headers=headers, data=led_off)
                user_is_present = 0
                count = 0

    time.sleep(20)
bk1285 commented 4 years ago

Nice! :)