For sending data from Arduino/ESP to Node-RED (running on a Raspberry Pi), JSON format is an option as Node-RED provides simple methods for parsing JSON.
The example sub writes via asyncstream a Key:Value Pair Array as a JSON string.
Notes
Output
{"counter1":1958,"counter2":19.58}
Node-RED
Use a JSON Node, fed from f.e. Serial or UDP, which payload can be taken in f.e. a Function Node.
The example sub writes via asyncstream a Key:Value Pair Array as a JSON string.
Notes
- Watch the program storage space when using - the example sub with 2 parameter takes about 5%.
- There might be more efficient ways providing this functionality (like a JSON Library) but did not came across a suitable (simple and easy to parse) library (yet).
- Another option is to use MQTT - but in this case wanted some minimal effort to sent data.
B4X:
Private astream As AsyncStreams
Private counter1 As Int = 1958
Private counter2 As Double = 19.58
ToJSON(astream, Array As String("counter1", "counter2"), Array As String(counter1, counter2))
{"counter1":1958,"counter2":19.58}
B4X:
Sub ToJSON(astr As AsyncStreams, k() As String, v() As String)
astr.Write("{")
For i = 0 To k.Length - 1
astream.Write("""")
astream.Write(k(i).GetBytes)
astream.Write(""":")
astream.Write(v(i).GetBytes)
If i < k.length - 1 Then astream.Write(",")
Next
astr.Write("}")
astr.Write(CRLF)
End Sub
Node-RED
Use a JSON Node, fed from f.e. Serial or UDP, which payload can be taken in f.e. a Function Node.
B4X:
var counter1 = msg.payload.counter1;
var counter2 = msg.payload.counter2;
node.warn(counter1 + " = " + counter2);