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.
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
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.
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
@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.
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..