mobizt / Firebase-ESP32

[DEPRECATED]🔥 Firebase RTDB Arduino Library for ESP32. The complete, fast, secured and reliable Firebase Arduino client library that supports CRUD (create, read, update, delete) and Stream operations.
MIT License
415 stars 118 forks source link

Strange behavior from library to a specific data after new update #184

Closed floatAsNeeded closed 3 years ago

floatAsNeeded commented 3 years ago

Hi!

I just found a weird issue that I was thinking is related to float numbers but it also seems not be related at the same time... I try to explain better:

Basically, I was using the version of the library 3.11.2 and I encountered a problem when doing the get function

  if (Firebase.getFloat(Weather, "SHT3x/Temperature/Temperature"))
  {
    temp = Weather.floatData() + OffsetTemp;
  }
  else
  {
    Serial.println(Weather.errorReason());
  }

temp is float data and also OffsetTemp. Basically it will get a data completely random from database and not the actual data, and this happens JUST for this raw. I've other raw with others float data, the same like that and this happens just to the first data. It is very strange. Temperature max and min will get with no problems, but not temperature, that will return a random number without error from database (I checked if there are any errors but everything works fine)

Temperature: 11.15702 TemperatureMax: 12.31861 TemperatureMin: 8.64157

Temperature max and min and also other data float will get with no issues! Just one in particular won't get that is the one I posted. After downgrading to the version 3.11.0 of the library it works again as always. I was wondering if is something related to some changes on how to get float data. Must be something related to the library.

Unfortunately I can't replicate the error just using the example of the library, because as I said it is a really strange problem, and happens just on that raw, no other raw. And it doesn't give any error either.

It will be good to follow up on this.

My version of Arduino is 1.8.13 Esp32 1.0.6

P.S. I just noticed in the example in the new version there is also a line called

Firebase.getDouble

I tried also with getdouble but same result, the issue just goes away by using the 3.11.0 version of the library

mobizt commented 3 years ago

Can you test with download the zip file of latest version on Github and replace the version that installed on the Arduino Library Manager?

There are some changes in mainstream but not the released version.

mobizt commented 3 years ago

I did not found any issue related to your post as I test from the example.

You should create the simple example code which clean and complete that can reproduce the issue and I will check from that.

floatAsNeeded commented 3 years ago

It is in fact a really strange issue. I will check now downloading the zip version. I'll let you know! Thank you

mobizt commented 3 years ago

Since v3.11.0, there is some bugs in data type checking.

In v3.11.1 and latest, this bugs were fixed.

With the latest version, data type mismatch checking is disabled by default, if you intend to get some data with getBool, getInt, getFloat, getDouble functions which the response payload data type does not match to the type you request,, and you call intData(), floatData() or doubleData from FirebaseData object, the value that returns will be the original value of previous (old) payload data prior to the current request.

This is because the internal payload was unchanged as you try to get the wrong type from payload.

To enable data type checking, you need to set this config.

config.rtdb.data_type_stricted = true;

floatAsNeeded commented 3 years ago

It could be there the problem maybe, but that's strange happens just with that specific raw, the other raw is basically the same, and it doesn't have the error. Maybe because as you said is maybe some old data, but that's strange because is totally a random number, like 67 and it just happens on that raw. Where I can set that line, in setup? Is that line also make slower the receiving of data? Because it says config is not declared. Is it have I to put that in some other files?

Tried the zip file and nothing. I will try that line you suggested me

Also another question: is the same thing apply for your other library ESP8266? Or is different?

mobizt commented 3 years ago

You can set/unset at any place in your code, may be before you call getXXX and can be disabled after that. Nothing is different in speed due to small peace of type checking code from the currently downloaded response payload.

I recommend to use generic get function and check the data type before use. The latest version, I introduce to\<type>() which you can cast to any value type instead of fix type e.g. intData or floatData.

You can check the new examples.

Data type mismatch checking is applied to all my Firebase libraries.

floatAsNeeded commented 3 years ago

Thanks for the help! I just tried with the new version

temp = Weather.to<float>() + OffsetTemp;

but when I added the line:

_config.rtdb.data_typestricted = true;

it says that "config was not declared in this scope"

Sorry I need to declare this first

FirebaseConfig config;

floatAsNeeded commented 3 years ago

I'm using like this, but still same issue. mmm Do I wrong something?

  config.rtdb.data_type_stricted = true;
  if (Firebase.getFloat(Weather, "SHT3x/Temperature/Temperature"))
  {
    temp = Weather.to<float>() + OffsetTemp;
  }
  else
  {
    Serial.println(Weather.errorReason());
  }
floatAsNeeded commented 3 years ago

If I use this simple code works. That's odd

#include <FirebaseESP32.h>
#include <WiFi.h>
////////////////////*********FIREBASE DETAILS************///////////////////////////////////
#define FIREBASE_HOST ""                 // the project name address from firebase id
#define FIREBASE_AUTH ""            // the secret key generated from firebase
FirebaseData Weather;
FirebaseConfig config;
float temp;
float OffsetTemp;

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin();
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    //Serial.print(".");
  }
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);   // connect to firebase
  //Serial.println(Weather.errorReason());
  Firebase.reconnectWiFi(true);
  Firebase.setMaxRetry(Weather, 2);
  config.rtdb.data_type_stricted = true;
  if (Firebase.getFloat(Weather, "SHT3x/Temperature/Temperature"))
  {
    temp = Weather.to<float>() + OffsetTemp;
  }
  else
  {
    Serial.println(Weather.errorReason());
  }
}

void loop() {
  Serial.println(temp);
  delay(2000);
}

It is indeed very strange because this issue just happens on that specific raw and just on the new 3.11.1 and after version of the library

floatAsNeeded commented 3 years ago

I found the issue. Eventually was my fault. Seems the issue is related not on that line of the Firebase, but instead on this line

  if (Firebase.getFloat(Weather, "SHT3x/Offset"))
  {
    OffsetTemp = Weather.to<float>();
  }

Basically, I was getting that data in wrong path of the database and it returned me 62 that then plus to the temperature it gave a weird reading. That raw doesn't exist anymore.. I don't know how in the previous version it could detect, or maybe before it just was ignoring that.

Anyway now I fixed that raw with the correct get from the database and now is all working good

Also I didn't need to use config.rtdb.data_type_stricted = true;