Help with extraction of data from API response

in my code i just blanked out Lon and Lat.
i just added

Serial.println(location_latitude)

gives 0.00

So, it seems that json string is empty :thinking:

How do i populate the Json with the API data

Do you know how to use HTTPClient instead of webhook ?
For me, webhook never works :shushing_face:

Have no idea

I use this function for my forecast station :

HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("http://api.openweathermap.org/data/2.5/weather?id=3032869&APPID=**key****&units=metric&lang=fr");  //Specify request destination

    int httpCode = http.GET();     //Send the request

    if (httpCode > 0) { //Check the returning code

      payload = http.getString();   //Get the request response payload
      }

    http.end();   //Close connection

The Json result is in payload string

don’t forget #include <ESP8266HTTPClient.h>

so replace

  String webhookdata = param.asStr();
  Serial.println(webhookdata);

const size_t capacity = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(18) + 320;
DynamicJsonDocument doc(capacity);

with you code and change

String webhookdata = param.asStr();

to

 String payload = param.asStr();

and add the ESP8266HTTPClient.h

1 Like

No, don’t use

String payload = param.asStr();

use

String payload = http.getString(); 

new code


#define BLYNK_PRINT Serial

#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#incluse <ESP8266HTTPClient.h>
char auth[] = "*****************";
char ssid[] = "******0";
char pass[] = "**************************";
BlynkTimer timer;  

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,120), 8080);
  delay (3000);
   int rssi = WiFi.RSSI() ; //get signal strenth in DB
 Blynk.virtualWrite(V51,rssi);
 Blynk.virtualWrite(V0, 1); //update for webhooks
 Serial.println("Booting");
 timer.setInterval(6000L, APIupdate);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

void APIupdate(){
   Blynk.virtualWrite(V0, 1); //update for webhooks
}

BLYNK_WRITE(V0){
  HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("https://api.ipgeolocation.io/astronomy?apiKey=c5f0ed1688714f2eaa25a37a8c720912&lat=51.31962&long=-4.80284");  //Specify request destination

    int httpCode = http.GET();     //Send the request

    if (httpCode > 0) { //Check the returning code

      payload = http.getString();   //Get the request response payload
      }

    http.end();   //Close connection
  String payload = http.getString(); 
  Serial.println(payload);

const size_t capacity = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(18) + 320;
DynamicJsonDocument doc(capacity);


const char* json = "(payload)";


deserializeJson(doc, json);

float location_latitude = doc["location"]["latitude"]; // 
float location_longitude = doc["location"]["longitude"]; // 
const char* date = doc["date"]; // "2020-09-30"
const char* current_time = doc["current_time"]; // "19:06:46.27"
const char* sunrise = doc["sunrise"]; // "07:18"
const char* sunset = doc["sunset"]; // "18:59"
const char* sun_status = doc["sun_status"]; // "-"
const char* solar_noon = doc["solar_noon"]; // "13:09"
const char* day_length = doc["day_length"]; // "11:41"
float sun_altitude = doc["sun_altitude"]; // -2.0393480763745733
float sun_distance = doc["sun_distance"]; // 149810371.16339526
float sun_azimuth = doc["sun_azimuth"]; // 267.4597814448824
const char* moonrise = doc["moonrise"]; // "19:02"
const char* moonset = doc["moonset"]; // "05:23"
const char* moon_status = doc["moon_status"]; // "-"
float moon_altitude = doc["moon_altitude"]; // 1.0738509355096701
float moon_distance = doc["moon_distance"]; // 402084.5279459498
float moon_azimuth = doc["moon_azimuth"]; // 100.76227521973334
float moon_parallactic_angle = doc["moon_parallactic_angle"]; // -38.56220668133565
Serial.println(sun_altitude);
Serial.println(sun_azimuth);
Serial.println(location_latitude);
}

getting an error

ESP32_newCleanStart2HTTP:8:2: error: invalid preprocessing directive #incluse
#incluse <ESP8266HTTPClient.h>

You have to use ESP32HTTPClient instead of ESP8266HTTPClient

This should say include

Pete.

Yes but he has esp32 too.
So double mistake :joy:

opps typo, well spotted,
im having real problems finding the ESP32HTTPClient.h i have hunted guthub without any luck, any ideas?

Try #include <HTTPClient.h>
:wink:

current code only gives the words BOOTING on serial monitor.



#include <HTTPClient.h>
//#include <ESP32HTTPClient.h>


#define BLYNK_PRINT Serial

#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "************************b";
char ssid[] = "**************";
char pass[] = "********************";
BlynkTimer timer;  
  String    payload;

void setup()
{
  
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,120), 8080);
  delay (3000);
   int rssi = WiFi.RSSI() ; //get signal strenth in DB
 Blynk.virtualWrite(V51,rssi);
 Blynk.virtualWrite(V0, 1); //update for webhooks
 Serial.println("Booting");
 timer.setInterval(6000L, APIupdate);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

void APIupdate(){
   Blynk.virtualWrite(V0, 1); //update for webhooks
}

BLYNK_WRITE(V0){
  HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("https://api.ipgeolocation.io/astronomy?apiKey=c5f0ed1688714f2eaa25a37a8c720912&lat=51.31962&long=-4.80284");  //Specify request destination

    int httpCode = http.GET();     //Send the request

    if (httpCode > 0) { //Check the returning code

   payload = http.getString();   //Get the request response payload
      }

    http.end();   //Close connection
  //String payload = http.getString(); 
  Serial.println(payload);

const size_t capacity = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(18) + 320;
DynamicJsonDocument doc(capacity);


const char* json = "(payload)";


deserializeJson(doc, json);

float location_latitude = doc["location"]["latitude"]; // 
float location_longitude = doc["location"]["longitude"]; // 
const char* date = doc["date"]; // "2020-09-30"
const char* current_time = doc["current_time"]; // "19:06:46.27"
const char* sunrise = doc["sunrise"]; // "07:18"
const char* sunset = doc["sunset"]; // "18:59"
const char* sun_status = doc["sun_status"]; // "-"
const char* solar_noon = doc["solar_noon"]; // "13:09"
const char* day_length = doc["day_length"]; // "11:41"
float sun_altitude = doc["sun_altitude"]; // -2.0393480763745733
float sun_distance = doc["sun_distance"]; // 149810371.16339526
float sun_azimuth = doc["sun_azimuth"]; // 267.4597814448824
const char* moonrise = doc["moonrise"]; // "19:02"
const char* moonset = doc["moonset"]; // "05:23"
const char* moon_status = doc["moon_status"]; // "-"
float moon_altitude = doc["moon_altitude"]; // 1.0738509355096701
float moon_distance = doc["moon_distance"]; // 402084.5279459498
float moon_azimuth = doc["moon_azimuth"]; // 100.76227521973334
float moon_parallactic_angle = doc["moon_parallactic_angle"]; // -38.56220668133565
Serial.println(sun_altitude);
Serial.println(sun_azimuth);
Serial.println(location_latitude);
}~~~

What do you imagine that this piece of code, which is called every 6 seconds, will actually achieve?

If you still have a webhook widget attached to V0 then you need to remove it, as that was your old way of calling the API.

I would have expected you to put the API call in its own function and call it directly with a timer.

Pete.

1 Like
timer.setInterval(1800 *1000L, APIupdate); // every 30 mn  else you will reach 1K Daily limit with the free plan!



void APIupdate(){ 
HTTPClient http; //Declare an object of class HTTPClient 
http.begin("https://api.ipgeolocation.io/astronomy?apiKey=c5f0ed1688714f2eaa25a37a8c720912&lat=51.31962&long=-4.80284"); //Specify request destination
int httpCode = http.GET();     //Send the request

    if (httpCode > 0) { //Check the returning code
   payload = http.getString();   //Get the request response payload
   }
    http.end();   //Close connection
.
.
.
.
}

https://ipgeolocation.io/pricing.html

webhooks in app removed
APIupdate noe in its own update timer. (set to 6 seconds for test only. ill change to 1 minute when i deply the controller.)
im now getting the payload to print, however the Parsing program is not working as i only get the following in serial

{“location”:{“latitude”:51.31962,“longitude”:-4.80284},“date”:“2020-10-02”,“current_time”:“10:30:55.459”,“sunrise”:“07:22”,“sunset”:“18:53”,“sun_status”:"-",“solar_noon”:“13:08”,“day_length”:“11:31”,“sun_altitude”:25.459822211511852,“sun_distance”:1.4972452886243457E8,“sun_azimuth”:135.4784035319139,“moonrise”:“19:33”,“moonset”:“07:37”,“moon_status”:"-",“moon_altitude”:-24.012372435987086,“moon_distance”:405307.1086694112,“moon_azimuth”:307.5952265543657,“moon_parallactic_angle”:31.322954949496296}
0.00
0.00
0.00

thats the same as when i was using webhooks. so im gessing problem is in the Parsing program code that i got from https://arduinojson.org/v6/assistant/

const char* json = "(payload)";

No sense as payload is a string

Replace with

//const char* json = "(payload)";


deserializeJson(doc, payload);

Thank you for the help on this stage of my project it looks like it is now working.

API responce

Booting
{“location”:{“latitude”:50.3,“longitude”:-4.9},“date”:“2020-10-02”,“current_time”:“13:11:48.405”,“sunrise”:“07:21”,“sunset”:“18:54”,“sun_status”:"-",“solar_noon”:“13:08”,“day_length”:“11:33”,“sun_altitude”:35.82599056814347,“sun_distance”:1.4972452886243457E8,“sun_azimuth”:180.92215988420256,“moonrise”:“19:34”,“moonset”:“07:37”,“moon_status”:"-",“moon_altitude”:-37.01633439847199,“moon_distance”:405427.02896909765,“moon_azimuth”:350.76196309335023,“moon_parallactic_angle”:8.368900430136783}
35.83
180.92
50.30

working code



#include <HTTPClient.h>
//#include <ESP32HTTPClient.h>


#define BLYNK_PRINT Serial

#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "************************";
char ssid[] = "************************";
char pass[] = "**************************;
BlynkTimer timer;  
  String    payload;

void setup()
{
  
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,120), 8080);
  delay (3000);
   int rssi = WiFi.RSSI() ; //get signal strenth in DB
 Blynk.virtualWrite(V51,rssi);
 //Blynk.virtualWrite(V0, 1); //update for webhooks
 Serial.println("Booting");
 ////////////////
 timer.setInterval(12000L, APIupdate);// set to 12 seconds for testing
}//must change to 1800*1000L 30min/////

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

void APIupdate(){ 
  HTTPClient http;  //Declare an object of class HTTPClient
//new API key
    http.begin("https://api.ipgeolocation.io/astronomy?apiKey=***********&lat=50.3&long=-4.9");  //Specify request destination

    int httpCode = http.GET();     //Send the request

    if (httpCode > 0) { //Check the returning code

   payload = http.getString();   //Get the request response payload
      }

    http.end();   //Close connection
  //String payload = http.getString(); 
  Serial.println(payload);

const size_t capacity = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(18) + 320;
DynamicJsonDocument doc(capacity);


//const char* json = "(payload)";


deserializeJson(doc, payload);

float location_latitude = doc["location"]["latitude"]; // 
float location_longitude = doc["location"]["longitude"]; // 
const char* date = doc["date"]; // "2020-09-30"
const char* current_time = doc["current_time"]; // "19:06:46.27"
const char* sunrise = doc["sunrise"]; // "07:18"
const char* sunset = doc["sunset"]; // "18:59"
const char* sun_status = doc["sun_status"]; // "-"
const char* solar_noon = doc["solar_noon"]; // "13:09"
const char* day_length = doc["day_length"]; // "11:41"
float sun_altitude = doc["sun_altitude"]; // -2.0393480763745733
float sun_distance = doc["sun_distance"]; // 149810371.16339526
float sun_azimuth = doc["sun_azimuth"]; // 267.4597814448824
const char* moonrise = doc["moonrise"]; // "19:02"
const char* moonset = doc["moonset"]; // "05:23"
const char* moon_status = doc["moon_status"]; // "-"
float moon_altitude = doc["moon_altitude"]; // 1.0738509355096701
float moon_distance = doc["moon_distance"]; // 402084.5279459498
float moon_azimuth = doc["moon_azimuth"]; // 100.76227521973334
float moon_parallactic_angle = doc["moon_parallactic_angle"]; // -38.56220668133565
Serial.println(sun_altitude);
Serial.println(sun_azimuth);
Serial.println(location_latitude);
}

I thought you should see what is is going to control. its been up and running but only so far by manual remote control. The frame size is 3 metres by 2 metres.

Now i will be able to track the sun and move the panels to follow.
it is currently in a flat position (as in pictures) as we have 56MPH winds today.
can you recomend a weather site that i can make another API resonce too to obtain wind speed or if this would be problomatic running two Json responces, or a site that gives wind speed and sun position.


1 Like