用esp8266与esp01s用espnow通讯的代码

时间: 2024-03-22 09:37:15 浏览: 19
以下是使用ESP8266和ESP-01S利用ESP-NOW进行通信的示例代码: 发送数据的ESP8266代码: ```c++ #include <ESP8266WiFi.h> #include <espnow.h> // Replace with the MAC address of the receiving ESP-01S module uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Callback function that will be executed when data is sent void OnDataSent(uint8_t *mac_addr, uint8_t status) { Serial.print("Packet sent to: "); for (int i = 0; i < 6; i++) { Serial.print(mac_addr[i], HEX); if (i != 5) { Serial.print(":"); } } Serial.print(" Status: "); Serial.println(status); } void setup() { Serial.begin(115200); // Initialize ESP-NOW if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } // Register the callback function esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); esp_now_register_send_cb(OnDataSent); // Add the receiving ESP-01S module as a peer esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0); } void loop() { // Send data to the receiving ESP-01S module String message = "Hello from ESP8266!"; uint8_t data[message.length() + 1]; message.getBytes(data, message.length() + 1); esp_now_send(broadcastAddress, data, message.length() + 1); delay(1000); } ``` 接收数据的ESP-01S代码: ```c++ #include <ESP8266WiFi.h> #include <espnow.h> // Replace with the MAC address of the receiving ESP8266 module uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; void OnDataReceived(uint8_t *mac_addr, uint8_t *data, uint8_t len) { Serial.print("Data received from: "); for (int i = 0; i < 6; i++) { Serial.print(mac_addr[i], HEX); if (i != 5) { Serial.print(":"); } } Serial.print(" Data: "); Serial.write(data, len); Serial.println(); } void setup() { Serial.begin(115200); // Initialize ESP-NOW if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } // Register the callback function esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); esp_now_register_recv_cb(OnDataReceived); // Add the sending ESP8266 module as a peer esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_CONTROLLER, 1, NULL, 0); } void loop() { // Do nothing } ``` 在以上代码中,需要将发送ESP8266和接收ESP-01S的MAC地址替换为实际的地址。此外,还需要在发送ESP8266中将数据转换为字节数组,并在接收ESP-01S中使用Serial.write()函数来打印接收到的数据。

相关推荐

最新推荐

recommend-type

ESP8266实现PC与模块通讯

通过串口输入AT指令来进行设置 1. AT+CWMODE=3 //设置为服务器客户端模式 2. AT+CIPMUX=1 //设置模块允许多连接,这条命令一定要比CIPSERVER先执行...3. AT+CIPSERVER=1,8080 //开启服务器,并使用8080端口进行通信。
recommend-type

esp8266 AT指令之建立TCP透传客户端

下面大家跟着小编使用esp8266建立tcp连接吧。 连接准备 ①已经刷好固件的esp8266。若未刷固件请看上一篇。 ②usb转ttl模块 ③网络调试助手 下载链接:https://pan.baidu.com/s/1D73_jyD-4xDBVfok7FSupA 提取码:8mwi...
recommend-type

ESP8266 ESP-01调试全过程

ESP8266 ESP-01调试全过程此版本若想从FLASH启动进入AT系统,只需CH-PD引脚接VCC或接上拉(不接上拉的情况下,串口可能无数据),其余三个引脚可选择悬空或接VCC(但群里(@云海之梦)反映接VCC后导致无法正常启动)
recommend-type

ESP8266的TCP服务器连接错误解决办法

解决WIN8/WIN10下ESP8266作为客户端,电脑作为服务器,所连接失败的问题
recommend-type

无线模块esp8266控制led灯亮灭51单片机底层程序

无线模块esp8266控制led灯亮灭51单片机底层程序,只有51单片机的程序没有控制app,使用网络调试助手可以实现手机与51单片机通讯,从而达到控制led灯的亮灭,亲测可行
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。