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