espnow 例程解析

本文介绍了ESP-NOW,一种短距离无连接的快速通信技术,适用于智能照明控制。通过详细解析ESPNOW的初始化、事件处理、发送和接收数据的回调函数,以及如何处理接收到的数据,展示了如何在ESP32上实现ESPNOW通信。文中提供了完整的示例代码,包括初始化WiFi、设置发送和接收回调、解析数据等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、什么是espnow

短数据传输、无连接的快速通讯技术

2、应用范围智能照明控制

(一)、 ESPNOW例程。

下面的例程展示了怎样使用ESPNOW

步骤:

1、初始化wifi

A、TCP/ip适配器初始化

a、注册事件处理函数初始化。

b、设置配置信息

c、选择存储类型

d、选择wifi模式AP

e、启动wifi

f、启动wifi后设置通道,假如两个设备在同一个通道上就不需要设置

2、初始化espnow

a、创建个队列

b、注册发送数据回调函数

c、注册接受数据回调函数

d、设置主秘钥

e、添加广播点信息来点列表

f、初始化发送参数

g、创建espnow任务

*/

#include <stdlib.h>

#include <time.h>

#include <string.h>

#include <assert.h>

#include "freertos/FreeRTOS.h"

#include "freertos/semphr.h"

#include "freertos/timers.h"

#include "nvs_flash.h"

#include "esp_event_loop.h"

#include "tcpip_adapter.h"

#include "esp_wifi.h"

#include "esp_log.h"

#include "esp_system.h"

#include "esp_now.h"

#include "rom/ets_sys.h"

#include "rom/crc.h"

#include "espnow_example.h"


static const char *TAG = "espnow_example";


static xQueueHandle example_espnow_queue;


static uint8_t example_broadcast_mac[ESP_NOW_ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

static uint16_t s_example_espnow_seq[EXAMPLE_ESPNOW_DATA_MAX] = { 0, 0 };


static void example_espnow_deinit(example_espnow_send_param_t *send_param);

1、事件处理函数

static esp_err_t example_event_handler(void *ctx, system_event_t *event)

{

  switch(event->event_id) {

  case SYSTEM_EVENT_STA_START:                    wifi启动完成

    ESP_LOGI(TAG, "WiFi started");

    break;

  default:

    break;

  }

  return ESP_OK;

}


2、wifi初始化

static void example_wifi_init(void)

{

  tcpip_adapter_init(); TCP/ip适配器初始化

  ESP_ERROR_CHECK( esp_event_loop_init(example_event_handler, NULL) ); 注册事件处理函数初始化。

  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); 获取wifi配置信息

  ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); 设置配置信息

  ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); 选择存储类型

  ESP_ERROR_CHECK( esp_wifi_set_mode(ESPNOW_WIFI_MODE) ); 选择wifi模式AP

  ESP_ERROR_CHECK( esp_wifi_start()); 启动wifi

  ESP_ERROR_CHECK( esp_wifi_set_channel(CONFIG_ESPNOW_CHANNEL, 0) ); 启动wifi后设置通道,假如两个设备在同一个通道上就不需要设置

}


1、发送数据回调函数

static void example_espnow_send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)

{

  example_espnow_event_t evt;

  example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb;


  if (mac_addr == NULL) {

    ESP_LOGE(TAG, "Send cb arg error");

    return;

  }

设置发送事件,并添加到队列中去

  evt.id = EXAMPLE_ESPNOW_SEND_CB;

  memcpy(send_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN);

  send_cb->status = status;

  if (xQueueSend(exampl

### ESP32蓝牙功能示例代码与教程 ESP32是一款支持Wi-Fi和蓝牙的微控制器,其蓝牙模块可以用于多种应用场景,例如设备间的数据通信、与智能手机交互等。以下是基于Arduino IDE环境下的ESP32蓝牙功能实现的具体示例代码。 #### 示例一:两个ESP32设备间的蓝牙数据传输 此示例展示了如何通过ESP32的经典蓝牙模式实现两台设备之间的数据交换[^1]。 ```cpp #include <BluetoothSerial.h> // 如果硬件不支持BLE,则定义BluetoothSerial为NULL #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to enable it. #endif BluetoothSerial SerialBT; void setup() { Serial.begin(115200); SerialBT.begin("ESP32_Bluetooth"); // 设备名称 Serial.println("The device started, now you can pair it with another device"); } void loop() { if (Serial.available()) { SerialBT.write(Serial.read()); // 将串口接收到的内容转发到蓝牙 } if (SerialBT.available()) { Serial.write(SerialBT.read()); // 将蓝牙接收到的内容转发到串口 } delay(20); // 延迟时间可以根据实际需求调整 } ``` 上述代码实现了简单的双向通信机制,其中一台ESP32作为发送端,另一台作为接收端。 --- #### 示例二:ESP32与智能手机之间的蓝牙数据传输 该示例演示了如何让ESP32与安装有特定应用程序的智能手机建立蓝牙连接并传递数据。 ```cpp #include <BluetoothSerial.h> // 如果硬件不支持BLE,则定义BluetoothSerial为NULL #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to enable it. #endif BluetoothSerial SerialBT; void setup() { Serial.begin(115200); SerialBT.begin("ESP32_Smartphone"); // 设置蓝牙设备名称 Serial.println("Waiting for devices to connect..."); } void loop() { if (SerialBT.available()) { // 检查是否有来自手机的数据 String data = SerialBT.readString(); // 读取字符串 Serial.println(data); // 打印到串口监视器 } static unsigned long lastSendTime = 0; if ((millis() - lastSendTime) > 2000) { // 每隔2秒向手机发送一次消息 lastSendTime = millis(); SerialBT.println("Hello from ESP32!"); } } ``` 在此场景下,需确保手机上的应用能够解析ESP32发出的消息,并允许用户输入自定义命令回传给ESP32。 --- #### 注意事项 当尝试与其他仅支持静态密码验证的传统蓝牙模块(如JDY-31)配对时,请注意ESP32默认采用SPP协议中的动态密码认证方式,这可能导致兼容性问题[^3]。因此,在设计项目前应确认双方所遵循的安全策略一致。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值