Cov txheej txheem:

Ntse Tswj ntawm Raspberry Pi Ntxuam Siv Python & Thingspeak: 7 Kauj Ruam
Ntse Tswj ntawm Raspberry Pi Ntxuam Siv Python & Thingspeak: 7 Kauj Ruam

Video: Ntse Tswj ntawm Raspberry Pi Ntxuam Siv Python & Thingspeak: 7 Kauj Ruam

Video: Ntse Tswj ntawm Raspberry Pi Ntxuam Siv Python & Thingspeak: 7 Kauj Ruam
Video: tuav wb lub neej kom ruaj khov - Mang Vang ft. Christina Xyooj [Official Audio] 2021-22 2024, Kaum ib hlis
Anonim
Ntse Tswj ntawm Raspberry Pi Ntxuam Siv Python & Thingspeak
Ntse Tswj ntawm Raspberry Pi Ntxuam Siv Python & Thingspeak

Qhia me ntsis

Los ntawm lub neej ntawd, tus kiv cua tau txuas ncaj qha rau GPIO - qhov no cuam tshuam nws txoj haujlwm tsis tu ncua. Txawm hais tias tus txheeb ze ua haujlwm ntsiag to ntawm tus kiv cua, nws txoj haujlwm txuas ntxiv tsis yog siv tau zoo ntawm lub tshuab ua kom txias txias. Nyob rau tib lub sijhawm, kev ua haujlwm tas li ntawm tus kiv cua tuaj yeem ua rau muaj kev ntxhov siab xwb. Tsis tas li, yog tias Raspberry Pi raug kaw, tus kiv cua tseem yuav ua haujlwm yog tias lub zog txuas nrog.

Kab lus no yuav qhia yuav ua li cas, siv yooj yim thiab tsis cuam tshuam kev hloov pauv, ua kom lub tshuab txias uas twb muaj lawm dhau los ua qhov ntse, uas yuav tsuas yog qhib thaum tus txheej txheem xav tau tiag tiag. Lub kiv cua yuav tig rau tsuas yog thaum muaj kev siv hnyav, yog li txo cov kiv cua siv zog thiab suab nrov. Kuj txuas ntxiv lub neej kiv cua los ntawm kev ua kom nws tawm thaum tsis xav tau.

Yam Koj Yuav Kawm

Yuav ua li cas txhawm rau siv Python tsab ntawv los tswj tus kiv cua raws qhov ntsuas kub tam sim no ntawm Raspberry CPU siv On-Off tswj nrog kub hysteresis. Yuav ua li cas thauj cov ntaub ntawv los ntawm koj li RaspberryPi rau Yam Hais Lus Huab.

Khoom siv

Cov khoom koj yuav xav tau rau txoj haujlwm no yog raws li hauv qab no

  • Raspberry Pi 4 Computer Qauv B 4GB
  • NPN transistor S8050330ohms resistor
  • Armor Aluminium Hlau Case nrog Dual Kiv cua rau Raspberry Pi
  • Jumper cables
  • Cov qhob cij

Kauj Ruam 1: Tsim Circuit Court

Tsim lub Circuit
Tsim lub Circuit
Tsim lub Circuit
Tsim lub Circuit

Lub voj voog yog yooj yim zoo nkauj. Lub zog rau kiv cua raug txiav tawm siv NPN transistor. Hauv qhov kev teeb tsa no, lub transistor ua lub luag haujlwm qis. Resistor tsuas yog yuav tsum tau txwv tam sim no los ntawm GPIO. Raspberry Pi's GPIO muaj qhov tso zis ntau tshaj tam sim no ntawm 16mA. Kuv siv 330 ohms uas muab rau peb lub hauv paus tam sim no txog (5-0.7)/330 = 13mA. Kuv xaiv NPN transistor S8050, yog li hloov 400mA load los ntawm ob tus kiv cua tsis muaj teeb meem.

Kauj Ruam 2: Nkag CPU Kub Nrog ThingSpeak

Nkag CPU Kub Nrog ThingSpeak
Nkag CPU Kub Nrog ThingSpeak

ThingSpeak yog lub platform rau cov haujlwm raws li Internet ntawm Yam Lub Tswv Yim. Lub platform no tso cai rau koj los tsim cov ntawv thov raws cov ntaub ntawv sau los ntawm cov ntsuas. Cov yam ntxwv tseem ceeb ntawm ThingSpeak suav nrog: khaws cov ntaub ntawv ntawm lub sijhawm, ua cov ntaub ntawv thiab pom kev. ThingSpeak API tsis tsuas yog tso cai rau koj xa, khaws thiab nkag tau cov ntaub ntawv, tab sis kuj tseem muab ntau txoj hauv kev txheeb cais los ua lawv.

ThingSpeak tuaj yeem ua ke cov khoom siv nrov thiab cov kev pabcuam xws li:

  • Arduino
  • Raspberry pib
  • oBridge / RealTime.io
  • Fais fab imp
  • Mobile thiab Web applications
  • Kev sib raug zoo
  • Kev Txheeb Xyuas Cov Ntaub Ntawv hauv MATLAB

Ua ntej peb pib, koj xav tau tus as khauj ntawm ThingSpeak.

  1. Mus rau qhov txuas hauv qab no thiab kos npe rau ThingSpeak.
  2. Tom qab koj tus account qhib, nkag mus.
  3. Mus rau Channels -> Kuv Channels
  4. Nyem rau ntawm Tshiab Channel khawm.
  5. Nkag mus rau lub npe, piav qhia thiab thaj tsam ntawm cov ntaub ntawv koj xav tshaj tawm
  6. Nyem rau Txuag Channel khawm kom txuag tag nrho koj cov chaw.

Peb xav tau tus lej API, uas tom qab peb yuav ntxiv rau cov nab npawb nab npawb txhawm rau txhawm rau rub peb lub CPU kub mus rau Thingspeak huab.

Nyem rau ntawm API Keys tab kom tau txais Kev Sau API Key

Thaum koj muaj Tus Sau API Key, peb yuav luag npaj txhij xa peb cov ntaub ntawv.

Kauj Ruam 3: Tau Txais CPU Kub Los ntawm Raspberry Pi Siv Python

Tsab ntawv yog ua raws qhov ntsuas qhov ntsuas kub, uas tshwm sim txhua ob. Nws tuaj yeem tau txais los ntawm lub davhlau ya nyob twg los ntawm kev khiav vcgencmd hais kom ua nrog ntsuas_temp parameter.

vcgencmd ntsuas_temp

Subprocess.check_output () lub tsev qiv ntawv tau siv los ua qhov kev hais kom ua thiab tom qab ntawd siv qhov kev hais qhia ib txwm los rho tawm tus nqi tiag los ntawm txoj hlua rov qab.

los ntawm subprocess ntshuam check_output

los ntawm re import findalldef get_temp (): temp = check_output (["vcgencmd", "measure_temp"]). decode () temp = float (findall ('\ d+\. / d+', temp) [0]) return (temp) print (get_temp ())

Tom qab ntsuas qhov ntsuas kub, cov ntaub ntawv xav tau xa mus rau ThingSpeak huab. Siv koj Cov Ntawv Sau API los hloov pauv myApi hauv qab Python code hauv qab no.

los ntawm urllib ntshuam thov

los ntawm re import findall los ntawm lub sijhawm ntshuam pw los ntawm subprocess import check_output myAPI = '################' baseURL = 'https://api.thingspeak.com/update?api_key=% s ' % myAPIdef get_temp (): temp = check_output (["vcgencmd", "measure_temp"]). decode () temp = float (findall (' / d+\. / d+', temp) [0]) return (temp) sim: thaum Tseeb: temp = get_temp () conn = thov.urlopen (baseURL + '& field1 = % s' % (temp)) print (str (temp)) conn.close () pw (1) tshwj tsis yog KeyboardInterrupt: luam tawm ("Tawm tawm nias Ctrl+C")

Kauj Ruam 4: Tswj Tus Kheej Raws Li Kub

Python tsab ntawv qhia hauv qab no siv cov laj thawj uas tig rau lub kiv cua thaum qhov kub nce siab tshaj qhov ntsuas sov thiab tawm tsuas yog thaum qhov kub tau poob qis dua qhov pib. Txoj kev no, tus kiv cua yuav tsis qhib thiab tawm sai.

ntshuam RPi. GPIO ua GPIO

ntshuam sys los ntawm re import findall los ntawm lub sijhawm ntshuam pw ntawm subprocess ntshuam check_output def get_temp (): temp = check_output (["vcgencmd", "measure_temp"]). decode () temp = float (findall ('\ d+\. / d+ ', temp) [0]) rov (temp) sim: GPIO.setwarnings (Cuav) tempOn = 50 threshold = 10 controlPin = 14 pinState = False GPIO.setmode (GPIO. BCM) GPIO.setup (controlPin, GPIO. OUT, pib = 0) thaum Tseeb: temp = get_temp () yog temp> tempOn thiab tsis pinState lossis temp <tempOn - threshold thiab pinState: pinState = tsis pinState GPIO.output (controlPin, pinState) luam tawm (str (temp) + "" + str (pinState)) pw tsaug zog (1) tshwj tsis yog KeyboardInterrupt: luam tawm ("Tawm nias Ctrl+C") tshwj tsis yog: luam tawm ("Lwm Yam Kev zam") luam tawm ("--- Pib Cov Ntaub Ntawv Tsis Pub:") traceback.print_exc (txwv = 2, file = sys.stdout) print ("--- End Exception Data:") thaum kawg: print ("CleanUp") GPIO.cleanup () print ("End of program")

Kauj Ruam 5: Kawg Python Code

Tus nab npawb nab npawb loj tuaj yeem pom ntawm kuv tus GitHub tus lej hauv qhov txuas hauv qab no. Nco ntsoov muab koj tus kheej Sau API Key.

  1. Nkag mus rau hauv koj lub Raspberry PI board
  2. Khiav cov lus txib hauv qab no ntawm lub davhlau ya nyob twg

python3 cpu.py ua

Kauj Ruam 6: Saib Xyuas Cov Ntaub Ntawv Ntawm Thingspeak Cloud

Saib Xyuas Cov Ntaub Ntawv Ntawm Thingspeak Cloud
Saib Xyuas Cov Ntaub Ntawv Ntawm Thingspeak Cloud

Tom qab ib pliag, qhib koj cov channel ntawm ThingSpeak thiab koj yuav tsum pom qhov ntsuas kub nce mus rau Thingspeak huab hauv lub sijhawm tiag.

Kauj Ruam 7: Khiav Python Script ntawm Startup

Txhawm rau ua qhov no, thaum kawg ntawm /etc/rc.local cov ntaub ntawv:

sudo nano /etc/rc.local

Koj yuav tsum tso cov ntawv pib hais kom ua ntej ntawm kab tawm 0:

sudo nab hab sej /home/pi/cpu.py &

Lub xub ntiag ntawm & cim ntawm qhov kawg ntawm cov lus txib yog qhov yuav tsum tau ua, vim nws yog tus chij kom pib txheej txheem hauv keeb kwm yav dhau. Thaum tom qab rov pib dua, tsab ntawv yuav cia li khiav thiab tus kiv cua yuav qhib thaum lub sijhawm tshwj xeeb tau ntsib.

Pom zoo: