B4R Tutorial How to get and parse Json from an Http server, inline C code

Hello,

Since a while i searched to do the same thing i did on an ESP01, but with B4R.
I did it 1st time in Arduino IDE o_O, see the video at bottom, that works well :)

I searched long time to see for a library, code, an example in B4R to parse the Json, but no luck :(.
I read i was not the alone, several time, who search a solution.
So, as i didn't found anything i have do it in Inline C code.
Attached the source code, i hope that'll be helpful.

I have put many comments in the source, i also put a Region with all informations to do the setup to run the example. Read it.
You need to have ArduinoJson library in your own Arduino lib folder.
I'm not a gourou in C, C++, so, i think many of us will can improve it, use it to do many things.
Enjoy !

Of course for the example you need to have Domoticz who running to interact with.
But it's a good starting to get many things other on this base.
I continue to work to do the same on different weather servers on the web.
Also i'll try to implement other devices from Domoticz, at this time it's a Temperature/Humidity device.

If i can i'll do a library, but i catched a big headhache already :D
If you have questions, don't hesitate.

Regards,
Laurent
 

Attachments

  • parseJson_InlineC_Domoticz.zip
    4.8 KB · Views: 416

hatzisn

Well-Known Member
Licensed User
Longtime User
Excellent work Laurent.
 

Laurent95

Active Member
Licensed User
Longtime User
Hello,
Thank you @ hatzisn,
You're not "the boy next door" too with the many things you did and post
;)


Have a nice day all
 

hatzisn

Well-Known Member
Licensed User
Longtime User
Thank you for your good words Laurent. I still have a lot to learn as my C-like languages (C++, Java, Objective C) are limited. My only C-like language that I have more experience is C# although I prefer to program in Visual Basic. If you say to me that i am not "the boy next door" then what can you say about @Erel who built the B4x universe to give us endless possibilities.
 

hatzisn

Well-Known Member
Licensed User
Longtime User
:)
 

Laurent95

Active Member
Licensed User
Longtime User
Hello,

I saw already your example code.
And i'm not stubborn and know well that, even i'm 64, i can learn yet

So, i study your method this morning, result of tests for my next step, tons of errors :confused:
But indeed that'll surely work with the 1st example above :)


But the final trick, i prepare another example, it's also to get weather, i said it in 1st post.
I continue to work to do the same on different weather servers on the web.
It's a bit more complicated, for weather there are several similar "Json Objects" for each forecast daily.
Each of them are indexed in a list, in that case. I tried it this morning.
To be honest i even don't saw how it can be solved with ByteConverter without tons of lines code.

The advantage of ArduinoJson it's that library parse all, with a structure who stay the exact reflect of the Json.
At my idea really more readable to decide which values we want or not and more easy to transpose the code to get the good ones.
Yep, the more useful thing i learned in my life is, but why reinvent the wheel ? :D

That stay my own opinion and in any case don't means that your code is wrong :)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yep, the more useful thing i learned in my life is, but why reinvent the wheel ? :D

That stay my own opinion and in any case don't means that your code is wrong
I agree. The code I posted is not a complete json parser and there is nothing bad with your code.

As an experiment you can post the complete json string and I will try to show the B4R code to parse it.
 

Laurent95

Active Member
Licensed User
Longtime User
I agree. The code I posted is not a complete json parser and there is nothing bad with your code.

As an experiment you can post the complete json string and I will try to show the B4R code to parse it.

Ho YES ! :) You, our gourou, can do that really more quickly than me :p
But i want not abuse of your time, i hate that :( I imagine you're busy with all.

Me, i continue to fight with the arrays of bytes, between the C string and this crap of String class in Arduino it's a nightmare for me :D

Attached the 2 Json from Openweathermap, current weather and forecast daily.
I put the 2 URL to use in case you need it, but i get out my Api Key. If you need i can share in PM.

Regards
Laurent
 

Attachments

  • Json_OWM.zip
    1.9 KB · Views: 270

Erel

B4X founder
Staff member
Licensed User
Longtime User
The problem with the Arduino strings is that they are allocated dynamically. This can eventually lead to memory fragmentation.
It is better to allocate everything statically. This assures that the program will run forever without memory issues.

Example of parsing some of the values from the first file:
B4X:
Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   Dim json As String = "{""coord"":{""lon"":2.82,""lat"":44.38},""weather"":[{""id"":804,""main"":""Clouds"",""description"":""overcast clouds"",""icon"":""04d""}],""base"":""stations"",""main"":{""temp"":15.21,""pressure"":1016,""humidity"":82,""temp_min"":15,""temp_max"":15.56},""visibility"":10000,""wind"":{""speed"":6.7,""deg"":290},""clouds"":{""all"":90},""dt"":1570022051,""sys"":{""type"":1,""id"":6480,""message"":0.006,""country"":""FR"",""sunrise"":1569995221,""sunset"":1570037352},""timezone"":7200,""id"":3008636,""name"":""Laissac"",""cod"":200}"
   Dim humidity As Double = GetNumberValueFromKey(json, "humidity", 0)
   Dim temperature As Double =  GetNumberValueFromKey(json, "temp", 0)
   Log("humidity: ", humidity)
   Log("temperature: ", temperature)
   Dim MaxLength As Int = 50
   Dim buffer(MaxLength) As Byte
   GetTextValueFromKey(json, "main", 0, buffer, MaxLength)
   Log("Weather status: ", buffer)
   GetTextValueFromKey(json, "description", LastIndex, buffer, MaxLength)
   Log("Weather description: ", buffer)
End Sub
Based on: https://www.b4x.com/android/forum/threads/json-parsing.107410/
 

Laurent95

Active Member
Licensed User
Longtime User
The problem with the Arduino strings is that they are allocated dynamically. This can eventually lead to memory fragmentation.
It is better to allocate everything statically. This assures that the program will run forever without memory issues.

Hello,

I know well, it's why i continue to fight between C string and String Arduino class for the inline C example.
To be honest i didn't even understand why they let that if that's not safe ?
In more even with new version they continue to let all examples on String class, who are numerous compared to other examples :rolleyes:
In fact the new team is more commercial than developers, as before :mad:

But as i have another example from you, good news :

I prepare an example for all in B4R directly, 1st part works already with all the data, present in the json example or not.
Yes there are possibilities of Rain, Snow, who came only when there are.. :confused:

I will do the same for forecast daily too.

Regards
Laurent
 
Last edited:

Laurent95

Active Member
Licensed User
Longtime User
NEW, NEW, NEW

Hi all,

I promised it, it's done. New example
A parser for http requests on the Openweathermap weather site, totally in B4R code.
Just one library, very useful, for the treatment of timestamps.

I bet it will be useful for many of us, enjoy !
My project is starting to be more advanced now ;)
If you ever find an error, I tested the code a lot, but I could forget something, tell me.
Or if you make an improvement, please share it.
If you have a question, ask.

Thank you all for your support on the first example :)

Regards,
Laurent
 

Attachments

  • parseJson_B4R_OWM.zip
    20.1 KB · Views: 316
Top