B4R Question Problem with pushing data to firebase realtime database...

rbghongade

Active Member
Licensed User
Longtime User
Dear friends, Just like ESP8266 Firebase push message tutorial can someone post an example for firebase database example? if it is possible.
 

rbghongade

Active Member
Licensed User
Longtime User
Actually I want to use ESP8266 to periodically log data to Firebase Database which can then be accessed with Android app.
This strategy takes care of the user being able to access all data without connecting to internet continuously. All without a dedicated server.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Hello,
I tried this code (with a pointer from Erel) but am not able to push the data into firebase realtime database:

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Dim timer1 As Timer
    Dim uid As Long
    Dim esp As ESP8266extras
    Dim bc As ByteConverter
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    uid=esp.UniqueID
    If wifi.Connect2("****","****") Then
        Log("Connected to router.")
        timer1.Initialize("timer1_Tick",2000)
        timer1.Enabled=True
    Else
        Log("Failed to connect to router.")
        Return
    End If

End Sub
Sub timer1_tick
    Dim datetime As String="26/12/2018,18:54:00"
    Dim otemp As Float=Rnd(20,30)
    Dim o As String=NumberFormat(otemp,1,2)
    Dim atemp As Float=Rnd(20,30)
    Dim a As String=NumberFormat(atemp,1,2)
    Dim u As String=uid
    Dim id As String=JoinStrings(Array As String("https://***.firebaseio.com/",u,".json"))
    Dim data As String=JoinStrings(Array As String("'",datetime,",",o,",",a,"'"))
    Log(id)
    Log(data)
    HttpJob.Initialize("")
    HttpJob.Post(id,data)
End Sub

Sub JobDone (Job As JobResult)
    Log("*******************************")
    Log("JobName: ", Job.JobName)
    If Job.Success Then
       
        Log("Response: ", bc.SubString2(Job.Response, 0, Min(200, Job.Response.Length))) 'truncate to 200 characters
    Else
        Log("ErrorMessage: ", Job.ErrorMessage)
        Log("Status: ", Job.Status)
        Log(Job.Response)
    End If
End Sub
The error I got was:
B4X:
https://***.firebaseio.com/12615289.json
26/12/2018,18:54:00,28.00,29.00
*******************************
JobName:
ErrorMessage:
Status: 400
{
  "error" : "Invalid data; couldn't parse JSON object, array, or value."
}
I am using rHttpUtils2 module.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Actually I am concatenating date time and data as a single string, hence I think the date time format should not matter.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel, I tried it in B4J with this code:
B4X:
Sub btnSEND_Click
  
    Dim j As HttpJob
    j.Initialize("",Me)
  
    Dim DT As String=DateTime.Date(DateTime.Now)&","&DateTime.Time(DateTime.Now)
    Dim otemp As Float=Rnd(20,30)
    Dim o As String=NumberFormat(otemp,1,2)
    Dim atemp As Float=Rnd(20,30)
    Dim a As String=NumberFormat(atemp,1,2)
    Dim u As String="12345678"
    Dim id As String="https://***b.firebaseio.com/"&u&".json"
    Dim data As String="{"& $"""$&"Value"&$"""$&":"& $"""$&DT&","&o&","&a& $"""$&"}"
    Log(data)
    j.PostString(id,data)
    j.Release
End Sub
Was able to push the data in database:

But how to do the same in B4R as there is no support for smart strings?
Note that the value field is not required but without that the same error mentioned appears.
 
Last edited:
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
One more observation :
The values that I get using the following code (B4J) are not in order!
B4X:
Dim j As HttpJob
    j.Initialize("",Me)
    j.Download("https://***.firebaseio.com/12345678.json")
    Wait For (j) jobdone (j As HttpJob)
    If j.Success Then
        'Log(j.GetString)
        Dim s As String=j.GetString
        Dim parser As JSONParser
        parser.Initialize(s)
        Dim root As Map = parser.NextObject
        Dim FirstValue As String = root.Get("-LUiTbdp89H7EqqBUDCm")
        Dim i As Int

        Dim otemp(root.size) As String
        Dim atemp(root.size) As String
        Dim dt(root.size) As String
        Dim tm(root.size) As String
        For Each Key As String In root.Keys
           
            Dim list1 As List
            list1.Initialize
            list1.Add (root.Get(Key))
            Dim map1 As Map
            map1 = list1.Get(0)
            Dim d As String=map1.GetValueAt(0)
            Log(d)
            Dim temp(4) As String=Regex.Split(",",d)
            dt(i)=temp(0)
            tm(i)=temp(1)
            otemp(i)=temp(2)
            atemp(i)=temp(3)
            i=i+1
           
        Next

       
    Else
        Log("Error:" &j.ErrorMessage)
       
    End If
    j.Release
   
   
End Sub

JSON string received is:
B4X:
{"-LUiTbdp89H7EqqBUDCm":{"Value":"12/27/2018,12:34:26,28,26"},"-LUiTsTmgH5MMRwe_-Be":{"Value":"12/27/2018,12:35:36,24,26"},"-LUiXlocwfiQFhPqMnC2":{"Value":"12/27/2018,12:52:36,29,24"},"-LUiZ0VhxFzQTSFW2q1i":{"Value":"12/27/2018,12:58:03,23,23"},"-LUiZ5-nnrthMgBBtUdL":{"Value":"12/27/2018,12:58:22,20,20"},"-LUjDwJMb8E20WST3b6M":{"Value":"12/27/2018,16:05:33,23,26"},"-LUjDyjjYuH5lAWgJgor":{"Value":"12/27/2018,16:05:44,27,25"},"-LUjGeZBqyJLuiOjeTio":{"Value":"12/27/2018,16:17:28,24,25"},"-LUjGjd64loOAwO3U1ee":{"Value":"12/27/2018,16:17:49,22,21"}}

Processed output:
B4X:
12/27/2018,12:34:26,28,26
12/27/2018,12:35:36,24,26
12/27/2018,16:17:28,24,25
12/27/2018,12:52:36,29,24
12/27/2018,16:17:49,22,21
12/27/2018,12:58:03,23,23
12/27/2018,16:05:33,23,26
12/27/2018,16:05:44,27,25
12/27/2018,12:58:22,20,20

PS:OK I realized that with map ordering is not preserved! But I want it ordered please let me know the way!
 
Last edited:
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
@DonManfred
Data is obtained in the way I want , it is the conversion from JSON string to Map which removes the ordering. If you see the obtained JSON string the order is fine.
Friends, I am not a software person hence asking for help at crucial steps. I have gone through the documentation already but am not able to correlate my problem. BTW this is the first time I am using Firebase Realtime Database and REST API.
 
Upvote 0

embedded

Active Member
Licensed User
Longtime User
@Erel,
How can we port the code in post #10 to B4R? We do not have smart string capability there.
Hi...I successfully logged into Firebase...but the real problem is to obtained Joan format in B4R...so I converted all the JSON format data into bytes...by ASCII code converter...and it's works for me..
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…