用arduino写一个esp32与esp8266通过espnow协议通讯
时间: 2024-06-12 18:03:44 浏览: 15
这里提供一个简单的示例代码,其中ESP32作为主机,ESP8266作为从机:
ESP32代码:
#include <esp_now.h>
#include <WiFi.h>
#define CHANNEL 1
#define PRINTSCANRESULTS 0
#define PRINTDEBUG 1
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Set the slave MAC address
uint8_t slaveAddress[] = {0x24, 0x0A, 0xC4, 0x01, 0x23, 0x45};
// Create a struct to store data
typedef struct struct_message {
int id;
float temperature;
float humidity;
} struct_message;
// Create a struct_message to send data
struct_message myData;
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
Serial.println(macToStr(mac_addr, macStr));
Serial.print("Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}
// Scan for slaves in AP mode
void scanForSlave() {
int8_t scanResults = WiFi.scanNetworks();
if (scanResults == 0) {
Serial.println("No WiFi devices in AP Mode found");
} else {
Serial.print("Found ");
Serial.print(scanResults);
Serial.println(" devices ");
for (int i = 0; i < scanResults; ++i) {
// Print SSID and RSSI for each device found
String SSID = WiFi.SSID(i);
int32_t RSSI = WiFi.RSSI(i);
String BSSIDstr = WiFi.BSSIDstr(i);
Serial.printf("%d: %s (%d) MAC: %s\n", i + 1, SSID.c_str(), RSSI, BSSIDstr.c_str());
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}
// Convert MAC address to String
String macToStr(const uint8_t* mac, char* buf) {
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return buf;
}
void setup() {
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, slaveAddress, 6);
peerInfo.channel = CHANNEL;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
// Set up Wi-Fi connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Send data via ESP-NOW
myData.id = 123;
myData.temperature = 25.6;
myData.humidity = 60.2;
esp_err_t result = esp_now_send(slaveAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
// Wait a bit before sending again
delay(5000);
}
ESP8266代码:
#include <esp_now.h>
#include <WiFi.h>
#define CHANNEL 1
#define PRINTSCANRESULTS 0
#define PRINTDEBUG 1
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Create a struct to store data
typedef struct struct_message {
int id;
float temperature;
float humidity;
} struct_message;
// Create a struct_message to receive data
struct_message myData;
// Callback when data is received
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Received from: ");
char macStr[18];
Serial.println(macToStr(mac_addr, macStr));
Serial.print("ID: ");
Serial.println(myData.id);
Serial.print("Temperature: ");
Serial.println(myData.temperature);
Serial.print("Humidity: ");
Serial.println(myData.humidity);
}
// Convert MAC address to String
String macToStr(const uint8_t* mac, char* buf) {
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return buf;
}
void setup() {
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register callback function
esp_now_register_recv_cb(OnDataRecv);
// Set up Wi-Fi connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Do nothing
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)