Problem in Data logging to Firebase ESP32 and AHT20 sensor

Hey guys, I have a problem when I upload the code in Arduino. The problem is
AHTFire:167:43: error: 'class Adafruit_AHTX0' has no member named 'readTemperature'
json.set(tempPath.c_str(), String(aht.readTemperature()));
^
AHTFire:168:42: error: 'class Adafruit_AHTX0' has no member named 'readHumidity'
json.set(humPath.c_str(), String(aht.readHumidity()));
^
AHTFire:169:43: error: 'class Adafruit_AHTX0' has no member named 'readPressure'
json.set(presPath.c_str(), String(aht.readPressure()/100.0F));

Here is my code :


#include <Arduino.h>
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_AHTX0.h>
#include "time.h"

// Provide the token generation process info.
#include "addons/TokenHelper.h"
// Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"

// Insert your network credentials
#define WIFI_SSID "thar_*****"
#define WIFI_PASSWORD "*****"

// Insert Firebase project API Key
#define API_KEY "**********"

// Insert Authorized Email and Corresponding Password
#define USER_EMAIL "*********"
#define USER_PASSWORD "*****"

// Insert RTDB URLefine the RTDB URL
#define DATABASE_URL "https://iotlab2-a1a47-default-rtdb.asia-southeast1.firebasedatabase.app/"

// Define Firebase objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

// Variable to save USER UID
String uid;

// Database main path (to be updated in setup with the user UID)
String databasePath;
// Database child nodes
String tempPath = "/temperature";
String humPath = "/humidity";
String presPath = "/pressure";
String timePath = "/timestamp";

// Parent Node (to be updated in every loop)
String parentPath;

int timestamp;
FirebaseJson json;

const char* ntpServer = "pool.ntp.org";

// BME280 sensor
Adafruit_AHTX0 aht; // I2C
float temperature;
float humidity;
float pressure;

// Timer variables (send new readings every three minutes)
unsigned long sendDataPrevMillis = 0;
unsigned long timerDelay = 180000;

// Initialize BME280
void initAHT(){
  if (! aht.begin()) {
    Serial.println("Could not find a valid AHT20 sensor, check wiring!");
    while (1);
  }
}

// Initialize WiFi
void initWiFi() {
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
  Serial.println();
}

// Function that gets current epoch time
unsigned long getTime() {
  time_t now;
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    //Serial.println("Failed to obtain time");
    return(0);
  }
  time(&now);
  return now;
}

void setup(){
  Serial.begin(115200);

  // Initialize BME280 sensor
  initAHT();
  initWiFi();
  configTime(0, 0, ntpServer);

  // 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;

  Firebase.reconnectWiFi(true);
  fbdo.setResponseSize(4096);

  // Assign the callback function for the long running token generation task */
  config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

  // Assign the maximum retry of token generation
  config.max_token_generation_retry = 5;

  // Initialize the library with the Firebase authen and config
  Firebase.begin(&config, &auth);

  // Getting the user UID might take a few seconds
  Serial.println("Getting User UID");
  while ((auth.token.uid) == "") {
    Serial.print('.');
    delay(1000);
  }
  // Print user UID
  uid = auth.token.uid.c_str();
  Serial.print("User UID: ");
  Serial.println(uid);

  // Update database path
  databasePath = "/UsersData/" + uid + "/readings";
}

void loop(){

  // Send new readings to database
  if (Firebase.ready() && (millis() - sendDataPrevMillis > timerDelay || sendDataPrevMillis == 0)){
    sendDataPrevMillis = millis();

    //Get current timestamp
    timestamp = getTime();
    Serial.print ("time: ");
    Serial.println (timestamp);

    parentPath= databasePath + "/" + String(timestamp);

    json.set(tempPath.c_str(), String(aht.readTemperature()));
    json.set(humPath.c_str(), String(aht.readHumidity()));
    json.set(presPath.c_str(), String(aht.readPressure()/100.0F));
    json.set(timePath, String(timestamp));
    Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str());
  }
}

have a look at Adafruit_AHTX0.h - make sure you have the method names correct, e.g. should readTemperature() be getTemperatureSensor()
have you had a look at Adafruit_AHTX0 examples

1 Like

yeah all correct. I found my problem it should be like this

json.set(tempPath.c_str(), String(temp.temperature));
    json.set(humPath.c_str(), String(humidity.relative_humidity));
    json.set(timePath, String(timestamp));

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.