0

I'm trying to connect the arduino mega and ethernet shield and try to send some data to a php script. I have connect the ethernet shield to the laptop. Lap is connected to router through wi-fi. Any help in advance please.

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

/******************** ETHERNET SETTINGS ********************/

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD9 };   //physical mac address
byte ip[] = { 192, 168, 1, 5 };                   // ip in lan
byte subnet[] = { 255, 255, 255, 0 };              //subnet mask
byte gateway[] = { 192, 168, 0, 1 };              // default gateway

IPAddress server(192,168,1,2);
EthernetClient client;

void setup() {
    Serial.begin(9600);  // Initialize serial communications with the PC
    while (!Serial);     // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    Ethernet.begin(mac,ip,gateway,subnet);     // initialize Ethernet device
    Serial.print("My IP address: ");
    for (byte thisByte = 0; thisByte < 4; thisByte++) {
      Serial.print(Ethernet.localIP()[thisByte], DEC);
      Serial.print(".");
    }
    Serial.println();
    Serial.println("connecting....");
}

void loop()
{     
  if (client.connect(server,1234)) { // REPLACE WITH YOUR SERVER ADDRESS
    Serial.println("Server connection OK");
    client.println("GET /add.php?temp1=10&hum1=20 HTTP/1.1");
    client.println("Host:" + server);// SERVER ADDRESS HERE TOO
    client.println("Content-Type: text/plain");
  } else {
    Serial.println("connection failure");
  }

  if (client.connected()) {
    client.stop();// DISCONNECT FROM THE SERVER
    Serial.println("disconnected");
  }

  delay(5000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}

Serial Out Put

My IP address: 192.168.1.5.
connecting....
connection failure
connection failure
connection failure
connection failure
connection failure
connection failure
connection failure
1
  • and one more thing to add, I create a server on the shield instead of the client. It's working fine, I checked the IP and port using the laptop browser and I can connect to the server. Shield's client is the issue... Dec 31, 2016 at 3:52

2 Answers 2

1

I had this problem for quite a long time and then I did a little bit of research! Here it is in an easy explanation-

When you connect your Ethernet Shield to the Laptop the laptop takes the Ethernet Shield as a source of internet and tries to get an IP Address and other network stuff FROM the shield! BUT what we want is the other way round.

We want the Ethernet shield to take the laptop as a source of internet and get itself an IP assigned by the laptop!

What I do is I make a Laptop-Ethernet Shield network and a Laptop-WiFi network.(Simply means that I connect my laptop to WiFi and Ethernet shield)

Then I JOIN the two networks!

This joining of networks is called network bridging and is done easily, I'll tell you how-

If you're using linux then follow this link here.

else if you're running windows-

1) Connect your Ethernet Shield (With Arduino attached obviously) to the laptop and make sure that your laptop is connected to the WiFi.

2) Go to Network and Sharing Center.

3) Go to Change adapter settings.

4) Select your Wifi adapter and the Ethernet adapter together.

5) Right click on any of the selected adapter and click Bridge connections.

6) Wait till a new network bridge is connected and is done verifying after it's creation.

7) Upload the sketch again on Arduino and you'd be connected.

NOTE

DHCP may not always work as it's a feature of your router/WiFi and not your laptop so try assigning a mannual IP by calling Ethernet.begin(mac,ip) instead of using DHCP by caling Ethernet.begin(mc);

AND do change your ip to 192.168.1.XXX I mean change the last tuple so it does not clash with the ip of any other device.(XXX any number less than 255, generally 100-200)

Do let me know the outcome after trying this out! :D

6
  • I did as you pointed, then it created a new connection with wifi and ethernet, but stii I can't connect. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD9 }; //physical mac address byte ip[] = { 192, 168, 1, 5 }; // ip in lan Ethernet.begin(mac,ip); // initialize Ethernet device Dec 31, 2016 at 3:47
  • and one more thing to add, I create a server on the shield instead of the client. It's working fine, I checked the IP and port using the laptop browser and I can connect to the server. Shield's client is the issue... Dec 31, 2016 at 3:52
  • Hi @Manav, now it's working.. I check the apache access log and the request is there, but it's returning 400 - bad request ? `data = "temp1=10&hum1=20"; client.println("POST /add.php HTTP/1.1"); client.println("Host: " + server); // SERVER ADDRESS HERE TOO client.println("Content-Type: application/x-www-form-urlencoded"); client.print("Content-Length: "); client.println(data.length()); client.println(); client.print(data);' Dec 31, 2016 at 4:44
  • 192.168.1.120 - - [31/Dec/2016:10:19:52 +0530] "POST /add.php HTTP/1.1" 400 472 "-" "-" 192.168.1.120 - - [31/Dec/2016:10:19:57 +0530] "POST /add.php HTTP/1.1" 400 472 "-" "-" Dec 31, 2016 at 4:50
  • changing following line worked 'client.println("Host: localhost");' tnx for all the support Dec 31, 2016 at 5:10
0

First your network config is wrong. The gateway is outside the scope of your netmask. This is invalid and may fail the tcpip stack depending on how the Arduino library is implemented.

Second, once you fix this, your laptop needs to be configured as a router to access resources on your network resources unless 192.168.1.2 is the laptop you are connected to.

3
  • 1)then what is a correct gateway? 2)192.168.1.2 is the laptop Dec 31, 2016 at 3:48
  • and one more thing to add, I create a server on the shield instead of the client. It's working fine, I checked the IP and port using the laptop browser and I can connect to the server. Shield's client is the issue... Dec 31, 2016 at 3:52
  • I would say that you need to set the gateway to the IP of the laptop (i.e. 192.168.1.2) in this kind of setup. So with IP 192.168.1.5/255.255.255.0 with gateway of 192.168.1.2, anything in the range 192.168.1.XXX will be local. If the arduino tried to access anything outside of 192.168.1.XXX then it will be routed through the gateway IP to then routed through the proper route. Dec 31, 2016 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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