Cov txheej txheem:
- Kauj Ruam 1: Pawg Wemos D1 Mini ESP8266
- Kauj Ruam 2: Npaj Rau MicroPython ntawm ESP Chip
- Kauj Ruam 3: Pib Nrog MiPy-ESP
- Kauj Ruam 4: MiPy-ESP Framework Architecture
- Kauj Ruam 5: Npaj Koj Txoj Cai Code
- Kauj Ruam 6: Txheeb thiab xa cov Cai rau Microcontroller
Video: Pib Nrog Python rau ESP8266 & ESP32: 6 Cov Kauj Ruam
2024 Tus sau: John Day | [email protected]. Kawg hloov kho: 2024-01-30 09:25
Bacground
ESP8266 thiab nws tus tij laug loj ESP32 yog tus nqi qis Wi-nkaus microchips nrog tag nrho TCP/IP pawg thiab muaj peev xwm tswj tau me me. ESP8266 nti thawj zaug tau mloog los ntawm cov neeg tsim khoom hauv xyoo 2014. Txij thaum ntawd los, tus nqi qis (<5 USD), nws lub peev xwm Wi-nkaus, ua rau hauv lub cim xeeb nco ntawm 1 lossis 4 MB, thiab ntau yam kev tsim kho cov laug cam, tau ua ESP nti yog ib qhov nrov tshaj plaws tswj hwm rau WiFi thiab IoT DIY cov haujlwm.
MicroPython yog qhov ua tau zoo thiab muaj txiaj ntsig ntawm kev ua kom nrov nrov Python cov lus uas suav nrog ib pawg me me ntawm Python tus qauv tsev qiv ntawv thiab tau ua kom zoo los khiav ntawm microcontrollers.
Kev sib xyaw ntawm ob qho no yog qhov kev xaiv zoo heev rau DIY cov haujlwm, ob qho tib si rau cov pib thiab cov neeg siv ntau dua.
Qhov project MiPy-ESP
Rov qab rau xyoo 2015 kuv thawj qhov haujlwm nrog ESP8266 tau pib tawm nrog ESP-01 nti siv Arudions rau kev ua haujlwm chip AT cov lus txib hla txuas txuas. Tom qab ntawd, ntau xyoo tom ntej no kuv tau thov Arduino core rau ESP8266 rau kev ua haujlwm rau cov chips nrog C ++ lus. Qhov no ua haujlwm tau zoo, tab sis rau Python tus neeg txhawb siab, kuv pom ntawm MicroPython siv Python 3 yog xov xwm zoo.
MiPy-ESP txoj haujlwm yog lub hauv paus hloov pauv tau thov MicroPython rau kev teeb tsa Python IoT cov haujlwm ntawm ESP-tsev neeg micro-controllers.
Lub moj khaum yog tsim los ntawm LeGarage Technical Comcomm Software Developer Team (LG-TC-SWDT-01) lub hom phiaj ntawm kev hloov pauv twb tau tsim C ++ raws txoj cai rau peb daim ntawv thov microcontroller.
Txoj haujlwm muab cov yam ntxwv tseem ceeb xws li
- Cov txheej txheem sib txuas hauv network
- Chip access point webserver (rau wifi txuas thiab ua haujlwm ntawm chip webpages rau cov ntaub ntawv I/O)
- MQTT cov haujlwm
- Logging/debugging
- Microcontroller lub sijhawm teem sijhawm
- Kho vajtse I/O niaj hnub
Nrog ib lub ntsiab lus tseem ceeb sau tsab ntawv (main.py), txhua yam nrog kev teeb tsa thoob ntiaj teb (config.py).
Txoj cai mictocontroller no ua haujlwm nrog kev saib xyuas ruaj khov ntawm cov txuas sib txuas rau WiFi network thiab MQTT cov tswv lag luam. Muaj MicroPython cov qauv rau ntau yam kho vajtse tuaj yeem yooj yim sib xyaw ua ke rau hauv cov kab ke.
Lub moj khaum MiPy-ESP tau dhau los ua lub hauv paus ntawm txhua qhov peb nyiam siv hluav taws xob IoT cov phiaj xwm koom nrog ESP-tsev neeg micro-tswj. Nws tau raug sim ntawm ntau pawg ESP-tsev neeg, zoo li NodeMCU, Wemos thiab Lolin cov laug cam.
Cov lus qhia hauv qab no yog phau ntawv qhia yuav pib li cas nrog ESP-tsev neeg microcontrollers thiab MicroPython siv MiPy-ESP moj khaum.
Kauj Ruam 1: Pawg Wemos D1 Mini ESP8266
MiPy-ESP moj khaum ua haujlwm nrog feem ntau ESP8266 raws li microcontrollers.
Wemos D1 lub rooj tsav xwm txhim kho me me yog ua raws li ESP-8266EX nti. Ntawm tus hneev taw ntawm 2.5 x 3.5 cm, nws muaj 4MB flash nco, 11 digital input/output pins, txhua tus pin txhawb cuam tshuam, PWM, I2C, SPI, serial thiab 1 analog input nrog 3.3V qhov siab tshaj plaws, tuaj yeem ua haujlwm ntawm 5V zog, muaj micro USB txuas thiab yog breadboard tau tshaj. Tus nqi qis thiab nws qhov loj me tau ua rau kuv nyiam ESP board.
Ib qho ntxiv, D1 mini pro version ntawm lub rooj tsavxwm los nrog kev xaiv rau txuas lub kav hlau txais xov sab nraud, nce kev sib txuas ntau yam (+100 m ntau). Ntxiv rau qhov ntawd, lub rooj tsavxwm kuj tseem muaj ntau yam tawm ntawm-lub-thawv ntawv txuas ntxiv cov laug cam nrog qhov sib thooj me me.
Kauj Ruam 2: Npaj Rau MicroPython ntawm ESP Chip
Hauv thawj kauj ruam no, koj yuav
- Txuas ESP pawg thawj coj ntawm USB rau koj lub computer
- Nruab Esptool software rau ntsais lub nti
- Lwv nti nco
- Nyem lub nti nrog MicroPython firmware
- Nruab Rshell rau kev ua kom muaj kab hais kom ua cuam tshuam nrog koj cov nti
- Nruab mpy-cross (rau muab tso ua ke.py cov ntaub ntawv rau binary)
Txuas lub rooj tsavxwm rau koj lub khoos phis tawj ntawm USBBoards nrog lub USB-serial chaw nres nkoj ua rau UART muaj rau koj lub PC thiab yog qhov kev xaiv yooj yim tshaj plaws rau kev pib. Rau cov laug cam tsis muaj USB txuas, ib qho FTDI module nrog USB rau cov lej tuaj yeem siv los txuas GPIO pins rau ntsais txuas nrog lub ntiaj teb sab nraud, tab sis qhov no tsis tau hais hauv qhov kev qhia no.
Rau MicroPython siv MiPy-ESP code, qhov xav tau yam tsawg kawg nkaus rau nti loj yog 1MB. Kuj tseem muaj qhov tshwj xeeb tsim rau cov laug cam nrog 512kB, tab sis qhov no tsis muaj kev txhawb nqa rau cov ntaub ntawv, uas MiPy-ESP nyob ntawm.
Thaum siv USB cable, lub rooj tsav xwm tau txais kev txhawb nqa los ntawm koj lub computer thaum txuas nrog. Qhov no tseem tso cai rau kev ua haujlwm thiab debugging dhau qhov txuas txuas. Thaum cov phiaj xwm phiaj xwm tau muab tso rau thiab koj txoj haujlwm tau xa tawm, lub zog sab nraud tau siv dua li lub hwj huam txuas ntawm lub rooj tsavxwm.
Txhim kho Esptool Cov ntaub ntawv hais txog Esptool software tuaj yeem pom hauv Esptool GitHub chaw cia khoom. Yog tias koj xav siv Windows/Linux/OSX (MAC), qhov txuas saum toj no tseem suav nrog qhov ntawd. Python pob tuaj yeem teeb tsa los ntawm
pip nruab esptool
Rau Linux cov neeg siv, pob khoom rau Esptool tau khaws cia rau Debian thiab Ubuntu, thiab tseem tuaj yeem teeb tsa nrog
sudo apt nruab esptool
Erasing ESP flash memoryUsing Esptool, koj tom qab ntawd lwv ESP flash nco nrog cov lus txib
esptool.py --port /dev /ttyUSB0 lwv_flash
Rub tawm MicroPyton firmware Lub MicroPython firmware nyob hauv.bin cov ntaub ntawv uas tuaj yeem rub tawm los ntawm MicroPython lub vev xaib.
Tam sim no txoj haujlwm tus tswv tswv ntawm repo tau raug sim thiab ua haujlwm nrog Micropython v.1.12. Txhawm rau kom ua tiav nrog MiPY-ESP moj khaum, rub cov ntaub ntawv 'esp8266-20191220-v1.12.bin' los ntawm qhov txuas no thiab sau cov firmware rau lub nti los ntawm cov lus txib:
esptool.py --port /dev /ttyUSB0 --baud 460800 write_flash --flash_size = ntes 0 esp8266-20191220-v1.12.bin
Txhim Kho RshellThe Rshell pob ua kom muaj kab hais kom ua cuam tshuam nrog koj ib puag ncig MicroPython teeb tsa ntawm lub nti. Nws tuaj yeem pom nyob hauv qhov txuas no. Rshell yog lub plhaub yooj yim uas khiav ntawm tus tswv tsev thiab siv MicroPython's raw-REPL los xa cov nab nab nab mus rau hauv lub pyboard txhawm rau kom tau txais cov ntaub ntawv cov ntaub ntawv, thiab theej cov ntaub ntawv rau thiab los ntawm MicroPython cov ntaub ntawv. REPL sawv rau Nyeem Ntawv Txheeb Xyuas Luam Loop, thiab yog lub npe muab rau kev sib tham MicroPython tam sim uas koj tuaj yeem nkag mus rau ntawm ESP8266. Siv REPL yog txoj hauv kev yooj yim tshaj plaws los ntsuas koj cov cai thiab ua cov lus txib. Nruab Rshell los ntawm cov lus txib:
sudo pip nruab rshell
Txhim kho lub mpy-cross compiler MicroPython tuaj yeem siv nrog ascii.py cov ntaub ntawv xa mus rau cov ntaub ntawv nti. MicroPython tseem piav qhia lub tswv yim ntawm.mpy cov ntaub ntawv uas yog cov thawv ntim cov ntaub ntawv hom uas tuav cov ntawv ua ntej, thiab uas tuaj yeem raug xa mus zoo li ib txwm.py module. Los ntawm kev sau ua ke.py cov ntaub ntawv rau.mpy, ntau lub cim xeeb RAM yuav muaj rau koj tus lej ua haujlwm - thiab qhov no xav tau txhawm rau kom muaj lub luag haujlwm tseem ceeb ntawm MiPy -ESP moj khaum.
Txog MiPy-ESP txoj cai xa tawm, mpy-cross MicroPython cross compiler sau ua.py cov ntawv sau rau.mpy ua ntej xa cov ntawv. Nruab pob mpy-hla pob los ntawm cov lus qhia hauv qhov txuas no. Xwb, mpy-cross hais kom ua tuaj yeem teeb tsa los ntawm Python pip hais kom ua lossis khiav los ntawm mpy-hla kab ntawv yog tias koj clone MicroPython chaw cia khoom los ntawm GitHub ntawm no.
Tam sim no koj muaj MicroPython thiab txhua yam cuab yeej xav tau teeb tsa rau kev pib nrog tsim koj thawj MiPy-ESP project
Kauj Ruam 3: Pib Nrog MiPy-ESP
Hauv cov kauj ruam no koj yuav
Rub tawm MyPy-ESP lub moj khaum
Rub tawm MiPy-ESP lub moj khaum MiPy-ESP txoj haujlwm tuaj yeem pom ntawm GitHub hauv qhov chaw tso cai no. Los ntawm GitHub koj tuaj yeem rub tawm cov ntaub ntawv khaws cia, lossis clone nws rau koj lub khoos phis tawj los ntawm
git clone
Nrog rau qhov chaw cia khoom tso rau hauv koj lub khoos phis tawj, tam sim no koj muaj txhua tus lej qauv uas koj xav tau rau kev tsim tawm ntawm lub thawv ESP IoT project. Xav paub ntau ntxiv ntawm lub cuab yeej hauv kauj ruam tom ntej.
Kauj Ruam 4: MiPy-ESP Framework Architecture
Hauv cov kauj ruam no koj yuav
kawm paub txog MiPy-ESP cov cai ua haujlwm
MiPy-ESP code architecture
Txhua qhov qauv txheej txheem Python tau pom hauv /src nplaub tshev ntawm MiPY-ESP code repository. Src/core nplaub tshev muaj cov qauv tseem ceeb uas nkag mus rau txhua qhov haujlwm. Src/cov ntawv tsav tsheb muaj xaiv cov qauv rau ntau yam khoom siv los txuas rau koj lub nti. Src/cov khoom siv hluav taws xob muaj cov txheej txheem xaiv hluav taws xob kom suav nrog hauv koj qhov project.
Cov ntaub ntawv main.py thiab config.py muaj nyob hauv src/ nplaub tshev. Nov yog cov ntaub ntawv tseem ceeb txhawm rau kho kom tsim koj txoj haujlwm:
config.py:
Cov ntaub ntawv no yog cov ntaub ntawv teeb tsa thoob ntiaj teb rau koj txoj haujlwm. Nws muaj ntau yam teeb tsa, txhua yam nrog cov lus piav qhia hauv cov ntawv.
main.py:
Nov yog tsab ntawv tseem ceeb rau lub micro-controller code loop. Nws muaj daim ntawv thov tshwj xeeb cov cai hauv lub moj khaum. Raws li khau raj khau raj, main.py khiav thiab ntshuam tag nrho cov phiaj xwm vam khom cov qauv nrog muab cov tswv yim los ntawm cov ntaub ntawv config.py. Daim phiaj saum toj saud qhia qhov txheej txheem ntawm tsab ntawv main.py.
Daim duab saum toj no piav qhia txog kev ua haujlwm ntawm main.py:
- Thaum khau raj, txoj cai sim txuas lub nti rau Wi-nkaus network Yav dhau los tau thov cov tes hauj lwm thiab lawv cov password (encrypted ntawm nti) raug khaws cia hauv flash nco. Network SSIDs thiab lawv tus lej password tuaj yeem muab rau hauv cov ntaub ntawv wifi.json ntawm hom ntawv " SSID1 ":" Password "," SSID ":" Password2 "}. Cov tes hauj lwm muab hauv cov ntaub ntawv no tau muab khaws cia, cov password tau muab zais, thiab cov ntaub ntawv raug tshem tawm thaum khau raj.
- Yog tias tseem tsis tau paub cov tes hauj lwm tau pom, tus lej teeb tsa qhov nkag mus nkag (AP) webserverThe chip AP server SSID thiab password tau teeb tsa hauv config.py file. Los ntawm kev nkag mus rau tus lej SSID, lub vev xaib rau kev nkag mus ntawm lub nti rau Wi-nkaus tau txais kev pabcuam ntawm 192.168.4.1. Detected networks tau qhia hauv cov ntawv qhia zaub mov, lossis SSID tuaj yeem nkag mus rau tus kheej (zais network) ua ke nrog Wi-nkaus password. Thaum ua tiav kev sib txuas ntawm cov nti rau Wi-nkaus, AP server kaw thiab lub main.py code txuas ntxiv mus rau nws cov kauj ruam tom ntej.
-
Hauv Kev teeb tsa ntu ntawm main.py,
- kev ua haujlwm rau kev ua haujlwm thiab kev hu rov qab (lwm yam. MQTT callbacks) thiab cov xwm txheej tsis tu ncua tau teev tseg.
- Cov sijhawm sib txawv rau kev ua haujlwm tau teeb tsa.
- MQTT broker tus neeg siv khoom tau tsim
-
Txoj cai ntawd nkag mus rau lub ntsiab micro-controller voj,
- txuas ntxiv mus xyuas lub network thiab MQTT broker kev sib txuas,
- MQTT kev tso npe,
- kho vajtse I/O
- thiab teem sijhawm ua haujlwm.
- Thaum poob lub network lossis MQTT broker kev sib txuas, cov cai sim rov tsim dua.
Kauj Ruam 5: Npaj Koj Txoj Cai Code
Hauv cov kauj ruam no koj yuav
- kawm paub txog MiPy-ESP cov ntaub ntawv khaws cia cov qauv
- npaj koj cov phiaj xwm phiaj xwm rau kev xa cov nti
Daim duab saum toj no piav qhia cov qauv txheej txheem khaws cia thiab teev cov txheej txheem tam sim no ntawm lub moj khaum. Koj qhov haujlwm yog theem hauv src/ nplaub tshev. Cov tub ntxhais MiPy-ESP cov qauv txheej txheem nyob rau hauv src/core, xaiv tau cov txheej txheem kev siv hluav taws xob hauv src/cov khoom siv hluav taws xob thiab cov khoom kho vajtse hauv src/tsav tsheb.
Feem ntau muaj MicroPython cov tsev qiv ntawv kho vajtse tuaj yeem nkag mus rau cov tsav tsheb/ nplaub tshev yam tsis muaj kev hloov kho. Txhua tus neeg tsav tsheb tam sim no tau sim nrog MiPy-ESP lub moj khaum. Hais txog cov qauv hauv cov khoom siv/ daim nplaub tshev, yuav muaj ntau ntxiv thaum lawv los rau hauv lub neej.
Kev ua haujlwm ntawm txoj haujlwm txoj cai Koj cov phiaj xwm tshwj xeeb yuav tsum tau muab tso rau hauv src/ nplaub tshev. Twb muaj lawm, yog lub ntsiab.py thiab cov ntaub ntawv config.py koj tuaj yeem hloov kho. Kuj tseem luam qhov xav tau cov khoom siv hluav taws xob los ntawm src/cov khoom siv hluav taws xob thiab src/tsav tsheb mus rau src/.
Yog tias koj xav muab kev paub Wi-nkaus tes hauj lwm thiab tus password rau lub nti, ntxiv cov ntawv wifi.json rau src/.
Ua ke thiab npaj rau uploadA muab Makefile tuaj yeem thov rau kev npaj cov ntaub ntawv rau kev xa mus rau nti los ntawm kev suav ua.py cov ntaub ntawv hauv / src, suav nrog cov qauv tseem ceeb thiab xa cov ntaub ntawv suav mus rau daim nplaub tshev tshiab hu ua tsim / los ntawm cov lus txib
ua tsim
Cov ntaub ntawv hauv kev tsim tau npaj txhij rau kev xa mus rau cov ntaub ntawv nti. Los ntawm lub neej ntawd, main.py thiab config.py tsis suav nrog binary, txhawm rau nkag tau yooj yim rau lawv rau kev tshuaj xyuas ntawm kev xa cov chips. Qhov hais kom ua:
ua kom huv
Tshem tawm qhov tsim/ nplaub tshev thiab nws cov ntsiab lus.
Kauj Ruam 6: Txheeb thiab xa cov Cai rau Microcontroller
Hauv seem no koj yuav
- upload cov ntaub ntawv npaj hauv tsim/ los ntawm ntu kawg
- pib thiab saib xyuas cov cai khiav
Uploading tsim/ cov ntaub ntawv nrog Rshell
Rub tawm txhua cov ntaub ntawv hauv /tsim cov npe rau ESP nti siv Rshell. Nrog lub microcontroller txuas nrog USB, los ntawm kev tsim/ nplaub tshev pib Rshell nrog cov lus txib
rshell -p /dev /ttyUSB0
Tom qab ntawv tshuaj xyuas cov ntawv nti (yog tias muaj) los ntawm
ls /pib
Txhua cov ntaub ntawv ntawm nti tuaj yeem raug tshem tawm los ntawm
rm /pawg /****
Luam tag nrho cov ntaub ntawv phiaj xwm hauv kev tsim/ rau lub nti:
cp *. * /pyboard ntawv
Tom qab ntawd pib sib tham sib Python davhlau ya nyob twg los ntawm cov lus txib
rov qab
Tam sim no koj tuaj yeem thov Python cov lus txib lossis ntsaws cov qauv thiab saib xyuas cov nti tawm ntawm MiPy-ESP cov cav logger.
Pib dua lub nti los ntawm nias lub pob pib dua, lossis los ntawm kab hais kom ua los ntawm
ntshuam loj
los yog
ntshuam tshuab
thiab ces
tshuab.reset ()
Nyob ntawm koj qhov kev nkag/debug teeb tsa hauv qhov project teeb tsa cov ntaub ntawv, tam sim no qhov hloov tshiab yuav qhia pom debugging cov lus los ntawm ESP nti hla qhov txuas txuas.
Qhov no yuav tsum cia siab tias koj pib.
Pom zoo:
Cov Haujlwm Hluav Taws Xob rau Cov Pib: 14 Cov Kauj Ruam (nrog Duab)
Cov Haujlwm Hluav Taws Xob rau Cov Pib: Yog tias koj xav tau nkag mus rau hluav taws xob thiab xav tau qhov chaw pib qhov kev qhia no yog rau koj. Muaj tus lej ntawm cov khoom siv pheej yig heev ntawm eBay thiab Aliexpress uas koj tuaj yeem tau txais rau 2 lossis 3 duas uas tuaj yeem muab qee qhov kev paub rau koj hauv kev tiv thaiv tus kheej
Nab hab sej rau Tsis Yog-Pib Pib Pib: 7 Kauj Ruam
Nab hab sej rau Tsis -Yog li Pib Pib: Nyob zoo, zaum kawg, yog tias koj tau mob siab rau, peb tau kov ntawm qhov pib ntawm nab nab - luam tawm, thaum thiab rau loops, nkag & tso tawm, yog, thiab pib ntawm easygui. kuj yog kev faib dawb pub dawb ntawm easygui thiab pycal-kuv tus kheej module.this nyeem yuav suav nrog: ntau dua
Ntxiv Qhov Pib Pib Suab rau Google AIY Cov Khoom Siv Suab: 6 Cov Kauj Ruam (nrog Duab)
Ntxiv Qhov Pib Pib Suab rau Google AIY Lub Suab Khoom: Cov ntawv qhia no yooj yim zoo nkauj. Kuv tau txaus siab heev rau Google AIY Lub Suab Khoom, tab sis zoo li ntawm kuv li Google Tsev ib txwm muaj suab nrov uas lawv ua kom paub tseeb tias lawv nquag mloog. Qhov no tsis yog teeb tsa los ntawm lub hauv paus ntawm ib qho piv txwv tha
Phau Ntawv Qhia Pib Pib rau Arduino IDE Nrog NodeMCU ESP8266: 6 Cov Kauj Ruam
Phau Ntawv Qhia Pib Pib rau Arduino IDE Nrog NodeMCU ESP8266: Arduino IDE Kev teeb tsa rau NodeMCU ESP8266
Txaus Siab Rau Koj Tus Kheej Tsuas Yog Siv 12V-rau-AC-kab Inverter rau LED Lub Teeb Hluav Taws Xob Tsis txhob Rov Ua Rau Lawv rau 12V.: 3 Cov Kauj Ruam
Txaus Siab Rau Koj Tus Kheej Tsuas Yog Siv 12V-rau-AC-kab Inverter rau LED Lub Teeb Hluav Taws Xob Tsis txhob Rewiring Lawv rau 12V: Kuv txoj kev npaj tau yooj yim. Kuv xav txiav cov phab ntsa uas siv hluav taws xob LED teeb ua ib daim ces rov txuas nws kom khiav tawm 12 volts. Lwm txoj hauv kev yog siv lub zog hloov pauv, tab sis peb txhua tus paub tias lawv tsis muaj txiaj ntsig zoo, puas yog? Txoj cai? Los yog lawv?