Arduinojson: Reading JSON using serial communication on Arduino

Created on 30 Mar 2015  路  5Comments  路  Source: bblanchon/ArduinoJson

This library is great, helps me sending JSON data from Arduino to Web. But I also want to send JSON data from web to Arduino to control some of devices.
At the moment I'm using serial interface for communication between Arduino and Node.js script. I managed to send JSON stream to Arduino but I cannot store it to variable that can be parsed using this library.

I tried this method to read each line:

 while (Serial.available() > 0)
    {
        char recieved = Serial.read();
        inData += recieved; 

        // Process message when new line character is recieved
        if (recieved == '\n')
        {
            Serial.print("Arduino Received: ");
            Serial.print(inData);

        }   
            inData = ""; // Clear recieved buffer
        }
    }

But this example doesn't work for reading line of JSON data.
Any idea why?

question

All 5 comments

Maybe the end of line is not \n, it could be \r or \0.
Also, you should try Serial.readBytesUntil()

Hmmm, not sure how to use Serial.readBytesUntil() with strings .... but I tried this:

if(Serial.available() > 0)
    {
        inData = Serial.readStringUntil('\n');
       Serial.println("data: "+inData);

       inData="";
     }

And strange thing is happening again ...
For input:

{"type_json":1,"count_json":68,"codeValue_json":"20df40bf","bits_json":32}

I get this output:

data: {"type_json":1,"count_json":68,"codeValue_json":"20df40bf","bit

Any reason why it doesn't store the whole line of input text?

I solved problem.
I was using delay function in my loop. Looks like delay function has influence on readStringUntil() ...

Organizing my code in separate timers and leaving readStringUntil() in loop fixed my problem.

readStringUntil() has a timeout configurable with setTimeout()

how do you parse your variable "inData" to JSON?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dimityrivanov picture dimityrivanov  路  5Comments

egadgetjnr picture egadgetjnr  路  3Comments

ghost picture ghost  路  7Comments

cdaller picture cdaller  路  4Comments

mhazley picture mhazley  路  4Comments