15浏览
查看: 15|回复: 2

[项目] 【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信

[复制链接]
这里将向您展示如何使用 ESP-NOW 通信协议在两个 ESP32 板之间建立双向通信。例如,两个 ESP32 板将交换传感器读数(在开阔场地的范围可达 220 米 ~ 722 英尺)。

ESP-NOW 简介
ESP-NOW 是乐鑫开发的一种无连接通信协议,具有短数据包传输的特点。此协议使多个设备能够在不使用 Wi-Fi 的情况下相互通信。

这是一种快速通信协议,可用于在 ESP32 板之间交换小消息(最大 250 字节)。ESP-NOW 用途广泛,您可以采用不同的安排进行单向或双向通信。
在本教程中,我们将向您展示如何在两个 ESP32 板之间建立双向通信。

注意:请阅读我们的 ESP-NOW 入门指南,了解有关 ESP32 的 ESP-NOW 协议的完整介绍。
项目概况

■ 在这个项目中,我们将有两个 ESP32 板。每块板都连接到一个 OLED 显示屏和一个 BME280 传感器;
■ 每个板子都从相应的传感器获取温度、湿度和压力读数;
■ 每个开发板都通过 ESP-NOW 将其读数发送到另一个开发板;
■ 当电路板收到读数时,它会将其显示在 OLED 显示屏上;
■ 发送读数后,如果消息成功传递,则板会显示在 OLED 上;
■ 每个板都需要知道另一个板的 MAC 地址才能发送消息。

在此示例中,我们在两个板之间使用双向通信,但您可以向此设置添加更多板,并让所有板相互通信。
先决条件
在继续此项目之前,请确保检查以下先决条件。
ESP32 插件 Arduino IDE
我们将使用 Arduino IDE 对 ESP32 进行编程,因此在继续本教程之前,您应该在 Arduino IDE 中安装 ESP32 插件。请按照以下指南作:
■ 在 Arduino IDE 中安装 ESP32 开发板(Windows、Mac OS X 和 Linux)
安装库
在 Arduino IDE 中安装以下库。这些库可以通过 Arduino Library Manager 进行安装。转到 Sketch > Include Library> Manage Libraries 并搜索库名称。
■ OLED 库(ESP32 OLED 指南):Adafruit_SSD1306 库和 Adafruit_GFX 库
■ BME280 库(ESP32 BME280 指南):Adafruit_BME280 库和 Adafruit 统一传感器库

所需零件
对于本教程,您需要以下部分:
■ 2 个 ESP32 开发板(阅读最佳 ESP32 开发板)
■ 2 个 BME280 传感器(BME280 完整指南)
■ 2 个 0.96 英寸 OLED 显示器(OLED 完整指南)
■ 面包板
■ 跳线
您可以使用前面的链接或直接访问 MakerAdvisor.com/tools 以最优惠的价格找到适合您项目的所有零件!

获取 Boards MAC 地址
要在每个板之间发送消息,我们需要知道它们的 MAC 地址。每个开发板都有一个唯一的 MAC 地址(了解如何获取和更改 ESP32 MAC 地址)。

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图1

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图2

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图4

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图3

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图6

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图5

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图7

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图8



驴友花雕  中级技神
 楼主|

发表于 4 小时前

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信

项目代码

  1. /*
  2.   Rui Santos & Sara Santos - Random Nerd Tutorials
  3.   Complete project details at https://RandomNerdTutorials.com/esp-now-two-way-communication-esp32/
  4.   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  5.   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  6. */
  7. #include <esp_now.h>
  8. #include <WiFi.h>
  9. #include <Wire.h>
  10. #include <Adafruit_Sensor.h>
  11. #include <Adafruit_BME280.h>
  12. #include <Adafruit_GFX.h>
  13. #include <Adafruit_SSD1306.h>
  14. #define SCREEN_WIDTH 128  // OLED display width, in pixels
  15. #define SCREEN_HEIGHT 64  // OLED display height, in pixels
  16. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  17. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  18. Adafruit_BME280 bme;
  19. // REPLACE WITH THE MAC Address of your receiver
  20. uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  21. // Define variables to store BME280 readings to be sent
  22. float temperature;
  23. float humidity;
  24. float pressure;
  25. // Define variables to store incoming readings
  26. float incomingTemp;
  27. float incomingHum;
  28. float incomingPres;
  29. // Variable to store if sending data was successful
  30. String success;
  31. //Structure example to send data
  32. //Must match the receiver structure
  33. typedef struct struct_message {
  34.     float temp;
  35.     float hum;
  36.     float pres;
  37. } struct_message;
  38. // Create a struct_message called BME280Readings to hold sensor readings
  39. struct_message BME280Readings;
  40. // Create a struct_message to hold incoming sensor readings
  41. struct_message incomingReadings;
  42. esp_now_peer_info_t peerInfo;
  43. // Callback when data is sent
  44. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  45.   Serial.print("\r\nLast Packet Send Status:\t");
  46.   Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  47.   if (status ==0){
  48.     success = "Delivery Success :)";
  49.   }
  50.   else{
  51.     success = "Delivery Fail :(";
  52.   }
  53. }
  54. // Callback when data is received
  55. void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  56.   memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  57.   Serial.print("Bytes received: ");
  58.   Serial.println(len);
  59.   incomingTemp = incomingReadings.temp;
  60.   incomingHum = incomingReadings.hum;
  61.   incomingPres = incomingReadings.pres;
  62. }
  63. void setup() {
  64.   // Init Serial Monitor
  65.   Serial.begin(115200);
  66.   // Init BME280 sensor
  67.   bool status = bme.begin(0x76);  
  68.   if (!status) {
  69.     Serial.println("Could not find a valid BME280 sensor, check wiring!");
  70.     while (1);
  71.   }
  72.   // Init OLED display
  73.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  74.     Serial.println(F("SSD1306 allocation failed"));
  75.     for(;;);
  76.   }
  77.   // Set device as a Wi-Fi Station
  78.   WiFi.mode(WIFI_STA);
  79.   // Init ESP-NOW
  80.   if (esp_now_init() != ESP_OK) {
  81.     Serial.println("Error initializing ESP-NOW");
  82.     return;
  83.   }
  84.   // Once ESPNow is successfully Init, we will register for Send CB to
  85.   // get the status of Trasnmitted packet
  86.   esp_now_register_send_cb(OnDataSent);
  87.   
  88.   // Register peer
  89.   memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  90.   peerInfo.channel = 0;  
  91.   peerInfo.encrypt = false;
  92.   
  93.   // Add peer        
  94.   if (esp_now_add_peer(&peerInfo) != ESP_OK){
  95.     Serial.println("Failed to add peer");
  96.     return;
  97.   }
  98.   // Register for a callback function that will be called when data is received
  99.   esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
  100. }
  101. void loop() {
  102.   getReadings();
  103.   // Set values to send
  104.   BME280Readings.temp = temperature;
  105.   BME280Readings.hum = humidity;
  106.   BME280Readings.pres = pressure;
  107.   // Send message via ESP-NOW
  108.   esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &BME280Readings, sizeof(BME280Readings));
  109.    
  110.   if (result == ESP_OK) {
  111.     Serial.println("Sent with success");
  112.   }
  113.   else {
  114.     Serial.println("Error sending the data");
  115.   }
  116.   updateDisplay();
  117.   delay(10000);
  118. }
  119. void getReadings(){
  120.   temperature = bme.readTemperature();
  121.   humidity = bme.readHumidity();
  122.   pressure = (bme.readPressure() / 100.0F);
  123. }
  124. void updateDisplay(){
  125.   // Display Readings on OLED Display
  126.   display.clearDisplay();
  127.   display.setTextSize(1);
  128.   display.setTextColor(WHITE);
  129.   display.setCursor(0, 0);
  130.   display.println("INCOMING READINGS");
  131.   display.setCursor(0, 15);
  132.   display.print("Temperature: ");
  133.   display.print(incomingTemp);
  134.   display.cp437(true);
  135.   display.write(248);
  136.   display.print("C");
  137.   display.setCursor(0, 25);
  138.   display.print("Humidity: ");
  139.   display.print(incomingHum);
  140.   display.print("%");
  141.   display.setCursor(0, 35);
  142.   display.print("Pressure: ");
  143.   display.print(incomingPres);
  144.   display.print("hPa");
  145.   display.setCursor(0, 56);
  146.   display.print(success);
  147.   display.display();
  148.   
  149.   // Display Readings in Serial Monitor
  150.   Serial.println("INCOMING READINGS");
  151.   Serial.print("Temperature: ");
  152.   Serial.print(incomingReadings.temp);
  153.   Serial.println(" ºC");
  154.   Serial.print("Humidity: ");
  155.   Serial.print(incomingReadings.hum);
  156.   Serial.println(" %");
  157.   Serial.print("Pressure: ");
  158.   Serial.print(incomingReadings.pres);
  159.   Serial.println(" hPa");
  160.   Serial.println();
  161. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 4 小时前

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信
项目链接:https://randomnerdtutorials.com/ ... ommunication-esp32/
项目作者:鲁伊桑托斯
项目参考:ESP-NOW 入门(ESP32 与 Arduino IDE)
https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/
项目视频 :https://www.youtube.com/watch?v=qxwXwNS3Avw
项目代码:https://raw.githubusercontent.co ... y_Communication.ino

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图2

【Arduino 动手做】ESP-NOW与ESP32:板间最简单的无线通信图1

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail