In my recent project, I'm working with Arduino with JSON.
I made software on JSON. Now, I want to send data from this software to my Arduino. Serial communication is done between Arduino and JSON. But somehow, I can't able to get proper data from JSON to my Arduino. So, I google out and find library named ArduinoJson
. And here, I found out example code. So, I tried out that code and modified based on my requirement.
Here, below is my code.
Now, Software sends me value. And I split it out into three variable MAP_Json, TPS_Json, and LOAD_Json. In Arduino side, I have already the value of MAP, TPS, and LOAD.
So, now my main intention is to compare MAP_Json with MAP
& TPS_Json with TPS
. If they are matched then LOAD_Json is added to LOAD
. But, when I try it out, then LOAD_Json not added to LOAD
. I don't understand why this happened?
#include <ArduinoJson.h>
//----------------------------For MAP calculation----------------------------//
float MAP_MIN = 0.85;
float MAP_MAX = 1.90;
int LOAD_MIN_MAP[] = {1, 2};
int LOAD_MAX_MAP[] = {3, 4};
float mMAP[2] = {};
float yMAP[2] = {};
int i, j, a;
float MAP, mFinalMAP, yFinalMAP;
//----------------------------For TPS calculation----------------------------//
float TPS_MIN = 0.00;
float TPS_MAX = 5.00;
int LOAD_MIN_TPS[] = {1, 3};
int LOAD_MAX_TPS[] = {2, 4};
float mTPS[2] = {};
float yTPS[2] = {};
int k, l, b;
float TPS, mFinalTPS, yFinalTPS;
//----------------------------For LOAD calculation---------------------------//
int LOAD_MIN = 2;
int LOAD_MAX = 100;
float injTime_MIN_LOAD[] = {8.00, 9.06};
float injTime_MAX_LOAD[] = {10.60, 11.86};
float mLOAD[2] = {};
float yLOAD[2] = {};
int m, n, c;
float LOAD, mFinalLOAD, yFinalLOAD;
//------------------------------ Variable ---------------------------------//
float load;
void setup()
{
Serial.begin(9600);
}
void loop()
{
//----------------------ARDUINO DATA-------------------------//
//----------------------------MAP----------------------------//
MAP = analogRead(A0) * (5.0 / 1023.0);
for(a = 0; a < 2; a++)
{
mMAP[i] = (LOAD_MIN_MAP[a] - LOAD_MIN_MAP[a]) / (MAP_MAX - MAP_MIN);
yMAP[j] = mMAP[i] * (MAP - MAP_MIN) + LOAD_MIN_MAP[a];
}
//----------------------------TPS----------------------------//
TPS = analogRead(A1) * (5.0 / 1023.0);
for(b = 0; b < 2; b++)
{
mTPS[k] = (LOAD_MAX_TPS[b] - LOAD_MIN_TPS[b]) / (TPS_MAX - TPS_MIN);
yTPS[l] = mTPS[k] * (TPS - TPS_MIN) + LOAD_MIN_TPS[b];
}
//----------------TPS/MAP CALLIBRATION / LOAD----------------//
mFinalTPS = (yMAP[1] - yMAP[0]) / (TPS_MAX - TPS_MIN);
yFinalTPS = mFinalTPS * (TPS - TPS_MIN) + yMAP[0];
mFinalMAP = (yTPS[1] - yTPS[0]) / (MAP_MAX - MAP_MIN);
yFinalMAP = mFinalMAP * (MAP - MAP_MIN) + yTPS[0];
LOAD = (yFinalTPS + yFinalMAP) / 2;
Serial.print("T : ");
Serial.print(TPS);
Serial.print("\t");
Serial.print("M : ");
Serial.print(MAP);
Serial.print("\t");
Serial.print("L : ");
Serial.print(LOAD);
Serial.print("\t");
while(!Serial)
{
// wait serial port initialization
}
StaticJsonBuffer<200> jsonBuffer;
char json[] = "{\"TPS\":1.67, \"MAP\":1.83,\"LOAD\":30}"; // JSON DATA
JsonObject& root = jsonBuffer.parseObject(json);
// Test if parsing succeeds.
if(!root.success())
{
Serial.println("parseObject() failed");
return;
}
// Splitted JSON DATA
float TPS_Json = root["TPS"];
float MAP_Json = root["MAP"];
float LOAD_Json = root["LOAD"];
Serial.print("T_J : ");
Serial.print(TPS_Json);
Serial.print("\t");
Serial.print("M_J : ");
Serial.print(MAP_Json);
Serial.print("\t");
Serial.print("L_J : ");
Serial.print(LOAD_Json);
Serial.print("\t");
//Compare JSON DATA and ARDUINO DATA
if(TPS_Json == TPS && MAP_Json == MAP) //If matched
{
load = LOAD_Json + LOAD; // Add LOAD_Json and LOAD
}
Serial.print("Final Load :");
Serial.print(load);
Serial.print("\n");
}
EDIT 1: Improve question formatting
EDIT 2: Upload Complete code