E!cockpit JSON string, sort data

Hello. I’m trying and retrieve information from a JSON file. I manage to pick up the json string. But I can’t sort the data with WagoAppJSON.FbWrite_ToIEC_ByRule.
The data I try and retrieve is electricity prices in Norway during 24 hours.

link to json file https://www.hvakosterstrommen.no/api/v1/prices/2022/10-30_NO5.json

VAR
	FbWrite_ToIEC_ByRuleStrom		: WagoAppJSON.FbWrite_ToIEC_ByRule;		// Function block to sort the info from the JSON file
	stringPath						: STRING(20):='/';						// Path the JSON file uses
	My1								: FbWrite_ToIEC_ByRule;
 	xHttpsGetTrigger2				: BOOL;
	xWriteToIEC_Error				: BOOL;
	iWriteToIEC_ActualArrayRow		: INT;

	MyJsonData		: ARRAY[0..23] OF typEnergyPrice;						// JSON data to sort the data inn
	
	bTestRule		: ARRAY[0..MAX_ARRAY_ELEMENTS_CUSTOM] OF typRuleBase:=[								
							(sPath:='NOK_per_kWh',iConvertType:=eReal),		// 1
							(sPath:='EUR_per_kWh',iConvertType:=eReal),		// 2
							(sPath:='EXR',iConvertType:=eReal),				// 3
							(sPath:='time_start',iConvertType:=eString),	// 4	Tride to use eTime but got error
							(sPath:='time_end',iConvertType:=eString)		// 5
						];
END_VAR 

FbWrite_ToIEC_ByRuleStrom(														// Function block to sort the info from the JSON file
	sPathBase:= stringPath, 
	pDataIn:= ADR(unHttpsGetRxBuffer.sVar), 
	udiSizeDataIn:= SIZEOF(unHttpsGetRxBuffer.abVar), 
	aRule:= bTestRule, 
	pIecVariable:= ADR(MyJsonData), 
	iArraySizeIecVariable:= 24, 
	udiSizeIecVariable:= SIZEOF(MyJsonData), 
	xError=> xWriteToIEC_Error, 
	iActualArrayRow=> iWriteToIEC_ActualArrayRow, 
	xTrigger:= xHttpsGetTrigger2 );												// xHttpsGetTrigger2 = TRUE to run

I made a GitLab project with all the info inn.
https://gitlab.com/espenbo/Codesys_JSON_hvakosterstrommen/-/tree/main

I’ll be happy if someone can help. Mabey I could make a HowTo for other that are trying the same.

Hi @espenbo ,
For this purpose, the SAX parser of the WagoAppJSON is perfect.
You just need to define the token, and the associated rule to process the string data.

You’ll find attached the way to implement it in your example.
eCockpit_Json.txt (139.7 KB)
(This is an export file change the extension file to .export)
Set xTriggerJSONParser to start the parsing process.

3 Likes

Thanks.
I got it to work. Get my prices. I added a sort function. And fixed a bug when the days a between 1 and 9.

I Uploaded a new version on GitLab if someone else is interested.

1 Like

I see that you created some big function for formatting time, take a look at WagoAppString.TimeComponents_To_String where you can easy format time/date:

2 Likes

I am continuing to work on this project and have added HMI tables. I also added extra information in typEnergyPrice.
There I added iArrayPlace to have an overview of where in the array the info is located when I sort from cheap to most expensive. But when I added iArrayPlace then I get weird values in HMI tables


In array 1, 3, 5, etc., I get 125.0 as the value. But when I look in the SortedJsonData Array, I don’t get those values.

When I remove. iArrayPlace : INT;

TYPE typEnergyPrice :
STRUCT
	NOK_per_kWh		: REAL;
	EUR_per_kWh		: REAL;
	EXR				: REAL;
	time_start		: DATE_AND_TIME;
	time_end		: DATE_AND_TIME;
	NOK_per_kWh_mva : REAL;
//	iArrayPlace		: INT;
END_STRUCT
END_TYPE

AND the sorthing in HTTP_HvaKosterStrommen. I get the right vaules in tables inn HMI.

// Add array placement in array	
//	FOR i := 0 TO (n) DO
//		SortedJsonData[i].iArrayPlace := i;
//	END_FOR

I have tride to delete the tables in HMI and add it back. But I get the same result.

Code at GitLab

2 Likes

delete “{attribute ‘pack_mode’ := ‘0’}” from typEnergyPrice
image

1 Like

Hello,

I’am working in a project similar to you. I have to decode a LoraWan Signal to the wago parcel. For that i’am using IoT Sensors. Those sensors will send the information into the LoraWan.

Do think this Library will suits my project ?

I’am using Codesys 3.5 V.

Thank you

image

Hello,
Yes this library will suit your requirement.
We also have a LoRaWAN solution based on Multitech gateways in France :wink:
We have already decoded Watteco, Adeunis, and Nexelec sensors.

2 Likes

thank you very much for the help

I know this is an old thread, but have to try anyway. :innocent:

I’m using FbHTTPs_Get, where the receive buffer is dword, how do I convert it to string or json-format? From your posts it seems like you display stringformat on the functionblock.

Show the code how you do.

I’ve recently started looking at this, and used “https://www.hvakosterstrommen.no/api/v1/prices/2022/10-30_NO5.json” as example, as interfacing my router was obviously too hard due to certificate issues, but need to resolve the basics before I take the next step. There are no errors in the status of the function block.

It seems like its just a converting job that need to be done as the hexadecimal value in the buffer

(“5B 7B 22 4E 4F 4B 5F 70 65 72”) means ‘[{"NOK_per’

image

:slight_smile:

You need to add to the project:
1)
Type Myunion:
Union
WRECEIVEBUFFER: Dword: = 50,000;
Sreceivestring: String (1200);
End_union
End_type

  1. replace and supplement the program in the program:
    Var
    Mybuffer_get_commands_0: Myunion;
    int_0: int;
    _result_string_1: string (1023);
    End_var

PRXBUFFER: = Adr (Mybuffer_get_commands_0),
UDIRXBUFFERSIZE: = SIZEOF (Mybuffer_get_commands_0),

int_0 := LEN (myBuffer_Get_Commands_0.sReceiveString);
_result_string_0 := LEFT (myBuffer_Get_Commands_0.sReceiveString, int_0-2 );

So you will see the text line and work with it. Correctly I understand, do you need a text in the end?

1 Like

Yes, perfect!

It gave me the expected result. Thank you very much! Now back to the 5G-router :slight_smile:

1 Like

I would be happy for all feedback at

It would be great if we could make it even better, or easier and use it for others.

@hanselot
It would be nice if you could share something about what you will use 5G for and how you will use it too

I wasn’t interested in hvakosterstrommen in particular, so I’m sorry if it feels like I’m kidnapping your thread.

I was looking for help on how to handle the incoming data from a httpsget-request and how to use the WagoAppJSON.FbWrite_ToIEC_ByRule. I couldnt figure out why my project only had numbers in the receive buffer, while I noticed you had text.

My project is about my PLC communicating with a 5G-routers API (Rutx50) in order to send SMS. For that I need to send a login request to the router and single out the authorization token to use in my following request to send the SMS. Not sure how it will work yet. :stuck_out_tongue:

1 Like

Just declare Receive buffer as STRING(255)

1 Like

So simple, yet so difficult :slight_smile:

1 Like

sorry for late replay, have been on vacation.

Looks like you got the solution, grate:) I have used the RUTx09 with Tailscale to get a mesh VPN. But it looks like grate fun to use it to send SMS messages to. Don’t worry about kidnapping the tread, I just want to read about more projects and solutions, so anything you can share is grate.

Have a nice summer