The idea:
I have a pressure sensor that feeds the pointer ( I think this is a pointer) "userdata->pressure" with a number between 0 and 1023. Most of the time the sensor is just listening / staying 0. Whenever the sensor is in use (greater than 0), a LED turns on. Otherwise its off.
Testing:
I am trying to compare userdata->pressure with the integer 0 inside an if statement.
Whenever I manually change the comparison operator > like this (userdata->pressure > 0) the tweet is on HIGH. Whenever I change the comparison operator < like this (user->pressure < 0) the tweet is on LOW. So far this testing works.
Problem:
What I want is that the dynamic data (0-1023) inside userdata->pressure turns on and off the LED.
When I run the modified code, not much happens. The print statement shows me only HIGH ((userdata->pressure > 0) ) but the if statement never changes to LOW, no matter how often sensed data is coming in.
I am suspecting that this pointer cannot not be compared with an integer for some reason, but with a string. Yet, I need to compare userdata->pressure with an integer since userdata->pressure holds two conditions (0 and greater than 0) which I need to map on HIGH and LOW.
I am thankful for any hints.
void printUserData(const struct UserData* userData) {
int tweet = LOW;
Serial.print("data = ");
Serial.println(userData->pressure);
if ((userData->pressure) > 0)
{
Serial.println("HIGH");
tweet = HIGH;
}
else
{
Serial.println("LOW");
tweet = LOW;
}
}
About output of userData:
Below you will see incoming data that fills userData anew every second. The tricky part about all this, is to get access to the JSON part (“pressure”). By writing this userData->pressure I am selecting specifically {“pressure:0”}. In order to get to the “pressure” you can do the following: strcpy(userData->pressure, root[“bag”][0][“content”][“pressure”].
The author of ArduinoJson mentions the following about strcpy:
// Here were copy the strings we're interested in
- // It's not mandatory to make a copy, you could just use the pointers*
- // Since, they are pointing inside the "content" buffer, so you need to make*
- // sure it's still in memory when you read the string*
In context of my project {“pressure:0”} means that the sensor is currently on standby. Otherwise you see a number popping up there. If I do not use this library ArduinoJson, the serial monitor replaces the requested data with a question mark.
{
"name":"Its me Wario",
"location":"Wario Land",
"time”:"1995",
"bag" [{
"color":"red",
"created":"2017-01-18",
"content":{"pressure":0}
}]
}
Here this code snippet shows where userdata->pressure comes from:
bool readReponseContent(struct UserData* userData) {
// Compute optimal size of the JSON buffer according to what we need to parse.
// This is only required if you use StaticJsonBuffer.
const size_t BUFFER_SIZE =
JSON_OBJECT_SIZE(4) // the root object has 4 elements
+ JSON_OBJECT_SIZE(3) // the “with” object has 3 elements
+ JSON_OBJECT_SIZE(1) // the “content” object has 1 element
+ MAX_CONTENT_SIZE; // additional space for strings
// Allocate a temporary memory pool
DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);
JsonObject& root = jsonBuffer.parseObject(client);
if (!root.success()) {
Serial.println("JSON parsing failed!");
return false;
}
// Here were copy the strings we're interested in
strcpy(userData->pressure, root["with"][0]["content"]["pressure"]);
// strcpy(userData->company, root["company"]["name"]);
// It's not mandatory to make a copy, you could just use the pointers
// Since, they are pointing inside the "content" buffer, so you need to make
// sure it's still in memory when you read the string
return true;
}
I am working with code example JsonHttpClient provided by the custom library ArduinoJson:
// Sample Arduino Json Web Client
// Downloads and parse http://jsonplaceholder.typicode.com/users/1
//
// Copyright Benoit Blanchon 2014-2016
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!