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

Why does my sensor return 0 when using ESP-NOW Two-Way Communication?

我已经将 ESP32 LoRa 连接到湿度传感器。我有一个用于从传感器读取数据的脚本,它本身运行良好:


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

它 returns 大约 1800-2000 当它在一些泥土中时。

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

它在 ESP 之间发送数据方面工作正常,但现在我的传感器一直 returns 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");
}

使用 WiFi 时 - 您不能将 ADC2 引脚用于模拟输入。我见过同样的问题。这是一个 link 显示关于 ESP32 github 页面的讨论。

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