Cov txheej txheem:

SmartWand: 6 Kauj Ruam
SmartWand: 6 Kauj Ruam

Video: SmartWand: 6 Kauj Ruam

Video: SmartWand: 6 Kauj Ruam
Video: Mi Band 6. Калибровка шагов и скрытые фишки. Обзор и сравнение с Mi Band 5. 2024, Hlis ntuj nqeg
Anonim
SmartWand
SmartWand

Txoj haujlwm no yog kom tau txais Python tsab ntawv los tswj Smartthings nrog Kano coding wand ua cov tswv yim.

Kuv cov ntxhais (8 thiab 12) yog Harry Potter kiv cua loj thiab tau txais Kano Coding Wand rau Christmas. Kano coding app yog qhov txias thiab lawv tau lom zem nrog nws. Cov cuab yeej zoo los qhia cov ntsiab lus coding.

Peb kuj tseem muaj lub tsev me me hloov pauv thoob plaws lub tsev nrog SmartThings, Phillips Hawj txawm, Logitech Harmony hub, thiab lwm yam … Lawv tsis tuaj yeem mus txog qhov uas lawv xav tig lub Christmas Ntoo Teeb nrog tus pas thiab pib cuam cov ntsiab lus zoo li Lumos thiab Nox kuv. Suab zoo li kev sib tw lom zem yog li kuv coj nws mus.

Yuav tsum mus nrog Linux OS txij li lub hauv paus tseem ceeb (Bluepy nab hab sej tsev qiv ntawv rau txuas rau tus pas nrig Bluetooth) tsuas yog muaj nyob ntawm Linux platforms. Yooj yim dua los ntawm qhov pom ntawm qhov kawg xav ua qhov no ntawm Raspberry Pi.

Muaj ob qhov tseem ceeb siv ntawm no, tsis muaj qhov twg, Kuv yuav tsis muaj peev xwm ua qhov no.

Ua tsaug rau GammaGames rau kev tsim thiab sib qhia ib tsab ntawv nab nab rau nyeem Kano coding wand.

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

thiab

Ua tsaug rau rllynch rau kev tsim thiab sib qhia ib tsab ntawv nab nab rau SmartThings hais kom ua kab interface.

github.com/rllynch/smartthings_cli

Txhawm rau ua kom zoo li no, Kuv yuav theej feem ntau ntawm cov kauj ruam hla mus rau Cov Lus Qhia no txhawm rau ntes ib qho tweaks uas kuv xav tau kom nws ua haujlwm ntawm kuv teeb tsa.

Nov yog yam koj xav tau:

  • Harry Potter Kano Coding Kit (https://www.amazon.com/Kano-Harry-Potter-Coding-Ki…
  • Linux Raws Li OS (Kuv tau txais ib qho thiab ua haujlwm nrog VirtualBox, tom qab ntawd teeb tsa ntawm Raspberry Pi)
  • Bluetooth USB adapter (Kuv siv Kensington Bluetooth 4.0 USB Adapter)
  • Kev Sib Txuas Hauv Is Taws Nem (Rau RPi, Kuv siv Edimax USB WiFi adapter uas kuv muaj los ntawm txoj haujlwm yav dhau los)

Kauj Ruam 1: Nruab Linux ntawm Raspberry Pi

Ua raws qhov txuas hauv qab no txhawm rau teeb tsa Linux OS mus rau Raspberry Pi. Kuv siv Raspbian Stretch nrog lub desktop thiab pom zoo software thiab flashed nrog Etcher.

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

Thaum koj tau ua tiav kev teeb tsa thiab muaj Linux Command Prompt, nws yog kev coj ua zoo los ua ob qho lus txib hauv qab no kom ntseeg tau tias txhua yam yog qhov tshiab.

sudo apt-tau hloov tshiab

sudo apt-tau hloov tshiab

Ntaus cov hauv qab no ntawm kab lus hais kom ua kom qhib lub desktop UI.

sudo pibx

Yam tom ntej los teeb tsa yog kev sib txuas is taws nem kom koj tuaj yeem tau txais repos thiab xws li txhawm rau txhim kho software ntxiv. Kev sib txuas hauv Is Taws Nem tseem yuav xav tau txhawm rau txhawm rau SmartThings API. Ua raws li phau ntawv qhia no kom tau txais kev sib txuas. Zoo nkauj ncaj nraim rau ntawm lub desktop. Kuv siv USB wifi Adapter Kuv tau pw ib puag ncig.

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

Lwm txoj hauv kev rau Raspberry Pi, koj tuaj yeem siv lwm lub khoos phis tawj uas twb muaj lawm los ua ob lub khau raj Linux OS nrog rau koj li OS uas twb muaj lawm (yam mob vim tias koj yuav tsum tau rov pib dua los hloov ntawm ob) lossis khiav ib qho piv txwv ntawm Linux OS ntawm VirtualBox. Ua ntej tau txais txoj haujlwm no pib ua haujlwm, Kuv teeb tsa Debian Stretch nrog Raspberry Pi Desktop hauv Virtual Box siv phau ntawv qhia no:

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

(ceeb toom: Kuv muaj lub sijhawm tau txais qhua Addons teeb tsa kom raug raws li cov lus qhia saum toj no. kuv qhov kev tshuaj xyuas tag nrho. Qhov no yog ntu ntawm google rabbit qhov uas kuv yuav tsis sau ntawv ntawm no.)

Kauj ruam 2: Nruab Nab hab sej 3

Python3 yuav tsum tau teeb tsa nrog Raspian Stretch.

Kauj ruam 3: Teeb Wand Module

Ua raws phau ntawv qhia no tsim los ntawm GammaGames

Kuv xub tau hloov pauv mus rau cov npe sib txawv ua ntej cloning kano_wand repo, txwv tsis pub kuv cov ntawv nab nab tsis tuaj yeem pom nws. Tej zaum yuav tau hloov kho qee txoj hauv kev hais txog hauv qee cov ntaub ntawv nyob qhov twg, tab sis kuv tsis tau khawb rau hauv.

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

git clone

sudo pip3 nruab bluepy moosegesture

Yuav tsum siv sudo rau cov no kom tau txais kev tso cai kom raug. Kuj yuav tsum tau siv cov lus txib hauv qab no txhawm rau txhawm rau nruab numpy, vim li cas los xij, tsis tuaj yeem tau txais pip los ua haujlwm. Tej zaum yuav yog lwm qhov teeb meem, tab sis qhov no ua haujlwm rau kuv yog li kuv tau mus nrog nws:

sudo apt-tau nruab python3-numpy

Thaum kawg, txhawm rau kom tau txais kev tso cai kom khiav bluepy los ntawm cov ntawv nab nab Kuv pom cov lus txib no.

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

Nov yog txhua yam uas yuav tsum tau ua kom peb cov ntawv sau thiab ua haujlwm. Tus so ntawm GammaGames phau ntawv qhia taug kev los ntawm cov kauj ruam ib kauj ruam uas yuav tsum tau ua hauv tsab ntawv nab. Nws yog qhov kev rhuav tshem kom nkag siab ntxiv tias tsab ntawv tau teeb tsa li cas thiab txhua yam khoom siv tau ua dab tsi. Ua tsaug ntau rau GammaGames rau muab cov ntaub ntawv no. Kuj tseem tuaj yeem pab tau rau kev daws teeb meem ib feem ntawm tsab ntawv. Piv txwv li, thawj qhov khoom pom tau ua tiav txoj haujlwm ntawm kev tshuaj xyuas cov wands thiab xa rov qab daim ntawv teev cov wands pom. Kev pov thawj zoo uas koj teeb tsa bluetooth tau tua ntawm txhua lub tog raj kheej. Txhawm rau ua qhov no, koj tuaj yeem luam qhov chaws los ntawm test1_BLE_wand_detect.py pom hauv cov lus hais hauv qab no:

github.com/maspieljr/SmartWand

Kauj Ruam 4: Teeb SmartThings CLI

Hauv qab no yog daim ntawv theej ntawm cov lus qhia suav nrog hauv smartthings_cli repo ntawm github (https://github.com/rllynch/smartthings_cli).

Kuv tau suav nrog qhov me me tweaks uas kuv yuav tsum tau ua ntawm kuv teeb tsa kom tau txhua yam ua haujlwm. Ua tsaug dua rau rllynch rau muab qhov no.

1) Nkag mus rau thiab hauv qab Kuv SmartApps, tsim SmartApp tshiab nrog cov cai hauv groovy/app.groovy.

*Nco tseg hauv thawj kauj ruam muaj qhov siv rau SmartThings lub vev xaib. Ua tib zoo saib xyuas qhov twg koj tus account ntse yog. Qhov no hla kuv mus ib pliag li lwm qhov chaw tso cai rau kuv nkag mus, tab sis tsis pom ib qho ntawm kuv cov khoom. Kuv yuav tsum siv qhov txuas hauv qab no kom nkag mus rau kuv tus lej SmartThings.https://graph-na04-useast2.api.smartthings.com

(Koj zoo siab tos txais 2 teev uas kuv siv sijhawm txheeb xyuas qhov ntawd:) qhov no yuav yog qhov tseem ceeb ntawm cov kauj ruam tom ntej ib yam.)

2) Nyem App Settings thiab hauv qab OAuth, nyem Enable OAuth hauv Smart App. Nco tseg OAuth Client ID thiab OAuth Client Secret. Hloov kho OAuth Client Display rau SmartThings CLI Control. Nyem Hloov tshiab

3) Rov qab mus rau Kuv SmartApps ces nyem rau ntawm SmartThings CLI Tswj. Nyem Publish => Rau Kuv.

4) Clone lub smartthings_cli chaw cia khoom, tsim lub virtualenv yog tias xav tau (Kuv tsis tau ua qhov no), tom qab ntawv ua raws cov lus txib hauv qab no, hloov CLIENTID thiab CLIENTSECRET nrog tus ID thiab zais cia ntawm theem 2.

Txhawm rau clone cov smartthings repo koj tuaj yeem siv cov lus txib hauv qab no. Nco ntsoov tias linux cov lus txib tam sim yog nyob hauv koj phau ntawv teev npe tsim thaum lub sij hawm teeb tsa Module teeb tsa.

clit ua >

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…

Pom zoo: