I am trying to get a valid response from api.apixu.com. I was using Weather Underground but they turned off my key so I started using Apixu but they told me that you cannot use an IP address. So I am at a loss how to use my Photon to get it.
Thanks
I am trying to get a valid response from api.apixu.com. I was using Weather Underground but they turned off my key so I started using Apixu but they told me that you cannot use an IP address. So I am at a loss how to use my Photon to get it.
Thanks
More context please.
What IP where and for what?
I was using code like this to get weather form Weather Underground and it worked very well, but I can’t use them now so I am trying to use Apixu to download local weather conditions. I dinked with the code for a few day, but could not get a valid response. So I sent off an email and they said to use a call like http://api.apixu.com/v1/current.json?key=APIXU_Key &q=91321, not an IP address. And this works fine in my browser. Below is the code that I was trying to use, but it seems that this is not going to work w/Apixu. If you have any ideas I would like to get them.
Thanks
void ApixuWeather::Connect(String ZipCode, String *str)
{
int x=0;
int LocStart;
uint8_t i;
TCPClient client;
byte server[] = { 83, 136, 254, 59 };// api.apixu.com
String APIXU_Key = "My Key Data";
String Data = "GET /v1/current.json?key=APIXU_Key &q=91321 HTTP/1.1";
Serial.println("Connecting to the API...");
// Connect
if(client.connect(server, 80))
{
Serial.println("connected");
Serial.println(Data);
client.println(Data);
client.println("Host: api.apixu.com");
client.println("Content-Length: 0");
client.println();
}
else
{
Serial.println("connection failed");
return;
}
// API
for( i = 1; i < 20; i++)
{
if(!client.available())
delay(100);
else
break;
}
//Get data
if(client.available())
{
while(client.available())
RespString=client.readString();
}
for(i=0; i<10; i++)
{
if (!client.connected())
{
Serial.println();
Serial.println("Disconnecting from the API.");
client.stop();
Particle.connect();
break;
}
delay(20);
}
Serial.println(RespString);
}
The first ip address was for apixu.com,
api.apixu.com is 94.237.49.36
If you mean that the IP for that domain may change, then that’s what DNS is for.
Via WiFi.resolve()
you can get DNS to translate the domain into the IP address for your connect.
https://docs.particle.io/reference/device-os/firmware/photon/#resolve-
An alternative would be to use HTTPClient or Webhooks.
The latter even support https://
servers
BTW, I’m pretty sure there should not be a blank between APIXU_Key
and &q=91321
.
BTW²: We recommend to avoid String
and rather use C strings (char
arrays) wherever possible.
Thanks for the help I ended up using Webhooks and I am getting my data. I am curious why I get the data in two blocks, 512, and then 441 instead of one 953 chunk.
@mgcurtis, that is a function of the Particle Cloud in that it limits “chunk” responses to 512 bytes.