0

In my recent project, working with JSON and Arduino. Till now, I am getting proper JSON data and add this one into Arduino data. But here I face one problem. My data are not updated in real time.

For example, my JSON string is:

{"TPS_Json":"0.40"}

And below is my Arduino code:

#include<LiquidCrystal.h>
#include <ArduinoJson.h>

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);

String response = "";
bool begin = false, finished;

//------------------------------ Variable ---------------------------------//

void setup() 
{
  Serial.begin(9600);

  lcd.begin(16, 2);
}

void loop() 
{
  //----------------------------TPS----------------------------//

  float TPS = analogRead(A0) * (5.0 / 1023.0);

  //-----------------------JASON DATA--------------------------//

  StaticJsonBuffer<100> jsonBuffer;

  while (finished == false)
  {
    if (Serial.available())
    {
      const char in = Serial.read();
      if (in == '{')
      {
        response = "";  // Blank the string
        begin = true;
      }
      if (begin)
      {
        response += (in); // Only write if within the {}
      }
      if (in == '}')
      { 
        begin = false;   // Prevent any chars between } and { leaking through.
        break;
      }
    }
    delay(1);
  }

  JsonObject& root = jsonBuffer.parseObject(response);

  float TPS_Json = root["TPS_Json"];

  float TPS_Final = TPS + TPS_Json;

  lcd.setCursor(0, 0);
  lcd.print("      ");
  lcd.setCursor(0, 0);
  lcd.print(TPS);
  lcd.setCursor(8, 0);
  lcd.print("      ");
  lcd.setCursor(8, 0);
  lcd.print(TPS_Json);
  lcd.setCursor(0, 1);
  lcd.print("      ");
  lcd.setCursor(0, 1);
  lcd.print(TPS_Final);                              
}

So, now I add TPS_Json with TPS. Where TPS_Json is JSON data and TPS is an analog value from A0. And TPS_Final is the addition of TPS_Json and TPS. I displayed all this on LCD.

Suppose, TPS_Json = 0.40, TPS = 2.40... So addition is 2.80 means TPS_Final = 2.80... And this all data displayed on LCD successfully.

But when I changed my analog value means TPS value then it is not updated and I'm not getting the correct result.

Suppose, I change the value of TPS means makes it 3.00 from 2.40 than my TPS_Final must be 3.40.But nothing updated. It updated when I send JSON data again. I don't know why this happens. I want to change it in real time means When I changed TPS then TPS_Final must be changed in real time.

1

The reasons it's not updating is because your code is sitting in the loop while (finished == false) {...} waiting for more json to arrive.

Try replacing the while loop with something along these lines:

  while ((finished == false) || Serial.available()) {
   if (Serial.available()) {
    const char in = Serial.read();
    if (in == '{')  {
        response = "";  // Blank the string
        begin = true;
        finished = false;
    } 
    if (begin)
        response += (in); // Only write if within the {}
    if (in == '}') { 
        begin = false;   // Prevent any chars between } and { leaking through.
        finished = true;
    }
   }
  }

and change the initial variable declarations at the start of the code to set finished to false:

// personally I always put each variables on a separate lines when setting initial values.
bool begin = false;
bool finished = false;

This will block until it's received at least 1 json string. After that it will check if there is new data on the serial port, if there is it will block until it's received the whole string, it there isn't any data waiting it will exit.

Oh and your response string is currently including the { } characters at the start and end, this contradicts your comments which imply that it doesn't.

  • Thank you for your effort. I tried out your code. Now my data is updated but I'm not getting JSON Data...It's displayed 0.00 instead of 0.40... – Hasan Jul 14 '17 at 11:37
  • Code updated. If finished was false (not had any JSON or in the middle of a string) but there was no serial data waiting then invalid data would get added to the response string. I've added an if(serial.avalible()) to the code which should fix that. – Andrew Jul 14 '17 at 12:50
  • Thank you. Now, it's working fine. Can you explain what is the fault in previous code? – Hasan Jul 14 '17 at 13:19
  • I though I had explained it. When finished was false but there was no data waiting on the serial port it would still attempt to read the serial port. If this happened to be between the { and } characters junk would get inserted into the middle of the json string. Ultimately this is caused by serial ports being painfully slow in comparison to processors and so you end up having to wait for the message to finish. – Andrew Jul 14 '17 at 14:23
0

Your code reads data from the serial interface and once it has read the input it re-evaluates the value of TPS_Final. I've found it difficult to understand your question but I think you are saying this:

If you enter {"TPS_Json":"0.40"} in the serial monitor and A0 has a value that equates to 2.4 then you expect TPS = 2.80 on your LCD. So do I.

Now if you change what ever is feeding A0 so that the value equates to 3.00 I think you are saying you expect you LCD to update to say TPS = 3.40, but it doesn't and you don't understand why.

IF that is the case then the answer is simple. When you change the value for A0 you code is waiting for you to enter the serial data again, because it is a 'blocking function call' well a 'blocking' bit of code at any rate. And until you enter a value in the serial monitor you code won't evaluate TPS again.

You need to change/remove the while loop so it your code will update the LCD even when the data is being entered by the serial interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.