esp8266 espnow代码
时间: 2023-09-21 14:02:38 浏览: 174
以下是 ESP8266 使用 ESP-NOW 的示例代码:
```c++
#include <ESP8266WiFi.h>
#include <espnow.h>
#define CHANNEL 1
#define PRINTSCANRESULTS 0
#define WIFI_CHANNEL_SWITCH_INTERVAL 5000
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Send and receive structure
typedef struct struct_message {
int id;
float temperature;
float humidity;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// MAC address of the receiver ESP8266
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// callback function when data is received
void OnDataRecv(uint8_t *mac_addr, uint8_t *incomingData, uint8_t len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Received packet from: ");
Serial.println(mac_addr);
Serial.print("Message ID: ");
Serial.println(myData.id);
Serial.print("Temperature: ");
Serial.println(myData.temperature);
Serial.print("Humidity: ");
Serial.println(myData.humidity);
}
void setup() {
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = CHANNEL;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != 0) {
Serial.println("Failed to add peer");
return;
}
// Register for a callback function when data is received
esp_now_set_data_recv_cb(OnDataRecv);
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
myData.id = 1;
myData.temperature = 25.0;
myData.humidity = 50.0;
// 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 if (result == ESP_ERR_ESPNOW_NOT_INIT) {
Serial.println("ESPNOW not Init.");
}
else if (result == ESP_ERR_ESPNOW_ARG) {
Serial.println("Invalid Argument");
}
else if (result == ESP_ERR_ESPNOW_INTERNAL) {
Serial.println("Internal Error");
}
else if (result == ESP_ERR_ESPNOW_NO_MEM) {
Serial.println("ESP_ERR_ESPNOW_NO_MEM");
}
else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
Serial.println("Peer not found.");
}
else {
Serial.println("Not sure what happened");
}
delay(1000);
}
```
在这个例子中,我们定义了一个用于发送和接收的结构体 `struct_message`,该结构体包含三个变量:`id`、`temperature` 和 `humidity`。在 `setup` 函数中,我们初始化了 ESP-NOW,并注册了一个回调函数 `OnDataRecv`,以便在接收到数据时能够处理它。在 `loop` 函数中,我们创建了一个名为 `myData` 的 `struct_message` 结构体,并将其发送到广播地址。在发送数据后,我们等待 1 秒后重复此过程。
请注意,如果您使用的是 ESP8266 的 NodeMCU 开发板,则需要将 `tx_pin` 和 `rx_pin` 设为 `2` 和 `3`,而不是默认的 `Arduino` 引脚。可以使用以下代码将它们设置为正确的引脚:
```c++
#define RX_PIN 3
#define TX_PIN 2
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(RX_PIN, INPUT);
pinMode(TX_PIN, OUTPUT);
Serial.swap(); // swap RX/TX pins
Serial.begin(115200);
...
}
```
希望这能帮助您开始使用 ESP-NOW。
阅读全文