I have ethernet shield mounted on arduino mega, I am able to run it as server but when i try to run it as client it doesnt work , most of the time it assigns itself 255.255.255.255
ip address which i get to know as i am printing from my sketch using Ethernet.localIP()
it sometimes randomly works but then it does nt establish connection with server, I have double checked there is no firewall on my router that can block arduino ethernet web client.
#include "Ethernet.h"
#include <SPI.h>
#include<dht.h>
dht DHT;
#define DHT11_PIN 8
double tempInC;
int humidity;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
//IPAddress ip(192, 168, 1, 177);
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,177);
byte subnet[] = { 255, 255, 255, 0 }; //assigning subnet mask
byte gateway[] = { 192, 168, 1, 1 }; //assigning gateway
EthernetClient client;
char server[] = "192.168.1.3"; // IP Adres (or name) of server to dump data to
String data;
void setup() {
Serial.begin(9600);
while (!Serial) {
; //wait for serial port to connect. Needed for native USB port only
}
Ethernet.begin(mac, ip, gateway, subnet);
Serial.print(F("connected. My IP is "));
Serial.println(Ethernet.localIP());
// give the Ethernet shield a second to initialize:
delay(1000);
data = "";
}
void loop() {
int chk = DHT.read11(DHT11_PIN);
delay(200);
tempInC = DHT.temperature;
humidity = DHT.humidity;
Serial.print("Temp: ");
Serial.print(tempInC);
Serial.print(" Humidity: ");
Serial.println(humidity);
if (client.connect(server, 80)) {
// Make a HTTP request:
client.print(F("GET /arduino/add.php?temp1="));
client.print(tempInC);
client.print(F("&hum1="));
client.print(humidity);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(server);
client.println(F("Connection: close"));
client.println();
}
else {
Serial.println(F("connection failed"));
}
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
else {
Serial.println("Client not available");
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
// delay(60000); // WAIT FOR A MINUTE BEFORE SENDING AGAIN
delay(10000);
}
Here is the output of what I get from above sketch
connected. My IP is 255.255.255.255
Temp: 26.00 Humidity: 63
connection failed
Client not available
Even when it gets correct IP assigned which I am setting up in the sketch the connection to server fails. I am very upset not being able to run client. Please help.