esp_now_实例代码

参考链接外

参考链接转载

接受端:

#include <esp_now.h>
#include <WiFi.h>

//IP address: 192.168.4.1
//mac Address: 40:91:51:A1:9F:E9


typedef struct struct_message {
  char a[32];
  int b;
  float c;
  String d;
  bool e;
} struct_message;



void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 
{
  struct_message myData;
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("Char: ");
  Serial.println(myData.a);
  Serial.print("Int: ");
  Serial.println(myData.b);
  Serial.print("Float: ");
  Serial.println(myData.c);
  Serial.print("String: ");
  Serial.println(myData.d);
  Serial.print("Bool: ");
  Serial.println(myData.e);
  Serial.println();
  }

 

void setup()
{
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);

  if (esp_now_init() != ESP_OK) {
  Serial.println("Error initializing ESP-NOW");
  return;
}

  
}

void loop()
{
  delay(100);
   esp_now_register_recv_cb(OnDataRecv);
  

  Serial.print("mac Address: ");
  Serial.println(WiFi.macAddress()); //打印mac地址
}

1.不需要添加github上的espnow库,其是为idf设计的

2.注意打印mac的函数,不要用ap函数,此代码为在sta工作模式下的连接,不需要连接连接到网络

发送端

#include <WiFi.h>
#include <esp_now.h>


// REPLACE  MAC Address1
//mac Address: 40:91:51:A1:9F:E8

uint8_t broadcastAddress[] = {0x40, 0x91, 0x51, 0xA1, 0x9F, 0xE8};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  String d;
  bool e;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("rnLast Packet Send Status:t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}


void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.mode(WIFI_STA);

  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  esp_now_register_send_cb(OnDataSent);



  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.ifidx = WIFI_IF_STA;
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  //注册僚机mac信息

  // Add peer
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }

  Serial.println(esp_now_add_peer(&peerInfo));
  //  Peer interface is invalid

  Serial.print("mac Address: ");
  Serial.println(WiFi.macAddress()); //打印mac地址

}

void loop()
{
  // Set values to send
  strcpy(myData.a, "THIS IS A CHAR");
  myData.b = random(1, 20);
  myData.c = 1.2;
  myData.d = "Hello";
  myData.e = false;

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(100);
}

1.提入的mac地址为接收端地址

2.注意两个esp的工作模式,以下代码不添加时无法发送成功,显示 peer interface invalid

  peerInfo.ifidx = WIFI_IF_STA;

猜测是因为需要描述要发送端的工作状态

posted @ 2022-01-31 16:21  为鲸  阅读(923)  评论(0编辑  收藏  举报