Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Cannot run arduino as web client

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.

Answer*

Draft saved
Draft discarded
Cancel
5
  • So that means to be able to use both I would have to flip flop between SD card and w5100 ? Commented Dec 17, 2016 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. Commented Dec 17, 2016 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 ! Commented Dec 17, 2016 at 16:23
  • @Ciastopiekarz try changing the GET method to POST. Commented Dec 17, 2016 at 20:23
  • Done that already 😞 Commented Dec 18, 2016 at 1:22

AltStyle によって変換されたページ (->オリジナル) /