Need help with API call

:frowning:

Hi everyone... I am very frustrated trying to get an API call to Open Weather Maps to return usable data. I am new at programming with Arduino and am struggling here. I have managed to get the code to return something but not what I would expect.

Here is an example of one of my attempts at constructing an API call:

cmd = "GET http://api.openweathermap.org/data/2.5/forecast/city?id=6159905&APPID=xxxxxxxxxxxxxxxxxxxxx\r\n\r\n\r\n";

The city ID is for Surrey, BC and the API number is mine but here I have replaced it with 'x's for security reasons.
I have tried many different ways of constructing the code and have read the examples on the Open Weather web site but to no avail...

I am using a Arduino Mega 2560 with a ESP8266 connected to Serial1 and when it runs I get it to connect with my WiFi and it gives the correct 'OK' responses to each step of the setup but when it gets to sending the API call it seems to return a text version of the raw Open Weather web page and lots and lots of it. (Example of some of this text follows:

responseString = 
               <div class=small-2 columns hide-for-small-only climate>
 <h6>Mobile Apps</h6>
 <ul class=no-bullet>
 <li><a href=/download/index.asp#ios>Apple iOS</a></li>
 <li><a href=/download/index.asp#android>Google Android</a></li>
 <li><a href=/download/index.asp#roku>Roku</a></li>
 </ul>
 </div>
 <div class=small-2 columns hide-for-small-only activities>
 <h6>Activities &amp; Travel</h6>
 <ul class=no-bullet>
 <li><a href=/ski/>Ski &amp; Snow Reports</a></li>
 <li><a href=/MAR/>Marine Weather</a></li>
 <li><a href=/roadtrip/>Road Trip Planner</a></li>
 </ul>
 </div>
 </div>
 </nav>
 <div class=company>
 <div class=row collapse>
 <div class=small-12 medium-1 large-2 columns>
 <ul class=no-bullet 
+IPD,
Checking Data...
responseString = 
>
 <li><a href=https://www.facebook.com/wunderg
+IPD,
Checking Data...
responseString = 
/stickers/ title=Weather Widgets>collapse>
 <div class=small-12 medium-8 medium-push-4 large-8 columns>
 <ul class=inline-list>
 <li><a href=/about/contact.asp>Contact</a></li>
 <li><a href=http://help.wunderground.com/>Support &amp; Feedback</a></li>
 <li><a href=/members/tos.asp#terms>Terms of Use</a></li>
 <li><a href=/members/tos.asp#privacy>Privacy Statement</a></li>
 <li><a href=/adchoices.asp class=ad-choices>AdChoices</a></li>
 </ul>
 </div>
 <div class=small-12 medium-4 medium-pull-8 large-4 columns>
 <p>Copyright &copy; 2016 The Weather Channel,

Can anyone help set me straight or give me some clues?
(I would have submitted the whole code but there seems to be a limit of 9000 characters)

Thanks
Sam

Can you provide a link to documentation of the API call?

I would try the command without the GET.

ieee488:
Can you provide a link to documentation of the API call?

I would try the command without the GET.

I tried it like this:
cmd = "http://api.openweathermap.org/data/2.5/forecast/city?id=6159905&APPID=xxxxxxxxxxxxxxxxxxxxx\r\n\r\n\r\n";

and like this:
cmd = "api.openweathermap.org/data/2.5/forecast/city?id=6159905&APPID=xxxxxxxxxxxxxxxxxxxxx\r\n\r\n\r\n";

Both both returned the code for their web page.

I then tried this:
cmd = "GET api.openweathermap.org/data/2.5/forecast/city?id=6159905&APPID=xxxxxxxxxxxxxxxxxxxxx\r\n\r\n\r\n";

But it returned this:

responseString =
-//IETF//DTD HTML 2.0//EN>

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.

0,

The site that shows how to construct an API call is:

Thanks for your help..

Sam

Have you tried using your web browser to issue your query?
Does it work?

If I use my web browser to issue the query http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98 which is on their website, it works.

I get the response

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":310,"main":"Drizzle","description":"light intensity drizzle rain","icon":"09n"},{"id":721,"main":"Haze","description":"haze","icon":"50n"},{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"stations","main":{"temp":282.49,"pressure":1007,"humidity":81,"temp_min":282.15,"temp_max":283.15},"visibility":10000,"wind":{"speed":6.7,"deg":200,"gust":11.8},"clouds":{"all":92},"dt":1454728342,"sys":{"type":1,"id":5091,"message":0.0438,"country":"GB","sunrise":1454743892,"sunset":1454777898},"id":2643743,"name":"London","cod":200}

I don't know how your ESP8266 comes into the mix. I don't any experience with it.

hello this might help:
http://www.esp8266.com/viewtopic.php?f=29&t=3987&hilit=openweather

I used that as a base for my project this is my API call based on the code in there:

	if(forecast)	{
 client.print("GET /data/2.5/forecast/daily?cnt=2&id=2686657&units=metric&APPID=xxxxxxxxxxxxxxxxxxxxx");
 client.print(" HTTP/1.1\r\n");
 client.print("Host: ");
 client.print("api.openweathermap.org");
 client.print("\r\n");
 client.print("Connection: close\r\n\r\n");
	}
	else	{

 client.print("GET /data/2.5/weather?id=2686657&units=metric&APPID=xxxxxxxxxxxxxxxxxxxxx");
 client.print(" HTTP/1.1\r\n");
 client.print("Host: ");
 client.print("api.openweathermap.org");
 client.print("\r\n");
 client.print("Connection: close\r\n\r\n");


	}

If you ask me how its working i have no clue, but u might be able to pick the pieces you need.

Thanks, I will give it a go.

Sam