问题:我面临的问题是 ECG 传感器读数始终显示 0,并且发送到 Firebase 的数据不准确。我已经检查过连接,它们似乎是正确的。
附加信息:
我已验证 ECG 传感器正在接收正确的电源电压。
我还检查了导联脱落检测引脚(leadOffPin1 和 LeadOffPin2),它们似乎工作正常。
与 Firebase 的 Wi-Fi 连接已成功建立。
具体问题:
心电图传感器无法提供有效读数的可能原因是什么?如何解决此问题?
如何确保发送到 Firebase 的数据准确代表 ECG 传感器读数?
将 ECG 传感器与 ESP32 和 Firebase 集成时是否有我应该注意的常见陷阱或最佳实践?
这里是半代码
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <Wire.h>
#include <Arduino.h>
const int sensorPin = 14;
const int leadOffPin1 = 22;
const int leadOffPin2 = 23;
const char* ssid = "PROFESSOR";
const char* password = "star2star";
FirebaseConfig firebaseConfig;
FirebaseAuth firebaseAuth;
FirebaseData firebaseData;
#define REPORTING_PERIOD_MS 1000
#define Buzzer 25
uint32_t tsLastReport = 0;
volatile int ecgValue = 0;
void sensorTask(void *pvParameters) {
for (;;) {
// Check if either of the lead-off detection pins are indicating a lead-off condition
if (digitalRead(leadOffPin1) == 1 || digitalRead(leadOffPin2) == 1) {
Serial.println("0"); // If lead-off is detected, print "0" to the Serial Plotter
delay(500); // Wait for a while before the next check
} else {
// Read the ECG sensor value
int ecgValue = analogRead(sensorPin);
Serial.println(ecgValue); // Print the ECG sensor value to the Serial Plotter
delay(400); // Delay for stability and to match the rate of lead-off checks
}
// Check if it's time to send data to Firebase based on a time interval
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
// Send the ECG sensor value to Firebase under the path "/ECG/ECGVALUES"
Firebase.setInt(firebaseData, "/ECG/ECGVALUES", ecgValue);
Serial.println("ecg sent"); // Print a message indicating that the ECG data was sent
}
delay(10); // Small delay to control the loop speed
}
}
void setup() {
Serial.begin(9600); // Initialize serial communication with a baud rate of 9600.
pinMode(leadOffPin1, INPUT); // Set leadOffPin1 as an input pin.
pinMode(leadOffPin2, INPUT); // Set leadOffPin2 as an input pin.
WiFi.begin(ssid, password); // Connect to the Wi-Fi network with the specified SSID and password.
while (WiFi.status() != WL_CONNECTED) {
delay(500); // Wait for the Wi-Fi connection to establish. Checks every 500 milliseconds.
}
firebaseConfig.host = "ckd-web-interface-default-rtdb.firebaseio.com"; // Set the Firebase host URL.
firebaseConfig.api_key = "AIzaSyAhelk_39kII2gWBtqYZvf0BLbpb6VLkaE"; // Set the Firebase API key.
firebaseAuth.user.email = "[email protected]"; // Set the Firebase authentication email.
firebaseAuth.user.password = "1234567"; // Set the Firebase authentication password.
Firebase.begin(&firebaseConfig, &firebaseAuth); // Initialize the Firebase connection.
Firebase.reconnectNetwork(true); // Reconnect to the Firebase network if the connection is lost.
xTaskCreatePinnedToCore(sensorTask, "SensorTask", 10000, NULL, 1, NULL, 0); // Create a separate task named "SensorTask" with specified parameters.
}
您的代码从 GPIO14 读取模拟值,同时还使用 WiFi。
GPIO14 在 ADC2 上,当 WiFi 处于活动状态时不可用。
来自ESP32的GPIO文档:
ADC2:当使用 Wi-Fi 时,ADC2 引脚无法使用。所以,如果你是 使用 Wi-Fi 时无法从 ADC2 GPIO 获取值, 您可以考虑使用 ADC1 GPIO,这应该可以解决您的问题 问题。
ADC2 引脚为 0、2、4、12、13、14、15、25、26 和 27。使用 WiFi 时请勿尝试从这些引脚读取模拟值。 ADC1 引脚为 32 至 39。请改用这些引脚之一。
您询问如何解决此问题:有理由期望您能够随时从 ADC2 引脚读取模拟值,但是当您发现获得意外值时,您可以尝试将引脚连接到一个已知的值并看看是否有效。如果没有,您就知道
analogRead()
没有按预期工作。
阅读文档也是一个很好的开始。