Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getResponseBody does not get the JSON the request returns #94

Open
michaeloconnor1717 opened this issue Jul 26, 2020 · 0 comments
Open

Comments

@michaeloconnor1717
Copy link

I am sending a get request to the national weather service (api.weather.gov) and it sends back a json with a forecast which is printed to the terminal, but when I use getResponseBody(), the JSON is not stored under the variable it is assigned to.

Sketch:

extern SoftwareSerial espToMega;
HTTPSRedirect* weather = nullptr;
const char* host1 = "api.weather.gov";
String url1 = "/gridpoints/LZK/49,94/forecast";
void weatherConnect(){
  weather = new HTTPSRedirect(httpsPort);
  weather->setInsecure();
  weather->setPrintResponseBody(true);
  weather->setContentTypeHeader("application/json");

  espToMega.println("Connecting");
  Serial.print("Connecting to ");
  Serial.println(host1);

  bool flag1 = false;
  for (int i = 0; i < 5; i++) {
    int retval1 = weather->connect(host1, httpsPort);
    if (retval1 == 1) {
      flag1 = true;
      Serial.println("Connected");
      break;
    }
    else
      Serial.println("Connection failed. Retrying...");
  }

  if (!flag1) {
    Serial.print("Could not connect to server: ");
    Serial.println(host1);
    return;
  }
  String forecast = "";
  espToMega.println("clear");
  espToMega.flush();
  delay(500);
  
  // delete HTTPSRedirect object
  delete weather;
  weather = nullptr;

  static bool flag = false;
  if (!flag) {
    weather = new HTTPSRedirect(httpsPort);
    weather->setInsecure();
    flag = true;
    weather->setPrintResponseBody(true);
  }
  if (weather != nullptr) {
    if (!weather->connected()) {
      weather->connect(host1, httpsPort);
    }
  }
  else {
    Serial.println("Error creating client object!");
  }

  //getting data
  Serial.println("requesting weather data...");
  weather->_keepAlive = false;
  if (weather->GET(url1, host1)) {}
  else {
    // do stuff here if publish was not successful
    Serial.println("Error while connecting");
  }

  forecast = weather->getResponseBody();
  Serial.print((String)("Forecast:\n" + forecast));
  Serial.flush();
  Serial.println("Done with Setup");
  weather->stop();   
}

void setup() {
  pinMode(10, INPUT);
  espToMega.begin(115200);
  Serial.begin(9600);
  delay(10);
  Serial.println('\n');

  // Connect to WiFi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println('\n');
  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());

  googleConnect();
  weatherConnect();

  espToMega.println("clear");
  espToMega.flush();
  delay(500);

  timeClient.begin();
  timeClient.update();
  getInitialTime();
  delay(1000);
  requestEvents();
}

Serial Output

Connecting to Hogwarts ...
14:27:01.153 -> ....
14:27:04.937 -> 
14:27:04.971 -> Connection established!
14:27:04.971 -> IP address:	192.168.1.15
14:27:05.005 -> Connecting to script.google.com
14:27:06.163 -> Connected
14:27:07.763 -> requesting data...
14:27:09.599 -> Events: 
14:27:09.599 -> Connecting to api.weather.gov
14:27:10.722 -> Connected
14:27:12.256 -> requesting weather data...
14:27:12.359 -> {
14:27:12.359 ->     "@context": [
14:27:12.359 ->         "https://geojson.org/geojson-ld/geojson-context.jsonld",
14:27:12.427 ->         {
14:27:12.461 ->             "@version": "1.1",
14:27:12.495 ->             "wx": "https://api.weather.gov/ontology#",
14:27:12.529 ->             "geo": "http://www.opengis.net/ont/geosparql#",
14:27:12.597 ->             "unit": "http://codes.wmo.int/common/unit/",
14:27:12.665 ->             "@vocab": "https://api.weather.gov/ontology#"
14:27:12.733 ->         }
14:27:12.733 ->     ],
14:27:12.733 ->     "type": "Feature",
14:27:12.767 ->     "geometry": {
14:27:12.801 ->         "type": "Polygon",
14:27:12.801 ->         "coordinates": [
14:27:12.836 ->             [
14:27:12.870 ->                 [
14:27:12.870 ->                     -93.170613200000005,
14:27:12.938 ->                     35.271125400000003
14:27:12.972 ->                 ],
14:27:12.972 ->                 [
14:27:13.006 ->                     -93.170984500000003,
14:27:13.040 ->                     35.248660200000003
14:27:13.074 ->                 ],
14:27:13.108 ->                 [
14:27:13.142 ->                     -93.1434876,
14:27:13.176 ->                     35.248355000000004
14:27:13.210 ->                 ],
14:27:13.244 ->                 [
14:27:13.244 ->                     -93.143110800000002,
14:27:13.278 ->                     35.270820100000002
14:27:13.346 ->                 ],
14:27:13.346 ->                 [
14:27:13.380 ->                     -93.170613200000005,
14:27:13.415 ->                     35.271125400000003
14:27:13.449 ->                 ]
14:27:13.483 ->             ]
14:27:13.483 ->         ]
14:27:13.517 ->     },
14:27:13.517 ->     "properties": {
14:27:13.551 ->         "updated": "2020-07-26T18:36:34+00:00",
14:27:13.585 ->         "units": "us",
14:27:13.619 ->         "forecastGenerator": "BaselineForecastGenerator",
14:27:13.687 ->         "generatedAt": "2020-07-26T18:49:18+00:00",
14:27:13.721 ->         "updateTime": "2020-07-26T18:36:34+00:00",
14:27:13.789 ->         "validTimes": "2020-07-26T12:00:00+00:00/P7DT13H",
14:27:13.857 ->         "elevation": {
14:27:13.857 ->             "value": 146.9136,
14:27:13.891 ->             "unitCode": "unit:m"
14:27:13.925 ->         },
14:27:13.959 ->         "periods": [
14:27:13.959 ->             {
14:27:13.993 ->                 "number": 1,
14:27:14.027 ->                 "name": "This Afternoon",
14:27:14.061 ->                 "startTime": "2020-07-26T13:00:00-05:00",
14:27:14.128 ->                 "endTime": "2020-07-26T18:00:00-05:00",
14:27:14.196 ->                 "isDaytime": true,
14:27:14.230 ->                 "temperature": 92,
14:27:14.264 ->                 "temperatureUnit": "F",
14:27:14.299 ->                 "temperatureTrend": null,
14:27:14.367 ->                 "windSpeed": "5 mph",
14:27:14.401 ->                 "windDirection": "SSE",
14:27:14.435 ->                 "icon": "https://api.weather.gov/icons/land/day/tsra_hi,20?size=medium",
14:27:14.537 ->                 "shortForecast": "Slight Chance Showers And Thunderstorms",
14:27:14.606 ->                 "detailedForecast": "A slight chance of showers and thunderstorms. Mostly sunny, with a high near 92. Heat index values as high as 100. South southeast wind around 5 mph. Chance of precipitation is 20%. New rainfall amounts less than a tenth of an inch possible."
14:27:14.911 ->             },
14:27:14.911 ->             {
14:27:14.945 ->                 "number": 2,
14:27:14.979 ->                 "name": "Tonight",
14:27:15.013 ->                 "startTime": "2020-07-26T18:00:00-05:00",
14:27:15.047 ->                 "endTime": "2020-07-27T06:00:00-05:00",
14:27:15.116 ->                 "isDaytime": false,
14:27:15.150 ->                 "temperature": 74,
14:27:15.184 ->                 "temperatureUnit": "F",
14:27:15.252 ->                 "temperatureTrend": null,
14:27:15.286 ->                 "windSpeed": "0 to 5 mph",
14:27:15.320 ->                 "windDirection": "S",
14:27:15.389 ->                 "icon": "https://api.weather.gov/icons/land/night/tsra_hi,20/few?size=medium",
14:27:15.457 ->                 "shortForecast": "Slight Chance Showers And Thunderstorms then Mostly Clear",
14:27:15.559 ->                 "detailedForecast": "A slight chance of showers and thunderstorms before 7pm. Mostly clear, with a low around 74. Heat index values as high as 98. South wind 0 to 5 mph. Chance of precipitation is 20%. New rainfall amounts less than a tenth of an inch possible."
14:27:15.865 ->             },
14:27:15.865 ->             {
14:27:15.899 ->                 "number": 3,
14:27:15.933 ->                 "name": "Monday",
14:27:15.967 ->                 "startTime": "2020-07-27T06:00:00-05:00",
14:27:16.035 ->                 "endTime": "2020-07-27T18:00:00-05:00",
14:27:16.069 ->                 "isDaytime": true,
14:27:16.137 ->                 "temperature": 91,
14:27:16.171 ->                 "temperatureUnit": "F",
14:27:16.205 ->                 "temperatureTrend": null,
14:27:16.239 ->                 "windSpeed": "0 to 5 mph",
14:27:16.307 ->                 "windDirection": "SW",
14:27:16.341 ->                 "icon": "https://api.weather.gov/icons/land/day/sct/tsra_hi,20?size=medium",
14:27:16.443 ->                 "shortForecast": "Mostly Sunny then Slight Chance Showers And Thunderstorms",
14:27:16.545 ->                 "detailedForecast": "A slight chance of showers and thunderstorms after 1pm. Mostly sunny, with a high near 91. Heat index values as high as 97. Southwest wind 0 to 5 mph. Chance of precipitation is 20%. New rainfall amounts less than a tenth of an inch possible."
14:27:16.817 ->             },
14:27:16.851 ->             {
14:27:16.851 ->                 "number": 4,
14:27:16.885 ->                 "name": "Monday Night",
14:27:16.919 ->                 "startTime": "2020-07-27T18:00:00-05:00",
14:27:16.987 ->                 "endTime": "2020-07-28T06:00:00-05:00",
14:27:17.055 ->                 "isDaytime": false,
14:27:17.089 ->                 "temperature": 73,
14:27:17.123 ->                 "temperatureUnit": "F",
14:27:17.157 ->                 "temperatureTrend": null,
14:27:17.226 ->                 "windSpeed": "0 mph",
14:27:17.260 ->                 "windDirection": "SSW",
14:27:17.294 ->                 "icon": "https://api.weather.gov/icons/land/night/tsra_hi,20/tsra_hi,30?size=medium",
14:27:17.396 ->                 "shortForecast": "Chance Showers And Thunderstorms",
14:27:17.465 ->                 "detailedForecast": "A chance of showers and thunderstorms. Partly cloudy, with a low around 73. South southwest wind around 0 mph. Chance of precipitation is 30%."
14:27:17.671 ->             },
14:27:17.671 ->             {
14:27:17.705 ->                 "number": 5,
14:27:17.739 ->                 "name": "Tuesday",
14:27:17.774 ->                 "startTime": "2020-07-28T06:00:00-05:00",
14:27:17.843 ->                 "endTime": "2020-07-28T18:00:00-05:00",
14:27:17.877 ->                 "isDaytime": true,
14:27:17.911 ->                 "temperature": 89,
14:27:17.979 ->                 "temperatureUnit": "F",
14:27:18.013 ->                 "temperatureTrend": null,
14:27:18.048 ->                 "windSpeed": "0 mph",
14:27:18.082 ->                 "windDirection": "S",
14:27:18.150 ->                 "icon": "https://api.weather.gov/icons/land/day/tsra_hi,50/tsra_hi,60?size=medium",
14:27:18.253 ->                 "shortForecast": "Showers And Thunderstorms Likely",
14:27:18.321 ->                 "detailedForecast": "Showers and thunderstorms likely. Partly sunny, with a high near 89. South wind around 0 mph. Chance of precipitation is 60%."
14:27:18.493 ->             },
14:27:18.493 ->             {
14:27:18.527 ->                 "number": 6,
14:27:18.561 ->                 "name": "Tuesday Night",
14:27:18.595 ->                 "startTime": "2020-07-28T18:00:00-05:00",
14:27:18.663 ->                 "endTime": "2020-07-29T06:00:00-05:00",
14:27:18.697 ->                 "isDaytime": false,
14:27:18.765 ->                 "temperature": 72,
14:27:18.798 ->                 "temperatureUnit": "F",
14:27:18.832 ->                 "temperatureTrend": null,
14:27:18.866 ->                 "windSpeed": "0 mph",
14:27:18.899 ->                 "windDirection": "ESE",
14:27:18.966 ->                 "icon": "https://api.weather.gov/icons/land/night/tsra_sct,60/tsra_sct,50?size=medium",
14:27:19.067 ->                 "shortForecast": "Showers And Thunderstorms Likely",
14:27:19.135 ->                 "detailedForecast": "Showers and thunderstorms likely. Mostly cloudy, with a low around 72. East southeast wind around 0 mph. Chance of precipitation is 60%."
14:27:19.339 ->             },
14:27:19.339 ->             {
14:27:19.373 ->                 "number": 7,
14:27:19.373 ->                 "name": "Wednesday",
14:27:19.441 ->                 "startTime": "2020-07-29T06:00:00-05:00",
14:27:19.476 ->                 "endTime": "2020-07-29T18:00:00-05:00",
14:27:19.544 ->                 "isDaytime": true,
14:27:19.578 ->                 "temperature": 85,
14:27:19.612 ->                 "temperatureUnit": "F",
14:27:19.680 ->                 "temperatureTrend": null,
14:27:19.714 ->                 "windSpeed": "0 to 5 mph",
14:27:19.748 ->                 "windDirection": "SSE",
14:27:19.782 ->                 "icon": "https://api.weather.gov/icons/land/day/tsra,50/tsra,60?size=medium",
14:27:19.884 ->                 "shortForecast": "Showers And Thunderstorms Likely",
14:27:19.987 ->                 "detailedForecast": "Showers and thunderstorms likely. Mostly cloudy, with a high near 85. South southeast wind 0 to 5 mph. Chance of precipitation is 60%."
14:27:20.157 ->             },
14:27:20.157 ->             {
14:27:20.191 ->                 "number": 8,
14:27:20.225 ->                 "name": "Wednesday Night",
14:27:20.259 ->                 "startTime": "2020-07-29T18:00:00-05:00",
14:27:20.328 ->                 "endTime": "2020-07-30T06:00:00-05:00",
14:27:20.396 ->                 "isDaytime": false,
14:27:20.430 ->                 "temperature": 71,
14:27:20.464 ->                 "temperatureUnit": "F",
14:27:20.498 ->                 "temperatureTrend": null,
14:27:20.532 ->                 "windSpeed": "0 to 5 mph",
14:27:20.600 ->                 "windDirection": "SSW",
14:27:20.634 ->                 "icon": "https://api.weather.gov/icons/land/night/tsra,60?size=medium",
14:27:20.737 ->                 "shortForecast": "Showers And Thunderstorms Likely",
14:27:20.805 ->                 "detailedForecast": "Showers and thunderstorms likely. Mostly cloudy, with a low around 71. South southwest wind 0 to 5 mph. Chance of precipitation is 60%."
14:27:20.975 ->             },
14:27:21.009 ->             {
14:27:21.009 ->                 "number": 9,
14:27:21.043 ->                 "name": "Thursday",
14:27:21.077 ->                 "startTime": "2020-07-30T06:00:00-05:00",
14:27:21.145 ->                 "endTime": "2020-07-30T18:00:00-05:00",
14:27:21.213 ->                 "isDaytime": true,
14:27:21.248 ->                 "temperature": 87,
14:27:21.282 ->                 "temperatureUnit": "F",
14:27:21.316 ->                 "temperatureTrend": null,
14:27:21.384 ->                 "windSpeed": "5 mph",
14:27:21.418 ->                 "windDirection": "SW",
14:27:21.452 ->                 "icon": "https://api.weather.gov/icons/land/day/tsra,60?size=medium",
14:27:21.555 ->                 "shortForecast": "Showers And Thunderstorms Likely",
14:27:21.623 ->                 "detailedForecast": "Showers and thunderstorms likely. Mostly cloudy, with a high near 87. Southwest wind around 5 mph. Chance of precipitation is 60%."
14:27:21.793 ->             },
14:27:21.793 ->             {
14:27:21.827 ->                 "number": 10,
14:27:21.861 ->                 "name": "Thursday Night",
14:27:21.895 ->                 "startTime": "2020-07-30T18:00:00-05:00",
14:27:21.965 ->                 "endTime": "2020-07-31T06:00:00-05:00",
14:27:22.033 ->                 "isDaytime": false,
14:27:22.067 ->                 "temperature": 70,
14:27:22.101 ->                 "temperatureUnit": "F",
14:27:22.135 ->                 "temperatureTrend": null,
14:27:22.169 ->                 "windSpeed": "0 to 5 mph",
14:27:22.237 ->                 "windDirection": "SW",
14:27:22.271 ->                 "icon": "https://api.weather.gov/icons/land/night/tsra,60/tsra,40?size=medium",
14:27:22.374 ->                 "shortForecast": "Showers And Thunderstorms Likely",
14:27:22.442 ->                 "detailedForecast": "Showers and thunderstorms likely. Mostly cloudy, with a low around 70. Chance of precipitation is 60%."
14:27:22.578 ->             },
14:27:22.612 ->             {
14:27:22.612 ->                 "number": 11,
14:27:22.646 ->                 "name": "Friday",
14:27:22.680 ->                 "startTime": "2020-07-31T06:00:00-05:00",
14:27:22.748 ->                 "endTime": "2020-07-31T18:00:00-05:00",
14:27:22.816 ->                 "isDaytime": true,
14:27:22.850 ->                 "temperature": 87,
14:27:22.884 ->                 "temperatureUnit": "F",
14:27:22.918 ->                 "temperatureTrend": null,
14:27:22.986 ->                 "windSpeed": "0 to 5 mph",
14:27:23.020 ->                 "windDirection": "WSW",
14:27:23.054 ->                 "icon": "https://api.weather.gov/icons/land/day/tsra_sct,50?size=medium",
14:27:23.156 ->                 "shortForecast": "Chance Showers And Thunderstorms",
14:27:23.224 ->                 "detailedForecast": "A chance of showers and thunderstorms. Mostly cloudy, with a high near 87. Chance of precipitation is 50%."
14:27:23.395 ->             },
14:27:23.395 ->             {
14:27:23.429 ->                 "number": 12,
14:27:23.463 ->                 "name": "Friday Night",
14:27:23.497 ->                 "startTime": "2020-07-31T18:00:00-05:00",
14:27:23.565 ->                 "endTime": "2020-08-01T06:00:00-05:00",
14:27:23.599 ->                 "isDaytime": false,
14:27:23.667 ->                 "temperature": 70,
14:27:23.701 ->                 "temperatureUnit": "F",
14:27:23.735 ->                 "temperatureTrend": null,
14:27:23.769 ->                 "windSpeed": "0 to 5 mph",
14:27:23.837 ->                 "windDirection": "NW",
14:27:23.871 ->                 "icon": "https://api.weather.gov/icons/land/night/tsra_hi,50/tsra_hi,30?size=medium",
14:27:23.973 ->                 "shortForecast": "Chance Showers And Thunderstorms",
14:27:24.041 ->                 "detailedForecast": "A chance of showers and thunderstorms. Mostly cloudy, with a low around 70. Chance of precipitation is 50%."
14:27:24.212 ->             },
14:27:24.212 ->             {
14:27:24.247 ->                 "number": 13,
14:27:24.247 ->                 "name": "Saturday",
14:27:24.314 ->                 "startTime": "2020-08-01T06:00:00-05:00",
14:27:24.348 ->                 "endTime": "2020-08-01T18:00:00-05:00",
14:27:24.416 ->                 "isDaytime": true,
14:27:24.450 ->                 "temperature": 88,
14:27:24.484 ->                 "temperatureUnit": "F",
14:27:24.553 ->                 "temperatureTrend": null,
14:27:24.587 ->                 "windSpeed": "0 to 5 mph",
14:27:24.621 ->                 "windDirection": "NNE",
14:27:24.690 ->                 "icon": "https://api.weather.gov/icons/land/day/tsra_hi,40?size=medium",
14:27:24.758 ->                 "shortForecast": "Chance Showers And Thunderstorms",
14:27:24.827 ->                 "detailedForecast": "A chance of showers and thunderstorms. Mostly sunny, with a high near 88. Chance of precipitation is 40%."
14:27:24.998 ->             },
14:27:24.998 ->             {
14:27:25.032 ->                 "number": 14,
14:27:25.066 ->                 "name": "Saturday Night",
14:27:25.100 ->                 "startTime": "2020-08-01T18:00:00-05:00",
14:27:25.168 ->                 "endTime": "2020-08-02T06:00:00-05:00",
14:27:25.236 ->                 "isDaytime": false,
14:27:25.270 ->                 "temperature": 68,
14:27:25.304 ->                 "temperatureUnit": "F",
14:27:25.338 ->                 "temperatureTrend": null,
14:27:25.372 ->                 "windSpeed": "0 mph",
14:27:25.441 ->                 "windDirection": "NE",
14:27:25.475 ->                 "icon": "https://api.weather.gov/icons/land/night/tsra_hi,40/tsra_hi,30?size=medium",
14:27:25.577 ->                 "shortForecast": "Chance Showers And Thunderstorms",
14:27:25.645 ->                 "detailedForecast": "A chance of showers and thunderstorms. Partly cloudy, with a low around 68. Chance of precipitation is 40%."
14:27:25.816 ->             }
14:27:25.816 ->         ]
14:27:25.816 ->     }
14:27:30.686 -> }
14:27:30.721 -> Forecast:
14:27:30.721 -> Done with Setup
14:27:31.538 -> `26-07-2020
14:27:31.572 -> ~0 14:27:31
14:27:33.654 -> requesting data...
14:27:35.462 -> Events:

TLDR

the string variable "forecast" is supposed to contain a really long JSON from the getResponseBody() method and its acting as if its blank.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant