发文希望能够帮忙物联网爱好者少走弯路,少被割韭菜。如果觉得此文对您有帮助的话帮忙点个赞,感谢!!!
哔哩哔哩网址:开源免费代码_01_基于Arduino的ESP-NOW,ESP32发送指令、ESP-01S接收指令,通过ESP-01S继电器实现LED灯开关控制_哔哩哔哩_bilibili
博客网址:公羽兴
公羽兴的博客_CSDN博客-领域博主
阿里云盘免费共享文件:Arduino、ESP32、ESP8266、点灯科技安装包/库文件/APP等资源
https://www.aliyundrive.com/s/cvvtqV2XzuC
本文参考的原文链接:http://t.csdn.cn/lO3hK
实物图片
基于Arduino的ESP-NOW,ESP32发送指令、ESP-01S接收指令,通过ESP-01S继电器实现LED灯开关控制
物料清单:
1.ESP32(Arduino开发板:ESP32 Dev Module)
2.ESP-01S(Arduino开发板:NodeMCU 1.0)
(直接使用ESP8266相同代码及库文件,直接烧录即可)
3.ESP-01S继电器
4.LED灯
5.数据线、连接线、螺丝刀
物料使用方法:
1.Arduino的ESP32开发环境配置:ESP32 开发入门 使用 Arduino IDE - 知乎 (zhihu.com)
也可以去我的阿里云盘共享文件夹下载已经下载好的库文件,放到相应的路径或者直接双击打开即可。
2.Arduino的ESP-01S开发环境配置:Arduino + ESP8266 NodeMcu(超详细)_esp8266nodemcu 与arduino通信_TA很懒,还没有添加名字的博客-CSDN博客
也可以去我的阿里云盘共享文件夹下载已经下载好的库文件,放到相应的路径或者直接双击打开即可。
3.ESP-01S说明(直接使用ESP8266相同代码及库文件,直接烧录即可)
4.ESP-01S烧录器
5.ESP-01S继电器
6.LED灯(共阴极,一端接地,另外三个接口有一个收到信号即可点亮)
代码
ESP32发送端
#include
#include
uint8_t broadcastAddress[] = {0x60, 0x01, 0x94, 0x3E, 0x0F, 0x56};//需要自行获取接收设备的MAC地址60:01:94:3E:0F:56
// 发送结构体类型
typedef struct struct_message {
int AAA;
int BBB;
} struct_message;
struct_message myData;// 创建一个结构体变量
void setup() {
Serial.begin(115200); // 初始化串口波特率
WiFi.mode(WIFI_STA); // 设置WIFI模式为STA模式,即无线终端
// 初始化ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
// delay(10);
}
// 注册通信频道
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0; //通道
peerInfo.encrypt = false;//是否加密为False
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
myData.AAA = 111;
myData.BBB = 111;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.051 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(1000);
myData.AAA = 222;
myData.BBB = 222;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.052 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(1000);
myData.AAA = 111;
myData.BBB = 111;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.053 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(1000);
myData.AAA = 222;
myData.BBB = 222;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.054 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(1000);
myData.AAA = 111;
myData.BBB = 111;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.11 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(1000);
myData.AAA = 222;
myData.BBB = 222;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.12 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(1000);
myData.AAA = 111;
myData.BBB = 111;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.21 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(2000);
myData.AAA = 222;
myData.BBB = 222;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.22 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(2000);
myData.AAA = 111;
myData.BBB = 111;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.31 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(3000);
myData.AAA = 222;
myData.BBB = 222;
//发送消息
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
Serial.print("myData.32 = ");Serial.print(myData.AAA);Serial.println(myData.BBB);
//延时两秒
delay(3000);
}
//博客网址:公羽兴:https://blog.csdn.net/weixin_52850743?type=blog
ESP8266/ESP-01S接收端
#include
#include
int r = 0;
int g = 2;
typedef struct struct_message {
int AAA;
int BBB;
} struct_message;
//创建结构体变量
struct_message myData;
//创建一个回调函数作为接收数据后的串口显示
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("myData.B: "); Serial.println(myData.BBB);
Serial.print("myData.A: "); Serial.println(myData.AAA);
//接收数据亮灯关灯
if (myData.AAA == 111) {
Serial.println("AAA");
digitalWrite(0, 0);
digitalWrite(2, 0);
}
else if (myData.BBB == 222) {
Serial.println("BBB");
digitalWrite(0, 1);
digitalWrite(2, 1);
}
}
void setup() {
//初始化窗口
Serial.begin(115200);
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(0, 1);
digitalWrite(2, 1);
//设置ESP8266模式
WiFi.mode(WIFI_STA);
//初始化 ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
//设置ESP8266角色:
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
//先前创建的功能 测试ESP-NOW通信
esp_now_register_recv_cb(OnDataRecv);
delay(5000);
//打开串口监视器可查看本设备的MAC地址
WiFi.mode(WIFI_AP_STA);
Serial.println("hello");//加打印hello原因是模块刚上电有点乱码
Serial.println(WiFi.macAddress());
}
void loop() {
}
//博客网址:公羽兴:https://blog.csdn.net/weixin_52850743?type=blog
附加开发板管理器网址:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
http://arduino.esp8266.com/stable/package_esp8266com_index.json
https://dl.espressif.com/dl/package_esp32_index.json
连线说明:
发文希望能够帮忙物联网爱好者少走弯路,少被割韭菜。如果觉得此文对您有帮助的话帮忙点个赞,感谢!!!如果有什么意见或建议欢迎各位联系我。
2023.05.30更新,增加视频。
ESP-NOW,ESP32,ESP-01S继电器实现灯开关