[PYTHON] Home Control from Cortana.

Control the Raspberry PI from Cortana on Windows 10.

Cortana Cortana is voice recognition built into Windows 10. image.png To make the command recognized in Windows 10 Cortana, create a shortcut in the Start menu. Create a holder called ifttt and place the shortcut. For exampleImage.png "Turn on the lights. When you open "", the shortcut will be executed. ** Caution ** Immediately after placing it, it will not be recognized, so click it to execute.

C:\Users\{username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\ifttt

Create a shortcut for the execution command below.

image.png

image.png

image.png

The link destination is the following program.

C:\Users\hirat\Anaconda3\python.exe C:/Users/hirat/source/repos/shortCut0/ifttt.py TV

ifttt.py. I created a simple web request command. When coding a request to ifttt with python

ifttt.py


import os,sys,requests
if __name__ == '__main__':    r=requests.post('https://maker.ifttt.com/trigger/HomeControl/with/key/{Key string}',
                   {'value1':sys.argv[1]})
    print(r.text)

Install Windows Script Host Library

pip install pypiwin32

To programmatically create a shortcut for windows, use Windows Script Host.

shortcut0.py


import win32api as WScript
import win32com.client
if __name__ == '__main__':
    cortana={"1 channel":"1","2 channels":"2","3 channels":"3","4 channels":"4","5 channels":"5","6 channels":"6","7 channels":"7","8 channels":"8","9 channels":"9","10 channels":"10","11 channel":"11","12 channels":"12","TV power supply":"TV","TV channel plus":"cup","TV channel minus":"cdwn","Turn up the TV volume":"vup","Turn down the TV volume":"vdwn","Mute":"vcut","bs":"bs","cs":"cs","bs1":"bs1","bs2":"bs2","bs3":"bs3","bs4":"bs4","bs5":"bs5","bs6":"bs6","bs7":"bs7","bs8":"bs8","bs9":"bs9","bs10":"bs10","bs11":"bs11","bs12":"bs12","inUp":"inUp","inDwn":"inDwn","ent":"ent","Turn on the lights":"on","Turn off the lights":"off","Favorite":"fav","Security light":"small","Bright":"lup","Dark":"ldwn","Turn on the air conditioner":"ac","Stop the air conditioner":"acoff","Turn on the heating":"heeting","Dehumidify":"Joshitsu","Radio power":"ron","Radio 1":"r1","Radio 2":"r2","Radio 3":"r3","Radio 4":"r4","Radio 5":"r5","Radio 6":"r6","Radio 7":"r7","Radio 8":"r8","Radio 9":"r9","Radio 0":"r0","Raise the radio volume":"R+","Decrease radio volume":"R-","Radio next channel":"r>"}
    for key,item in cortana.items():    
        WshShell = win32com.client.Dispatch("WScript.Shell")
        shortCut=WshShell.CreateShortcut("C:/Users/hirat/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/ifttt_p/%s.lnk"%key)
        shortCut.TargetPath="python.exe"
        shortCut.Arguments = "C:/Users/hirat/source/repos/shortCut0/ifttt.py %s"%item
        shortCut.IconLocation ="C:/Users/hirat/source/repos/shortCut0/favicon.ico"
        shortCut.Save()

Define webhooks. image.png

image.png

You can get the ifttt request from the following URL. https://ifttt.com/maker_webhooksから image.png

Use MQ service.

MQ (message queuing) is one of the methods to send and receive data between different software. Instead of passing the data directly, it is temporarily deposited in the software of a third party, so that both the sender and the receiver can use the timing they like. A method that allows you to perform transmission / reception processing with. This time, I use it to connect the interface between the PC and Raspberry PI. Originally, it is used to link mainframes with different systems such as ATMs.

image.png

BeeBottle is a free MQ service.

image.png Immediately after logging in, test and channel will be created. image.png

image.png Make a note of the channel token. image.png Open the console and make sure you can send and receive. image.png

{ "channel": "test", "resource": "res", "eid": "test.res", "data": "Hello World", "ts": 1573277790737 }

If you can subscribe and Publish to Hello world, it's OK! Next, test whether the Raspberry PI python program connected in the LAN can subscribe. Download the certificate for access from https://beebotte.com/certs/mqtt.beebotte.com.pem.

Test program for Raspberry pi to subscribe

test.py


import paho.mqtt.client as mqtt
import json
HOST = 'mqtt.beebotte.com'
PORT = 8883
CA_CERTS = 'mqtt.beebotte.com.pem'
TOKEN = '{Token string}'
TOPIC = 'test/res' #Channel/resource
#When connected
def on_connect(client, userdata, flags, respons_code):
    print('on_connect status {0}'.format(respons_code))
#When you receive a message
def on_message(client, userdata, msg):
    msg=json.loads(msg.payload.decode("utf-8"))
    mqmsg=msg['data']
    print(mqmsg)
    

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set('token:%s' % TOKEN)
client.tls_set(CA_CERTS)
client.connect(HOST, PORT)
client.subscribe(TOPIC)
print('start')
#Event loop
while True:
    try:
        client.loop_start()
    except:
        break
print('exit')

Publish Tools https://beebotte.com/api/play

image.png

If you code this in a Python program

publish.py


import os,sys,requests,json
if __name__ == '__main__':
    payload = {'data': sys.argv[1]}
    r=requests.post('https://api.beebotte.com/v1/data/write/test/res?token=token_X5Gyvy1ymKHzpBQd',payload)
    print(r.text)

Execution result

C:\Users\hirat\source\repos\mqtt>python publish.py "hello world tokyo"
true

C:\Users\hirat\source\repos\mqtt>test.py
start
on_connect status 0
hello world tokyo
exit

Test Curl

curl -i -H "Content-Type: application/json" -X POST -d '{"data":"Hello World form curl"}' https://api.beebotte.com/v1/data/write/test/res?token=token_X5Gyvy1ymKHzpBQd

image.png

If you incorporate it in, it will work.

Recommended Posts

Home Control from Cortana.
Study from Python Hour2: Control statements
Control the Matrix LED panel from ROS