Arduino Forum

International => Português => Topic started by: RenanCastro on Dec 24, 2018, 01:55 pm

Title: Pegar IP externo por API
Post by: RenanCastro on Dec 24, 2018, 01:55 pm
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");
  }   
}


Title: Re: Pegar IP externo por API
Post by: Deva_Rishi on Dec 24, 2018, 03:26 pm
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.
Title: Re: Pegar IP externo por API
Post by: bubulindo on Dec 30, 2018, 12:55 pm
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?