In my recent project, I'm working with Arduino and JSON. I've successfully split the JSON data I want. My JSON data length is fixed. So, I used the length()
function and I'm getting the length of my data.
Now, my input JSON data is:
{"TPS":"0.40","MAP":"0.95","LOAD":"14"}
Using the length()
function, its length is 39
.
But when I use an if
statement like if(len == 39)
and put my logic inside this, I'm not getting the split data. Below is my whole code.
Here, I'm displaying my data on LCD.
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
String data_TPS_Json = "";
String data_MAP_Json = "";
String data_LOAD_TM_Json = "";
String response = "";
bool begin = false;
int counter = 0;
int len = 0;
char in;
float TPS_Json;
float MAP_Json;
float LOAD_TM_Json;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop() {
while(Serial.available() || !begin) {
in = Serial.read();
if (in == '{') {
begin = true;
}
if(begin) {
counter++;
response += (in);
len = response.length();
if(len == 39) {
if(counter >= 9 && counter <= 12) {
data_TPS_Json += (in);
}
if(counter >= 22 && counter <= 25) {
data_MAP_Json += (in);
}
if(counter >= 36 && counter <= 37) {
data_LOAD_TM_Json += (in);
}
}
}
if(in == '}') {
break;
}
delay(1);
}
TPS_Json = data_TPS_Json.toFloat();
MAP_Json = data_MAP_Json.toFloat();
LOAD_TM_Json = data_LOAD_TM_Json.toFloat();
lcd.setCursor(0, 0);
lcd.print(TPS_Json);
lcd.setCursor(8, 0);
lcd.print(MAP_Json);
lcd.setCursor(0, 1);
lcd.print(LOAD_TM_Json);
}