Cov txheej txheem:

Txuas koj Raspberry Pi mus rau Lub Vev Xaib: 9 Kauj Ruam
Txuas koj Raspberry Pi mus rau Lub Vev Xaib: 9 Kauj Ruam

Video: Txuas koj Raspberry Pi mus rau Lub Vev Xaib: 9 Kauj Ruam

Video: Txuas koj Raspberry Pi mus rau Lub Vev Xaib: 9 Kauj Ruam
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Kaum ib hlis
Anonim
Txuas koj Raspberry Pi mus rau Lub Vev Xaib
Txuas koj Raspberry Pi mus rau Lub Vev Xaib

Hauv zaj lus qhia no, koj yuav kawm tsim lub vev xaib, sau cov ntaub ntawv siv thoob ntiaj teb lub vev xaib, thiab siv APIs los tshaj tawm cov duab rau Tumblr thiab Twitter.

Kauj ruam 1: Client thiab Server

Hauv kev suav, tus neeg siv-server yog tus qauv software suav nrog ob ntu: tus neeg siv khoom thiab tus neeg rau zaub mov. Ob leeg sib tham hauv computer network lossis ua ke ntawm tib lub computer. Tus neeg siv khoom yog ib qho program lossis khoos phis tawj nrog software uas tso siab rau kev thov cov kev pabcuam rau server. Tus neeg rau zaub mov yog lub khoos phis tawj lossis lub cuab yeej uas tuaj yeem lees txais cov lus thov no, ua haujlwm rau lawv, thiab xa cov ntaub ntawv thov mus rau tus neeg siv khoom. Qee lub sij hawm nws tsis yooj yim los txiav txim seb qhov twg yog thaum ua haujlwm nrog cov tshuab uas ua ntau txoj haujlwm ib zaug. Nov yog qee qhov yam ntxwv los pab koj qhia tus neeg siv khoom thiab tus neeg rau zaub mov sib nrug.

Cov yam ntxwv ntawm cov neeg siv khoom:

  • Nws yog thawj qhov ua haujlwm
  • Xa ntawv thov mus rau tus neeg rau zaub mov
  • Nws cia siab thiab tau txais cov lus teb los ntawm server
  • Feem ntau cuam tshuam ncaj qha nrog cov neeg siv kawg siv ib tus neeg siv cuam tshuam xws li cov neeg siv graphical interface

Cov yam ntxwv ntawm tus neeg rau zaub mov:

  • Nws yog thawj zaug passive
  • Nws tau mloog, npaj los teb rau cov lus thov xa los ntawm cov neeg siv khoom
  • thaum thov tuaj, teb nrog thov cov ntaub ntawv rau cov neeg siv khoom
  • Cov neeg siv kawg feem ntau tsis cuam tshuam ncaj qha nrog lub server, tab sis siv cov neeg siv khoom.
Duab
Duab

Ib qho piv txwv ntawm cov neeg siv khoom thiab kev sib raug zoo ntawm tus neeg siv khoom yog lub web browser (tus thov kev pabcuam) thov thiab tau txais cov nplooj ntawv web nplooj ntawv tuav ntawm lub vev xaib.

Kauj Ruam 2: Tsim Lub Vev Xaib Hauv Zos

Raspberry Pi tuaj yeem dhau los ua lub web server los ntawm kev teeb tsa thiab ua haujlwm daim ntawv thov server. Ob qhov nrov qhib qhov server thov yog: NGINX (qhia lub cav x) thiab Apache. Hauv zaj lus qhia no, koj yuav siv Apache vim tias, raws li kev sau ntawv no, nws muaj ntaub ntawv hnyav dua. Qhov no ua rau nrhiav cov lus teb online yooj yim dua li koj kawm.

Koj Raspberry Pi yuav tuav lub vev xaib uas ib lub cuab yeej ntawm tib lub network tuaj yeem nkag tau. Cia peb pib!

Kauj ruam 3: Nruab Apache

Nruab apache2 pob. Cov -y chij teb cov lus nug ua ntej teeb tsa kev nyab xeeb rau koj.

sudo apt -tau nruab apache2 -y

Apache los nrog cov ntawv xeem HTML uas tsim lub vev xaib qhov chaw los ntsuas qhov kev teeb tsa nrog. Qhov kev xeem ntawv HTML no los hauv Apache lub hauv paus npe. Los ntawm lub neej ntawd, Apache tau teeb tsa los saib hauv phau ntawv no rau cov ntaub ntawv hauv web los tsim cov ntsiab lus hauv lub vev xaib. Txhawm rau saib ntawm nplooj ntawv sim no thiab paub meej tias Apache tau ua tiav txhim kho kom raug rub nws hauv koj lub vev xaib los ntawm kev ntaus hauv URL qhov chaw nyob no:

zos/

Yog Apache teeb tsa kom raug, koj yuav pom Apache xeem lub vev xaib hauv qhov browser:

Duab
Duab

Nrhiav Koj Tus Raspberry Pi Tus IP Chaw Nyob

Koj tuaj yeem siv qhov chaw nyob hauv zos txhawm rau nkag mus rau nplooj ntawv web thaum nyob ntawm Raspberry Pi. Txhawm rau nkag mus rau nplooj ntawv web los ntawm lwm lub khoos phis tawj koj xav tau koj li Raspberry Pi tus IP chaw nyob. Txhawm rau nrhiav hom IP chaw nyob hauv LXTerminal:

ifconfig

Duab
Duab

Koj tseem tuaj yeem pom nws ntawm lub desktop yog tias koj tso tus cursor hla lub cim txais tos WiFi.

Duab
Duab

Kauj Ruam 4: Ua Lub Vev Xaib Zoo Nkauj

Koj Raspberry Pi tam sim no tuaj yeem tuav lub vev xaib thiab txhua cov ntsiab lus uas yuav tshaj tawm rau nws. Muaj ob yam yooj yim ntawm nplooj ntawv web: zoo li qub thiab muaj zog. Ib nplooj ntawv zoo li qub muaj cov ntsiab lus uas tsis hloov pauv. Ib nplooj ntawv muaj peev xwm tuaj yeem tso saib cov ntaub ntawv hloov pauv xws li kev nyeem ntawv sensor lossis hloov sijhawm thiab hnub tim.

Cia peb pib nrog nplooj ntawv zoo li qub. Txhawm rau tsim ib qho koj yuav tsum siv hom lus hu ua HTML. Thaum koj mus rau nplooj ntawv web thawj qhov uas koj pom tso tawm yog tej zaum yog nplooj ntawv index.html. Nplooj ntawv no yog nplooj ntawv nplooj ntawv browser pom yog tias tsis muaj lwm nplooj ntawv tshwj xeeb. Los ntawm lub neej ntawd, Apache saib rau index.html cov ntaub ntawv ntawm no:

/var/www/html

Qhov no hu ua daim ntawv hauv paus, thiab nws yog daim ntawv zais. Nws tau tsim los tuav cov nplooj ntawv web. Mus rau ntawd thiab saib ib puag ncig:

cd/var/www/html

ls ua

Koj yuav pom qhov ntsuas ntsuas ntsuas tsis raug.html cov npe teev tseg. Yog tias koj xav khaws cov ntawv ua tiav, hloov nws mus rau qee yam xws li defaultIndex.html siv cov lus txib mv.

sudo mv index.html defaultIndex.html

yog tias koj tsis xav khaws nws, tshem cov ntawv los ntawm kev siv rm hais kom ua:

sudo rm index.html

Tam sim no, koj tuaj yeem tsim thiab pib kho koj tus kheej cov ntaub ntawv index.html:

sudo nano index.html

Nco ntsoov siv sudo, www thiab html phau ntawv teev npe yog cov hauv paus yog li koj yuav tsum ua raws li lub hauv paus los tsim, kho, thiab tswj cov ntaub ntawv nyob hauv cov npe ntawd.

Basic HTML Page

HTML yog hom lus uas nkag mus tob. Koj tuaj yeem ua ntau yam nrog nws. Yog tias koj xav paub ntxiv tshawb xyuas W3Schools lub vev xaib uas koj tuaj yeem pom ntau yam qhia paub yuav siv HTML li cas los tsim lub vev xaib. Peb yuav pib nrog nplooj ntawv HTML yooj yim.

Ua ntej, qhia rau tus browser seb koj siv HTML version li cas. Cov ntawv no tau tshaj tawm ua HTML5 doc:

Pib nrog html thiab cim lub cev:

Feem ntau ntawm koj cov ntsiab lus mus tom ntej, nruab nrab ntawm lub cev cim npe. Ua kab thawj kab lus kab lus nrog h1 tag. Tus lej tom qab "h" piav qhia qhov tseem ceeb ntawm cov ntawv uas cuam tshuam rau qhov loj ntawm cov ntawv. Siv daim ntawv p los txhais kab lus:

Kuv Raspberry Pi muaj lub vev xaib

Kuv yuav tso dab tsi ntawm no?

Ua kom tiav nplooj ntawv los ntawm kev kaw lub cev thiab html thiab cim npe:

Txuag daim ntawv nrog cov ntawv txuas ntxiv.html thiab mus ntsib localhost hauv koj tus browser. Koj yuav pom koj lub vev xaib!

Duab
Duab

Koj yuav tsum tso dab tsi rau ntawm nplooj ntawv web? Wb tso cov duab, lossis zoo dua li, animateMe.gif! Txhawm rau ib qho khoom muaj nqis los tso tawm ntawm nplooj ntawv web no nws yuav tsum muab tso rau hauv Apache cov ntawv hauv paus. Koj yuav tsum luam thiab muab tshuaj txhuam nws kom nws nyob hauv koj phau ntawv teev npe boof/fotos ib yam. Txhawm rau luam thiab muab cov ntawv tso rau hauv kab hais kom ua siv cov lus txib cp. Ua ntej, cd rau koj phau ntawv qhia tsev:

cd ~ ib

Luam thiab muab cov ntawv animateMe.gif:

sudo cp boof/fotos/animateMe.gif/var/www/html

Rov qab mus rau phau ntawv teev npe html:

cd/var/www/html

Rov qhib cov ntawv index.html yog li koj tuaj yeem ntxiv cov duab:

sudo nano index.html

Txhawm rau txhais thiab kos duab ntawm nplooj ntawv HTML siv cov cim npe img. Muab cov kab hauv qab no ntawm kab lus thiab kab lus.

Qhib nplooj ntawv hauv qhov browser thiab nws yuav zoo li qhov no tshwj tsis yog nrog koj tus kheej stylish selfie GIF.

Duab
Duab

Kauj Ruam 5: Ntxiv Qee Cov Qauv

Nplooj ntawv tab tom saib me ntsis. Tsis muaj xim thiab tsis muaj style. Nov yog qhov CSS los rau hauv. Nws yog hom lus uas ua haujlwm sib koom tes nrog HTML txhawm rau ua lub vev xaib zoo nkauj dua thiab pom kev zoo. Koj yuav tsuas yog kov ntawm no tab sis yog tias koj xav kawm ntau lub taub hau hla W3schools kom kawm paub ntau ntxiv.

Ua piv txwv, cia peb hloov xim ntawm keeb kwm yav dhau los ntawm kev ntxiv CSS rau koj cov ntawv HTML. Muaj ntau txoj hauv kev los hloov koj nplooj ntawv web page siv CSS. Rau chav kawm no, koj yuav siv cov cim cim los txhawm rau CSS txoj cai rau hauv koj cov ntawv HTML.

Muab cov kab hauv qab no nyob nruab nrab ntawm thawj html thiab lub cev cim npe nyob rau sab saum toj ntawm koj nplooj ntawv HTML:

lub cev {keeb kwm yav dhau-xim: hmoov av;} cim npe. Nws yuav zoo li no:

Duab
Duab

lub cev {keeb kwm yav dhau-xim: hmoov av;}

Duab
Duab

Kauj ruam 6: Nruab PHP

Hloov chaw ntawm nplooj ntawv zoo li qub, koj tuaj yeem ua qhov hloov pauv uas muaj peev xwm hloov pauv yam tsis muaj koj tus kheej xa cov ntaub ntawv rau nws. Ib txoj hauv kev nyiam ua qhov no yog siv cov lus sau ua lus PHP. Txhawm rau siv PHP ntawm Raspberry Pi koj yuav tsum tau teeb tsa nws ua ntej nrog cov qauv pob rau Apache:

sudo apt-tau nruab libapache2-mod-php5 php5 -y

Kauj Ruam 7: Ua Dynamic Web Page

Ua ke PHP nrog HTMLAs ntev li PHP code muaj nyob hauv cov cim npe koj tuaj yeem suav nrog nws hauv cov qauv HTML. Ua piv txwv, muab koj cov ntawv HTML thiab PHP tam sim no thiab ua cov ntawv loj dua los ntawm kev siv HTML cim npe.

Cia suav nrog cov ntawv PHP yooj yim uas qhia hnub thiab sijhawm. Muab cov ntawv hauv qab no nyob qhov twg ntawm cov ntawv cim npe:

Txuag cov ntaub ntawv nrog Ctrl + o tab sis hloov qhov txuas ntxiv los ntawm.html rau.php, uas yuav khaws cov ntawv tshiab. Yog li kom tsis txhob cuam tshuam qhov browser, tshem qhov qub.html version:

sudo rm index.html

Hloov kho localhost hauv koj lub vev xaib. Cov zis yuav zoo li no:

Duab
Duab

Ok, yog li cas txawv? Nws zoo li nplooj ntawv HTML ib txwm tsis yog? Hloov kho nplooj ntawv thiab saib cov khawv koob. Kev ua khawv koob yog lub sijhawm yuav hloov! Ntawd yog PHP thiab nws cov hnub ua haujlwm () ua haujlwm rau koj los ua nplooj ntawv web dynamic.

Kauj Ruam 8: Tsim Tumblr API Client

Raspberry Pi tuaj yeem thov thiab tau txais cov ntaub ntawv los ntawm lwm cov software thov online los ntawm API (Application Programming Interface). Ib qho API ua rau nws yooj yim rau qee yam xws li Raspberry Pi txiav los ntawm txhua cov ntaub ntawv ntawm lub vev xaib kom rub tsuas yog cov khoom muaj txiaj ntsig. Kom koj Raspberry Pi tham nrog Tumblr, Twitter, thiab weather.com txhawm rau tweet, tshaj tawm cov duab, thiab tso saib huab cua huab cua.

Raspberry Pi thiab Tumblr

Cov kev tawm dag zog hauv qab no tsim kev sib tham ntawm koj li Raspberry Pi thiab Tumblr. Raws li tus neeg siv khoom, koj Raspberry Pi yuav nug Tumblr rau cov ntaub ntawv luv luv yog li nws tuaj yeem tso cov duab rau Tumblr tus neeg rau zaub mov nyob deb, ua rau cov duab raug tshaj tawm rau Tumblr tus account. Txhawm rau kom Raspberry Pi ua haujlwm nrog API muaj feem ntau yuav yog lub tsev qiv ntawv tawm rau koj siv. Rau Tumblr muaj Pytumblr. Tus neeg siv khoom tau tsim nyob rau hauv Python program siv qhov ua haujlwm tsim hauv Pytumblr. Txoj haujlwm no siv plaub txoj cai tso cai los ntawm Tumblr:

  • tus neeg siv tus yuam sij
  • neeg siv tsis pub leej twg paub
  • token qhov tseem ceeb
  • token tsi

Ua ntej koj tuaj yeem siv Tumblr's API koj yuav tsum tau txais plaub tus yuam sij no (zoo ib yam li tus lej nkag). Kom tau lawv ua raws cov theem hauv qab no:

  1. Tsim tus account Tumblr dawb thiab nkag mus.
  2. Sau npe thov. Koj tsuas yog yuav tsum tau muab cov ntaub ntawv yooj yim xws li lub npe (sim "Kuv Raspberry Pi"), kev piav qhia, email, thiab lub vev xaib (siv qhov no yog tias koj tsis muaj). Tom qab sau npe, koj yuav tau txais tus lej tseem ceeb thiab tus neeg siv khoom zais. Luam thiab muab lawv tso rau qhov chaw nyab xeeb, zoo li cov ntawv lossis email. Txhawm rau nkag mus rau lawv dua mus rau koj tus account Tumblr nplooj ntawv, xaiv Chaw nyob hauv Tus as khauj Account, thiab nyem rau ntawm Apps.
  3. Nkag mus rau tus tsim tawm console siv koj tus lej thiab tus lej tso cai tso cai. Nyem tso cai thaum nug yog tias koj xav kom nws tshaj tawm rau koj.
  4. Thaum nkag mus rau tus tsim tawm console, koj yuav pom cov piv txwv piv txwv hauv ntau yam lus sib txawv. Ob qho nyem rau ntawm Python tab thiab luam tawm OAuth block, lossis hauv cov ntawv qhia zaub mov saum toj kawg nkaus Nyem Qhia Cov Keys kom pom tus lej token thiab token cov lej zais zais nrog rau ob tus lej uas koj twb muaj lawm.

Cia peb muab cov lej no los ua haujlwm thiab ua rau Python program uas tshaj tawm cov animateMe-g.webp

Ua ntej nruab Pytumblr:

sudo apt-tau hloov tshiab

sudo pip nruab pytumblr

Los ntawm koj cov npe hauv tsev, cd rau hauv phau ntawv tais ceev tseg thiab tsim cov ntawv Python:

cd hwb

Tsim koj cov ntaub ntawv siv IDLE tus kho kom yooj yim los txiav thiab muab koj cov lej tso cai ntev heev. Muab qhov no tso rau hauv koj daim ntawv testPytumblr.py, hloov kho plaub tus yuam sij thiab koj lub npe siv:

ntshuam pytumblr

# Kev lees paub ntawm OAuth, theej los ntawm https://api.tumblr.com/console/calls/user/info client = pytumblr. TumblrRestClient ('your_consumer_key', 'your_consumer_secret', 'your_token', 'your_token_secret') client. 'koj_account_username', xeev = "tshaj tawm", cim npe = ["raspberrypi", "picamera"], cov ntaub ntawv = "fotos/animateMe.gif") luam tawm ("upload")

Qhov kev zov me nyuam cim cov duab tso rau koj nrog "raspberrypi" thiab "picamera". Yog tias koj xav tau, koj tuaj yeem tshem tawm, hloov pauv, lossis ntxiv rau cov cim npe no. Cov no tau khaws cia hauv qhov sib txawv hu ua cim npe siv hauv client.create_photo ().

Nias F5 kom ua haujlwm. Yuav muaj qhov yuam kev… tab sis koj twb tau teeb tsa Pytumblr yog li vim li cas Python hais tias nws tsis tuaj yeem pom tus qauv? Qhov no vim tias Pytumblr tsis txhawb Python 3, nws tsuas ua haujlwm hauv Python 2*. Qhib Python 2 IDLE editor thiab txiav thiab muab tshuaj txhuam koj cov lej, sau dua qhov program Python 3 thiab tom qab ntawd ua nws. Thaum nws tau rub tawm GIF, "rub tawm" yuav luam tawm hauv Python lub plhaub qhov rai.

Qhov no yog qhov txaus siab! Mus rau koj nplooj Tumblr thiab txheeb xyuas GIF! Sib pauv "luam tawm" hauv koj qhov haujlwm rau "daim phiaj" yog tias koj xav tsim cov ntawv sau ua haujlwm xwb.

Duab
Duab

*Tom qab chav kawm no tau tshaj tawm, lwm tus neeg siv github tau khawb qhov qub, ntxiv kev txhawb nqa rau Python 3 rau qee cov lus txib.

Yog tias qhov tshaj tawm tsis tshwm, rov xyuas ob zaug tias koj tau nkag mus rau koj plaub tus yuam sij thiab Tumblr lub npe siv, thiab tias koj li Pi kev sib txuas hauv is taws nem tau ua haujlwm. Koj kuj tseem tuaj yeem sau koj tsab ntawv los ntawm kab hais kom ua siv Python 2 (cd rau koj daim ntawv tais ceev tseg yog tias koj tsis nyob ntawd):

python testPytumblr.py

Kauj Ruam 9: Siv Screenshot ntawm Tumblr Thiab/lossis Lub Vev Xaib

Tsim lub vev xaib los tso saib-g.webp

Pom zoo: