为什么我的传感器在使用ESP-NOW双向通信时返回0?

发布于 2025-01-27 06:54:36 字数 3693 浏览 5 评论 0原文

我已经将ESP32 Lora连接到了水分传感器。我有一个用于读取传感器数据的脚本,该脚本本身可以很好地工作:


#define SensorPin 27 
float sensorValue = 0; 
void setup() { 
 Serial.begin(9600); 
} 
void loop() { 
 Serial.println(analogRead(SensorPin)); 
 delay(1000); 
} 

它在某种污垢中返回大约1800-2000。

然后,我想将数据发送到另一个ESP32 LORA,我在其中使用了本教程: https://randomnerdtutorials.com/esp-now-two-way-way-communication-esp32/

在ESP之间发送数据方面正常工作,但是现在我的传感器不断返回0,这是非常怪异的。

#include <esp_now.h>
#include <WiFi.h>

#include <Wire.h>
#define SensorPin 27 
float sensorValue = 0; 
// REPLACE WITH THE MAC Address of your receiver 
uint8_t broadcastAddress[] = {0xA8, 0x03, 0x2A, 0xF3, 0x27, 0x88};

// Define variables to store BME280 readings to be sent
float temperature;
// Define variables to store incoming readings
float incomingTemp;
// Variable to store if sending data was successful
String success;

//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
    float temp;
} struct_message;

// Create a struct_message called BME280Readings to hold sensor readings
struct_message BME280Readings;

// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;

esp_now_peer_info_t peerInfo;

// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  if (status ==0){
    success = "Delivery Success :)";
  }
  else{
    success = "Delivery Fail :(";
  }
}

// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  incomingTemp = incomingReadings.temp;
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);

 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {
  Serial.println("Sensor readings:");
  


  getReadings();

  // Set values to send
  BME280Readings.temp = analogRead(SensorPin); // This returned 1800-2000 before, now it returns 0

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &BME280Readings, sizeof(BME280Readings));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  //updateDisplay();
  delay(5000);
}
void getReadings(){
  temperature = analogRead(SensorPin);
}

void updateDisplay(){ // currently not using this method because I do not care about getting data from the other ESP right now.
  
  // Display Readings in Serial Monitor
  Serial.println("INCOMING READINGS");
  Serial.print("Temperature: ");
  Serial.print(incomingReadings.temp);
  Serial.println(" ºC");
}

I have connected an ESP32 LoRa to a moisture sensor. I have a script for reading data from the sensor, which works perfectly fine on its own:


#define SensorPin 27 
float sensorValue = 0; 
void setup() { 
 Serial.begin(9600); 
} 
void loop() { 
 Serial.println(analogRead(SensorPin)); 
 delay(1000); 
} 

It returns about 1800-2000 when it is in some dirt.

I then want to send the data to another ESP32 LoRa, where i have used this tutorial: https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/

It works fine in terms of sending data between the ESPs, but now my sensor constantly returns 0, which is super weird.

#include <esp_now.h>
#include <WiFi.h>

#include <Wire.h>
#define SensorPin 27 
float sensorValue = 0; 
// REPLACE WITH THE MAC Address of your receiver 
uint8_t broadcastAddress[] = {0xA8, 0x03, 0x2A, 0xF3, 0x27, 0x88};

// Define variables to store BME280 readings to be sent
float temperature;
// Define variables to store incoming readings
float incomingTemp;
// Variable to store if sending data was successful
String success;

//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
    float temp;
} struct_message;

// Create a struct_message called BME280Readings to hold sensor readings
struct_message BME280Readings;

// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;

esp_now_peer_info_t peerInfo;

// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  if (status ==0){
    success = "Delivery Success :)";
  }
  else{
    success = "Delivery Fail :(";
  }
}

// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  incomingTemp = incomingReadings.temp;
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);

 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {
  Serial.println("Sensor readings:");
  


  getReadings();

  // Set values to send
  BME280Readings.temp = analogRead(SensorPin); // This returned 1800-2000 before, now it returns 0

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &BME280Readings, sizeof(BME280Readings));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  //updateDisplay();
  delay(5000);
}
void getReadings(){
  temperature = analogRead(SensorPin);
}

void updateDisplay(){ // currently not using this method because I do not care about getting data from the other ESP right now.
  
  // Display Readings in Serial Monitor
  Serial.println("INCOMING READINGS");
  Serial.print("Temperature: ");
  Serial.print(incomingReadings.temp);
  Serial.println(" ºC");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青朷 2025-02-03 06:54:36

使用WiFi时 - 您无法将ADC2引脚用于模拟输入。我看过同样的问题。这是 link 在ESP32 github页面上进行了讨论。

将传感器切换到ADC1引脚之一,应该可以工作。

When using WiFi - you can't use the ADC2 pins for analog input. I've seen the same issue. Here's a link showing a discussion on the ESP32 github pages.

Switch your sensor to one of the ADC1 pins and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文