Cov txheej txheem:

Plot DHT11 Cov Ntaub Ntawv Siv Raspberry Pi thiab Arduino UNO: 7 Cov Kauj Ruam
Plot DHT11 Cov Ntaub Ntawv Siv Raspberry Pi thiab Arduino UNO: 7 Cov Kauj Ruam

Video: Plot DHT11 Cov Ntaub Ntawv Siv Raspberry Pi thiab Arduino UNO: 7 Cov Kauj Ruam

Video: Plot DHT11 Cov Ntaub Ntawv Siv Raspberry Pi thiab Arduino UNO: 7 Cov Kauj Ruam
Video: Plot graph from sensor DHT11 Arduino+Matlab 2024, Hlis ntuj nqeg
Anonim
Plot DHT11 Cov Ntaub Ntawv Siv Raspberry Pi thiab Arduino UNO
Plot DHT11 Cov Ntaub Ntawv Siv Raspberry Pi thiab Arduino UNO

Cov lus qhia no piav qhia yuav ua li cas kuv npaj lub ntsuas kub ntsuas DHT11 cov ntaub ntawv siv Arduino Uno thiab Raspberry Pi. Hauv qhov ntsuas kub no txuas nrog Arduino Uno thiab Arduino Uno txuas nrog ntu nrog Raspberry Pi. Ntawm Raspberry Pi Sab, matplotlib, zauv thiab kos duab tsev qiv ntawv tau siv los npaj cov duab.

Kauj Ruam 1: Yam Yuav Tsum Tau Ua rau Txoj Haujlwm

Yam Yuav Tsum Tau Ua rau Txoj Haujlwm
Yam Yuav Tsum Tau Ua rau Txoj Haujlwm
Yam Yuav Tsum Tau Ua rau Txoj Haujlwm
Yam Yuav Tsum Tau Ua rau Txoj Haujlwm
Yam Yuav Tsum Tau Ua rau Txoj Haujlwm
Yam Yuav Tsum Tau Ua rau Txoj Haujlwm

1. Raspberry Pi

2. Arduino Uno

3. DHT11 Kub Sensor

4. Jumper Wires

5. Cov qhob cij

Kauj Ruam 2: Rub tawm thiab Nruab Arduino IDE hauv Raspberry Pi

Rub tawm thiab Nruab Arduino IDE hauv Raspberry Pi
Rub tawm thiab Nruab Arduino IDE hauv Raspberry Pi
Rub tawm thiab Nruab Arduino IDE hauv Raspberry Pi
Rub tawm thiab Nruab Arduino IDE hauv Raspberry Pi
Rub tawm thiab Nruab Arduino IDE hauv Raspberry Pi
Rub tawm thiab Nruab Arduino IDE hauv Raspberry Pi

Nco tseg:- Koj tuaj yeem siv Arduino IDE ntawm lub qhov rais, Linux lossis Mac los rub cov duab kos hauv Arduino UNO.

Thawj kauj ruam yog nruab Arduino IDE rau qhov qhib browser hauv Raspberry Pi thiab qhib qhov txuas hauv qab no

Arduino IDE dhau los

Tom qab ntawd rub tawm Linux ARM version thiab rho tawm nws siv cov lus txib

tar -xf cov ntaub ntawv npe

Tom qab rho tawm koj yuav pom cov npe tshiab. Ntawm no kuv siv arduino-1.8.2 IDE. Tom qab ntawd mus rau phau ntawv teev npe los ntawm kev siv cov lus txib.

cd arduino-1.8.1

Txhawm rau khiav Arduino IDE, siv cov lus txib no hauv arduino-1.8.2 cov npe

./arduino

Yuav siv cov tsev qiv ntawv li cas

Txhawm rau nruab ib lub tsev qiv ntawv hauv Arduino, tsuas yog rub lub tsev qiv ntawv thiab muab tso rau hauv arduino 1.8.2 ==> cov tsev qiv ntawv nplaub tshev.

NCO TSEG:-Nco ntsoov tias tsis muaj (-) hauv cov ntawv tais ceev tseg rau ex (DHT-sensor). Yog tias muaj (-), hloov nws.

peb yuav siv ob lub tsev qiv ntawv hauv phau ntawv qhia no, DHT_Sensor thiab Adafruit_Sensor

Kauj Ruam 3: Txoj Cai rau Arduino

Code rau Arduino
Code rau Arduino

Tam sim no, cia tau txais nab hab sej thiab Arduino tham ua ke. Ua ntej tshaj plaws peb xav tau txoj haujlwm yooj yim kom tau txais Arduino xa cov ntaub ntawv hla qhov chaw nres nkoj. Cov haujlwm hauv qab no yog cov haujlwm yooj yim uas yuav suav nrog Arduino suav thiab xa cov ntaub ntawv mus rau qhov chaw nres nkoj.

Arduino Txoj Cai

#suav nrog "DHT.h" ntab tempC; // Hloov pauv lossis tuav qhov ntsuas hauv C ntab tempF; // Txawv sib txawv rau kev tuav lub sijhawm hauv F ntab cov av noo; // Txawv txawv rau tuav siab nyeem ntawv

#define DHTPIN 7 // dab tsi tus lej digital peb txuas nrog

#define DHTTYPE DHT11 // DHT 11

//#txhais DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

//#txhais DHTTYPE DHT21 // DHT 21 (AM2301)

// Pib DHT sensor.

DHT dht (DHTPIN, DHTTYPE);

teeb tsa tsis muaj dab tsi () {Serial.begin (115200); // tig rau saib saib

dht.begin (); // pib dht}

lub voj void () {tempC = dht.readTemperature (); // Nco ntsoov tshaj tawm koj cov kev hloov pauv

vaum = dht.readHumidity (); // Nyeem cov av noo

Serial.print (tempC);

Serial.print (",");

Serial.print (av noo);

Serial.print ("\ n"); // rau cov kab tshiab tshiab (2000); // ncua ntawm kev nyeem ntawv. }

Thaum cov duab kos tau muab tso ua ke, xaiv lub rooj tsavxwm thiab chaw nres nkoj thiab rub nws.

Kauj Ruam 4: Teeb Raspberry Pi

Teeb Raspberry Pi
Teeb Raspberry Pi

Thaum cov lej tau muab tso rau, Nruab qee lub tsev qiv ntawv kom peb tuaj yeem npaj cov phiaj xwm ntawm cov ntaub ntawv uas tab tom los ntawm Arduino Uno.

1. PySerial yog lub tsev qiv ntawv uas muab kev txhawb nqa rau kev sib txuas txuas nrog ntau yam khoom siv sib txawv. Txhawm rau nruab nws siv cov lus txib.

Sudo apt-tau nruab sej-serial

2. Numpy yog pob ntawv uas piav qhia txog ntau yam khoom siv sib txuam thiab cuam tshuam nrog kev ua lej nrawm uas ua haujlwm ntawm nws. Nws kuj tseem muab cov txheej txheem yooj yooj yim rau cov lej algebra thiab FFT (Fast Fourier Transform) thiab cov lej tsis sib xws. Koj tuaj yeem teeb tsa nws ntau txoj hauv kev xws li siv pob apt lossis pip. Ntawm no kuv tab tom txhim kho siv pip rau thawj zaug peb yuav tsum nruab pip

sudo apt-tau nruab python-pip sej-dev tsim-qhov tseem ceeb

sudo pip nruab numpy

lossis yog tias koj xav siv apt pob

sudo apt nruab python-numpy

3. Matplotlib yog lub tsev qiv ntawv 2D cov phiaj xwm uas muab lub hom phiaj-taw qhia API rau kev teeb tsa cov phiaj rau hauv cov ntawv thov siv cov hom phiaj GUI cov cuab yeej zoo li Tkinter, wxPython, Qt, lossis GTK+. Txhawm rau nruab nws siv cov lus txib

sudo pip nruab matplotlib

los yog

sudo apt nruab python-matplotlib

4. Drawnow feem ntau siv los saib cov txiaj ntsig tom qab txhua qhov rov ua dua raws li peb siv "imshow" hauv MATLAB. Txhawm rau nruab nws siv cov lus txib

sudo pip nruab drawnow

Kauj Ruam 5: Python Scipt

Nab hab sej Scipt
Nab hab sej Scipt
Nab hab sej Scipt
Nab hab sej Scipt

Cov kauj ruam tom ntej yog sau tsab ntawv nab nab rau qhov uas koj tuaj yeem siv ib tus kws sau ntawv los sau nws.

1. Npaj cov ntaub ntawv hauv ib daim duab

ntshuam serial # ntshuam Serial Library

import numpy # ntshuam numpy

ntshuam matplotlib.pyplot li plt #import matplotlib tsev qiv ntawv

los ntawm drawnow ntshuam *

tempC = #Empty arrayhumidity =

arduino = serial. Serial ("/dev/ttyACM0", 115200)

plt.ion () # hom sib tham sib tham los npaj cov ntaub ntawv nyob datacount = 0

def makeFig (): #Tsim cov haujlwm uas ua rau peb cov phiaj xwm xav tau

plt.ylim (20, 30) #Set y min thiab qhov siab tshaj plaws

plt.title ('Lub Sijhawm Tiag DHT11 Cov Ntaub Ntawv') #Npaj lub npe

plt.grid (Tseeb) #Tig daim phiaj rau

plt.ylabel ('Temp C') #Set ylabel

plt.plot (tempC, 'b^-', daim ntawv lo = 'Degree C') #npaj lub ntsuas kub

plt.legend (loc = 'sab xis sab xis') #npaj zaj dab neeg

plt2 = plt.twinx () #Tsim lub y thib ob

plt.ylim (50, 70) #Set txwv ntawm ob y axis

plt2.plot (av noo, 'g*-', daim ntawv lo = 'Vaum') #phiaj xwm cov ntaub ntawv siab

plt2.set_ylabel ('Vaum') #label thib ob y axis

plt2.ticklabel_format (useOffset = Cuav)

plt2.legend (loc = 'sab laug')

thaum Muaj Tseeb: # Thaum lub voj uas loops mus ib txhis

thaum (arduino.inWaiting () == 0): #Tos ntawm no kom txog thaum muaj cov ntaub ntawv

hla #tsis muaj dab tsi

arduinoString = arduino.readline ()

dataArray = arduinoString.split (',') #Hloov nws mus rau hauv ib qho array

temp = ntab (dataArray [0])

hum = ntab (dataArray [1])

tempC.append (kub)

av noo.append (hum)

drawnow (uaFig)

plt.pause (.000001)

suav = suav+1 yog (suav> 20): #tsuas yog siv 20 cov ntaub ntawv kawg yog cov ntaub ntawv ntau dua nws yuav tshwm ua ntej

tempC.pop (0)

av noo.pop (0)

2. Los npaj cov av thiab qhov kub sib txawv

ntshuam serial # ntshuam Serial Library

import numpy # ntshuam numpy

ntshuam matplotlib.pyplot li plt #import matplotlib tsev qiv ntawv

los ntawm drawnow ntshuam *

tempC = #Empty array

vaum =

arduino = serial. Serial ("/dev/ttyACM0", 115200) #Serial chaw nres nkoj uas arduino txuas nrog thiab Baudrate

plt.ion () #Qhia rau matplotlib koj xav tau hom kev sib tham los npaj cov ntaub ntawv nyob

def CreatePlot (): #Tsim cov haujlwm uas ua rau peb xav tau cov phiaj xwm

plt.subplot (2, 1, 1) #Qhov siab, Dav, Thawj phiaj xwm

plt.ylim (22, 34) #Set y min thiab cov txiaj ntsig siab tshaj

plt.title ('Lub Sijhawm Tiag DHT11 Cov Ntaub Ntawv') #Npaj lub npe

plt.grid (Tseeb) #Tig daim phiaj rau

plt.ylabel ('Temp C') #Set ylabels

plt.plot (tempC, 'b^-', daim ntawv lo = 'Degree C') #npaj lub ntsuas kub

plt.legend (loc = 'center sab saud') #npaj zaj dab neeg

plt.subplot (2, 1, 2) # Qhov siab, Dav, Qhov thib ob zaj lus

plt.grid (Tseeb)

plt.ylim (45, 70) #Set txwv ntawm y axis thib ob

plt.plot (av noo, 'g*-', daim ntawv lo = 'Vaum (g/m^3)') #cov ntaub ntawv cov av noo

plt.ylabel ('Vaum (g/m^3)') #label thib ob y axis

plt.ticklabel_format (useOffset = False) #kom nres autoscale y axis

plt.legend (loc = 'qhov chaw siab tshaj')

thaum Muaj Tseeb: # Thaum lub voj uas loops mus ib txhis

thaum (arduino.inWaiting () == 0): #Tos ntawm no kom txog thaum muaj cov ntaub ntawv dhau #tsis ua dab tsi

arduinoString = arduino.readline () #nyeem cov ntaub ntawv los ntawm chaw nres nkoj txuas

dataArray = arduinoString.split (',') #Hloov nws mus rau hauv ib qho array

temp = ntab (dataArray [0]) #Convert thawj ntu rau tus lej ntab thiab muab tso rau hauv qhov kub

hum = ntab (dataArray [1]) #Hloov lub ntsiab lus thib ob rau tus lej ntab thiab muab tso rau hauv hum

tempC.append (temp) #Txhua peb cov tempC array los ntawm kev txuas ntxiv nyeem ntawv

humidity.append (hum) #Building peb cov av noo los ntawm kev ntxiv hum nyeem ntawv

kos duab (CreatePlot)

plt.pause (.000001)

suav = suav+1

yog (suav> 20): #tsuas yog siv 20 cov ntaub ntawv kawg yog cov ntaub ntawv ntau dua nws yuav tshwm ua ntej

tempC.pop (0) # pop tawm thawj lub caij

av noo.pop (0)

Kauj Ruam 6: Daim duab Circuit Court

Daim duab Circuit Court
Daim duab Circuit Court
Daim duab Circuit Court
Daim duab Circuit Court
Daim duab Circuit Court
Daim duab Circuit Court

Arduino ==> DHT11

3.3V ==> VCC

GND ==> GND

D7 ==> OUT

Pom zoo: