espnow 简介:
ESP-NOW 是由乐鑫开发的另一款协议,可以使多个设备在没有或不使用 Wi-Fi 的情况下进行通信。这种协议类似常见于无线鼠标中的低功耗 2.4GHz 无线连接——设备在进行通信之前要进行配对。配对之后,设备之间的连接是持续的、点对点的,并且不需要握手协议。
- #include <WiFi.h>
- #include <esp_now.h>
-
- // 引脚编号
- #define ADC0 0
- #define ADC1 1
- #define ADC2 2
- #define ADC3 3
- #define ADC4 4
- #define ADC5 5
-
- #define IO6 6
- #define IO7 7
- #define IO8 8
- #define IO9 9
- #define IO10 10
- #define IO20 20
- #define IO21 21
-
-
-
- // 设置数据结构体
- //5 adc 8io
- typedef struct struct_message
- {
- double adc0;
- double adc1;
- double adc2;
- double adc3;
- double adc4;
- double adc5;
- bool io6;
- bool io7;
- bool io8;
- bool io9;
- bool io10;
- bool io20;
- bool io21;
- //String board_name;
- } struct_message;
-
- struct_message myData;
-
- // 接收机设备的 MAC 地址
- uint8_t broadcastAddress[] = {0x84, 0xf7, 0x03, 0xbd, 0x15,0xd8};
-
-
- // 数据发送回调函数
- void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
- char macStr[18];
- //Serial.print("Packet to: ");
- snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
- mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
- /*
- Serial.println(macStr);
- Serial.print("Send status: ");
- Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
- Serial.println();
- */
- }
-
- void setup() {
- Serial.begin(115200);
-
- // 初始化 ESP-NOW
- WiFi.mode(WIFI_STA);
- if (esp_now_init() != ESP_OK) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
-
- // 设置发送数据回调函数
- esp_now_register_send_cb(OnDataSent);
-
- // 绑定数据接收端
- esp_now_peer_info_t peerInfo;
- memcpy(peerInfo.peer_addr, broadcastAddress, 6);
- peerInfo.channel = 0;
- peerInfo.encrypt = false;
- //peerInfo.retransmit_count=0;
-
- // 检查设备是否配对成功
- if (esp_now_add_peer(&peerInfo) != ESP_OK) {
- Serial.println("Failed to add peer");
- return;
- }
- pinMode(IO6 , INPUT_PULLUP);
- pinMode(IO7, INPUT_PULLUP);
- pinMode(IO8 , INPUT_PULLUP);
- pinMode(IO9 , INPUT_PULLUP);
- pinMode(IO10 , INPUT_PULLUP);
- pinMode(IO20 , INPUT_PULLUP);
- pinMode(IO21 , INPUT_PULLUP);
- }
-
- void loop() {
- // 设置要发送的数据
- //myData.board_name = "Fat Donkey";
-
- myData.adc0 = analogRead(ADC0);
-
- myData.adc1 = analogRead(ADC1);
- myData.adc2 = analogRead(ADC2);
- myData.adc3 = analogRead(ADC3);
- myData.adc4 = analogRead(ADC4);
- myData.adc5 = analogRead(ADC5);
- myData.io6 = digitalRead(IO6);
- myData.io7 = digitalRead(IO7);
- myData.io8 = digitalRead(IO8);
- myData.io9 = digitalRead(IO9);
- myData.io10 = digitalRead(IO10);
- myData.io20 = digitalRead(IO20);
- myData.io21 = digitalRead(IO21);
-
-
-
-
-
-
- // 发送数据
- 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);
- }
复制代码
接收机代码
- #include <WiFi.h>
- #include <esp_now.h>
-
- #if !defined( Arduino_ESP32C3_DEV )
- #error This code is intended to run on the ESP32_C3 platform! Please check your Tools->Board setting.
- #endif
-
- #define TIMER_INTERRUPT_DEBUG -1
- #define ISR_SERVO_DEBUG -1
-
- // Select different ESP32 timer number (0-1) to avoid conflict
- #define USE_ESP32_TIMER_NO 1
- #include "ESP32_C3_ISR_Servo.h"
-
- // 引脚编号
- #define ADC0 0
- #define ADC1 1
- #define ADC2 2
- #define ADC3 3
- #define ADC4 4
- #define ADC5 5
-
- #define IO6 6
- #define IO7 7
- #define IO8 8
- #define IO9 9
- #define IO10 10
- #define IO20 20
- #define IO21 21
-
-
-
- // 设置数据结构体
- //5 adc 8io
- typedef struct struct_message
- {
- double adc0;
- double adc1;
- double adc2;
- double adc3;
- double adc4;
- double adc5;
- bool io6;
- bool io7;
- bool io8;
- bool io9;
- bool io10;
- bool io20;
- bool io21;
- //String board_name;
- } struct_message;
- struct_message myData;
-
- // Published values for SG90 servos; adjust if needed
- #define MIN_MICROS 800 //544
- #define MAX_MICROS 2450
-
- int servoIndex0 = -1;
- int servoIndex1 = -1;
- int servoIndex2 = -1;
- int servoIndex3 = -1;
- int servoIndex4 = -1;
- int servoIndex5 = -1;
-
-
- // 数据接收回调函数
- void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
- memcpy(&myData, incomingData, sizeof(myData));
- //Serial.print("board_name: ");
- //Serial.println(myData.board_name);
- int pulseWidth=0;
-
- pulseWidth=map(myData.adc0,0,4095,MIN_MICROS,MAX_MICROS);
- ESP32_ISR_Servos.setPulseWidth(servoIndex0, pulseWidth);
- pulseWidth=map(myData.adc1,0,4095,MIN_MICROS,MAX_MICROS);
- ESP32_ISR_Servos.setPulseWidth(servoIndex1, pulseWidth);
- pulseWidth=map(myData.adc2,0,4095,MIN_MICROS,MAX_MICROS);
- ESP32_ISR_Servos.setPulseWidth(servoIndex2, pulseWidth);
- pulseWidth=map(myData.adc3,0,4095,MIN_MICROS,MAX_MICROS);
- ESP32_ISR_Servos.setPulseWidth(servoIndex3, pulseWidth);
- pulseWidth=map(myData.adc4,0,4095,MIN_MICROS,MAX_MICROS);
- ESP32_ISR_Servos.setPulseWidth(servoIndex4, pulseWidth);
- pulseWidth=map(myData.adc5,0,4095,MIN_MICROS,MAX_MICROS);
- ESP32_ISR_Servos.setPulseWidth(servoIndex5, pulseWidth);
-
- digitalWrite(IO6,myData.io6);
-
- digitalWrite(IO7,myData.io7);
- digitalWrite(IO8,myData.io8);
- digitalWrite(IO9,myData.io9);
- digitalWrite(IO10,myData.io10);
- digitalWrite(IO20,myData.io20);
- digitalWrite(IO21,myData.io21);
-
-
- }
-
- void setup() {
- Serial.begin(115200);
-
- // 初始化 ESP-NOW
- WiFi.mode(WIFI_STA);
- if (esp_now_init() != 0) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
- // 设置接收数据回调函数
- esp_now_register_recv_cb(OnDataRecv);
-
- ESP32_ISR_Servos.useTimer(USE_ESP32_TIMER_NO);
-
- servoIndex0 = ESP32_ISR_Servos.setupServo(ADC0, MIN_MICROS, MAX_MICROS);
- servoIndex1 = ESP32_ISR_Servos.setupServo(ADC1, MIN_MICROS, MAX_MICROS);
- servoIndex2 = ESP32_ISR_Servos.setupServo(ADC2, MIN_MICROS, MAX_MICROS);
- servoIndex3 = ESP32_ISR_Servos.setupServo(ADC3, MIN_MICROS, MAX_MICROS);
- servoIndex4 = ESP32_ISR_Servos.setupServo(ADC4, MIN_MICROS, MAX_MICROS);
- servoIndex5 = ESP32_ISR_Servos.setupServo(ADC5, MIN_MICROS, MAX_MICROS);
-
-
- pinMode(IO6 , OUTPUT);
- pinMode(IO7, OUTPUT);
- pinMode(IO8 , OUTPUT);
- pinMode(IO9 , OUTPUT);
- pinMode(IO10 , OUTPUT);
- pinMode(IO20 , OUTPUT);
- pinMode(IO21 , OUTPUT);
-
-
- }
-
- void loop() {
-
- //ESP32_ISR_Servos.setPosition(servoIndex1, position);
- //setPulseWidth(servoIndex1, uint16_t& pulseWidth);
- }
复制代码
性能测试
通过示波器在遥控和接收机做了延时测试
小结
arduino主要还是方便,估计也是espnow刚出库没完全更新,测试下来延时比较高,保持关注吧,如果对延时有需求可以考虑下用espidf来试试
|