Android Question B4A and firebase database.

DonManfred

Expert
Licensed User
Longtime User
To have the really flabbergasting feelings you have to use the Realtime lib from @DonManfred !!!
If you use the Realtime lib and write some data to a node from one device, all other online devices of that connection are updated locally in milliseconds. If some devices are offline they will be updated automatically if the device is online later.
Additional i need to say that at present day i´m not able to tell the right way to store/retrieve Values from the database. With the lib itself and not with rest :D
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
@Daniel-White and @freedom2000 be aware, the example from #28 uses the "REST api" and not the "Realtime lib".

The REST api ist helpful for the demonstrated PUT, GET and DELETE methods.
E.g. if you change data via PUT from one device the data is changed in the cloud, but the other devices are not aware of the change until you perform a GET and compare the local data.

To have the really flabbergasting feelings you have to use the Realtime lib from @DonManfred !!!
If you use the Realtime lib and write some data to a node from one device, all other online devices of that connection are updated locally in milliseconds. If some devices are offline they will be updated automatically if the device is online later.
Thank you to you Fredo !

I will test @DonManfred 's code to get the good vibration of real time update :)

But what I need is your code as I want to communicate in a one way between Smartphone (write proctected) and a sensor (read only)
I am not yet sure that it can work easily but could be really fun to implement a simple communication for IoT
 
Upvote 0

Daniel-White

Active Member
Licensed User
Longtime User
Doing some test with the mysterious lib of DonManfred LoL :eek: , I noticed when I applied an example like this:

B4X:
ref.Initialize("Reference",realtime.getReferencefromUrl("https://InTheMiddleOfNoWhere.firebaseio.com/imagesizes/0/ext"/"))) 
ref.setValue("IMG_20160602_163626.jpg","Finally perhaps I  know")


and import from FB DB console, the json show me these:

.value : "IMG_20160602_163626.jpg"
.priority : "Finally perhaps I know"

So I will use ref.setValue("IMG_20160602_163626.jpg","") and put the priority in Null. I don't need in my project.

I had been reading the docs and the priority can help us to organize the data. I am not sure, if this is related with this but sound like that https://www.firebase.com/docs/web/api/firebase/setpriority.html .

about setValue and how save data https://www.firebase.com/docs/android/guide/saving-data.html
 
Last edited:
Upvote 0

fredo

Well-Known Member
Licensed User
Longtime User
This topic has grown and there is much useful content for it. It would not be a good idea to give a compiled ?

Yes absolutely.

Since the Firebase Realtime Database takes so much burden (hosting, security, access, performance, continuity, scalability, ...) from developers for cloudoriented apps it would be a real benefit for the B4X community to have a tutorial series or a Wiki for the Firebase concept.

We have the first steps with @DonManfred 's Realtime library and the REST example project but today we all lack of expert knowledge of best practices for data structuring in a NoSQL environment.

And even if there is a lot of fbrtdb information for saving and retrieving variants out there, the samples have all to be translated from JAVA to B4X (and this will be not so quick and easy since there is no JAVA to B4X converter).

If someone takes the initiative to structure the know-how, I am willing to contribute.

Tags: #FIBAS
 
Last edited:
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
But what I need is your code as I want to communicate in a one way between Smartphone (write proctected) and a sensor (read only)
I am not yet sure that it can work easily but could be really fun to implement a simple communication for IoT

YESSSSS it works

I have successfully got a GET communication between an ESP8266 and firebase DB

A few difficulties however :
- connection MUST be https
- connection can be password protected using App's password (not Google's credential !)

That's a really fantastic nice feature :
- configure your ESP with a simple json file
- and next : send data from your sensors and push a firebase notification to your phone

well endless possibiities in iot world
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
It works as well for writing !

Although a little out of topic and not in BAR... I publish the Sketch code (C like for Arduino)

Here is the code for GET :

B4X:
//reading data with GET
  String url = "/.json";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(200);*/

and the code for PUT :
B4X:
 int16_t mySensorvalue = 12345;  //a dummy example of a sensor value

  String url = "/.json?auth=" + String(firebaseAPIKEY);

  String messagebody = "{\"value1\": \"Hello from ESP8266\", \"value2\": \"";
  messagebody += String(mySensorvalue);
  messagebody +=  "\"}\r\n";

  Serial.print("requesting URL: ");
  Serial.println(url);

  //sending data with PUT :
  client.print(String("PUT ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Content-Type: application/json\r\n" +
               "Content-Length: " +
               String(messagebody.length()) + "\r\n\r\n");
  client.print(messagebody);
  Serial.println("request sent");

  //print the response

  while (client.available() == 0);
  while (client.available()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
  }

and here the log of the serial print :

B4X:
connecting to testdatabase-xxxxx.firebaseio.com
certificate matches
requesting URL: /.json?auth=your firebase password
request sent
HTTP/1.1 200 OK

Content-Length: 48

Content-Type: application/json; charset=utf-8

Access-Control-Allow-Origin: *

Cache-Control: no-cache

Strict-Transport-Security: max-age=31556926; includeSubDomains; preload



{"value1":"Hello from ESP8266","value2":"12345"}
request sent

And finally the console :
testfirebaseESP8266.jpg


The tricky part remains the https with ESP8266. (which doesn't natively support it)
Clever guys found a way ...
they use a fingerprint of the SHA1 certificate of the domain we want to access (here the full database URL : "testdatabase-xxxx.firebaseio.com")
you can get it using https://www.grc.com/fingerprints.htm and entering host : yourDataBaseRef.firebaseio.com

B4X:
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "My SSID";
const char* password = "MyPasswordThatYouWontGet";

const char* host = "testdatabase-xxxx.firebaseio.com";
const int httpsPort = 443;
const char* firebaseAPIKEY = "MyDataBasePasswordThatYouWontGet"; //get it from your firebase DB account

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "7A 00 06 00 DC 00 25 00 86 8D 66 00 48 00 0B 00 42 00 B3 0A"; //got it using https://www.grc.com/fingerprints.htm and enter host : yourDataBaseRef.firebaseio.com

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  if (client.verify(fingerprint, host)) {
    Serial.println("certificate matches");
  } else {
    Serial.println("certificate doesn't match");
  }
 
Upvote 0
Top