Getting Website Data

photon
Tags: #<Tag:0x00007f1ca29a9820>

#1

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


#2

More context please.
What IP where and for what?


#3

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);
 }

#4

The first ip address was for apixu.com,
api.apixu.com is 94.237.49.36


#5

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.