Verification question if below is the right way to convert an Inline C string to a B4R string.
The result is OK, but there might be a better way to convert and return a string.
This example is using the ArduinoJson library. The library method parseObject returns a string.
In the example the idx and name are obtained and assigned to global array of bytes.
B4R Globals
B4R MQTT Message Arrived
Output Message Arrived
idxb=28
name=Windmesser
***
idxb=4
name=RPi Temp
***
B4R Inline C
The result is OK, but there might be a better way to convert and return a string.
This example is using the ArduinoJson library. The library method parseObject returns a string.
In the example the idx and name are obtained and assigned to global array of bytes.
B4R Globals
B4X:
Sub Process_Globals
Public idxb() As Byte
Public nameb() As Byte
...
B4R MQTT Message Arrived
B4X:
Sub Mqtt_MessageArrived (Topic As String, payload() As Byte)
Dim bc As ByteConverter
RunNative("jsonparse", bc.StringFromBytes(payload))
Log("idxb=", bc.StringFromBytes(idxb))
Log("name=", bc.StringFromBytes(nameb))
Output Message Arrived
idxb=28
name=Windmesser
***
idxb=4
name=RPi Temp
***
B4R Inline C
B4X:
#if C
// Include the ArduinoJson library (see https://github.com/bblanchon/ArduinoJson)
#include <ArduinoJson.h>
// Define the dynamic buffer - set size to 500 as Domoticz Payload can be quite large
DynamicJsonBuffer jsonBuffer(500);
// String for converting payload to array of bytes
String str;
// Parse the json payload
void jsonparse (B4R::Object* String) {
// Parse (see arduino parse example)
const char* Text = (const char*)String->data.PointerField;
JsonObject& root = jsonBuffer.parseObject(Text);
// Test if parsing succeeds
if (!root.success()) {
Serial.print("Parsing Failed");
return;
}
// Get the idx key, assign to the string str, convert to bytes strb and assign to the B4R global var idxb
str = root["idx"].asString();
byte strb[str.length() + 1];
str.getBytes(strb, sizeof(strb));
b4r_main::_idxb->data = strb;
b4r_main::_idxb->length = sizeof(strb);
// Same as previous but for name
str = root["name"].asString();
Serial.println(str);
byte nameb[str.length() + 1];
str.getBytes(nameb, sizeof(nameb));
b4r_main::_nameb->data = nameb;
b4r_main::_nameb->length = sizeof(nameb);
// MORE TO ADD ... make a function for it.
}
#End If