浊度数据不会发送到 Firebase 实时数据库中

问题描述 投票:0回答:1

我正在创建一个水监控系统。我想监测里面的PH、温度和浊度。我的问题是我的浊度数据不会发送到我的 firebase 实时数据库中。如果我想发送数据库中的数据,我的浊度不会给出数据。但是如果我删除 firebase 代码,它就可以正常工作。

#include <OneWire.h>
#include <DallasTemperature.h>
#include "DFRobot_ESP_PH.h"
#include "EEPROM.h"
#include <WiFi.h>
#include <Firebase_ESP_Client.h>

#define WIFI_SSID "ESP_32"
#define WIFI_PASSWORD "12345678"

#define API_KEY "****"

#define DATABASE_URL "****" 

#define USER_EMAIL "****"
#define USER_PASSWORD "****"

// Define Firebase Data object
FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

DFRobot_ESP_PH ph;
#define ESPADC 4096.0   //the esp Analog Digital Convertion value
#define ESPVOLTAGE 3300 //the esp voltage supply value
#define PH_PIN 35  
float phvoltage, phValue, temperature = 25;

#define SENSOR_PIN  19

OneWire oneWire(SENSOR_PIN);
DallasTemperature DS18B20(&oneWire);

float tempC; // temperature in Celsius
float tempF; // temperature in Fahrenheit
float tVoltage;
float turbidityValue;

#define BUTTON_PIN 15
#define LED_PIN 17
#define TURBIDITY_PIN 13

bool buttonState = false;

#include <ThreeWire.h>  
#include <RtcDS1302.h>

ThreeWire myWire(27,25,12); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);

/* 使用的传感器 */

void setup()
{
  Serial.begin(9600);
  DS18B20.begin();
  EEPROM.begin(32);
  ph.begin();
  Rtc.Begin();
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  /* Assign the api key (required) */
  config.api_key = API_KEY;

  /* Assign the user sign-in credentials */
  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;

  /* Assign the RTDB URL (required) */
  config.database_url = DATABASE_URL;

  // Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager
  Firebase.reconnectNetwork(true);
 read timeout can be occurred.
  fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);

  // Limit the size of response payload to be collected in FirebaseData
  fbdo.setResponseSize(2048);

  Firebase.begin(&config, &auth);

  Firebase.setDoubleDigits(5);

  config.timeout.serverResponse = 10 * 1000;

  // Setup button and LED pins
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
  // Read button state
  bool newButtonState = digitalRead(BUTTON_PIN);
  
  // Check if button state has changed
  if (newButtonState != buttonState) {
    delay(50); // Debounce
    if (newButtonState == LOW) { // Button is pressed
      sendDataToFirebase();
    }
  }
  buttonState = newButtonState;

  // Blink LED if data is being sent
  if (Firebase.ready() && newButtonState == LOW) {
    digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle LED
    delay(500); // Blink interval
  } else {
    digitalWrite(LED_PIN, HIGH); // Turn off LED
  }

  delay(100); // Adjust delay as needed
}

void sendDataToFirebase() {

  tVoltage = analogRead(TURBIDITY_PIN);
  turbidityValue = tVoltage * (5.0 / 1024.0);
  
  DS18B20.requestTemperatures();       // send the command to get temperatures
  tempC = DS18B20.getTempCByIndex(0);  // read temperature in °C
  tempF = tempC * 9 / 5 + 32; // convert °C to °F 
  
  phvoltage = analogRead(PH_PIN) / ESPADC * ESPVOLTAGE;
  phValue = ph.readPH(phvoltage, temperature);

  RtcDateTime now = Rtc.GetDateTime();
  char dateTimeString[20];
  printDateTime(now, dateTimeString); // Get date and time string

  // Print sensor data to serial monitor for debugging
  Serial.print("Temperature (C): ");
  Serial.println(tempC);
  Serial.print("PH Value: ");
  Serial.println(phValue);
  Serial.print("Turbidity: ");
  Serial.println(turbidityValue);

  // Firebase.ready() should be called repeatedly to handle authentication tasks.
  if (Firebase.ready())
  {
    String firebasePath = "/SensorData/" + String(dateTimeString) + "/";

    Firebase.RTDB.setFloat(&fbdo, firebasePath + "Temperature", tempC);
    Firebase.RTDB.setFloat(&fbdo, firebasePath + "PH Value", phValue);
    Firebase.RTDB.setFloat(&fbdo, firebasePath + "Turbidity", turbidityValue);
   
  }
  delay(2000); // Delay for 2 seconds after sending data
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt, char* dateString)
{
    snprintf_P(dateString, 
            20,
            PSTR("%02u-%02u-%04u %02u:%02u:%02u"),
            dt.Day(),
            dt.Month(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
}

 In this part of the code I Get all the sensors data analyze it and send it to the firebase database I've search on internet how to fix it. But I havent found any. I hope someone will help me fix it. 

firebase arduino-esp32
1个回答
0
投票

这可能是由于浊度值的类型所致。检查这一行:

Firebase.RTDB.setFloat(&fbdo, firebasePath + "Turbidity", turbidityValue);

类型异常将阻止 Firebase 记录它。检查该循环内的实际值并检查 Firebase 端的字段类型定义。唯一合理的解释可能就是这个了。

© www.soinside.com 2019 - 2024. All rights reserved.