Cov txheej txheem:

Taug qab Huab Cua Zoo Siv Grafana thiab Raspberry Pi: 7 Cov Kauj Ruam
Taug qab Huab Cua Zoo Siv Grafana thiab Raspberry Pi: 7 Cov Kauj Ruam

Video: Taug qab Huab Cua Zoo Siv Grafana thiab Raspberry Pi: 7 Cov Kauj Ruam

Video: Taug qab Huab Cua Zoo Siv Grafana thiab Raspberry Pi: 7 Cov Kauj Ruam
Video: Plab Plab Ntswg..(Pluav Pluav Ntswg) Maiv Twm New Song 2023-2024 2024, Hlis ntuj nqeg
Anonim
Taug qab Huab Cua Zoo Siv Grafana thiab Raspberry Pi
Taug qab Huab Cua Zoo Siv Grafana thiab Raspberry Pi

Kuv tab tom nrhiav rau qhov project me me IOT thiab tus phooj ywg pom zoo kuv txheeb xyuas qhov kev qhia no:

dzone.com/articles/raspberry-pi-iot-sensor…

Kuv pom zoo ua raws cov lus qhia kom ua raws hauv kev teeb tsa Raspberry Pi rau kev saib xyuas. Cov ntawv qhia no yuav ua tiav cov theem ntxiv hauv kev tsim cov cuab yeej IoT yooj yim uas tso cai rau kev ua yuam kev siab, nrog rau qhov muaj txiaj ntsig zoo li Raspberry Pi tuaj yeem yog thaum ua ke nrog Arduino.

Kuv kuj nkag mus rau qhov ua tau zoo thiab txwv me ntsis ntawm MQ* cov qauv ntawm huab cua ntsuas. MQ* sensors yog pheej yig thiab siv tau zoo, thiab yooj yim heev rau teeb tsa.

Zuag qhia tag nrho qhov no yuav pab koj kom pib ua haujlwm txuas nrog Arduino rau hauv internet hauv txoj hauv kev yooj yim tshaj plaws, thiab teeb tsa txoj hauv kev siv cov hneev taw hneev taw hnyav dua (rov: ESP8266).

Muaj kev lom zem!

Kauj Ruam 1: Khoom + Teeb

Khoom siv

  • Raspberry Pi nrog Raspbian ntsia
  • Raspberry Pi fais fab mov
  • Arduino Uno/Sib npaug
  • Txiv neej rau txiv neej USB hom B rau Hom A (yuav tsum tuaj nrog koj Arduino)
  • Ib qho ntawm MQ* sensors (Kuv siv MQ-2, 4, 5, thiab 135)
  • Assorted jumper xov hlau
  • mini khob cij

Teeb tsa

Cov ntawv qhia no yog txhais raws li kev qhia maj mam siv Arduino thiab Raspberry Pi - nws yuav pab kom paub siv linux davhlau ya nyob twg. Txawm li cas los xij, Kuv tsis xav tias muaj kev paub ntau nrog ua haujlwm ntawm Arduino lossis Raspberry Pi - txhua yam koj xav tau tiag tiag yog cov cuab yeej muab thiab tus cwj pwm xav paub.

  • Koj yuav tsum ua kom tiav cov theem hauv qhov kev qhia no.
  • Kuv pom zoo kom koj siv Ruaj Ntseg Plhaub (SSH) los cuam tshuam nrog Raspberry Pi, vim qhov no tso cai rau koj nkag mus rau cov lus txib yooj yim. Txuas ntawm SSH sib txawv txawm koj siv Windows, Linux lossis Mac. Linux thiab Mac yog qhov yooj yim siv los hais txog ssh (cov lus txib qhib SSH yog lus ssh). Txheeb xyuas Putty rau Windows. Kuv pom zoo kom koj txheeb xyuas lub vijtsam raws li txoj hauv kev los tswj koj qhov kev sib tham thaum lub sijhawm ua haujlwm.
  • Koj kuj tseem yuav tsum teeb tsa Python ntawm Raspbian. Thaum kuv ua tiav cov theem no, Kuv tau luam ib daim npav SD qub uas kuv tau dag ib ncig ntawm ib qhov haujlwm yav dhau los, uas twb tau nruab Python lawm. Yog tias koj qhov kev faib tawm ntawm NOOBS/Raspbian tsis muaj Python 3.7 lossis siab dua, txheeb xyuas cov theem no txhawm rau suav Python los ntawm qhov chaw.
  • Tau txais kev paub nrog git thiab teeb tsa nws thaum nws tseem tsis tau teeb tsa hauv koj kev faib khoom ntawm Raspbian.

Kauj Ruam 2: Teeb Lub Circuit Court

Kev teeb tsa Circuit
Kev teeb tsa Circuit

Muaj ib qho Circuit Court koj yuav xav teeb tsa hauv Arduino.

Kuv tau muab cov txheej txheem uas koj tuaj yeem siv ua qhov siv tau.

Kev zoo nkauj ntawm txhua qhov ntawm MQ-* cov pa roj carbon monoxide yog tias ib zaug 5 Volt thiab Av txuas tau ua, cov tswv yim tsis kam ntawm Arduino's analog pins tso cai rau lub sensor ua haujlwm kom raug.

Ua tib zoo xyuas kom ntseeg tau tias qhov sib txuas sib piv los ntawm pawg thawj coj tawg hauv lub sensor txuas nrog rau Arduino thiab tsis yog kev sib txuas digital. Yog tias koj tab tom ntsib qhov nqaim nqaim ntawm qhov txiaj ntsig thaum sim, kuv xav kom koj txheeb xyuas koj qhov kev sib txuas ntawm no ua ntej.

Kauj Ruam 3: Arduino Code thiab Flashing

Hauv cov theem hauv qab no ib qho, peb yuav txuas Arduino pawg thawj coj mus rau Raspberry Pi. Ua ntej peb ua qhov no, peb yuav tsum nyem lub Arduino nrog cov lej txhawm rau nyeem lub sensor thiab ntxiv rau xa cov ntaub ntawv sensor mus rau Raspberry Pi. Qhov no tuaj yeem ua tiav hauv txhua txoj kev uas koj ib txwm thawb chaws rau Arduino. Kuv siv cov cuab yeej thib peb sib nrug los ntawm Arduino IDE - yog li, kuv suav nrog Arduino lub tsev qiv ntawv nyob rau saum toj. Qhov no tsis tsim nyog rau lwm txoj haujlwm.

Txheeb cov cai txhawm rau luam/muab tshuaj txhuam ntawm qhov kawg ntawm ntu no.

Txoj cai ua dab tsi

Cov cai tau teeb tsa kom tau txais cov ntaub ntawv los ntawm plaub lub cim sib txawv - yog tias koj siv ntau hom kev ntsuas, nws yuav nkag siab zoo los hloov cov npe ntawm cov teeb liab tso tawm los ntawm Serial chaw nres nkoj.

Hauv lub voj peb txheeb xyuas yog Raspberry Pi thov cov ntaub ntawv los ntawm peb. Yog li ntawd, peb tab tom siv tus qauv yooj yim heev Tus tswv/qhev teeb tsa qhov twg Raspberry Pi yuav txuas ntxiv thov mus rau Arduino rau cov ntaub ntawv. Qhov no yooj yim dua li muaj lub txee hauv Arduino code vim tias nws yooj yim los ntsuas seb qhov txiaj ntsig ua haujlwm li cas los ntawm Raspberry Pi, es tsis txhob nyem qhov txiaj ntsig tshiab rau Arduino.

Arduino, ib zaug tau txais kev thov rau cov ntaub ntawv, yuav teeb tsa qhov tso tawm raws li GET qhov ntsuas - qhov no cuam tshuam nrog HTTP txoj hauv kev thiab tsuas yog kev xaiv tsim. Yog tias koj yuav tsim kev sib tham sib tham los ntawm Arduino ntawm Serial Port, koj tuaj yeem yooj yim mus rau lwm yam, tsuav yog koj tsim nws yog li cov ntaub ntawv raug cais sib cais. Kuv xaiv GET vim nws paub thiab muaj zog.

Kev sim yooj yim…

Thaum koj muaj Arduino flashed thiab chaws khiav, qhib Arduino IDE's Serial Monitor. Yog tias koj xa ib tus cim "H" (xyuas kom meej nws lub peev!) Koj yuav tau txais cov ntaub ntawv them nyiaj. Nrog koj zoo siab, nws ua haujlwm!

Tus qauv, asynchronous collector ntawm MQ-* cov ntaub ntawv

#suav nrog
int mq2 = A2;
mq4 = A3;
int mq5 = A4;
int mq135 = A5;
int khoomByte;
voidsetup () {
pinMode (mq2, INPUT);
pinMode (mq4, INPUT);
pinMode (mq5, INPUT);
pinMode (mq135, INPUT);
Serial.begin (9600);
}
/* valuePrint luam tawm tus nqi rau daim ntawv lo no.
* Tsim cov kev mob tshwm sim nkaus xwb.
*/
voidvaluePrint (String label, int nyeem ntawv) {
Serial.print (daim ntawv lo);
Serial.print ("=");
Serial.print (nyeem ntawv);
}
voidloop () {
// saib yog tias muaj cov ntaub ntawv xa xov tuaj:
yog (Serial.available ()> 0) {
// nyeem qhov qub tshaj plaws byte hauv ntu tsis:
// "Thaum koj hu Serial.read ib byte raug tshem tawm ntawm qhov tsis tau txais thiab xa rov qab mus rau koj tus lej"
incomingByte = Serial.read ();
// yog tias nws yog peev H (ASCII 72), nyeem qhov tseem ceeb thiab xa lawv mus rau tus tswv tsev raspberry.
// TODO: xyuas kom cov lus yeej ib txwm nyob ntev, txhua lub sijhawm
yog (incomingByte == 72) {
int mq2Reading = analogRead (mq2);
int mq4Reading = analogRead (mq4);
int mq5Reading = analogRead (mq5);
int mq135Reading = analogRead (mq135);
Serial.print ("?");
valuePrint ("mq2", mq2Reading);
Serial.print ("&");
valuePrint ("mq4", mq4Reading);
Serial.print ("&");
valuePrint ("mq5", mq5Reading);
Serial.print ("&");
valuePrint ("mq135", mq135Reading);
Serial.print ("\ n");
}
}
// nyeem cov ntawv tsuas yog txhua ob
ncua (1000);
}

saib rawmain.cpp tuav nrog ❤ los ntawm GitHub

Kauj Ruam 4: Raspberry Pi Code

Tam sim no koj tau teeb tsa Raspberry Pi raws li https://dzone.com/articles/raspberry-pi-iot-sensor … txuas rau Grafana.

  1. Nco ntsoov tias koj lub raspberry txuas nrog internet thiab tom qab ntawd ua qhov git clone hais kom luam tag nrho cov cai rau Raspberry Pi. Koj cov lus txib yuav zoo li me ntsis:

    git clone

  2. Tsis pub dhau raspberry Pi lub davhlau ya nyob twg, ua qhov hloov pauv cov lus txib (cd) rau hauv "raspberry_client":

    cd raspberry_client.

  3. Koj yuav tsum tau siv ib puag ncig virtual*. Yooj yim. Khiav

    python3 -m venv env. Qhov no yuav tsim ib puag ncig virtual hu ua "env" uas peb yuav siv los teeb tsa kev vam khom.

  4. Tam sim no, peb yuav tsum nkag mus rau peb ib puag ncig virtual. Khiav:

    qhov chaw env/bin/qhib. Tam sim no koj tau npaj rau nruab qhov kev vam khom ntawm txoj haujlwm.

  5. Hauv pob koj tsuas yog cloned muaj cov ntawv hu ua requirements.txt. Qhib cov ntawv no; koj yuav pom peb xav tau cov paho-mqtt thiab pyserial pob, nrog rau lawv cov ntawv sib txawv. Koj tuaj yeem pom cov ntsiab lus ntawm cov ntawv los ntawm kev khiav

    miv requirements.txt. Txhawm rau nruab cov pob khoom no, ru

    pip nruab -r requirements.txt.

  6. Qhov no suav txog qhov teeb tsa.

Cov lus qhia txhua qhov kev qhia uas siv nab hab sej hais txog Virtual env, thiab txawm tias rau qhov haujlwm me me no, kuv yuav hais. Ib puag ncig virtual tso cai rau koj cais cov qauv ntawm kev vam khom, ntxiv rau cais koj li nab nab ua haujlwm - Nws yog ib txoj hauv kev zoo los kho koj li Python chaw ua haujlwm. Yog tias qhov no yog koj thawj zaug siv qhov chaw ib puag ncig virtual, nyeem me ntsis txog lawv ntawm no.

Cov cai ua dab tsi …

Cov ntaub ntawv client.py yuav import cov txheej txheem yooj yim ntawm cov tsev qiv ntawv suav nrog peb tus kheej arduinosensor. Hauv lub luag haujlwm tseem ceeb, peb yuav tau txais qhov txiaj ntsig los ntawm Arduino, tshaj tawm cov ntaub ntawv rau MQTT broker, thiab tom qab ntawd pw tsaug zog 10 feeb.

Cov ntaub ntawv arduinosensor.py yog txheej txheem txheej txheem pab cuam uas nyob ib ncig ntawm lub tsev qiv ntawv paho.mqtt, nrog rau muab qee qhov kev sib tham muaj txiaj ntsig rau kev sib txuas lus nrog Arduino cov nyiaj them poob haujlwm (saib: parse_payload). Tau kawg, cov cai tau txuas nrog qhov kawg ntawm ntu no.

Ib tus neeg siv yooj yim uas sib txuas lus nrog cov khoom arduino ntawm Serial monitor. Cia siab tias yuav pom cov cai ntawm no thaum nws mus rau pej xeem:

fromimportlibimportimport_module
ntshuam
sij hawm tuaj
importarduinosensor
defmain ():
# qhib cov neeg siv khoom tshwj xeeb
start_time = sijhawm.time ()
thaum tseeb
nyeem ntawv = arduinosensor.get_values (os.environ.get ('PORT', "/dev/ttyUSB0"))
arduinosensor.pub ("python_client", payload = nyeem)
time.sleep (10.0- ((time.time () -start_time) %10.0))
yog _name _ == "_ main_":
lub ntsiab ()

saib rawclient.py tuav nrog ❤ los ntawm GitHub

Kauj Ruam 5: Muab Nws Txhua Qhov Ua Ke

Peb muaj Raspberry Python code teeb tsa, thiab peb muaj Arduino tus thov kev pabcuam teeb tsa. Cia peb txav mus txuas ob lub koom haum ua ke.

Ua ntej, cia peb txuas Arduino thiab teeb tsa lub kho kom raug:

  1. Ntawm koj lub Raspberry Pi lub davhlau ya nyob twg, khiav

    sej -m serial.tools.list_ports. Qhov no yuav teev txhua USB chaw nres nkoj uas txhawb nqa kev sib txuas lus.

  2. Tam sim no, ntsaws rau hauv koj Arduino thiab tos txog 2 vib nas this rau Raspberry kom paub nws. Ntaus hauv

    sej -m serial.tools.list_ports ib zaug ntxiv yuav qhia koj cov chaw nres nkoj dua. Koj tuaj yeem pom cov npe ntxiv tshwm - yog tias yog qhov tseeb, tom qab ntawv nkag tshiab no yog qhov nkag uas koj Arduino txuas nrog. Qhov no yuav zoo li yog "/dev/ttyUSB0".

  3. Sim khiav tus nab npawb nab npawb nyob hauv koj ib puag ncig virtual los ntawm kev khiav nab hab sej3.7 client.py. Tos ob peb feeb (tsawg kawg kaum) - yog tias koj ntsib qhov kev zam, qhov no txhais tau tias peb yuav tau hloov tus nqi rau peb qhov chaw nres nkoj com ntawm raspberry pi. Yog tias koj pom tias cov cai luam tawm kab pib nrog "Xa cov nyiaj them hauv qab no: …" Tom qab ntawd, koj yuav zoo mus rau theem kawg nrog Grafana. Tswv yim: nco ntsoov khiav

    screen -S nab ua ntej koj pib tus neeg siv nab hab sej, txwv tsis pub, thaum koj xaus koj qhov kev sib txuas rau koj raspberry pi, koj yuav plam koj txoj haujlwm ua haujlwm nab nab. Cov txheej txheem, koj tsis tas yuav siv "nab nab" nruj me ntsis raws li qhov ntsuas kawg, tab sis kuv nyiam muab kuv lub npe tshuaj ntsuam ua ntu zus.

    1. Txhawm rau hloov tus nqi rau qhov chaw nres nkoj COM, koj yuav tsum teeb tsa ib puag ncig sib txawv ua ntej yuav pib ua tus lej. Koj yuav tsum tau sim qhov no rau txhua qhov txiaj ntsig tau ntawm cov zis koj tau txais thaum khiav sej -m serial.tools.list_ports. Piv txwv li, yog tias tus lej nkag uas kuv tau txais yog ob, thiab yog cov hauv qab no:

      • /dev/ttyUSB6
      • /dev/acm0

ces cov lus txib kuv yuav ua yog:

PORT = "/dev/ttyUSB6" python3.7 client.py, thiab yog tias qhov ntawd tsis ua haujlwm, Kuv yuav tom qab ru

PORT = "/dev/acm0" python3.7 client.py

Thaum koj tau ua tiav cov theem no, cov cai yuav cog lus cov ntaub ntawv rau peb cov ntaub ntawv keeb kwm influxdb uas, thaum txuas nrog Grafana, yuav tso cai rau peb saib peb lub dashboard.

Kauj Ruam 6: Grafana Configuration thiab Dashboard Viewing

Grafana Configuration thiab Saib Dashboard
Grafana Configuration thiab Saib Dashboard

Zoo, peb tam sim no nyob rau hauv qhov kawg ncab! Tam sim no peb yuav siv Grafana los tsim lub dashboard yooj yim.

  1. Txuas rau koj qhov piv txwv Grafana. Txij li koj tau ua raws cov kauj ruam los ntawm kab lus dzone thawj, koj yuav tsum muaj peev xwm nkag mus nrog koj tus thawj tswj hwm siv. Mus tom ntej thiab nkag mus.
  2. Ntawm sab laug pane, hla tus cim "dashboards" - plaub lub xwmfab. Nyem rau ntawm "Tswj".
  3. Ntawm nplooj ntawv tshiab, nyem rau "New Dashboard". Ntxiv mus, nyem "Ntxiv vaj huam sib luag tshiab".
  4. Qhov no qhib Grafana editor. Peb yuav tsim qhov pom yooj yim uas qhia txog ib qho ntsuas.

    1. Hauv kab sab xis, hloov lub npe vaj huam sib luag rau qee yam muaj txiaj ntsig, xws li "Kev Nyeem Ntawv Hauv Chav Ua Noj". Koj kuj tseem tuaj yeem sau cov lus piav qhia ntxiv.
    2. Hauv qab sab laug, "Lus Nug", peb yuav ntxiv ib zaug ua ntu zus. Grafana tiag tiag ci ntawm no vim peb tuaj yeem yooj yim tsim SQL nqe lus nrog nyem raws li interface. Hauv qab "default", xaiv InfluxDB.
    3. Tam sim no, rau kev nyeem "A" - hauv FROM nqe lus, xaiv ntsuas "airtestt". Yog tias koj saib thawj tus nab npawb nab npawb hauv qhov get_values muaj nuj nqi ntawm arduinosensor.py, koj yuav pom tias peb txhais cov lus airtestt no hauv kab lus.
    4. Rau qhov piv txwv, cia peb mus rau "Xaiv" nqe lus thiab xaiv daim teb (mq4). Keeb kwm peb lub dashboard yuav muab rau peb xaiv "mean ()" - nyem rau ntawm qhov kev xaiv no thiab xaiv "Remove". tom qab ntawd, nyem rau ntawm qhov kos npe ntxiv thiab, hauv qab "Aggregations" xaiv "txawv ()". Qhov no yuav qhia cov sijhawm tshwj xeeb. Peb tuaj yeem xaiv lwm txoj kev ntsuas tab sis tam sim no, peb lub vaj huam sib luag yuav qhia pom qhov sib txawv ntawm mq4.
    5. Nyem Txuag nyob rau sab xis saum toj, thiab koj ua tiav!

Yog tias koj ntsib teeb meem, koj tuaj yeem txheeb xyuas koj cov teeb tsa nrog cov uas nyob hauv daim duab txuas nrog.

Kauj ruam 7: Qhib

Qhwv
Qhwv

Hauv qhov kev qhia no koj tuaj yeem teeb tsa MQTT lub zog muaj zog sib koom ua ke ntawm ib tus node thiab tus broker. Koj kuj tseem tuaj yeem pom koj cov ntaub ntawv IOT siv Grafana. Thaum kawg, koj muaj peev xwm los sau cov txheej txheem yooj yim no los ntawm (vam tias) kev nplij siab ntawm koj lub browser thiab PC ntawm kev siv SSH kev sib txuas.

Muaj qee yam uas peb yuav xav txhim kho.

  • Lub ntsuas nyeem ntawv hauv peb kab ntawv tsis yog qhov tseeb ntsuas qhov nyeem tau - lawv yog qhov hluav taws xob tso tawm los ntawm peb lub ntsuas cua. Lawv yuav tsum tau ntsuas, tham nrog cov ntawv blog no kom paub meej ntxiv.
  • Peb cov raspberry pi kev teeb tsa tuaj yeem ua rau hnyav dua los ntawm kev siv ESP8266 pawg thawj coj txuas nrog rau arduino, thiab tshem tawm pi tag. Txheeb tawm ib qho lus qhia rau ESP8266 tus qauv.
  • Peb yuav xav ntxiv kev ceeb toom rau cov xwm txheej tshwj xeeb. Ua tsaug, Grafana muab txoj hauv kev los ua qhov ntawd.

Kuv yuav tso qee qhov kev nyeem ntxiv txhawm rau ntxias koj lub tswv yim nrog lub ntiaj teb ntawm IOT. Kuv tos ntsoov yuav ntsib koj hauv cov lus qhia tom ntej!

Nyeem ntxiv:

Pom zoo: