Google AssistantとRaspberryPiで自宅の家電を操作する

Standard

pi@raspberrypi:~ $ wget https://beebotte.com/certs/mqtt.beebotte.com.pem
pi@raspberrypi:~ $ cat smarthome.py
import os
import paho.mqtt.client as mqtt
import json
 
TOKEN = "token_PSKvq93xxxxxxxx"
HOSTNAME = "mqtt.beebotte.com"
PORT = 8883
TOPIC = "MySmartHome/voice"
CACERT = "mqtt.beebotte.com.pem"
 
def on_connect(client, userdata, flags, respons_code):
    print('status {0}'.format(respons_code))
    client.subscribe(TOPIC)
 
def on_message(client, userdata, msg):
    #print(msg.topic + " " + str(msg.payload))
    data = json.loads(msg.payload.decode("utf-8"))["data"][0]
    if data['room'] == "any":
        if data['device'] == "tv":
            if data['action'] == "on":
                os.system('/usr/bin/irsend SEND_ONCE tvjp on')
                print('tv on!')
            if data['action'] == "off":
                os.system('/usr/bin/irsend SEND_ONCE tvjp off')
                print('tv off!')
 
client = mqtt.Client()
client.username_pw_set("token:%s"%TOKEN)
client.on_connect = on_connect
client.on_message = on_message
client.tls_set(CACERT)
client.connect(HOSTNAME, port=PORT, keepalive=60)
client.loop_forever()

以下のサイト参考にさせていただきました。ありがとうございます。
https://qiita.com/104ki/items/9dcfe03246099d03d4dd

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.