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.