I am currently working with an ESP32 using a Weather API to pull data as a JSON and I want to check the values like temperature, wind speed, etc. at a certain value there should be an output, in this case i want to blink an LED to test it.
At the moment I can pull the data and print the certain values I want to use but I have no clue how to extract them not as a string to print but to use as a value for an if statement.
Could anyone help me with that and point me in the right direction?
#include < WiFi.h >
#include < HTTPClient.h >
#include < Arduino_JSON.h >
const char* ssid = "xxxxx";
const char* password = "xxxx";
String openWeatherMapApiKey = "c66687d91d685f5ecdda0007dxxxxxxx";
String city = "Berlin";
String countryCode = "BER";
unsigned long lastTime = 0;
unsigned long timerDelay = 10000;
String jsonBuffer;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&APPID=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(myObject);
Serial.print("Temperature: ");
Serial.println(myObject["main"]["temp"]);
Serial.print("Pressure: ");
Serial.println(myObject["main"]["pressure"]);
Serial.print("Humidity: ");
Serial.println(myObject["main"]["humidity"]);
Serial.print("Wind Speed: ");
Serial.println(myObject["wind"]["speed"]);
}
if (myObject["wind"]["speed"] >= 3) {
delay(1000);
digitalWrite(ONBOARD_LED, HIGH);
delay(100);
digitalWrite(ONBOARD_LED, LOW);
}
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
If I'm understanding your question correctly, you're able to get the data you need from the API, but it's coming as strings and you want numeric values. In this case, the C function atoi() can help. It takes ASCII (string) and gives you an integer. There is also atof(), for ASCII to float, though I have never used it. c++ - like atoi but to float - Stack Overflow has more information on that.
If you need to do the reverse operation (integer or float converted to string) check out sprintf() and snprintf().
johnwasser,
your example seems to be working, i can also see some things that look familiar but I couldn't use in the other code.
So now I can move on to the next stage.