Mundarija:

SmartWand: 6 qadam
SmartWand: 6 qadam

Video: SmartWand: 6 qadam

Video: SmartWand: 6 qadam
Video: En zo'r soat aqlli soatlar Mi band 4 soatlari Qadam, yurak urishi, uxlash, kaloriya sarfi aniqlaydi 2024, Noyabr
Anonim
SmartWand
SmartWand

Ushbu loyiha Smartthings -ni kirish uchun Kano kodlash tayog'i bilan boshqarish uchun Python skriptini olishdir.

Mening qizlarim (8 va 12) Garri Potterning katta muxlislari va Rojdestvo uchun Kano kodlash tayog'ini olishgan. Kano kodlash dasturi ajoyib va ular bundan zavqlanishadi. Kodlash tushunchalarini kiritish uchun ajoyib vosita.

SmartThings, Phillips Hue, Logitech Harmony uyasi va boshqalarni o'z ichiga olgan uy avtomatizatsiyasi ham bor. Ular muqarrar ravishda tayoq bilan Rojdestvo daraxti chiroqlarini yoqishni xohlashdi va Lumos va Menga yoq. Bu kulgili vazifa kabi eshitildi, shuning uchun men o'zimni oldim.

Linux OS bilan ishlashga to'g'ri keldi, chunki muhim element (tayoqchali Bluetooth -ga ulanish uchun Bluepy python kutubxonasi) faqat Linux platformalarida mavjud. Oxir oqibat, bu Raspberry Pi -da ishlashni xohlash nuqtai nazaridan qulay.

Bu erda ikkita asosiy ma'lumot bor, ularsiz men buni hech qachon qilmagan bo'lardim.

Kano kodlash tayoqchasini o'qish uchun python skriptini yaratgani va almashgani uchun GammaGamesga rahmat.

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

va

SmartThings buyruq qatori interfeysi uchun python skriptini yaratish va almashish uchun rllynchga rahmat.

github.com/rllynch/smartthings_cli

Bu o'zgarishsiz qolishi uchun, men ko'p qadamlarni ushbu yo'riqnomaga ko'chiraman, uni sozlashda ishlashim uchun kerak bo'lgan har qanday sozlamalarni yozib olaman.

Mana sizga nima kerak:

  • Garri Potter Kano kodlash to'plami (https://www.amazon.com/Kano-Harry-Potter-Coding-Ki…
  • Linux asosidagi operatsion tizim (men VirtualBox bilan ishladim, keyin Raspberry Pi -ga o'rnatdim)
  • Bluetooth USB adapteri (men Kensington Bluetooth 4.0 USB adapteridan foydalanganman)
  • Internetga ulanish (RPi uchun men oldingi loyihada bo'lgan Edimax USB WiFi adapteridan foydalandim)

1 -qadam: Linuxni Raspberry Pi -ga o'rnating

Raspberry Pi -ga Linux operatsion tizimini o'rnatish uchun quyidagi havolalarga rioya qiling. Men ish stoli va tavsiya etilgan dasturiy ta'minot bilan Raspbian Stretch -dan foydalandim va Etcher bilan ishladim.

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

O'rnatishni tugatganingizdan va Linux buyruq satriga ega bo'lgandan so'ng, hamma narsa yangilanganligiga ishonch hosil qilish uchun quyidagi ikkita buyruqni bajaring.

sudo apt-get yangilanishi

sudo apt-get upgrade

Ish stoli foydalanuvchi interfeysini ishga tushirish uchun buyruq qatori interfeysiga quyidagilarni kiriting.

sudo startx

Keyingi sozlash - bu Internetga ulanish, shuning uchun siz repo -ga o'tishingiz va boshqa dasturlarni o'rnatishingiz mumkin. SmartThings API -ni yoqish uchun Internetga ulanish ham talab qilinadi. Ulanish uchun ushbu qo'llanmani bajaring. Ish stolidan juda to'g'ri. Men o'zim qo'ygan USB Wi -Fi adapteridan foydalandim.

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

Shu bilan bir qatorda, Raspberry Pi -ga ko'ra, siz boshqa mavjud kompyuterni ishlatib, Linux operatsion tizimini, shuningdek, mavjud operatsion tizimingiz bilan birga yuklashingiz mumkin (og'riqli, chunki ikkalasini almashtirish uchun qayta yuklashingiz kerak) yoki Linux operatsion tizimining misolini ishga tushirishingiz mumkin. Virtual quti. Dastlab bu loyihani ishga tushirish uchun men Raspberry Pi Desktop bilan Debian Stretch dasturini Virtual qutiga o'rnatdim:

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

(eslatma: Men mehmonlar qo'shimchalarini yuqoridagi ko'rsatmalarga muvofiq to'g'ri o'rnatgan paytim ko'p edi. Hech qachon xost va mijoz o'rtasida kesish va joylashtirish ishi bo'lmagan, bu yaxshi bo'lardi, lekin men foydalanish uchun piksellar sonini yangilashga muvaffaq bo'ldim. Mening to'liq monitor o'lchamim. Bu Google quyon teshiklari edi, men bu erda hujjat qilmayman.)

2 -qadam: Python 3 -ni o'rnating

Python3 allaqachon Raspian Stretch bilan o'rnatilishi kerak.

3 -qadam: tayoq modulini sozlash

GammaGames tomonidan yaratilgan ushbu qo'llanmani bajaring

Kano_wand repo -ni klonlashdan oldin men boshqa katalogga o'tishim kerak edi, aks holda mening python skriptim topa olmadi. Ehtimol, biron -bir faylda ba'zi yo'l ma'lumotlarini yangilash mumkin edi, lekin men buni o'rganmadim.

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

git klon

sudo pip3 bluepy moosegesture -ni o'rnating

To'g'ri ruxsat olish uchun ular uchun sudo -dan foydalanish kerak edi. Shuningdek, numpy -ni o'rnatish uchun quyidagi buyruqlarni ishlatishga to'g'ri keldi, nima sababdan pip ishga tusha olmadi. Boshqa yo'l muammosi bo'lishi mumkin, lekin bu men uchun ishlagan, shuning uchun men u bilan bordim:

sudo apt-get install python3-numpy

Nihoyat, python skriptidan bluepy ishlashga to'g'ri ruxsat olish uchun men bu buyruqni topdim.

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

Bu bizning skriptimizni ishga tushirish uchun kerak. Qolgan GammaGames qo'llanmasi python skriptida talab qilinadigan bo'laklarni bosqichma -bosqich o'rganib chiqadi. Bu skript qanday tuzilganligini va har bir ob'ekt nima qilayotganini tushunish uchun ajoyib bo'linma. Ushbu hujjatni taqdim etgan GammaGamesga katta rahmat. Shuningdek, skriptning bir qismini nosozliklarni tuzatish uchun foydali bo'lishi mumkin. Masalan, birinchi ko'rsatilgan ob'ekt tayoqlarni skanerlash va topilgan tayoqlar ro'yxatini qaytarish vazifasini bajaradi. Bluetooth sozlamalari barcha tsilindrlarda ishlayotganini yaxshi tekshirish. Buni amalga oshirish uchun kodni quyidagi repoda topilgan test1_BLE_wand_detect.py dan nusxalashingiz mumkin:

github.com/maspieljr/SmartWand

4 -qadam: SmartThings CLI -ni o'rnating

Quyida github -da smartthings_cli repo -ga kiritilgan ko'rsatmalar nusxasi keltirilgan (https://github.com/rllynch/smartthings_cli).

Men bu erda hamma narsani ishga tushirish uchun sozlashim kerak bo'lgan kichik tuzatishlarni qo'shdim. Buni ta'minlaganingiz uchun rllynchga yana bir bor rahmat.

1) Mening SmartApps -ga kiring va uning ostida groovy/app.groovy -da kodli yangi SmartApp yarating.

*E'tibor bering, birinchi bosqichda SmartThings veb -saytiga havola mavjud. Smartthings hisobingiz qayerda joylashganiga diqqat bilan qarang. Bu meni bir muddat qoqib qo'ydi, chunki boshqa sayt menga kirishga ruxsat berdi, lekin hech narsa topa olmadi. SmartThings hisobimga kirish uchun quyidagi havolani ishlatishim kerak edi.

(Men buni saralashga sarflagan 2 soatga xush kelibsiz:) bu keyingi bosqichda ham muhim bo'ladi.)

2) Ilova sozlamalarini bosing va OAuth ostida Smart App -da OAuth -ni yoqishni bosing. OAuth mijoz identifikatori va OAuth mijoz siri haqida yozing. OAuth mijozlar displeyini SmartThings CLI boshqaruviga yangilang. Yangilash -ni bosing.

3) Mening SmartApps -ga qaytib, SmartThings CLI Control -ni bosing. Nashr qilish => Men uchun bosing.

4) smartthings_cli omborini klonlang, agar xohlasangiz virtual tarmoq yarating (men buni qilmaganman), so'ngra CLIENTID va CLIENTSECRETni 2 -qadamdagi identifikator va sir bilan almashtirib, quyidagi buyruqlarni bajaring.

Smartthings repo -ni klonlash uchun siz quyidagi buyruqdan foydalanishingiz mumkin. Linux buyruq satri tayoq modulini sozlash paytida yaratilgan loyiha katalogida ekanligiga ishonch hosil qiling.

git klon >

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…

Tavsiya: