Cov txheej txheem:

Saib Xyuas thiab Sau Kub Nrog Bluetooth LE thiab RaspberryPi: 9 Kauj Ruam (nrog Duab)
Saib Xyuas thiab Sau Kub Nrog Bluetooth LE thiab RaspberryPi: 9 Kauj Ruam (nrog Duab)

Video: Saib Xyuas thiab Sau Kub Nrog Bluetooth LE thiab RaspberryPi: 9 Kauj Ruam (nrog Duab)

Video: Saib Xyuas thiab Sau Kub Nrog Bluetooth LE thiab RaspberryPi: 9 Kauj Ruam (nrog Duab)
Video: Часы G-Shock до 250 долларов-15 лучших часов Casio G до 250 доллар... 2024, Hlis ntuj nqeg
Anonim
Saib xyuas thiab Sau Kub Nrog Bluetooth LE thiab RaspberryPi
Saib xyuas thiab Sau Kub Nrog Bluetooth LE thiab RaspberryPi
Saib xyuas thiab Sau Kub Nrog Bluetooth LE thiab RaspberryPi
Saib xyuas thiab Sau Kub Nrog Bluetooth LE thiab RaspberryPi

Cov lus qhia no yog hais txog yuav ua li cas ua ke ntau qhov ntsuas qhov ntsuas kub nrog Bluetooth LE sensor kab los ntawm Blue Radios (BLEHome) thiab RaspberryPi 3BT ua tsaug rau kev txhim kho ntawm Bluetooth LE tus qauv, tam sim no muaj peev xwm nkag tau yooj yim uas tsis muaj zog wireless sensors ntawm lub khw rau tus nqi qis heev thiab tuaj yeem ua haujlwm ntawm ib lub xov tooj ntawm tes rau lub hlis ib zaug. Ib qho ntawm cov ntsuas no kuv tau khaws yog los ntawm Blue Radio hu ua Sensor Bugs. Tus nqi kwv yees li $ 25 ntawm Amazon, nws yog Bluetooth LE ntaus ntawv nrog Lub ntsuas kub, lub teeb pom kev thiab lub ntsuas hluav taws xob txhua qhov tsim rau hauv chav me me uas tuaj yeem sib txuas lus tsis siv tau..

Kauj ruam 1: Teeb Raspberry Pi

Thawj kauj ruam yog kom tau txais kev ua haujlwm Raspberry Pi teeb tsa. Ua raws li kev qhia los ntawm Raspberry Pi lub vev xaib, ntsaws Raspbian ntawm SD daim npav, ntxig rau hauv Raspberry Pi thiab khau raj nws. thaj tsam sijhawm rau thaj tsam sijhawm tam sim no tsis yog UTC. Koj tuaj yeem ua qhov no txawm hais tias cov lus txib: $ sudo dpkg-reconfigure tzdataRest ntawm cov lus qhia xav tias teeb tsa ua tiav txawm hais kom ua kab interface.

Kauj Ruam 2: Teeb MySQL ntawm Raspberry Pi

Nws muaj txiaj ntsig kom muaj lub hauv paus teeb tsa hauv zos khaws tag nrho cov ntaub ntawv khaws tseg. Txhim kho MySQL ntawm Raspberry Pi yog yooj yim heev. Nws kuj tseem tsis nyuaj hloov tsab ntawv txhawm rau txuas rau SQL server sab nraud, koj tuaj yeem hla cov kauj ruam no yog tias koj xav siv SQL server ntawm lub network. Muaj ntau cov lus qhia hauv net, Kuv qhia qhov no: https:// www.stewright.me/2014/06/tutorial-install-…

Thaum SQL neeg rau zaub mov raug teeb tsa, koj tuaj yeem siv MySQL CLI cov neeg siv los tsim cov neeg siv, database thiab cov lus.

$ sudo mysql -uroot -pFirst, tsim tus neeg siv hauv zos txhawm rau ntxig cov ntaub ntawv khaws tseg:> CREATE USER 'datasrc'@'localhost' IDENTIFYED BY 'datasrc000'; Tom ntej no, tsim cov ntaub ntawv thiab cov lus:> CREATE DATABASE SensorBug; Teeb tus neeg siv kev tso cai:> YOOJ YIM YOOJ YIM RAU QHOV TSEEB NTAWM SensorBug.* TO 'datasrc'@'localhost'; Tam sim no ntxiv cov lus tshiab rau lub database. Txog qhov piv txwv no, Kuv yuav ntxiv ib lub rooj nrog cov kab hauv qab no: DATE, TIME, ADDRESS, LOCATION, TEMPERATURE and ACCEROMETER

  • DATE/TIME - Nov yog hnub thiab sijhawm cov ntaub ntawv raug kaw
  • ADDRESS - Qhov no yog MAC ntawm SensorBug cov lus raug ntes los ntawm
  • LOCATION - Tib neeg nyeem tau txoj hlua los qhia qhov chaw uas lub sensor nyob
  • TEMPERATURE - Qhov no yog qhov ntsuas kub
  • ACCELE - Qhov no yog tus nqi ntawm qhov ntsuas qhov ntsuas qhov ntsuas, muaj txiaj ntsig zoo rau kaw qhov ntsuas qhov ntsuas (yog tias ua tau)

Cov lus txib uas ua qhov no yog:> Siv SensorBug; > CREATE TABLE cov ntaub ntawv (hnub DATE, sijhawm TIME, chaw nyob TINYTEXT, qhov chaw TINYTEXT, Kub FLOAT, nrawm INT); Tam sim no cov ntaub ntawv tau npaj tiav, peb tuaj yeem txav mus rau teeb tsa lub sensorBugs.

Kauj Ruam 3: Teeb tsa SensorBugs

Cov kab mob sensor yog cov cuab yeej me me zoo nkauj. Hmoov tsis zoo, cov chaw tsim khoom tsuas yog muab IOS app rau kev tsim nws. Tsis txhob tsawg dua, nws tseem tuaj yeem ua haujlwm nrog nws yog tias koj tsuas yog muaj lub cuab yeej Android. Ua ntej tshaj plaws, txuas lub cuab yeej nrog lub xov tooj. Yog tsis muaj kev sib txuas ntawm lub cuab yeej, SensorBug yuav tsis tshaj tawm cov ntaub ntawv. Kuv tau sim saib yog tias kuv tuaj yeem ua qhov no ncaj qha nrog RaspberryPi, hmoov tsis zoo, nws zoo li Bluetooth LE tus tsav tsheb ntawm RaspberryPi tseem tab tom sim thiab muaj kab los tiv thaiv nws los ntawm kev sib txuas nrog Bluetooth LE cov cuab yeej. Yav tom ntej ntawm tus tsav tsheb blueZ tuaj yeem kho qhov no, tab sis raws li kev sau tam sim no, tsis muaj txoj hauv kev los ua ke SensorBug nrog RaspberryPi. Tsuas yog qhov peb xav tau yog lub xov tooj los teeb tsa SensorBug. Los ntawm lub neej ntawd, SensorBug yuav pib tshaj tawm cov ntaub ntawv ntsuas kub ntawm 1s ntu ib zaug thaum nws ua ke nrog lub cuab yeej. Txhawm rau ntes cov ntaub ntawv ntsuas kub, qhov ntawd yog txhua yam uas xav tau. Yog tias koj npaj yuav nthuav dav los siv txoj haujlwm lossis lub teeb pom kev zoo, dua li kev teeb tsa lub cuab yeej yuav xav tau. Txhawm rau pib, peb yuav txuas lub cuab yeej thiab txiav tawm. Qhov no yuav zoo txaus rau lub hom phiaj ntsuas kub. Pib los ntawm nias ob lub khawm ntawm SensorBug. Lub xiav/ntsuab LED yuav ntsais muag, uas qhia tias nws tau qhib. Nias ib qho ntawm lub pob, LED ntsuab yuav tsum teeb, qhia tias lub zog qhib. Yog tias lub teeb ntsuab ntsuab tsis tso tawm, nyem ob lub pob kom rov qhib lub cuab yeej dua. Nias thiab tuav ib lub pob kom txog thaum lub xiav LED pib ntsais muag. Qhov no yuav tso cov cuab yeej ua khub ua ke. Mus rau hauv Bluetooth teeb tsa cov ntawv qhia hauv xov tooj thiab saib rau lub cuab yeej SensorBug. Thaum nws tshwm sim, xaiv nws los ua ke nrog lub cuab yeej. Tus ntawd yog nws, tam sim no SensorBug tau siv thiab tshaj tawm cov ntaub ntawv ntsuas kub.

Kauj Ruam 4: Txhim Kho Bluetooth LE Python Wrapper

Tom ntej no peb yuav tsum teeb tsa lub tsev qiv ntawv rau nab nab tham nrog Bluetooth LE pawg. Cov lus qhia tuaj yeem pom ntawm no:

$ sudo apt-tau nruab python-pip libglib2.0-dev $ sudo pip nruab bluepy

Kauj ruam 5: Luam theej duab thiab Tshawb Nrhiav Qhov Chaw Nyob ntawm SensorBug

Txhawm rau kom paub qhov SensorBug MAC chaw nyob, siv cov lus txib no: $ sudo hcitool lescan Koj yuav tsum pom cov zis zoo li:

EC: FE: 7E: 10: B1: 92 (tsis paub) Yog tias koj muaj ntau lub bluetooth LE cov cuab yeej nyob ib puag ncig, nws yuav nyuaj rau nrhiav seb koj tab tom tham nrog tus twg. Koj tuaj yeem sim bluetoothctl uas muab cov ncauj lus kom ntxaws:

$ sudo bluetoothctl [bluetooth]# scan on [NEW] Ntaus EC: FE: 7E: 10: B1: 92 SensorBug10B192 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Data: 0x0085 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Cov Ntaub Ntawv: 0x02 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Data Tus Nqi: 0x00 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Dua Tus Nqi: 0x3c [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Cov Chaw Tsim Khoom Cov Ntaub Ntawv: 0x25 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Data Tus nqi: 0x09 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Data Tus Nqi: 0x41 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Data Tus Nqi: 0x02 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Dua Tus Nqi: 0x02 [CHG] Ntaus Ntawv EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Data Tus nqi: 0x43 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Data Tus nqi: 0x0b [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Chaw Tsim Khoom Tus nqi: 0x01 [CHG] Ntaus EC: FE: 7E: 10: B1: 92 Cov Chaw Tsim Khoom Tus Nqi: 0x6f

Sau MAC chaw nyob, qhov no yuav tsum tau nkag mus rau hauv tsab ntawv nab nab los lim tawm qhov tsis xav tau Bluetooth LE cov cuab yeej

Kauj Ruam 6: Ntxiv Python Script

Ib daim ntawv ntawm Python tsab ntawv muaj los ntawm:

drive.google.com/open?id=10vOeEAbS7mi_eXn_…

Nov yog tib cov ntaub ntawv, saib xyuas qhov indent thaum luam:

Tsis tas li, hloov kho MAC chaw nyob hauv cov ntaub ntawv nab hab sej kom pom qhov chaw nyob sensor tau los ntawm kev tshuaj xyuas.

# Qhov program no yog software pub dawb: koj tuaj yeem faib nws thiab/lossis hloov kho

# nws nyob rau hauv cov nqe lus ntawm GNU Daim Ntawv Tso Cai Pej Xeem General raws li luam tawm los ntawm

# Lub Free Software Foundation, ib qho version 3 ntawm Daim Ntawv Tso Cai, lossis

# (ntawm koj qhov kev xaiv) ib qho ntxiv tom qab.

#

# Txoj haujlwm no tau faib rau hauv kev cia siab tias nws yuav muaj txiaj ntsig, # tab sis tsis muaj ib qho kev lees paub; yam tsis muaj kev lees paub ntawm

# MERCHANTABILITY lossis haum rau lub hom phiaj tshwj xeeb. Saib cov

# GNU Daim Ntawv Tso Cai Saib Xyuas Kev Ncaj Ncees rau pej xeem kom paub meej ntxiv.

#

# Koj yuav tsum tau txais daim ntawv theej ntawm GNU Daim Ntawv Tso Cai Pej Xeem Loj

# nrog rau txoj haujlwm no. Yog tsis yog, saib.

# bscan.py - Yooj yim bluetooth LE scanner thiab cov ntaub ntawv rho tawm

los ntawm bluepy.btle ntshuam Scanner, DefaultDelegate

lub sij hawm ntshuam

ntshuam pymysql

ntshuam struct

hostname = 'zos'

username = 'cov ntaub ntawv rc'

lo lus zais = 'datasrc000'

database = 'SensorBug'

#Nkag mus rau MAC chaw nyob ntawm lub sensor los ntawm lescan

SENSOR_ADDRESS = ["ec: fe: 7e: 10: b9: 92", "ec: fe: 7e: 10: b9: 93"]

SENSOR_LOCATION = ["Chaw rau tsheb", "Sab Nraud"]

chav kawm DecodeErrorException (Kev zam):

def _init _ (tus kheej, tus nqi):

self.value = tus nqi

def _str _ (tus kheej):

rov repr (tus kheej.value)

chav kawm ScanDelegate (DefaultDelegate):

def _init _ (tus kheej):

DefaultDelegate._ init _ (tus kheej)

def handleDiscovery (tus kheej, dev, isNewDev, isNewData):

yog tias yog NewDev:

luam "Discovery device", dev.addr

elif yog NewData:

luam "Tau txais cov ntaub ntawv tshiab los ntawm", dev.addr

def doQueryInsert (conn, addr, loc, temp, accero):

#blesensor cov lus yog hnub tim, sijhawm, ntxiv, qhov chaw, qhov kub thiab txias

cur = conn.cursor () cov

dostr = 'INSERT INTO cov ntaub ntawv VALUES (CURRENT_DATE (), TAM SIM NO (), %s, %s, %s, %s);

cur.execute (dostr, (addr, loc, temp, accero))

conn.com tso ()

scanner = Scanner (). withDelegate (ScanDelegate ())

myConnection = pymysql.connect (host = hostname, user = username, passwd = password, db = database)

ManuDataHex =

ReadLoop = Muaj tseeb

sim:

thaum (ReadLoop):

khoom = scanner.scan (2.0)

Cov ntaub ntawv = ""

rau dev hauv cov khoom siv:

nkag = 0

AcceroData = 0

AcceroType = 0

Cov ntaub ntawv TempData = 0

rau saddr hauv SENSOR_ADDRESS:

nkag += 1

yog (dev.addr == saddr):

print "Ntaus ntawv %s (%s), RSSI = %d dB" %(dev.addr, dev.addrType, dev.rssi)

CurrentDevAddr = saddr

CurrentDevLoc = SENSOR_LOCATION [nkag-1]

rau (adtype, desc, value) hauv dev.getScanData ():

print " %s = %s" %(desc, tus nqi)

yog (desc == "Chaw Tsim Tshuaj"):

ManuData = tus nqi

yog (ManuData == ""):

luam tawm "Tsis tau txais cov ntaub ntawv, xaus kev txiav txim siab"

txuas ntxiv

#Luam ManuData

rau kuv, j hauv zip (ManuData [:: 2], ManuData [1:: 2]):

ManuDataHex.append (int (i+j, 16))

#Pib txiav txim siab cov ntaub ntawv Chaw Tsim Khoom raw

yog ((ManuDataHex [0] == 0x85) thiab (ManuDataHex [1] == 0x00)):

luam "Header byte 0x0085 pom"

lwm tus:

luam "Header byte 0x0085 tsis pom, txiav txim siab nres"

txuas ntxiv

#Skip Loj/Me

#Index 5 yog 0x3c, qhia txog qib roj teeb thiab teeb tsa #

yog (ManuDataHex [4] == 0x3c):

BatteryLevel = ManuDataHex [5]

ConfigCounter = ManuDataHex [6]

idx = 7

#print "TotalLen:" + str (len (ManuDataHex))

thaum (idx <len (ManuDataHex)):

#print "Idx:" + str (idx)

#print "Cov ntaub ntawv:" + hex (ManuDataHex [idx])

yog (ManuDataHex [idx] == 0x41):

#Cov ntaub ntawv ncaws pob

idx += 1

AcceleroType = ManuDataHex [idx]

AcceleroData = ManuDataHex [idx+1]

idx += 2

elif (ManuDataHex [idx] == 0x43): cov

#Cov ntaub ntawv kub

idx += 1

TempData = ManuDataHex [idx]

TempData += ManuDataHex [idx +1] * 0x100

TempData = TempData * 0.0625

idx += 2

lwm tus:

idx += 1

luam "Cov Chaw Nyob:" + CurrentDevAddr

luam tawm "Ntaus Chaw:" + CurrentDevLoc

luam tawm "Roj Teeb Qib:" + str (BatteryLevel) + "%"

luam "Config Counter:" + str (ConfigCounter)

luam tawm "Accelero Data:" + hex (AcceleroType) + "" + hex (AcceleroData)

luam "Temp Data:" + str (TempData)

doQueryInsert (myConnection, CurrentDevAddr, CurrentDevLoc, TempData, AcceleroData)

ReadLoop = Tsis tseeb

tshwj tsis yog DecodeErrorException:

dhau

Kauj Ruam 7: Kuaj Cov Python Script

Tsab ntawv yuav tsum tau khiav hauv paus, yog li:

$ sudo nab hab sej bscan.pyDiscovered device ec: 6e: 7e: 10: b1: 92 Ntaus ec: 6e: 7e: 10: b1: 92 (pej xeem), RSSI = -80 dB Flags = 06 Tsis tiav 16b Cov Kev Pabcuam = 0a18 Chaw Tsim Khoom = 850002003c25094102024309016f Header byte 0x0085 pom Cov Chaw Nyob: ec: 6e: 7e: 10: b1: 92 Ntaus Qhov Chaw: Cov Roj Roj Roj Qib: 37% Config Counter: 9 Accero Data: 0x2 0x2 Temp Data: 16.5625

Kauj Ruam 8: Ntxiv Python Scrip rau Crontab

Python tsab ntawv yuav tsum tau khiav hauv paus, yog li yog tias koj xav khaws cov ntaub ntawv cia, nws yuav xav tau ntxiv rau hauv paus hauv paus crontab. Txog qhov piv txwv no, Kuv khiav tsab ntawv txhua 20 feeb Siv qhov hais kom ua:

$ sudo crontab -e

# Kho cov ntawv no los qhia ua haujlwm kom ua los ntawm cron.

# # Txhua txoj haujlwm kom khiav yuav tsum tau txhais los ntawm ib kab nkaus xwb # qhia nrog cov haujlwm sib txawv thaum txoj haujlwm yuav khiav # thiab cov lus txib dab tsi los ua haujlwm # # Txhawm rau txheeb xyuas lub sijhawm koj tuaj yeem muab cov txiaj ntsig tseem ceeb rau # feeb (m), teev (h), hnub ntawm lub hlis (dom), lub hlis (mon), # thiab hnub ntawm lub lim tiam (dow) lossis siv '*' hauv cov haujlwm no (rau 'ib qho'). # # ceeb toom tias cov haujlwm yuav pib raws li cron cov txheej txheem # daemon kev xav ntawm lub sijhawm thiab sijhawm sijhawm. # # Cov txiaj ntsig ntawm kev ua haujlwm crontab (suav nrog qhov tsis raug) raug xa los ntawm # email rau tus neeg siv cov crontab cov ntawv yog (tshwj tsis yog xa rov qab). # # Piv txwv li, koj tuaj yeem ua haujlwm thaub qab ntawm tag nrho koj cov neeg siv nyiaj # thaum 5 teev sawv ntxov txhua lub lim tiam nrog: # 0 5 * * 1 tar -zcf /var/backups/home.tgz/home/ # # Xav paub ntau ntxiv saib phau ntawv nplooj ntawv ntawm crontab (5) thiab cron (8) # # mh dom mon dow hais kom ua 0 * * * * python /home/pi/bscan.py 20 * * * * python /home/pi/bscan.py 40 * * * * nab hab sej /home/pi/bscan.py

Qhov ntawd yog nws. Python tsab ntawv yuav ua haujlwm tsis tu ncua thiab rov tsim tawm cov ntaub ntawv rau hauv SQL database

Kauj Ruam 9: Ntxiv: Txhim Kho SensorBug rau Txoj Haujlwm Pom Kev Pom Zoo

Ntxiv: Kho lub SensorBug rau Txoj Haujlwm Pom Kev Pom Zoo
Ntxiv: Kho lub SensorBug rau Txoj Haujlwm Pom Kev Pom Zoo
Ntxiv: Kho lub SensorBug rau Txoj Haujlwm Pom Kev Pom Zoo
Ntxiv: Kho lub SensorBug rau Txoj Haujlwm Pom Kev Pom Zoo

Nws muaj peev xwm teeb tsa lub SensorBug ntawm Android rau txoj haujlwm pom kev tso tawm Rau txoj haujlwm hloov pauv kev nkag siab, yog li hu rau lub qhov rooj tsheb.sensing, SensorBug yuav txheeb xyuas yog tias lub cuab yeej sawv ntsug ncaj lossis tso ncaj. Thaum lub cuab yeej tiaj tus, tus nqi kaw yog 0x20 thaum lub cuab yeej sawv ntsug ncaj, tus nqi 0x02 Nws tsis txawv yog tias X lossis Y txoj haujlwm nce, tsuav Z axis tsis nce lossis nqis. Txoj hauv kev yooj yim tshaj plaws yog siv LightBlue App. SensorBug yuav tsum tshwm nyob rau hauv lub tshuab raj scan. Select lub cuab yeej koj xav teeb tsa, mus rau GATT yam ntxwv rau Accelerometer teeb tsa UUID: 9DC84838-7619-4F09-A1CE-DDCF63225B11

Saib duab: Sau kab ntawv teeb tsa tshiab:

010d3f02020000002d00000002 Nyeem rov qab cov teeb tsa teeb tsa kom paub meej tias sau ntawv Qhov no ua rau lub ntsuas kev ntsuas rau txoj haujlwm paub.

Pom zoo: