目录
Espnow库函数注释
ESP-NOW单向通信(One-way communication)
ESP-NOW介绍
ESP-NOW是一种由Espressif开发的协议,可以让多个设备在不使用Wi-Fi的情况下相互通信。该协议类似于低功耗的2.4GHz无线连接。设备之间的配对需要在通信之前完成。配对完成后,连接是安全的、点对点的,不需要握手。这意味着在设备彼此配对后,连接是持久的。换句话说,如果你的某块单板突然失去电源或复位,当它重启时,它将自动连接到它的频道继续通信
ESP-NOW支持以下特性
- 混合加密和未加密的对端设备
- 加密和不加密的单播通信
- 最多可携带250字节的有效载荷(小数据传输);
- 发送回调函数,可以设置为通知应用层传输成功或失败;
ESP-NOW技术也存在以下局限性
- 有限的加密。Station模式最多支持10个加密对等体;“软拨号”或“软拨号+工作站”模式最多为6个
- 支持多个未加密的对等体,包括加密的对等体,总数不能超过20个
- 最大消息长度限制在250字节
Espnow库函数注释
#ifndef __ESPNOW_H__
#define __ESPNOW_H__
#ifdef __cplusplus
extern "C" {
#endif
enum esp_now_role {
ESP_NOW_ROLE_IDLE = 0,//未设置角色,不允许发送数据
ESP_NOW_ROLE_CONTROLLER,//控制方
ESP_NOW_ROLE_SLAVE,//被控制方
ESP_NOW_ROLE_COMBO,//控制方&被控制方双角色,双向通信时就用它
ESP_NOW_ROLE_MAX,//不懂
};
//回调函数
typedef void (*esp_now_recv_cb_t)(u8 *mac_addr, u8 *data, u8 len);
typedef void (*esp_now_send_cb_t)(u8 *mac_addr, u8 status);
int esp_now_init(void);//初始化esp_now
int esp_now_deinit(void);//取消esp_now的初始化
int esp_now_register_send_cb(esp_now_send_cb_t cb);//使用该函数之后,接收到数据会自动调用接收回调函数,回调函数的写法可以参考我上面的代码
int esp_now_unregister_send_cb(void);//与上面的函数作用相反
int esp_now_register_recv_cb(esp_now_recv_cb_t cb);//使用该函数之后,发送数据后会自动调用发送回调函数,回调函数的写法可以参考我上面的代码
int esp_now_unregister_recv_cb(void);//与上面的函数作用相反
int esp_now_send(u8 *da, u8 *data, int len);//发送数据,MAC地址中传入NULL会广播
int esp_now_add_peer(u8 *mac_addr, u8 role, u8 channel, u8 *key, u8 key_len);//与新设备配对
int esp_now_del_peer(u8 *mac_addr);//将已配对的设备删除
int esp_now_set_self_role(u8 role);//设定设备自己的角色
int esp_now_get_self_role(void);//获取设备自己的角色
int esp_now_set_peer_role(u8 *mac_addr, u8 role);//设定某个已配对设备的角色
int esp_now_get_peer_role(u8 *mac_addr);//获取某个已配对设备的角色
int esp_now_set_peer_channel(u8 *mac_addr, u8 channel);//设定某个已配对设备的WiFi通道
int esp_now_get_peer_channel(u8 *mac_addr);//获取某个已配对设备的WiFi通道
int esp_now_set_peer_key(u8 *mac_addr, u8 *key, u8 key_len);//设定某个已配对设备的密钥
int esp_now_get_peer_key(u8 *mac_addr, u8 *key, u8 *key_len);//获取某个已配对设备的密钥
u8 *esp_now_fetch_peer(bool restart);//不懂
int esp_now_is_peer_exist(u8 *mac_addr);//检查已经配对的设备是否在线
int esp_now_get_cnt_info(u8 *all_cnt, u8 *encrypt_cnt);//不懂
int esp_now_set_kok(u8 *key, u8 len);//对通信的key进行加密,不设置时使用默认的PMK
#ifdef __cplusplus
}
#endif
#endif
获取ESP32的MAC地址
在使用ESP-NOW协议前需要知道ESP32 的MAC地址
#include "WiFi.h"
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_MODE_STA);
Serial.println(WiFi.macAddress());
}
void loop(){
}
上串口打开串口监视器,可以得到板子的MAC地址,例如
最好拿个小纸条记下来
修改MAC地址
#include <ESP8266WiFi.h>
// Set your new MAC Address
uint8_t newMACAddress[] = {0x00, 0x08, 0x22, 0xa0, 0xa1, 0x34};
void setup(){
Serial.begin(9600);
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.softAP("SD", "sd168888");
Serial.print("[OLD] ESP8266 Board MAC Address: ");
Serial.println(WiFi.macAddress());
// For Soft Access Point (AP) Mode
//wifi_set_macaddr(SOFTAP_IF, &newMACAddress[0]);
// For Station Mode
wifi_set_macaddr(STATION_IF, &newMACAddress[0]);
Serial.print("[NEW] ESP8266 Board MAC Address: ");
Serial.println(WiFi.macAddress());
}
void loop(){
}
ESP-NOW单向通信(One-way communication)
一个ESP32作为发送方,另一个ESP32作为接收方
发送端的程序
#include <esp_now.h>
#include &l