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

Help --------How to send uint8_t array which has const byte values to firebase? #66

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi, I'm using adafruit pn532 library (https://github.com/adafruit/Adafruit-PN532) to read nfc tags. Here it is reading the unique id into a uint8_t array as const byte values. How can we send this data into the real time database?

Thank you. Hope you and your loved ones are safe. Thank you.

mobizt commented 4 years ago

You can cast the uint8_t array to const char array (c string) and pass this value to setString or pushString function.

uint8_t myArr[] = {,,,,,};
Firebase.setString(firebaseData, "Path_to_your_node",  (const char*)myArr);

When reading it back, cast it back to uint8_t for each char.

Firebase.getString(firebaseData, "Path_to_your_node");

uint8_t *myArr2 = new uint8_t[firebaseData.stringData().length()];
for (size_t i = 0; i< firebaseData.stringData().length();i++){
  myArr2[i] = (uint8_t)firebaseData.stringData().c_str()[i];
}
ghost commented 4 years ago

Hi, I tried it . Firebase.setString(firebaseData, (const char*)uid, "test1");

Since the uint8_t array has byte values it is not working properly.If you see below you can see the result in firebase. 1

The same value ( as the pic above )comes in arduino serial monitor when I'm using Serial.print function - Serial.print(uid); . When I'm individually printing the values in each position using a for loop as given below it is working out fine.

for (uint8_t i=0; i < 7; i++) 
    {
      Serial.println(uid[i]); 
    }

The answer for the above code in serial monitor is this;

119 153 148 53 0 0 0

I think that the issue is in converting byte to char or string . When the number of digits in a position of the array is above 1 it is not working. What can we do here?

I want the result in firebase like this;

11915314853000

I tried this one before. Same issue ;

  FirebaseJsonArray arr;
  arr.set("/[0]/[1]", (const char*) uid);
  Firebase.setArray(firebaseData, "test1", arr);

Thank you

mobizt commented 4 years ago

It is because the string is a null-terminated char array. If any byte data in your byte array is zero (null char), the casted string will be truncated.

Binary data in Firebase RTDB is not supported. The library provides an indirect way to store these binary data as base64 encoded string with function setBlob, pushBlob and getBlob.

Please see this example for the usage.

Typo in my above comment about setString, it should be

Firebase.setString(firebaseData, "Path_to_your_node",  (const char*)myArr);
ghost commented 4 years ago

Hi , thank you for the fast response. I have one query. Is there a way where we can send data without the prefix blob,base64, . It's coming like this in the rtdb. 1

and I wish to have just the encoded value alone. What can be done here?

Thanking you in advance.

mobizt commented 4 years ago

Did you see the example? Use getBlob function. https://github.com/mobizt/Firebase-ESP32/blob/master/examples/Blob/Blob.ino#L138

ghost commented 4 years ago

I'm making an android app where a string query will be done. If it is just the encoded string then it is easy to do in the RTDB , right?

mobizt commented 4 years ago

See this https://github.com/mobizt/Firebase-ESP32/issues/65#issuecomment-602629455 Then https://stackoverflow.com/questions/44631141/base64-decode-string-in-android/44631222

ghost commented 4 years ago

Thank you, but won't it make the app slower with more computations. I'm just worried about the computation time. Also I'm not planning to decode the base64 string. I'm planning to take the string as such and compare with another encoded strings from a table to see if the ID belongs to us.

mobizt commented 4 years ago

Do you know about base64 encode/decode algorithm? It just the basic thing in the computer world.

It also uses in email transactions. The attachment (any binary data) will be converted to base64 string prior to sending it. It can implement fast in 8-bit MCU, low clock speed as MCS51 too.

Please learn how to encode and decode first.

If you intend to use byte array as a string, you need to convert your byte array to string by cast it as I mentioned in the above post.

String myString = (char*)myByteArray;

So print to serial to check you get the right string before storing to RTDB.

Sorry, I need to close conversation due to my posts tell all your requirements and you need to learn more about how to handle byte and char array.