Cov txheej txheem:

Yuav Ua Li Cas Ntau ESP Tham Nrog ESP-TAM SIM NO Siv ESP32 thiab ESP8266: 8 Cov Kauj Ruam
Yuav Ua Li Cas Ntau ESP Tham Nrog ESP-TAM SIM NO Siv ESP32 thiab ESP8266: 8 Cov Kauj Ruam

Video: Yuav Ua Li Cas Ntau ESP Tham Nrog ESP-TAM SIM NO Siv ESP32 thiab ESP8266: 8 Cov Kauj Ruam

Video: Yuav Ua Li Cas Ntau ESP Tham Nrog ESP-TAM SIM NO Siv ESP32 thiab ESP8266: 8 Cov Kauj Ruam
Video: Qhia thov kyc, thiab yuav muag nyiaj pi li cas. 2024, Hlis ntuj nqeg
Anonim
Yuav Ua Li Cas Ntau ESP Tham Nrog ESP-TAM SIM NO Siv ESP32 thiab ESP8266
Yuav Ua Li Cas Ntau ESP Tham Nrog ESP-TAM SIM NO Siv ESP32 thiab ESP8266

Ntawm kuv qhov haujlwm txuas ntxiv, Kuv xav tau ntau ESP los tham nrog txhua tus yam tsis muaj lub router. Txhawm rau ua qhov no, Kuv yuav siv ESP-TAM SIM NO los ua kev sib txuas lus wireless nrog txhua tus yam tsis muaj lub router ntawm ESP.

Khoom siv

Yam Kuv Siv:

ESP32 DEV Module

NODEMCU 1.0 (ESP12E Module)

Kauj ruam 1: Tau txais Board Mac Chaw Nyob

Tau txais Board Mac Chaw Nyob
Tau txais Board Mac Chaw Nyob
Tau txais Board Mac Chaw Nyob
Tau txais Board Mac Chaw Nyob

Los ntawm ESP-tam sim no, ESP cov cuab yeej sib tham sib tham los ntawm kev xa cov ntaub ntawv mus rau lawv qhov chaw nyob tshwj xeeb thaum txuas nrog rau sab hauv qhov chaw sib txuas ua ke tau ua raws kev nkag siab tshwj xeeb tam sim no.. Yog li, txiav txim siab MAC chaw nyob ntawm txhua lub cuab yeej txuas nrog yog kuv li ESP32 thiab ESP8266 Pawg Chaw

Rau ESP32

#suav nrog "WiFi.h" // Txhawm rau nkag mus rau ESP32 WIFI lub peev xwm

teeb tsa tsis muaj dab tsi () {Serial.begin (115200); Serial.print ("ESP32 Board MAC Chaw Nyob:"); Serial.println (WiFi.macAddress ()); // luam tawm nws MAC Chaw Nyob} void loop () {}

Rau ESP8266

#suav nrog // Lub tsev qiv ntawv siv nkag mus rau ESP8266 WIFI lub peev xwm

teeb tsa tsis muaj dab tsi () {Serial.begin (115200); Serial.println (); Serial.print ("ESP8266 Board MAC Chaw Nyob:"); Serial.println (WiFi.macAddress ()); // luam tawm nws MAC Chaw Nyob} void loop () {}

Kuv MAC chaw nyob yog:

  • ESP32 - 30: AE: A4: F5: 03: A4
  • ESP8266: A4: CF: 12: C7: 9C: 77

Kauj Ruam 2: Yuav Ua Li Cas ESP-TAM SIM NO Ua Haujlwm

Nov yog cov ntsiab lus hais txog yuav ua li cas thiaj ua haujlwm:

  1. Xws li esp tam sim no thiab cov tsev qiv ntawv wifi
  2. Txuag mac chaw nyob ntawm tus neeg txais ESP
  3. Txhais cov qauv ntaub ntawv ntawm cov lus xa/tau txais
  4. Ntawm qhov teeb tsa, teeb lub wifi rau hom chaw nres tsheb
  5. Pib ua esp_now
  6. ua thiab sau npe rov qab ua haujlwm hu ua tom qab xa thiab tau txais cov ntaub ntawv
  7. Rau Esp8266, txhais nws lub luag haujlwm
  8. sau npe tus phooj ywg lossis tus tau txais esp
  9. Xa cov ntaub ntawv

Kauj Ruam 3: ESP-TAM SIM NO FUNCTIONS (ESP32)

esp_now_init (tsis muaj dab tsi)

Rov qab los:

  • ESP_OK: ua tiav
  • ESP_ERR_ESPNOW_INTERNAL: Sab hauv yuam kev

Nqe lus piav qhia:

Pib ua haujlwm ESPNOW

esp_now_register_send_cb (cb)

Rov qab los:

  • ESP_OK: ua tiav
  • ESP_ERR_ESPNOW_NOT_INIT: ESPNOW tsis tau pib ua ntej
  • ESP_ERR_ESPNOW_INTERNAL: sab hauv yuam kev

Tsis:

  • cb: hu rov qab lub npe tom qab xa ESPNOW cov ntaub ntawv nrog cov ntsuas no:

    • void cb (const uint8_t *mac_addr, esp_now_send_status_t xwm txheej)

      • mac_addr: mac chaw nyob ntawm tus txais
      • xwm txheej:

        • 1 = kev vam meej
        • 0 = tsis tau

Nqe lus piav qhia:

Hu rau qhov ua haujlwm OnDataSent tom qab xa ESPNOW cov ntaub ntawv

esp_now_add_peerconst esp_now_peer_info_t *phooj ywg)

Rov qab los:

  • ESP_OK: ua tiav
  • ESP_ERR_ESPNOW_NOT_INIT: ESPNOW tsis tau pib ua ntej
  • ESP_ERR_ESPNOW_ARG: kev sib cav tsis raug
  • ESP_ERR_ESPNOW_FULL: daim ntawv teev cov phooj ywg puv lawm
  • ESP_ERR_ESPNOW_NO_MEM: tsis nco
  • ESP_ERR_ESPNOW_EXIST: phooj ywg tau muaj

Tsis:

  • phooj ywg: phooj ywg cov ntaub ntawv nrog cov ntaub ntawv hauv qab no:

    • ua 8_t

      peer_addr [ESP_NOW_ETH_ALEN]; ESPNOW phooj ywg MAC chaw nyob uas tseem yog MAC chaw nyob ntawm chaw nres tsheb lossis muag muag

    • uint8_t lmk [ESP_NOW_KEY_LEN]

      ESPNOW tus phooj ywg hauv zos tus tswv tseem ceeb uas yog siv los encrypt cov ntaub ntawv

    • uint8_t channel

      Wi-nkaus channel uas cov phooj ywg siv xa/tau txais ESPNOW cov ntaub ntawv. Yog tias tus nqi yog 0, siv cov channel tam sim no uas chaw nres tsheb lossis softap nyob. Txwv tsis pub, nws yuav tsum tau teeb raws li cov channel uas chaw nres tsheb lossis softap nyob

    • wifi_interface_t ifidx

      Wi-nkaus interface uas cov phooj ywg siv xa/tau txais ESPNOW cov ntaub ntawv

    • bool encrypt cov

      ESPNOW cov ntaub ntawv uas tus phooj ywg no xa/tau txais yog encrypted lossis tsis

    • void *cov

      ESPNOW phooj ywg cov ntaub ntawv ntiag tug

Nqe lus piav qhia:

Ntxiv ib tus phooj ywg rau phooj ywg npe

esp_now_send (const uint8_t *peer_addr, const uint8_t *cov ntaub ntawv, size_t len)

Rov qab los:

  • ESP_OK: ua tiav
  • ESP_ERR_ESPNOW_NOT_INIT: ESPNOW tsis tau pib ua ntej
  • ESP_ERR_ESPNOW_ARG: kev sib cav tsis raug
  • ESP_ERR_ESPNOW_INTERNAL: sab hauv yuam kev
  • ESP_ERR_ESPNOW_NO_MEM: tsis nco
  • ESP_ERR_ESPNOW_NOT_FOUND: tsis pom tus phooj ywg
  • ESP_ERR_ESPNOW_IF: tam sim no WiFi interface tsis sib xws ntawm cov phooj ywg

Tsis:

  • peer_addr: peer MAC chaw nyob
  • cov ntaub ntawv: cov ntaub ntawv xa
  • len: ntev ntawm cov ntaub ntawv

Nqe lus piav qhia:

Xa ESPNOW cov ntaub ntawv. Rau qee kis, qhov no tshwm sim:

  • Yog peer_addr tsis yog NULL, xa cov ntaub ntawv mus rau tus phooj ywg uas nws MAC chaw nyob sib phim peer_addr
  • Yog tias peer_addr yog NULL, xa cov ntaub ntawv rau txhua tus phooj ywg uas tau ntxiv rau hauv cov npe phooj ywg
  • Qhov ntev tshaj plaws ntawm cov ntaub ntawv yuav tsum tsawg dua ESP_NOW_MAX_DATA_LEN
  • Qhov tsis tau taw qhia los ntawm cov ntaub ntawv sib cav tsis tas yuav siv tau tom qab esp_now_send xa rov qab

esp_now_register_recv_cb (cb)

Rov qab los:

  • ESP_OK: ua tiav
  • ESP_ERR_ESPNOW_NOT_INIT: ESPNOW tsis tau pib ua ntej
  • ESP_ERR_ESPNOW_INTERNAL: sab hauv yuam kev

Tsis:

  • cb: hu rov qab ua haujlwm kom tau txais ESPNOW cov ntaub ntawv

    • void cb (const uint8_t *mac_addr, const uint8_t *cov ntaub ntawv, int data_len)

      • mac_addr:

        mac chaw nyob ntawm tus txais

      • *cov ntaub ntawv:

        cov ntaub ntawv tau txais

      • cov_len

        cov ntaub ntawv byte ntev

Nqe lus piav qhia:

Hu rau lub luag haujlwm cb tom qab tau txais cov ntaub ntawv ESPNOW

Kauj Ruam 4: ESP-TAM SIM NO FUNCTIONS (ESP8266)

FUNCTIONS DESCRIPTION ESP32 ESP8266

int esp_now_init (tsis muaj dab tsi)

Rov qab los:

  • 1 = kev vam meej
  • 0 = tsis tau

Nqe lus piav qhia

Pib ua haujlwm ESPNOW

int esp_now_set_self_role (u8 lub luag haujlwm)

Tsis:

  • ESP_NOW_ROLE_IDLE: tsis pub xa cov ntaub ntawv.
  • ESP_NOW_ROLE_CONTROLLER: qhov muaj feem thib yog muab rau Sation interface
  • ESP_NOW_ROLE_SLAVE: qhov muaj feem thib yog muab rau SoftAP interface
  • ESP_NOW_ROLE_COMBO: qhov muaj feem thib yog muab rau SoftAPinterface

Nqe lus piav qhia

Teeb lub cuab yeej lub luag haujlwm

koj esp_now_register_send_cb (cb)

Rov qab los:

  • 1 = kev vam meej
  • 0 = tsis tau

Tsis:

  • cb: hu rov qab ua haujlwm lub npe tom qab xa ESPNOW cov ntaub ntawv nrog qhov ntsuas no:

    • void cb (const uint8_t *mac_addr, esp_now_send_status_t xwm txheej)

      • mac_addr: mac chaw nyob ntawm tus txais
      • xwm txheej:

        • 1 = kev vam meej
        • 0 = tsis tau

Nqe lus piav qhia

Hu rau qhov ua haujlwm OnDataSent tom qab xa ESPNOW cov ntaub ntawv

int esp_now_add_peer (u8 *mac_addr, u8 lub luag haujlwm, u8 channel, u8 *qhov tseem ceeb, u8 key_len)

Rov qab los:

  • 1 = kev vam meej
  • 0 = tsis tau

Tsis:

  • mac_addr

    mac address ntawm peer

  • lub luag hauj lwm
  • channel

    Yog tias tus nqi yog 0, siv cov channel tam sim no uas chaw nres tsheb lossis softap nyob. Txwv tsis pub, nws yuav tsum tau teeb raws li cov channel uas chaw nres tsheb lossis softap nyob

  • *tus yuam sij

    tus yuam sij rau encryption

  • key_len

    qhov ntev ntawm qhov tseem ceeb

Nqe lus piav qhia:

Ntxiv ib tus phooj ywg rau phooj ywg npe

int esp_now_send (const uint8_t *peer_addr, const uint8_t *cov ntaub ntawv, size_t len)

Rov qab los:

  • 1 = Kev vam meej
  • 0 = Ua tsis tau

Tsis:

  • peer_addr: peer MAC chaw nyob
  • cov ntaub ntawv: cov ntaub ntawv xa
  • len: ntev ntawm cov ntaub ntawv

Nqe lus piav qhia:

Xa ESPNOW cov ntaub ntawv. Rau qee kis, qhov no tshwm sim:

  • Yog peer_addr tsis yog NULL, xa cov ntaub ntawv mus rau tus phooj ywg uas nws MAC chaw nyob sib phim peer_addr
  • Yog tias peer_addr yog NULL, xa cov ntaub ntawv rau txhua tus phooj ywg uas tau ntxiv rau hauv cov npe phooj ywg
  • Qhov ntev tshaj plaws ntawm cov ntaub ntawv yuav tsum tsawg dua ESP_NOW_MAX_DATA_LEN
  • Qhov tsis tau taw qhia los ntawm cov ntaub ntawv sib cav tsis tas yuav siv tau tom qab esp_now_send xa rov qab

int esp_now_register_recv_cb (cb)

Rov qab los:

  • 1 = Kev vam meej
  • 0 = Ua tsis tau

Tsis:

  • cb: hu rov qab ua haujlwm kom tau txais ESPNOW cov ntaub ntawv

    • void cb (const uint8_t *mac_addr, const uint8_t *cov ntaub ntawv, int data_len)

      • mac_addr:

        mac chaw nyob ntawm tus txais

      • *cov ntaub ntawv:

        cov ntaub ntawv tau txais

      • cov_len

        cov ntaub ntawv byte ntev

Nqe lus piav qhia:

Hu rau lub luag haujlwm cb tom qab tau txais cov ntaub ntawv ESPNOW

Kauj Ruam 5: Ib Txoj Kev Sib Txuas Lus (ESP32 Raws Li Xa)

ESP32 xa cov ntaub ntawv mus rau ESP8266. nrog txoj cai no. Hloov qhov broadcastAddress rau koj tus neeg teb xov tooj txais mac chaw nyob. Kuv yog A4: CF: 12: C7: 9C: 77

// Ntxiv cov tsev qiv ntawv tsim nyog

#include // Txhawm rau nkag mus rau esp tam sim no ua haujlwm #include // Ntxiv Wifi Peev Xwm ntawm ESP32 // txuag MAC Chaw Nyob hauv ib qho npe hu ua broadcastAddress; uint8_t broadcastAddress = {0xA4, 0xCF, 0x12, 0xC7, 0x9C, 0x77}; // MAC chaw nyob ntawm kuv tus neeg txais/*txheeb xyuas cov ntaub ntawv hom ntawm ntau yam kev hloov pauv teeb tsa thiab hloov npe tag nrho nws li struct_message*/typedef struct struct_message {char a [32]; tus b; ploj c; Txoj hlua d; kub e; } struct_message; // Tsim cov struct_message hu ua myData struct_message myData; // muaj nuj nqi hu thaum cov ntaub ntawv raug xa mus luam nws cov xwm txheej tsis muaj dab tsi OnDataSent (const uint8_t *mac_addr, esp_now_send_status_t xwm txheej) {Serial.print ("\ r / nLast Packet Send Status: / t"); Serial.println (xwm txheej == ESP_NOW_SEND_SUCCESS? } tsis muaj teeb tsa teeb tsa () {// Teeb tsa tus nqi baud rau kev sib txuas lus txuas nrog ESP Serial.begin (115200); // Teeb tsa lub cuab yeej ua Wi-Fi Chaw Nres Tsheb WiFi.mode (WIFI_STA); // Pib wifi // Init ESP-TAM SIM NO thiab rov qab nws cov xwm txheej yog (esp_now_init ()! = ESP_OK) {Serial.println ("Yuam kev pib ESP -NOW "); rov qab; } // hu ua haujlwm OnDataSent tom qab xa ESPNOW cov ntaub ntawv esp_now_register_send_cb (OnDataSent); // Sau npe tus phooj ywg esp_now_peer_info_t peerInfo; // pib thiab muab cov ntaub ntawv tus phooj ywg ua tus taw qhia rau tus addres memcpy (peerInfo.peer_addr, broadcastAddress, 6); // luam tus nqi ntawm broadcastAddress nrog 6 bytes rau peerInfo.peer_addr peerInfo.channel = 0; // channel uas esp tham. 0 txhais tau tias tsis tau txhais thiab cov ntaub ntawv yuav raug xa mus rau cov channel tam sim no. 1-14 yog cov channel raug uas zoo ib yam nrog cov khoom siv hauv cheeb tsam peerInfo.encrypt = tsis tseeb; // tsis encrypted // Ntxiv cov cuab yeej rau cov khoom siv ua ke yog (esp_now_add_peer (& peerInfo)! = ESP_OK) {Serial.println ("Ua tsis tau ntxiv cov phooj ywg"); rov qab; }} void loop () {// Teem qhov tseem ceeb kom xa strcpy (myData.a, "Qhov no yog CHAR"); // txuag "Qhov no yog CHAR" kom hloov pauv ntawm kuv "cov ntaub ntawv" txhais ua ntej myData.b = random (1, 20); // txuag tus nqi random myData.c = 1.2; // txuag ntab myData.d = "Nyob Zoo"; // txuag txoj hlua myData.e = cuav; // txuag bool // Xa cov ntaub ntawv tsawg dua lossis sib npaug 250 bytes ntawm ESP-TAM SIM NO thiab rov qab nws cov xwm txheej esp_err_t tshwm sim = esp_now_send (broadcastAddress, (uint8_t *) & myData, sizeof (myData)); yog (qhov tshwm sim == ESP_OK) {Serial.println ("Xa nrog kev ua tiav"); } lwm {Serial.println ("Yuam kev xa cov ntaub ntawv"); } ncua (2000); }

ESP8266 tau txais cov ntaub ntawv los ntawm ESP32 siv tus lej no.

// Ntxiv cov tsev qiv ntawv tsim nyog

#include // Txhawm rau Ntxiv Kev Muaj Peev Xwm Wifi ntawm ESP32 #include // Txhawm rau nkag mus rau esp tam sim no ua haujlwm /*txheeb xyuas cov hom ntaub ntawv ntawm ntau qhov kev hloov pauv teeb tsa thiab hloov npe tag nrho nws li struct_message* /typedef struct struct_message {char a [32]; tus b; ploj c; Txoj hlua d; kub e; } struct_message; // Tsim qhov sib txawv struct_message hu ua myData struct_message myData; // muaj nuj nqi hu ua thaum cov ntaub ntawv tau txais thiab luam tawm nws tsis muaj dab tsi OnDataRecv (uint8_t * mac, uint8_t * khoom xa tuaj, uint8_t len) {memcpy (& myData, incomingData, sizeof (myData))); Serial.print ("Bytes tau txais:"); Serial.println (len); Serial.print ("Char:"); Serial.println (myData.a); Serial.print ("Int:"); Serial.println (myData.b); Serial.print ("Ntab:"); Serial.println (myData.c); Serial.print ("Txoj hlua:"); Serial.println (myData.d); Serial.print ("Bool:"); Serial.println (myData.e); Serial.println (); } tsis muaj teeb tsa teeb tsa () {// Teev tus nqi baud rau kev sib txuas lus nrog ESP Serial.begin (115200); // Teeb tsa lub cuab yeej ua chaw nres tsheb Wi-nkaus WiFi.mode (WIFI_STA); // Pib wifi // Init ESP-NOW thiab rov qab nws cov xwm txheej yog (esp_now_init ()! = 0) {Serial.println ("Yuam kev pib ESP-NOW"); rov qab; } esp_now_set_self_role (ESP_NOW_ROLE_SLAVE); // Txhais lub luag haujlwm ntawm qhov no esp_now_register_recv_cb (OnDataRecv); // hu rau qhov ua haujlwm OnDataRecv tom qab tau txais ESPNOW cov ntaub ntawv} void loop () {}

Kauj Ruam 6: Ib Txoj Kev Sib Txuas Lus (ESP8266 Raws Li Xa)

ESP8266 xa cov ntaub ntawv mus rau ESP32. nrog txoj cai no. Hloov qhov broadcastAddress rau koj tus neeg teb xov tooj txais mac chaw nyob. Kuv qhov chaw nyob esp32 yog 30: AE: A4: F5: 03: A4. Rau lwm txoj haujlwm rau esp8266 mus ntawm no

// Ntxiv cov tsev qiv ntawv tsim nyog

#include // Txhawm rau Ntxiv Kev Muaj Peev Xwm Wifi ntawm ESP32 #include // Txhawm rau nkag mus rau esp tam sim no ua haujlwm // txuag MAC Chaw Nyob hauv kab lus uas muaj npe broadcastAddress; uint8_t broadcastAddress = {0x30, 0xAE, 0xA4, 0xF5, 0x03, 0xA4}; /*txhais cov hom ntaub ntawv ntawm ntau qhov kev hloov pauv hloov pauv thiab hloov pauv tag nrho nws li struct_message*/ typedef struct struct_message {char a [32]; tus b; ploj c; Txoj hlua d; kub e; } struct_message; // Tsim cov qauv sib txawv hu ua myData struct_message myData; // muaj nuj nqi hu thaum cov ntaub ntawv raug xa thiab luam nws cov xwm txheej tsis muaj dab tsi OnDataSent (uint8_t *mac_addr, uint8_t xaStatus) {Serial.print ("\ r / nLast Packet Send Status: / t"); Serial.println (sendStatus == 1? "Kev xa khoom tau zoo": "Kev Xa Tsis Tuaj"); } tsis muaj teeb tsa teeb tsa () {// Teev tus nqi baud rau kev sib txuas lus nrog ESP Serial.begin (115200); // Teeb tsa lub cuab yeej ua Wi-Fi Chaw Nres Tsheb WiFi.mode (WIFI_STA); // Pib lub wifi // Pib ESP-TAM SIM NO thiab xa nws cov xwm txheej yog (esp_now_init ()) {Serial.println ("Yuam kev pib ESP-NOW"); rov qab; } esp_now_register_send_cb (OnDataSent); // hu lub luag haujlwm OnDataSent tom qab xa ESPNOW cov ntaub ntawv // Ntxiv cov cuab yeej rau cov npe khoom ua ke yog (esp_now_add_peer (broadcastAddress, ESP_NOW_ROLE_CONTROLLER, 1, NULL, 0)) {Serial.println ("Ua tsis tau ntxiv cov phooj ywg"); rov qab; }} void loop () {// Teem qhov tseem ceeb kom xa strcpy (myData.a, "Qhov no yog CHAR"); // txuag "Qhov no yog CHAR" kom hloov pauv ntawm kuv "cov ntaub ntawv" txhais ua ntej myData.b = random (1, 20); // txuag tus nqi random myData.c = 1.2; // txuag ntab myData.d = "SP8266"; // txuag txoj hlua myData.e = cuav; // txuag bool // Xa cov ntaub ntawv tsawg dua lossis sib npaug 250 bytes ntawm ESP-TAM SIM NO thiab rov qab nws cov xwm txheej int tshwm sim = esp_now_send (broadcastAddress, (uint8_t *) & myData, sizeof (myData)); yog (esp_now_init ()! = 0) {Serial.println ("Xa nrog kev ua tiav"); } lwm {Serial.println ("Yuam kev xa cov ntaub ntawv"); } ncua (2000); }

ESP32 tau txais cov ntaub ntawv los ntawm ESP8266. nrog txoj cai no. Rau lwm lub luag haujlwm hais txog ntawm no

// Ntxiv cov tsev qiv ntawv tsim nyog

#include // Txhawm rau nkag mus rau esp tam sim no ua haujlwm #include // Ntxiv Wifi Peev Xwm ntawm ESP32 /*txheeb xyuas cov hom ntaub ntawv ntawm ntau qhov kev hloov pauv teeb tsa thiab hloov npe tag nrho nws li struct_message* /typedef struct struct_message {char a [32]; tus b; ploj c; Txoj hlua d; kub e; } struct_message; // Tsim qhov sib txawv struct_message hu ua myData struct_message myData; // muaj nuj nqi hu thaum cov ntaub ntawv tau txais thiab luam nws tsis muaj dab tsi OnDataRecv (const uint8_t * mac, const uint8_t * incomingData, int len) {memcpy (& myData, incomingData, sizeof (myData)); Serial.print ("Bytes tau txais:"); Serial.println (len); Serial.print ("Char:"); Serial.println (myData.a); Serial.print ("Int:"); Serial.println (myData.b); Serial.print ("Ntab:"); Serial.println (myData.c); Serial.print ("Txoj hlua:"); Serial.println (myData.d); Serial.print ("Bool:"); Serial.println (myData.e); Serial.println (); } tsis muaj teeb tsa teeb tsa () {// Teev tus nqi baud rau kev sib txuas lus nrog ESP Serial.begin (115200); // Teeb tsa lub cuab yeej ua chaw nres tsheb Wi-nkaus WiFi.mode (WIFI_STA); // Pib wifi // Init ESP-NOW thiab rov qab nws cov xwm txheej yog (esp_now_init ()! = 0) {Serial.println ("Yuam kev pib ESP-NOW"); rov qab; } esp_now_register_recv_cb (OnDataRecv); // hu rau qhov ua haujlwm OnDataRecv tom qab tau txais ESPNOW cov ntaub ntawv} void loop () {}

Kauj Ruam 7: Ob Txoj Kev Sib Tham

Ob Txoj Kev Sib Tham
Ob Txoj Kev Sib Tham
Ob Txoj Kev Sib Tham
Ob Txoj Kev Sib Tham

ESP32 xa cov ntaub ntawv ntawm kev pib rau ESP8266. ESP8266 luam tawm cov ntawv tau txais thiab tom qab ntawv teb uas ESP32 luam tawm ntawm nws cov ntawv xov xwm.

ESP32 CODE NPE

// Ntxiv cov tsev qiv ntawv tsim nyog

#include // Txhawm rau nkag mus rau esp tam sim no ua haujlwm #include // Ntxiv Wifi Peev Xwm ntawm ESP32 // txuag MAC Chaw Nyob hauv ib qho npe hu ua broadcastAddress; uint8_t broadcastAddress = {0xA4, 0xCF, 0x12, 0xC7, 0x9C, 0x77}; // MAC chaw nyob ntawm kuv tus neeg txais/*txheeb xyuas cov ntaub ntawv hom ntawm ntau yam kev hloov pauv teeb tsa thiab hloov npe tag nrho nws li struct_message*/typedef struct struct_message {char a [32]; tus b; ploj c; Txoj hlua d; kub e; } struct_message; // Tsim cov struct_message hu ua myData struct_message myData; // muaj nuj nqi hu thaum cov ntaub ntawv raug xa mus luam nws cov xwm txheej tsis muaj dab tsi OnDataSent (const uint8_t *mac_addr, esp_now_send_status_t xwm txheej) {Serial.print ("\ r / nLast Packet Send Status: / t"); Serial.println (xwm txheej == ESP_NOW_SEND_SUCCESS? yog (xwm txheej! = ESP_NOW_SEND_SUCCESS) {xa_data ();}} tsis muaj dab tsi OnDataRecv (const uint8_t * mac, const uint8_t * incomingData, int len) {memcpy (& myData, incomingData, sizeof (myData)); Serial.print ("Bytes tau txais:"); Serial.println (len); Serial.print ("Char:"); Serial.println (myData.a); Serial.print ("Int:"); Serial.println (myData.b); Serial.print ("Ntab:"); Serial.println (myData.c); Serial.print ("Txoj hlua:"); Serial.println (myData.d); Serial.print ("Bool:"); Serial.println (myData.e); Serial.println (); } tsis muaj teeb tsa teeb tsa () {// Teev tus nqi baud rau kev sib txuas lus txuas nrog ESP Serial.begin (115200); // Teeb tsa lub cuab yeej ua Wi-Fi Chaw Nres Tsheb WiFi.mode (WIFI_STA); // Pib wifi // Init ESP-TAM SIM NO thiab xa nws cov xwm txheej yog (esp_now_init ()! = ESP_OK) {Serial.println ("Yuam kev pib ESP -NOW "); rov qab; } // hu ua haujlwm OnDataSent tom qab xa ESPNOW cov ntaub ntawv esp_now_register_send_cb (OnDataSent); // Sau npe tus phooj ywg esp_now_peer_info_t peerInfo; // pib thiab muab cov ntaub ntawv tus phooj ywg ua tus taw qhia rau tus addres memcpy (peerInfo.peer_addr, broadcastAddress, 6); // luam tus nqi ntawm broadcastAddress nrog 6 bytes rau peerInfo.peer_addr peerInfo.channel = 0; // channel uas esp tham. 0 txhais tau tias tsis tau txhais thiab cov ntaub ntawv yuav raug xa mus rau cov channel tam sim no.1-14 yog cov channel raug uas zoo ib yam nrog cov khoom siv hauv cheeb tsam peerInfo.encrypt = tsis tseeb; // tsis encrypted // Ntxiv cov cuab yeej rau cov khoom siv ua ke yog (esp_now_add_peer (& peerInfo)! = ESP_OK) {Serial.println ("Ua tsis tau ntxiv cov phooj ywg"); rov qab; } esp_now_register_recv_cb (OnDataRecv); // hu ua haujlwm OnDataRecv tom qab tau txais ESPNOW cov ntaub ntawv xa_data (); } void loop () {} void send_data () {Serial.println ("Xa")); // Teeb qhov tseem ceeb xa strcpy (myData.a, "Qhov no yog CHAR"); // txuag "Qhov no yog CHAR" kom hloov pauv ntawm kuv "cov ntaub ntawv" txhais ua ntej myData.b = random (1, 20); // txuag tus nqi random myData.c = 1.2; // txuag ntab myData.d = "ESP32"; // txuag txoj hlua myData.e = cuav; // txuag bool // Xa cov ntaub ntawv tsawg dua lossis sib npaug 250 bytes ntawm ESP-TAM SIM NO thiab rov qab nws cov xwm txheej esp_err_t tshwm sim = esp_now_send (broadcastAddress, (uint8_t *) & myData, sizeof (myData)); yog (qhov tshwm sim == ESP_OK) {Serial.println ("Xa nrog ua tiav");} lwm yam {Serial.println ("Yuam kev xa cov ntaub ntawv"); }}

ESP8266 CODE

// Ntxiv cov tsev qiv ntawv tsim nyog

#include // Txhawm rau Ntxiv Kev Muaj Peev Xwm Wifi ntawm ESP32 #include // Txhawm rau nkag mus rau esp tam sim no ua haujlwm // txuag MAC Chaw Nyob hauv kab lus uas muaj npe broadcastAddress; uint8_t broadcastAddress = {0x30, 0xAE, 0xA4, 0xF5, 0x03, 0xA4}; /*txhais cov hom ntaub ntawv ntawm ntau qhov kev hloov pauv hloov pauv thiab hloov pauv tag nrho nws li struct_message*/ typedef struct struct_message {char a [32]; tus b; ploj c; Txoj hlua d; kub e; } struct_message; // Tsim qhov sib txawv struct_message hu ua myData struct_message myData; // muaj nuj nqi hu ua thaum cov ntaub ntawv tau txais thiab luam tawm nws tsis muaj dab tsi OnDataRecv (uint8_t * mac, uint8_t * khoom xa tuaj, uint8_t len) {memcpy (& myData, incomingData, sizeof (myData))); Serial.print ("Bytes tau txais:"); Serial.println (len); Serial.print ("Char:"); Serial.println (myData.a); Serial.print ("Int:"); Serial.println (myData.b); Serial.print ("Ntab:"); Serial.println (myData.c); Serial.print ("Txoj hlua:"); Serial.println (myData.d); Serial.print ("Bool:"); Serial.println (myData.e); Serial.println (); xa_data (); } tsis muaj dabtsis OnDataSent (uint8_t *mac_addr, uint8_t sendStatus) {Serial.print ("\ r / nLub pob ntawv xa xwm txheej: / t"); Serial.println (sendStatus == 1? "Kev xa khoom tau zoo": "Kev Xa Tsis Tuaj"); yog (sendStatus! = 1) {send_data (); }} tsis muaj dab tsi xa_data () {// Teem qhov tseem ceeb xa strcpy (myData.a, "Qhov no yog CHAR"); // txuag "Qhov no yog CHAR" kom hloov pauv ntawm kuv "cov ntaub ntawv" txhais ua ntej myData.b = random (1, 20); // txuag tus nqi random myData.c = 1.2; // txuag ntab myData.d = "ESP8266"; // txuag txoj hlua myData.e = cuav; // txuag bool esp_now_send (broadcastAddress, (uint8_t *) & myData, sizeof (myData)); } tsis muaj teeb tsa teeb tsa () {// Teev tus nqi baud rau kev sib txuas lus txuas nrog ESP Serial.begin (115200); // Teeb tsa lub cuab yeej ua chaw nres tsheb Wi-nkaus WiFi.mode (WIFI_STA); // Pib wifi // Init ESP-NOW thiab rov qab nws cov xwm txheej yog (esp_now_init ()! = 0) {Serial.println ("Yuam kev pib ESP-NOW"); rov qab; } yog (esp_now_add_peer (broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0)) {Serial.println ("Ua tsis tau ntxiv cov phooj ywg"); rov qab; } esp_now_set_self_role (ESP_NOW_ROLE_COMBO); esp_now_register_send_cb (OnDataSent); esp_now_set_self_role (ESP_NOW_ROLE_COMBO); // Txhais lub luag haujlwm ntawm qhov no esp_now_register_recv_cb (OnDataRecv); // hu rau qhov ua haujlwm OnDataRecv tom qab tau txais ESPNOW cov ntaub ntawv} void loop () {}

Kauj Ruam 8: NYEEM NTAWV

ESPNOW_32_Example

ESPNOW_8266 Piv txwv

WIFI.h

ESP8266WiFi.h

esp_now.h rau ESP8266

esp_now.h rau ESP32

esp_now cov ntaub ntawv raug cai (Kev piav qhia zoo dua ntawm kev ua haujlwm)

ESP-TAM SIM NO Cov Lus Qhia Tseem Ceeb

Pom zoo: