0

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.

2

This could be an SD card SPI bus issue.

Note that because the W5100 and SD card share the SPI bus, only one can be active at a time. If you are using both peripherals in your program, this should be taken care of by the corresponding libraries. If you're not using one of the peripherals in your program, however, you'll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and write a high to it. For the W5100, set digital pin 10 as a high output.

Check the Arduino shield datasheet.

Changing pin mode:

pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
  • So that means to be able to use both I would have to flip flop between SD card and w5100 ? – Ciasto piekarz Dec 17 '16 at 15:30
  • Yes most probably you will make pin 4 OUT when you will not use then 4 IN when you will be using it. And i think it is preferable not to use pin 4 for any i/o data. Like don't put your DHT on pin 4. I can see from your code you are using pin 8 so its fine. – Kegham K. Dec 17 '16 at 15:33
  • thanks that helps, however even after it is connected I see it is not pushing data based on the web client call to URL as shown in above post ! – Ciasto piekarz Dec 17 '16 at 16:23
  • @Ciastopiekarz try changing the GET method to POST. – Kegham K. Dec 17 '16 at 20:23
  • Done that already 😞 – Ciasto piekarz Dec 18 '16 at 1:22
0

I don't know what the problem is, but I can see some issues with your code. Perhaps fixing them will make it work, or at least get you closer.

Reading between the lines in your code, it looks like you are expecting an IP address from DHCP. You won't get that, according to the documentation because you are specifying one. Just so that you know.

You're calling the four-parameter version of Ethernet.begin() but using an invalid set of parameters. You're probably meaning to use one of these:

Ethernet.begin(mac, ip, dns, gateway); 
Ethernet.begin(mac, ip, dns, gateway, subnet);

or if you want to use DHCP (which I'd recommend, if you can, because managing them yourself is a pain), just use

int success == Ethernet.begin(mac);
if (success) { /* it worked */ }

Lastly -- and this is a guess -- I wonder if adding a delay(1000) after Ethernet.begin() would change the behaviour. It's not really a solution, but it might help you debug.

  • I removed SD card I inserted yesterday night, and then it seems to work but not able to insert into db also instead of using server port 80 I used port 443, since I have HTTP to HTTPS redirection on apache web server running at home. – Ciasto piekarz Dec 17 '16 at 9:09
  • I don't understand the significance of what you're saying. New problems for a new question perhaps? – Mark Smith Dec 17 '16 at 9:13
  • however , atleast you can tell me why removing SD card make it works. – Ciasto piekarz Dec 17 '16 at 9:52
  • 1
    I can't, sorry. – Mark Smith Dec 17 '16 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.