Convert serial to object


#1

Hi!

I have a arduino with 4 sensors connected to my pi.
The serial sends the ouput like below
{cell_1:0.01,cell_2:0.03,cell_3:-0.02,cell_4:0.03}

I was hoping that i could use this as an object in my javascript node but thats not wokring
Also msg.payload.cell_1 is giving undefined

How can i make this work as an object?
Or isnt that possible in this way?


#2

Try sending the serial output through a JSON node. If that fails, you will need to wrap the property names in double quotes when sending.


#3

i tried both, that gives me "Unexpected token c in JSON at position 1"


#4

Your incoming data msg.payload should look like:

{"cell_1":0.01,"cell_2":0.03,"cell_3":-0.02,"cell_4":0.03}

Use a debug node after the serial-in node to see exactly what you are getting, if it is still failing, post the debug output so we can see the issue.


#5

thanks!
I did this and it works now

    Serial.print("{");
Serial.print("\"cell_1\":");
Serial.print(a);
Serial.print(",\"cell_2\":");
Serial.print(b);
Serial.print(",\"cell_3\":");
Serial.print(c);
Serial.print(",\"cell_4\":");
Serial.print(d);
Serial.println("}");