Cov txheej txheem:

ESP32 Bluetooth BLE Chaw Taws Teeb: 8 Kauj Ruam
ESP32 Bluetooth BLE Chaw Taws Teeb: 8 Kauj Ruam

Video: ESP32 Bluetooth BLE Chaw Taws Teeb: 8 Kauj Ruam

Video: ESP32 Bluetooth BLE Chaw Taws Teeb: 8 Kauj Ruam
Video: How I Made this Electric Car that can go Anywhere 2024, Hlis ntuj nqeg
Anonim
ESP32 Bluetooth BLE Chaw Taws Teeb
ESP32 Bluetooth BLE Chaw Taws Teeb
ESP32 Bluetooth BLE Chaw Taws Teeb
ESP32 Bluetooth BLE Chaw Taws Teeb
ESP32 Bluetooth BLE Chaw Taws Teeb
ESP32 Bluetooth BLE Chaw Taws Teeb
ESP32 Bluetooth BLE Chaw Taws Teeb
ESP32 Bluetooth BLE Chaw Taws Teeb

Txoj haujlwm no yog piv txwv ntawm yuav ua li cas txuas qhov pheej yig Bluetooth BLE joystick rau ESP32. Cov cai tau sau siv Arduino IDE Ver 1.8.5 nrog ESP32 addon. BLE joystick siv yog khoom lag luam uas koj tuaj yeem yuav hauv Is Taws Nem tau qis dua $ 20.00 lossis los ntawm koj lub khw tsibBELoW hauv zos rau $ 5.00.

Tus joystick uas kuv siv rau txoj haujlwm no yog Spektrum VR Control Bluetooth Remote Controller. Nws tab tom ua lag luam raws li ua haujlwm nrog Android thiab IOS ntsiav tshuaj ua joystick/nas rau siv nrog VR pob ntseg.

VR Kev Tswj Bluetooth Chaw Taws Teeb Bluetooth muaj ib tus joystick nrog X thiab Y axis, ob lub khawm ua rau pem hauv ntej thiab rau lub khawm ntawm tus kov. Ob ntawm cov nyees khawm yog rau lub zog rau/tawm thiab tswj hom. Lwm plaub khawm tuaj yeem siv rau yam koj xav tau. Txoj haujlwm no yog lub cev pob txha lossis lub moj khaum uas ua haujlwm tag nrho ntawm Bluetooth cuam tshuam thiab txiav txim siab ntawm cov nyees khawm & joystick. Txhua yam koj yuav tsum ua yog ntxiv kev hu xov tooj los tswj qhov koj xav tau cov nyees khawm thiab joystick ua. Koj tsis tas yuav paub dab tsi txog Bluetooth los siv lub moj khaum no.

Muaj ntau lub vev xaib nrog cov lus qhia ntxaws rau kev txhim kho Arduino IDE thiab ESP32 addon. Kuv yuav tsis sim rov ua cov ntaub ntawv ntawm no. Google nws thiab ua raws cov lus qhia.

Lub hauv paus yog kev hloov pauv ntawm BLE tus neeg siv piv txwv luam tawm los ntawm IoT Sib Koom. Koj tuaj yeem pom nws ntawm no. Koj tuaj yeem kawm txoj cai no kom tau txais lub tswv yim ntawm BLE ua haujlwm li cas. Expressif muaj qhov ua piv txwv tiav rau GATT BLE cov neeg siv khoom thiab piav qhia kev ua haujlwm kom ntxaws (tsis sau rau Arduino IDE). Koj tuaj yeem tau txais ntawm no.

Qhov no tsis yog nyeem BLE. Kuv yuav tsis piav qhia tias txoj cai ua haujlwm li cas. Kuv yuav siv BLE cov lus piav qhia qee yam ntawm cov yam ntxwv ntawm tus joystick. Ib feem ntawm cov cai uas koj xav tau hloov kho rau koj txoj haujlwm yuav piav qhia ntxaws kom pab koj hloov kho nws. Cov peev txheej tau txwv kom khaws Cov Lus Qhia luv luv no thiab tsom mus rau siv lub joystick.

Kauj Ruam 1: Me Me Txog Bluetooth Tsawg Zog (BLE)

Nov tsis yog lub hom phiaj los ua kev qhia ntawm BLE. Thaum kuv pib txoj haujlwm no, Kuv tsis paub qhov sib txawv ntawm BLE thiab Classic Bluetooth. Kuv tsuas yog xav pom yog tias kuv tuaj yeem tau txais tus joystick kuv yuav los ua haujlwm nrog ESP32. Hauv cov ntawv hauv qab no kuv tab tom siv BLE cov ntsiab lus los muab cov lus qhia yooj yim ntawm BLE ua haujlwm li cas.

BLE siv tus neeg siv khoom/server architecture. Ib lub cuab yeej yog server uas muab kev pabcuam. Lwm lub cuab yeej yog tus neeg siv khoom siv cov kev pabcuam. Txhawm rau ua kom lub zog xav tau qis, BLE tsuas yog xa cov ntaub ntawv me me thaum hloov pauv. Nyob rau hauv rooj plaub ntawm tus joystick, lub joystick ntaus ntawv yog tus neeg rau zaub mov. Raws li tus neeg rau zaub mov, nws tshaj tawm nws tus kheej thiab yuav xa cov npe ntawm cov kev pabcuam nws muab thaum nug. Lub joystick ntaus ntawv tshaj tawm tsib qhov kev pabcuam. Tsuas yog cov kev pabcuam uas peb xav tau yog BLE HID (Tib Neeg Cov Khoom Siv Sib Txuas). Qhov kev pabcuam BLE muaj qhov uas paub tias yog Tus Cwj Pwm cuam tshuam nrog nws. Tus yam ntxwv yog feem ntau yog qhov chaw ntawm cov ntaub ntawv. Qhov kev pabcuam HID ntawm tus joystick muaj kaum tus yam ntxwv. Qee tus yam ntxwv yog theej tawm thiab tsis quav ntsej. Peb tsuas yog xav paub txog BLE Cov Cwj Pwm Tus Cwj Pwm uas tau Nyeem thiab Tshaj Tawm lub peev xwm. Peb ntawm cov yam ntxwv ua tau raws cov kev xav tau no thiab muab cov ntaub ntawv hais txog txoj haujlwm ntawm tus joystick thiab lub xeev ntawm cov nyees khawm. Thaum kev ceeb toom tau qhib, tus neeg rau zaub mov yuav xa cov ntaub ntawv ntim thaum hloov pauv tau pom ntawm cov yam ntxwv cuam tshuam.

Lub moj khaum lees paub tias tus neeg rau zaub mov nws pom muaj BLE HID cov kev pabcuam thiab tom qab ntawd yuav pab kom Cov Ntawv Ceeb Toom ntawm peb Daim Ntawv Qhia Tus Cwj Pwm uas muab cov ntaub ntawv joystick thiab khawm lub xeev. Tom qab ntawd, thaum lub pob nyem lossis tso tawm lossis tus joystick tau txav mus, ESP32 tau txais pob ntawv ntawm cov ntaub ntawv qhia nws tias txoj haujlwm joystick tshiab yog dab tsi thiab/lossis lub xeev ntawm qee lub khawm.

Kauj Ruam 2: Kev Ntsuas thiab Kev Ntsuas Kev Sib Txuas

Lub moj khaum txhais ob LEDS, GREENLED thiab BLUELED thiab muab lawv rau ob ntawm ESP32's GPIO pins. GREENLED tau teeb thaum ESP32 tab tom nrhiav rau BLE joystick. Thaum pom tus joystick pom GREENLED raug tua thiab BLUELED tau teeb los qhia tias qhov kev sib txuas tau tsim thiab koj tau npaj mus. Yog tias qhov kev sib txuas tau ploj, BLUELED raug kaw, ESP32 tau rov pib dua, GREENLED tau taws thiab rov pib dua. Yog tias tsis pom tus joystick tsis pub dhau peb caug vib nas this ces scan nres thiab GREENLED tua. Tom qab tsib vib nas this, kev tshuaj xyuas pib dua thiab GREENLED tau qhib.

Qhov kawg tshwm sim yog tias ESP32 tseem yuav tshuaj xyuas lub joystick kom txog thaum nws pom nws. Thaum qhov kev sib txuas tau ua tiav, yog tias nws tau ploj lawm, ESP32 yuav rov pib dua nws tus kheej thiab pib luam dua. ESP32 rov pib dua vim tias tsis muaj ESP32 SDK cov haujlwm los rov pib dua Bluetooth pawg kom rov pib dua.

Kauj Ruam 3: Txiav txim siab Joystick thiab Khawm Txheej Txheem

Ib qho kev hu rov qab ntawm ESP32 tau txais peb daim ntawv sib txawv ntawm cov neeg rau zaub mov rau peb Tus Cwj Pwm uas tau teeb tsa los muab Kev Ceeb Toom. Ib pob ntawv ntev li plaub bytes. Peb ntawm cov bytes muaj txoj haujlwm X axis, Y txoj haujlwm axis thiab cov khawm ua kom pom, uas tau teeb tsa me ntsis hauv byte. Lwm ob pob ntawv yog ob bytes txhua thiab muaj ib qho byte uas muaj lub pob me ntsis khawm lub xeev. Cov pob ntawv tau txais yog txiav txim siab thiab theej rau hauv byte array hauv kev nco. Cov ntaub ntawv joystick axis nkag mus rau X thiab Y cov ntaub ntawv bytes thiab txhua ntawm peb qhov me ntsis mapped khawm bytes tau qhia rau hauv qhov tsim nyog byte rau cov nyees khawm ntawd.

Ib txoj haujlwm FreeRTOS yog tsim los tswj cov ntaub ntawv tau txais los ntawm cov ntawv ceeb toom. Ib txoj haujlwm rau joystick thiab ua kom cov nyees khawm, ib txoj haujlwm rau A & B cov nyees khawm thiab ib txoj haujlwm rau C & D khawm. Txhua qhov ntawm cov haujlwm no tau hais meej thaj chaw uas koj yuav tsum tau ntxiv tus lej txhawm rau ua qhov koj xav tau nrog qhov xwm txheej. Saib rau "// ===== ntxiv koj cov cai ntawm no =====" tawm tswv yim hauv lub cev ntawm txoj haujlwm thiab ntxiv koj cov cai tom qab nws. Txhua txoj haujlwm muaj cov lus hais qhia tias nws tau siv dab tsi thiab siv Serial.println () luam tawm cov lus hais txog qhov xwm txheej uas tshwm sim.

Nov yog qhov piv txwv los ntawm A/B khawm ua haujlwm;

void taskButtonAB (tsis muaj qhov *parameter) {uint8_t nyees khawm;

// ===== Yog tias txoj haujlwm xav tau ib zaug ua ntej pib, muab tso rau ntawm no =====

thaum (tseeb) {// muab CPU, tos rau cov ntaub ntawv tshiab vTaskSuspend (NULL); // peb nyuam qhuav sawv los, cov ntaub ntawv tshiab muaj cov nyees khawm = VrBoxData [VB_BTNAB]; Serial.printf ("A/B Buttons: %02X / n", cov nyees khawm); yog (cov nyees khawm & VB_BUTTON_A) {// khawm A nias lossis tab tom tuav Serial.println ("Button A"); // ===== ntxiv koj cov cai ntawm no =====}

yog (cov nyees khawm & VB_BUTTON_B)

{// khawm B nias lossis tab tom tuav Serial.println ("Button B");

// ===== ntxiv koj cov cai ntawm no =====

}} // rau} // taskButtonAB

Kauj Ruam 4: VR Box Ua Haujlwm: Joystick

Yog hais tias tus joystick tau nyob hauv txoj haujlwm nruab nrab, tsis muaj ntawv ceeb toom rau joystick raug xa mus. Thaum lub joystick raug tshem tawm ntawm qhov chaw, cov lus ceeb toom nrog cov ntaub ntawv joystick thiab ua kom cov ntaub ntawv khawm raug xa tawm txhua txhua 15mS. Thaum lub joystick tau rov qab mus rau qhov chaw, ceeb toom tias nws tau tsiv mus rau qhov chaw tsis raug xa mus. Hauv lwm lo lus, nws qhia koj lub joystick tau txav tawm ntawm qhov chaw, tab sis tsis yog tias nws tau txav mus rau qhov chaw. Qhov kawg tshwm sim yog tias koj tau txais cov lus qhia tias tus joystick tau txav mus rau qhov chaw, tab sis tsis yog tias nws tau mus txog qhov chaw. Txaus siab heev. Ob lub khawm ua haujlwm suav nrog cov ntaub ntawv joystick. Nias ib qho ntawm cov khawm ua tom qab rov qab lub joystick mus rau hauv nruab nrab yuav hloov kho txoj haujlwm joystick rau xoom. Cov moj khaum muaj lub sijhawm siv sijhawm ua-nyob rau hauv nws uas cia li simulates lub joystick ceeb toom cov lus luv luv tom qab txhua tus joystick/txhais khawm ceeb toom cov lus tsis tuaj txog. Lub timer teeb lub joystick rau xoom. Tus joystick muaj ntau txog +/- 25 ntawm txhua lub axis.

Kauj Ruam 5: VR Box Kev Ua Haujlwm: Ua kom khawm

Cov ntsuas khawm yuav xa lus ceeb toom ib zaug thaum nias thiab rov tso dua thaum tso tawm. Cov lus ceeb toom nyem yuav qhia lub pob uas raug nias. Cov lus ceeb toom tso tawm qhia tias ob lub khawm raug tso tawm.

Tuav lub khawm qis qis yuav tiv thaiv tus neeg rau zaub mov los ntawm kev tshawb pom tias lub pob khawm sab saud tau raug nias. Tuav lub khawm ua haujlwm sab saud thiab nias lub pob qis qis ua rau cov neeg rau zaub mov xa xov ceeb toom tias khawm qis ua rau raug nias (lub pob khawm sab saud yuav xoom!). Kev tso lub pob qis qis yuav ua rau cov neeg rau zaub mov xa ntawv ceeb toom tias lub khawm ua haujlwm sab saud raug nias thiab qis dua raug tso tawm.

Hauv lwm lo lus, lub khawm qis qis yog qhov tseem ceeb tshaj lub khawm ua rau sab saud thiab yuav hla nws thaum ob qho tib si raug nias. Koj yuav tsum txiav txim siab li cas los daws qhov teeb meem ntawm ob lub khawm raug nias.

Kauj Ruam 6: VR Box Ua Haujlwm: A/B Nyees khawm

Cov A thiab B khawm ua zoo li lub joystick thiab txuas ntxiv xa cov lus ceeb toom thaum nias thiab tuav cia. Cov lus nres thaum lub pob raug tso tawm. Cov A thiab B cov nyees khawm ua haujlwm zoo ib yam rau Trigger cov nyees khawm uas khawm A ua tus thawj B khawm ib yam li lub pob qis qis ua tus tswj lub pob khawm sab saud.

Kauj Ruam 7: VR Box Kev Ua Haujlwm: C/D Nyees khawm

Cov C thiab D cov nyees khawm xa lus ceeb toom ib zaug thaum nias thiab ib zaug ntxiv thaum tso tawm. Yog tias tuav tseg, tsis muaj xov xwm ntxiv xa mus txog thaum lawv raug tso tawm. Tuav ib qho ntawm C lossis D cov nyees khawm yuav tiv thaiv tus neeg rau zaub mov los ntawm kev ua haujlwm ntawm lwm lub pob.

Kauj Ruam 8: Xaus

Kev ua haujlwm ntawm cov nyees khawm yog hauv kuv lub tswv yim me ntsis yeej. Cov moj khaum muab rau qhov chaw tso koj cov cai los ua thaum lub pob nyem. Yog tias koj tseem xav tau txhawm rau txheeb xyuas cov ntawv tso tawm, uas yog sab laug rau koj kom paub seb yuav ua li cas.

Nws yog tag nrho rau koj los txiav txim siab seb koj xav kom txhua lub khawm ua dab tsi thiab txav lub joystick yuav tsum ua dab tsi. Yuav ua li cas koj lis qhov sib txawv ntawm tus txhais, A & B thiab C & D cov nyees khawm yog nyob ntawm koj.

Saib hauv qhov chaws rau; taskJoyStick (), taskButtonAB (), taskButtonCD () ua haujlwm thiab ntxiv koj cov cai tom qab "// ===== ntxiv koj cov cai ntawm no =====" tawm tswv yim.

Koj yuav xav tau txog plaub lub luag haujlwm los tuav lub joystick (rau pem hauv ntej, rov qab, sab xis & sab laug) thiab txog rau rau lub luag haujlwm los lis cov khawm sib txawv. Siv lawv tag nrho lossis tsuas yog qhov koj xav tau. Qhov kev xaiv yog koj li.

Yog tias koj siv lub moj khaum no. Kuv yuav nyiam qw nrov nrov txog qhov koj tau siv rau thiab yog tias koj pom nws yooj yim siv.

Yog tias koj muaj lus nug txog nws ua haujlwm lossis xav tau kev pab kom nws ua haujlwm, hu rau kuv.

Cov cai muaj nyob ntawm GitHub ntawm no.

Txaus siab rau.

Pom zoo: