Taula de continguts:

SmartWand: 6 passos
SmartWand: 6 passos

Vídeo: SmartWand: 6 passos

Vídeo: SmartWand: 6 passos
Vídeo: Resolva o problema com os Passos na Mi Band 6 2024, Juliol
Anonim
SmartWand
SmartWand

Aquest projecte consisteix a obtenir un script Python per controlar Smartthings amb la vareta de codificació Kano com a entrada.

Les meves filles (8 i 12 anys) són grans fans de Harry Potter i van aconseguir la vareta de codificació Kano per Nadal. L’aplicació de codificació Kano és genial i s’ho passen bé. Gran eina per introduir conceptes de codificació.

També tenim una mica d’automatització domèstica esquitxada per tota la casa amb SmartThings, Phillips Hue, el centre Logitech Harmony, etc … Inevitablement van arribar al punt que volien encendre els llums de l’arbre de Nadal amb la vareta i van començar a llançar termes com Lumos i Nox at me. Em va semblar un repte divertit, així que ho vaig assumir.

Havia d’anar amb un sistema operatiu Linux ja que un element crític (biblioteca Python de Bluepy per connectar-se a la vareta Bluetooth) només està disponible a les plataformes Linux. Convenient de totes maneres, des del punt de vista de voler executar-ho en un Raspberry Pi.

Aquí hi ha dues referències principals, sense les quals, mai no hauria pogut fer això.

Gràcies a GammaGames per crear i compartir un script Python per llegir la vareta de codificació Kano.

github.com/GammaGames/kano-wand-demos/blob…

i

Gràcies a rllynch per crear i compartir un script python per a una interfície de línia d'ordres SmartThings.

github.com/rllynch/smartthings_cli

Per tal de mantenir-ho intacte, copiaré la majoria dels passos d’aquest manual instructiu per capturar els ajustaments necessaris perquè funcioni en la meva configuració.

Això és el que necessiteu:

  • Kit de codificació de Harry Potter Kano (https://www.amazon.com/Kano-Harry-Potter-Coding-Ki…
  • Un sistema operatiu basat en Linux (en vaig tenir un que funcionava amb VirtualBox, i després el vaig instal·lar en un Raspberry Pi)
  • Un adaptador USB Bluetooth (he utilitzat un adaptador USB Kensington Bluetooth 4.0)
  • Connexió a Internet (per al RPi, he utilitzat un adaptador WiFi USB Edimax que tenia d’un projecte anterior)

Pas 1: instal·leu Linux a Raspberry Pi

Seguiu els enllaços següents per instal·lar un sistema operatiu Linux al Raspberry Pi. He utilitzat Raspbian Stretch amb l’escriptori i el programari recomanat i he fet flash amb Etcher.

www.raspberrypi.org/learning/software-guid…

Un cop hàgiu completat la instal·lació i tingueu un símbol del sistema, és una bona pràctica executar les dues ordres següents per assegurar-vos que tot estigui actualitzat.

sudo apt-get update

sudo apt-get upgrade

Escriviu el següent a la interfície de línia d'ordres per iniciar la interfície d'usuari de l'escriptori.

sudo startx

El següent que cal configurar és una connexió a Internet perquè pugueu accedir a repos i altres per instal·lar programari addicional. També es necessitarà connexió a Internet per accedir a l'API SmartThings. Seguiu aquesta guia per connectar-vos. Bastant directe des de l'escriptori. Vaig utilitzar un adaptador de wifi USB que tenia a la vora.

www.raspberrypi.org/learning/software-guid…

Alternativament al Raspberry Pi, podeu utilitzar un altre ordinador existent per arrencar dualment un sistema operatiu Linux juntament amb el sistema operatiu existent (és una mena de dolor perquè heu de reiniciar per canviar entre els dos) o executar una instància del sistema operatiu Linux en un VirtualBox. Per fer funcionar aquest projecte inicialment, he instal·lat Debian Stretch amb Raspberry Pi Desktop en una caixa virtual mitjançant aquesta guia:

thepi.io/how-to-run-raspberry-pi-desktop-o…

(Nota: Vaig tenir un munt de temps instal·lant correctament els complements de convidats segons les instruccions anteriors. Mai no vaig poder treballar i tallar entre l'amfitrió i el client, cosa que hauria estat agradable, però sí que vaig aconseguir que la resolució s'actualitzés per utilitzar-la) la mida del meu monitor complet. Aquesta era una sèrie de forats de rabit de Google que no documentaré aquí.)

Pas 2: instal·leu Python 3

Python3 ja s'hauria d'instal·lar amb Raspian Stretch.

Pas 3: Configureu el mòdul de vareta

Seguiu aquesta guia creada per GammaGames

Primer vaig haver de canviar a un directori diferent abans de clonar el repositori kano_wand, en cas contrari, el meu script Python no el podria trobar. Probablement hauria pogut actualitzar algunes referències de camí d'accés en algun fitxer en algun lloc, però no m'he endinsat en això.

cd /usr/local/lib/python3.5/dist-packages

git clone

sudo pip3 instal·la bluepy moosegesture

Va haver d’utilitzar sudo per obtenir els permisos adequats. També va haver d'utilitzar les ordres següents per instal·lar numpy, per qualsevol motiu, no podia fer funcionar pip. Podria haver estat un altre problema de camí, però això va funcionar per a mi, així que vaig anar amb ell:

sudo apt-get install python3-numpy

Finalment, per tal d'obtenir els permisos adequats per executar bluepy des de l'script python, he trobat aquesta ordre.

sudo setcap 'cap_net_raw, cap_net_admin + eip' /usr/local/lib/python3.5/dist-packages/bluepy/bluepy-helper

Això és tot el necessari per posar en funcionament el nostre script. La resta de la guia de GammaGames recorre les peces pas a pas necessàries a l’escriptura python. És un gran desglossament per comprendre encara més com s’estructura el guió i què fa cada objecte. Gràcies a GammaGames per proporcionar aquesta documentació. També pot ser útil per solucionar problemes d'una secció de l'script. Per exemple, el primer objecte mostrat realitza la tasca d'escanejar varetes i retornar una llista de varetes descobertes. Bona comprovació de que la configuració del bluetooth funciona en tots els cilindres. Per fer-ho, podeu copiar el codi de test1_BLE_wand_detect.py que es troba al repositori següent:

github.com/maspieljr/SmartWand

Pas 4: configureu SmartThings CLI

A continuació es mostra una còpia de les instruccions incloses a la reposició de smartthings_cli a github (https://github.com/rllynch/smartthings_cli).

He inclòs aquí els petits ajustaments que havia de fer a la meva configuració perquè tot funcionés. Gràcies de nou a rllynch per proporcionar-ho.

1) Inicieu la sessió i dins de Les meves SmartApps, creeu una nova SmartApp amb el codi a groovy / app.groovy.

* Tingueu en compte al primer pas que hi ha una referència a un lloc web SmartThings. Presteu molta atenció a on es troba el vostre compte de smartthings. Això em va provocar una estona, ja que l'altre lloc em va permetre iniciar la sessió, però no vaig trobar cap de les meves coses. Vaig haver d’utilitzar el següent enllaç per accedir al meu compte de SmartThings.https://graph-na04-useast2.api.smartthings.com

(Us donem la benvinguda durant les dues hores que vaig dedicar a solucionar-ho:) això també serà important en el pas posterior.)

2) Feu clic a Configuració de l'aplicació i, a OAuth, feu clic a Activa OAuth a Smart App. Anoteu l’identificador de client OAuth i el secret de client OAuth. Actualitzeu la pantalla del client OAuth a SmartThings CLI Control. Feu clic a Actualitza.

3) Torneu a My SmartApps i feu clic a SmartThings CLI Control. Feu clic a Publica => Per a mi.

4) Cloneu el dipòsit smartthings_cli, creeu un virtualenv si voleu (no ho he fet) i executeu les ordres següents, substituint CLIENTID i CLIENTSECRET per l'ID i el secret del pas 2.

Per clonar el repositori de smartthings, podeu utilitzar l'ordre següent. Assegureu-vos que l’indicador d’ordres de linux es troba al directori del projecte creat durant la configuració del mòdul Wand.

git clon >

then change directory again down to the smartthings_cli directory that was just created.

cd smartthings_cli

python setup.py install

smartthings_cli --clientid clientid --clientsecret clientsecret

5) smartthings_cli will direct you to a url to authorized access. copy the url from the response in the command window and be sure to update it with the proper path as we had to in step 1. go to that url in a browser and specify which devices the cli should be able to access. click authorize when finished. you should be redirected to a page reporting smartthings_cli.py received auth code.

last few things i needed to do in order to get rid of a warning that kept coming up:

sudo apt-get install libssl-dev

pip install service_identity

pip install attrs pip install pyopenssl pip install pyasn1 pip install pyasn1-modules pip install ipaddress

raspberry pi should now be set-up to issue smart thing commands from the command line interface, try it out with these examples:

smartthings_cli query switch all

smartthings_cli query switch "switch name"

smartthings_cli set switch "switch name" on

step 5: improve response

everything is running at this point but there's a bit of a lag once the wand gesture is captured. in attempt to speed up the response, i've embedded the smarthings logic into the smartwand python script rather than calling it from a command line as it does in smartwand.py. this eliminated the need to repeatedly import the modules required for smartthings communication, which is what was slowing everything down. here's what i had to do to get that working:

python3 -m pip install future

python3 -m pip install twisted

made update to the python script. see smartwand2.py stored in the following repo:

github.com/maspieljr/smartwand

step 6: make smartwand execute on raspberry pi bootup

so you only need to plug in the raspberry pi near your smartthings things and not require a monitor, and keyboard, i followed these instructions to get the script to run on boot or any time a command line terminal is launched. the script seems pretty robust but does get hung up from time to time, requiring a reboot. alternatively you could have a keyboard connected and use alt+f4 to kill a running script and ctrl+alt+t to launch a new terminal without needing a monitor to see anything.

method 2: modify the.bashrc file as described in the link below:

www.dexterindustries.com/howto/run-a-progr…

Recomanat: