Failing to connect Ethernet card

I had the code below working perfectly yesterday. code was uploaded multiple times to the arduino Uno with an 5100 ethernet shield.

However, today I just cannot get a connection. The responses from serial show no ip is obtained, no connection…

(i removed some digits from the api key) I think something similar happened a while back, but think I wrongly blamed the code back then. I think this time the problem is unrelated to the code.

Could anybody suggest what could be causing the problem. There are lights on the ethernet cable connection, and nothing has changed on the router. I don’t see a connection to the arduino on the router (im not sure if I could see the connection when i had this working previously).

really frustrating…

/*
  Web client

 */

#include <SPI.h>
#include <Ethernet.h>

//  MAC address 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 22);

//server
char server[] = "api.openweathermap.org";    
// Initialize the Ethernet client library

EthernetClient client;

void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");

  Ethernet.begin(mac);

  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.print("connected to ");
    Serial.println(client.remoteIP());
    
    // Make a HTTP request:
    client.println("GET http://api.openweathermap.org/data/2.5/weather?id=2629&appid=35628bb5def7f74aac9aa1 HTTP/1.1");
    client.println("Host: server");
    client.println("Connection: close");
    client.println();
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  int len = client.available();

    byte buffer[8];
    if (len > 8) len = 8;
    client.read(buffer, len);
      Serial.write(buffer, len); // show in the serial monitor (slows some boards)
  
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    
    // do nothing forevermore:
    while (true) {
      delay(1);
    }
  }
}

Try it like this.

/*|-------------------------------------------------|*/
/*|Project: OpenWeatherMap API connection W5100     |*/
/*|Edited by: Martin Chlebovec (martinius96)        |*/
/*|E-mail: martinius96@gmail.com                    |*/
/*|-------------------------------------------------|*/


#include <SPI.h>
#include <Ethernet.h>

//  MAC address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 22); //WHY, IF YOU WANT IP ADDRESS FROM DHCP SERVER?????
//YOU ARE NOT USING THIS IN YOUR CODE
const char* host = "api.openweathermap.org";
EthernetClient client;

void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");

  if (Ethernet.begin(mac) == 0) {
    Ethernet.begin(mac);
  }
  Serial.print("  DHCP assigned IP ");
  Serial.println(Ethernet.localIP());
  Serial.println("Ready");
  Serial.print("connecting to ");
  Serial.print(host);
  Serial.println("...");
  client.stop();
  if (client.connect(host, 80)) {
    Serial.println("Connection sucessful");
    String url = "/data/2.5/weather?id=2629&appid=XXXXXXXXXXXXXXXXXXX";
    client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: W5100\r\n" + "Connection: close\r\n\r\n");
    while (client.connected()) {
      String line = client.readStringUntil('\n'); //HTTP HEADER
      Serial.println(line);
      if (line == "\r") {
        break;
      }
    }
    String line = client.readString(); //PAYLOAD
    Serial.println(line);
  } else {
    Serial.println("Connection was not successful! Please retry");
  }
}

void loop() {

}

But also it looks like you have some problem in your query…
City was not found that you are using in your request.

Thanks for your response. I was trying to connect using DHCP and also by specifying. (I should have removed this line). Previously I was able to connect using either method. I removed some random characters from the api line to make it anonymous which is why the city is not found.

I don't think there's anything wrong with my code since it worked perfecly the last few days, but has since stopped connecting, and I cannot work out why. Perhaps there is any indication on the actual arduino which could indicate what the problem might be... or could it be a router issue not accepting the connection any longer for some reason... firewall, or something else?

Thanks