Cov txheej txheem:

Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266: 8 Cov Kauj Ruam (nrog Duab)
Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266: 8 Cov Kauj Ruam (nrog Duab)

Video: Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266: 8 Cov Kauj Ruam (nrog Duab)

Video: Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266: 8 Cov Kauj Ruam (nrog Duab)
Video: Tsis Paub Yuav Ua Li Cas 2024, Hlis ntuj nqeg
Anonim
Yuav Siv MQTT Nrog Raspberry Pi thiab ESP8266 li cas
Yuav Siv MQTT Nrog Raspberry Pi thiab ESP8266 li cas

Hauv Cov Lus Qhia no, Kuv yuav piav qhia txog MQTT raws tu qauv yog li cas thiab nws siv los sib txuas lus li cas ntawm cov cuab yeej. Then, raws li kev ua piv txwv, kuv yuav qhia koj yuav teeb tsa li cas yooj yim ob tus neeg siv khoom, qhov twg ESP8266 module yuav xa lus mus rau Python program thaum lub pob raug thawb. Tshwj xeeb, Kuv tab tom siv Adafruit HUZZAH tus qauv rau txoj haujlwm no, Raspberry Pi thiab lub khoos phis tawj desktop. Raspberry Pi yuav ua tus MQTT broker, thiab Python tus thov yuav raug khiav los ntawm lub khoos phis tawj sib cais (xaiv tau, vim qhov no tuaj yeem ua haujlwm ntawm Raspberry Pi).

Txhawm rau ua raws nrog Cov Lus Qhia no, koj yuav tsum muaj qee qhov kev paub yooj yim ntawm hluav taws xob, thiab yuav siv Arduino software li cas. Koj kuj yuav tsum paub nrog siv kab hais kom ua interface (rau Raspberry Pi). Vam tias, thaum koj tau txais kev paub ntawm MQTT yog dab tsi, thiab yuav siv nws li cas hauv qhov xwm txheej yooj yim, koj yuav tuaj yeem tsim koj li IoT cov phiaj xwm!

Yuav Tsum Muaj

  • 1 x Raspberry Pi, txuas nrog lub network hauv zos (khiav Jessie)
  • 1 x ESP8266 Module (Adafruit HUZZAH)
  • 1 x Breadboard
  • 3 x Jumper Wires (Txiv neej-rau-Txiv neej)
  • 1 x Pushbutton
  • 1 x 10k Ohm Resistor (Xim av-Dub-Txiv kab ntxwv xim xim)

Kuv tau tsim cov Lus Qhia no, vim MQTT ib txwm xav paub kuv raws li cov txheej txheem thiab muaj ntau txoj hauv kev sib txawv uas nws tuaj yeem siv. Txawm li cas los xij, Kuv tsis tuaj yeem zoo li tau txais kuv lub taub hau ib puag ncig yuav ua li cas rau cov cuab yeej siv nws. Qhov no yog vim kuv tsis paub/nkag siab dab tsi tau tshwm sim los coj kuv "Nyob Zoo, Ntiaj Teb!" los ntawm cov cuab yeej A thiab xa mus rau ntaus ntawv B.

Kauj Ruam 1: MQTT yog dab tsi?

MQTT yog dab tsi?
MQTT yog dab tsi?

MQTT, lossis MQ Telemetry Transport, yog cov txheej txheem xa xov uas tso cai rau ntau lub cuab yeej los sib tham. Tam sim no, nws yog cov txheej txheem nrov rau Internet ntawm Yam, txawm hais tias nws tau siv rau lwm lub hom phiaj - piv txwv li, Facebook Messenger. Qhov txaus siab MQTT tau tsim hauv xyoo 1999 - lub ntsiab lus nws laus li kuv!

MQTT yog nyob ib puag ncig lub tswv yim uas cov cuab yeej tuaj yeem tshaj tawm lossis sau npe rau cov ncauj lus. Yog li ntawd, piv txwv li. Yog tias Ntaus Ntawv #1 tau kaw qhov kub los ntawm ib qho ntawm nws lub ntsuas, nws tuaj yeem tshaj tawm cov lus uas muaj qhov ntsuas kub nws tau sau tseg, rau cov ncauj lus (piv txwv li "Kub"). Cov lus no tau xa mus rau MQTT Broker, uas koj tuaj yeem xav tias yog hloov pauv/router ntawm ib cheeb tsam network. Thaum MQTT Broker tau txais cov lus, nws yuav xa nws mus rau ib qho khoom siv (hauv qhov no, Ntaus #2) uas tau cuv npe rau tib lub ncauj lus.

Hauv txoj haujlwm no, peb yuav tshaj tawm rau lub ncauj lus siv ESP8266, thiab tsim cov ntawv Python uas yuav sau npe rau tib lub ncauj lus no, ntawm Raspberry Pi uas yuav ua raws li MQTT Broker. Qhov zoo tshaj plaws ntawm MQTT yog tias nws yog lub teeb yuag, yog li nws zoo meej rau kev khiav ntawm microcontrollers me me xws li ESP8266, tab sis nws kuj tseem muaj dav - yog li peb tuaj yeem ua nws ntawm Python tsab ntawv ib yam.

Vam tias, thaum kawg ntawm txoj haujlwm no, koj yuav nkag siab tias MQTT yog dab tsi thiab yuav siv nws li cas rau koj tus kheej txoj haujlwm yav tom ntej.

Kauj Ruam 2: Txhim Kho MQTT Broker ntawm Raspberry Pi

Txhim kho MQTT Broker ntawm Raspberry Pi
Txhim kho MQTT Broker ntawm Raspberry Pi
Txhim kho MQTT Broker ntawm Raspberry Pi
Txhim kho MQTT Broker ntawm Raspberry Pi
Txhim kho MQTT Broker ntawm Raspberry Pi
Txhim kho MQTT Broker ntawm Raspberry Pi

Txhawm rau teeb tsa peb cov txheej txheem MQTT, peb xav tau tus tswv lag luam, raws li tau piav qhia hauv cov kauj ruam dhau los. Txog Raspberry Pi, peb yuav siv tus "Mosquitto" MQTT broker. Ua ntej peb teeb tsa qhov no, nws yog qhov zoo tshaj los hloov kho peb Raspberry Pi.

sudo apt-tau hloov tshiab

sudo apt-tau hloov tshiab

Thaum koj tau ua tiav qhov no, teeb tsa mosquitto thiab tom qab ntawd cov mos txwv-cov neeg siv khoom pob khoom.

sudo apt -tau nruab mosquitto -y

sudo apt-tau nruab mosquitto-cov neeg siv -y

Thaum koj tau ua tiav txhim kho ob pob khoom no, peb yuav xav tau teeb tsa tus tswv lag luam. Cov ntaub ntawv teeb tsa tus tswv tsev mositto nyob ntawm /etc/mosquitto/mosquitto.conf, yog li qhib qhov no nrog koj cov ntawv nyeem uas koj nyiam. Yog tias koj tsis muaj tus kws kho kab ntawv nyiam lossis tsis paub siv ib qho ntawm cov kab lus hais kom kho, kuv yuav siv nano yog li koj tuaj yeem ua raws:

sudo nano /etc/mosquitto/mosquitto.conf

Hauv qab ntawm cov ntawv no, koj yuav tsum pom kab:

include_dir /etc/mosquitto/conf.d

Rho tawm kab no. Ntxiv cov kab hauv qab no rau hauv qab ntawm cov ntawv.

allow_anonymous cuav

password_file/etc/mosquitto/pwfile mloog 1883

Los ntawm kev ntaus cov kab no, peb tau hais rau mosquitto tias peb tsis xav kom leej twg txuas rau peb tus broker uas tsis muab lub npe siv tau thiab tus lej nkag mus siv tau (peb yuav tau teeb tsa cov no hauv ib pliag) thiab peb xav kom mositto mloog cov lus ntawm tus lej chaw nres nkoj 1883.

Yog tias koj tsis xav kom tus tswv lag luam xav tau lub npe siv thiab tus lej nkag, tsis txhob suav ob kab thawj zaug uas peb tau ntxiv (piv txwv li tso cai_anonymous… thiab password_file…). Yog tias koj tau ua qhov no, hla mus rau rebooting Raspberry Pi.

Tam sim no kaw (thiab txuag) cov ntawv ntawd. Yog tias koj ua raws nrog nano piv txwv, nias CTRL+X, thiab ntaus Y thaum hais kom ua.

Vim tias peb nyuam qhuav tau hais qhia mosquitto tias cov neeg siv sim siv MQTT tus neeg yuav khoom yuav tsum tau lees paub qhov tseeb, tam sim no peb yuav tsum qhia mosquitto dab tsi lub npe siv thiab tus lej nkag! Yog li, ntaus cov lus txib hauv qab no - hloov lub npe siv nrog lub npe siv uas koj xav tau - tom qab ntawd nkag tus password koj xav tau thaum hais kom sai (Nco tseg: yog tias, thaum kho cov ntaub ntawv teeb tsa, koj tau hais qhia lwm tus password_file txoj kev, hloov txoj hauv qab nrog ib qho koj siv).

sudo mosquitto_passwd -c/etc/mosquitto/pwfile username

Raws li peb nyuam qhuav hloov pauv cov ntaub ntawv teev npe mosquitto, peb yuav tsum rov pib dua Raspberry Pi.

sudo rov pib dua

Thaum Raspberry Pi tau ua tiav rov pib dua, koj yuav tsum muaj tus ua haujlwm MQTT ua haujlwm tau zoo! Tom ntej no, peb yuav sim cuam tshuam nrog nws, siv tus lej sib txawv ntawm cov cuab yeej/txheej txheem!

Kauj Ruam 3: Kuaj Tus Kheej Tus Kheej

Kuaj Tus Kheej
Kuaj Tus Kheej

Thaum koj tau teeb tsa mosquitto ntawm Raspberry Pi, koj tuaj yeem muab nws sim sai - tsuas yog kom ntseeg tau tias txhua yam ua haujlwm raug. Rau lub hom phiaj no, muaj ob lo lus txib uas peb tuaj yeem siv ntawm kab hais kom ua. mosquitto_pub thiab mosquitto_sub. Hauv theem no, Kuv yuav coj koj los ntawm kev siv txhua qhov ntawm no los sim peb tus broker.

Txhawm rau kuaj tus broker, koj yuav tsum qhib ob kab lus txib windows. Yog tias koj siv Putty lossis lwm tus neeg siv SSH, qhov no yooj yim li qhib lwm lub SSH qhov rai thiab nkag mus li qub. Yog tias koj nkag mus rau koj Pi los ntawm UNIX lub davhlau ya nyob twg, qhov no zoo ib yam. Yog tias koj siv Raspberry Pi ncaj qha, koj yuav tsum qhib ob lub qhov rai qhov rai hauv GUI hom (cov lus txib startxcan siv los pib GUI).

Tam sim no koj tau qhib ob lub qhov rais, peb tuaj yeem pib ntawm qhov kev sim. Hauv ib ntawm ob lub davhlau ya nyob twg, ntaus cov lus txib hauv qab no, hloov tus username thiab password nrog cov uas koj teeb tsa hauv cov kauj ruam dhau los.

mosquitto_sub -d -u username -P password -t xeem

Yog tias koj txiav txim siab tsis teeb lub npe siv thiab tus lej nkag rau hauv cov kauj ruam dhau los, txij tam sim no mus, tsis quav ntsej tus -u thiab -P chij hauv cov lus txib. Yog li, ua piv txwv, cov lus txib mositto_sub yuav tam sim no yog:

mosquitto_sub -d -t xeem

Cov lus txib mosquitto_sub yuav sau npe rau ib lub ncauj lus, thiab tso tawm txhua cov lus uas tau xa mus rau cov ncauj lus tshwj xeeb hauv lub qhov rai davhlau ya nyob twg. Ntawm no, -d txhais tau tias hom debug, yog li txhua cov lus thiab kev ua haujlwm yuav raug tso tawm ntawm qhov screen. -u thiab -P yuav tsum piav qhia tus kheej. Thaum kawg, -t yog lub npe ntawm cov ncauj lus peb xav sau npe rau - hauv qhov no, "xeem".

Ntxiv mus, hauv lwm lub qhov rai qhov rai, peb yuav sim thiab tshaj tawm cov lus rau "xeem" lub ncauj lus. Ntaus cov hauv qab no, nco qab dua los hloov tus username thiab password:

mosquitto_pub -d -u username -P password -t test -m "Nyob zoo, Ntiaj Teb!"

Thaum koj nias nkag mus, koj yuav tsum pom koj cov lus "Nyob Zoo, Ntiaj Teb!" tshwm nyob rau hauv thawj lub davhlau ya nyob twg qhov peb siv (rau npe). Yog tias qhov no yog, koj txhua tus tau npaj pib ua haujlwm ntawm ESP8266!

Kauj Ruam 4: Teeb ESP8266 (Adafruit HUZZAH)

Teeb tsa ESP8266 (Adafruit HUZZAH)
Teeb tsa ESP8266 (Adafruit HUZZAH)
Teeb tsa ESP8266 (Adafruit HUZZAH)
Teeb tsa ESP8266 (Adafruit HUZZAH)
Teeb tsa ESP8266 (Adafruit HUZZAH)
Teeb tsa ESP8266 (Adafruit HUZZAH)
Teeb tsa ESP8266 (Adafruit HUZZAH)
Teeb tsa ESP8266 (Adafruit HUZZAH)

Cov kauj ruam no tshwj xeeb rau Adafruit HUZZAH (raws li qhov uas kuv tau siv los ua kom tiav txoj haujlwm no). Yog tias koj siv lwm yam khoom Arduino / ESP8266 sib txawv, koj tuaj yeem xav hla cov kauj ruam no. Txawm li cas los xij, Kuv yuav qhia koj skim nyeem nws, tsuas yog muaj cov ntaub ntawv ntawm no uas yuav cuam tshuam rau koj.

Txog qhov haujlwm no, Kuv tab tom yuav tsim lub HUZZAH nrog rau Arduino software. Yog li, yog tias koj tsis tau ua tiav, nco ntsoov teeb tsa Arduino software (tshiab dua 1.6.4). Koj tuaj yeem rub nws ntawm no.

Thaum koj tau teeb tsa Arduino software, qhib nws thiab mus rau File-> Preferences. Ntawm no koj yuav tsum pom (ze rau hauv qab ntawm lub qhov rais) lub thawv ntawv nrog daim ntawv lo: "Tus Thawj Saib Xyuas Ntxiv URLs". Hauv kab ntawv no, theej thiab muab cov kab ntawv txuas hauv qab no:

arduino.esp8266.com/stable/package_esp8266com_index.json

Nyem OK kom txuag koj cov kev hloov pauv. Tam sim no qhib Pawg Thawj Coj (Cov Cuab Yeej-> Pawg Thawj Coj-> Tus Thawj Saib Xyuas Haujlwm) thiab tshawb rau ESP8266. Nruab esp8266 los ntawm ESP8266 Zej Zog pob. Rov pib dua Arduino software.

Tam sim no, ua ntej peb tuaj yeem teeb tsa lub rooj tsavxwm, peb yuav tsum xaiv ob peb yam kev xaiv sib txawv. Hauv cov cuab yeej ntawv qhia zaub mov xaiv, xaiv Adafruit HUZZAH ESP8266 rau Pawg Thawj Coj, 80 MHz rau CPU Zaus (koj tuaj yeem siv 160 MHz yog tias koj xav tau overclock nws, tab sis tam sim no kuv yuav siv 80 MHz), 4M (3M SPIFFS) rau Flash Loj, thiab 115200 rau Kev Tshaj Tawm. Tsis tas li, nco ntsoov xaiv qhov chaw nres nkoj COM uas koj siv (qhov no yuav yog nyob ntawm koj teeb tsa).

Ua ntej koj tuaj yeem xa cov cai, koj yuav tsum paub tseeb tias HUZZAH nyob hauv hom bootloader. Txhawm rau ua qhov no, tuav lub pob ntawm lub rooj tsavxwm cim GPIO0, thiab thaum qhov no tau tuav, tuav lub khawm Pib dua ib yam. Tom qab ntawd, tso lub pob Pib dua, thiab tom qab ntawd GPIO0. Yog tias koj tau ua qhov no kom raug, lub teeb liab liab uas tuaj thaum koj nias GPIO0 yuav tsum tam sim no tsis muaj teeb ci.

Txhawm rau upload cov cai rau lub microcontroller, thawj zaug kom ntseeg tau tias HUZZAH nyob rau hauv hom bootloader, tom qab ntawd tsuas yog nyem lub pob upload hauv Arduino IDE.

Yog tias koj muaj teeb meem teeb tsa HUZZAH, cov ntaub ntawv ntxiv tuaj yeem nrhiav tau ntawm Adafruit tus kheej qhia.

Kauj ruam 5: Programming ESP8266

Kev ua haujlwm ESP8266
Kev ua haujlwm ESP8266

Tam sim no peb yuav pib ua haujlwm ESP8266, tab sis ua ntej peb tuaj yeem pib, koj yuav tsum tau teeb tsa cov tsev qiv ntawv hauv qab no hauv Arduino Tus Thawj Saib Xyuas Tsev Kawm Ntawv (Sketch-> Xws li Tsev Qiv Ntawv-> Tswj Tsev Qiv Ntawv)

  • Thaws 2
  • PubSubClient

Thaum koj tau teeb tsa cov tsev qiv ntawv ntawd, koj yuav muaj peev xwm ua raws txoj cai uas kuv tau suav nrog hauv Cov Lus Qhia no (MQTT_Publish.zip). Kuv tau ua kom ntseeg tau los tawm tswv yim kom koj tuaj yeem nkag siab tias txhua ntu tab tom ua dab tsi, thiab qhov no yuav tsum cia siab tias yuav pab koj hloov kho nws raws li koj xav tau.

Nco ntsoov hloov tus lej nyob rau sab saum toj ntawm txoj cai kom koj ESP8266 tuaj yeem txuas rau koj li WiFi network thiab koj tus MQTT Broker (Raspberry Pi).

Yog tias koj txiav txim siab tsis teeb tus username thiab password rau MQTT Broker, tom qab ntawd rub tawm MQTT_PublishNoPassword.zip cov ntaub ntawv.

Kauj Ruam 6: Txhim Kho Python Client (paho-mqtt)

Txhim Kho Python Client (paho-mqtt)
Txhim Kho Python Client (paho-mqtt)

Ua tsaug, cov kauj ruam no yooj yim heev! Txhawm rau teeb tsa tus neeg siv mos txwv python, koj tsuas yog yuav tsum sau cov hauv qab no rau hauv kab hais kom ua (Linux/Mac) lossis txawm hais kom sai (Windows).

pip nruab paho-mqtt

Nco tseg: Windows hais kom sai tuaj yeem muaj teeb meem khiav cov lus txib pip yog tias koj tsis tau hais qhia tias koj xav tau pip nruab thiab nab nab ntxiv rau koj qhov PATH sib txawv thaum koj teeb tsa Python. Muaj ntau txoj hauv kev los kho qhov no, tab sis kuv xav tias tsuas yog rov nruab Python yog txoj hauv kev yooj yim tshaj plaws. Yog tias tsis ntseeg - muab nws rau google!

Kauj Ruam 7: Python Client - Sau npe

Python Client - Sau npe yuav ua
Python Client - Sau npe yuav ua

Hauv theem no, peb tab tom teeb tsa Python tsab ntawv (txawm nyob ntawm Raspberry Pi nws tus kheej lossis ntawm lwm lub khoos phis tawj txuas nrog lub network) los daws txhua cov lus uas tau xa (tshaj tawm) los ntawm ESP8266 mus rau MQTT lub ncauj lus.

Kuv tau suav nrog tus nab npawb nab npawb hauv qab no (PythonMQTT_Subscribe.py), uas tau tawm tswv yim los pab koj nkag siab tias yog dab tsi mus, tab sis kuv yuav piav qhia qee qhov tseem ceeb ntawm no ib yam.

Yog tias koj tsis tau teeb tsa tus username thiab password rau MQTT kev txuas ua ntej, rub tawm PythonMQTT_SubscribeNoPassword.py cov ntaub ntawv hloov chaw.

Kauj Ruam 8: Sib Txuas Ntawm ESP8266 Cov Khoom Siv

Sib Txuas Lus Ntawm ESP8266 Cov Khoom Siv
Sib Txuas Lus Ntawm ESP8266 Cov Khoom Siv

Yog tias koj xav teeb tsa IoT network, piv txwv li, koj tuaj yeem xav sib txuas lus ntawm ESP8266 cov cuab yeej. Ua tsaug, qhov no tsis nyuaj ntau dua li cov cai peb tau sau ua ntej, txawm li cas los xij, muaj ob peb qhov kev hloov pauv tseem ceeb.

Rau ib ESP xa cov ntaub ntawv mus rau lwm qhov, thawj ESP yuav tsum tshaj tawm rau lub ncauj lus, thiab ESP thib ob yuav xav tau sau npe rau lub ncauj lus ntawd. Qhov teeb tsa no yuav tso cai rau kev sib tham ib leeg xwb - ESP (1) txog ESP (2). Yog tias peb xav ESP (2) tham rov qab rau ESP (1), peb tuaj yeem tsim lub ncauj lus tshiab, uas ESP (2) yuav tshaj tawm, thiab ESP (1) yuav sau npe yuav. Ua tsaug, peb tuaj yeem muaj ntau tus neeg rau npe nyob hauv tib lub ncauj lus, yog li yog tias koj xav xa cov ntaub ntawv mus rau ntau lub tshuab, koj tsuas xav tau ib lub ncauj lus (uas lawv txhua tus tau sau npe, tshwj tsis yog lub cuab yeej uas xa cov ntaub ntawv, raws li qhov yuav luam tawm).

Yog tias koj xav tau kev pab txheeb xyuas seb txhua lub cuab yeej yuav tsum tau ua dab tsi, xav txog lub kaw lus ua chav nyob ntawm tib neeg. Yog tias ESP (1) tshaj tawm, koj tuaj yeem xav txog lub cuab yeej no ua "tus hais lus", thiab ib qho khoom siv twg uas tau tso npe rau lub ncauj lus yog "mloog" hauv qhov piv txwv no.

Kuv tau suav qee qhov piv txwv hauv qab no, uas qhia tau tias yuav ua li cas ESP8266 tuaj yeem sau npe rau ib lub ncauj lus, thiab mloog qee cov lus - 1 thiab 0. Yog tias tau txais 1, LED -on -board (rau HUZZAH - GPIO 0) tau hloov ntawm. Yog tias 0 tau txais, qhov LED no tau muab tua.

Yog tias koj xav ua cov ntaub ntawv nyuaj dua, qhov no yuav tsum ua tiav hauv Kev Ua Tau Txais Kev Xa Ntawv (saib cov cai).

Txog koj tus kheej cov phiaj xwm, yog tias koj xav tau ob qho tib si xa thiab tau txais cov ntaub ntawv, koj tuaj yeem suav nrog kev tshaj tawm ua haujlwm los ntawm tus piv txwv dhau los rau hauv cov cai suav nrog hauv cov kauj ruam no. Qhov no yuav tsum tau ua nyob rau hauv lub ntsiab Arduino voj () ua haujlwm.

Nco ntsoov hloov qhov hloov pauv nyob rau sab saum toj ntawm txoj cai kom haum rau koj lub network!

Pom zoo: