Go Down

Topic: Pegar IP externo por API (Read 598 times) previous topic - next topic

RenanCastro

Pessoal, estou com dificuldade pra fazer esse código funcionar, preciso pegar um IP externo com essa API, porém ele entra no else.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xA4, 0x28, 0x72, 0xCA, 0x55, 0x2F };
byte ip[] = { 192, 168, 0, 110 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.println(F("My ip is: ")); Serial.println(Ethernet.localIP());
}
void loop()
{
  EthernetClient client = server.available();
  if (client.connect("api.ipify.org", 80)) {
    Serial.println("connected");
    client.println("GET / HTTP/1.0");
    client.println("Host: api.ipify.org");
    client.println();
  } else {
      Serial.println("connection failed");
  }   
}



Deva_Rishi

thank you Google  !
Quote
Personally, I'm having trouble making this code work , I need to get an external IP with this API, but it goes into the else.
It's Portugese.. don't know the answer to the question, but maybe someone else does.
To 'Correct' you have to be Correct. (and not be condescending..)

bubulindo

Há algo de muito estranho no teu código... porque é que inicias um servidor web quando só precisas dum cliente?

Já experimentaste este exemplo?

https://www.arduino.cc/en/Tutorial/WebClient

E tavez adaptar para o site que queres aceder?
This... is a hobby.

Go Up