Connecting my Ethernet shield

Hello,

I am completely new to the Arduino ethernet shield, and have only vague idea of networking. Althouth this is what i am trying to learn.

My problem is connecting my ethernet shield. I will try and add as much information for you to help me.

  1. My shield and Arduino didn’t come with any documentation.
  2. I am using a macbook pro
  3. Arduino uno port is /dev/cu.usbmodem1421
  4. Im using a WAMP server

Can you please check if this code should be working please, and where i can find the information in arrows.

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};    // Youtube: 'default, doesn't really matter'

byte ip[] = {192, 168, 0, 10};   <----- Where can i find this? this is my wifi for the laptop at the moment   

byte subnet[] = {255, 255, 255, 0};                              // Class   

byte gateway[] = {192, 168, 0, 1};                               // This is my router

IPAddress server(134, 119, 45, 10);       <---- these are just random numbers, how do i find the actual IP?
  1. Im using a WAMP server

You are using Windows, Apache, MySQL and PHP on a Mac?

byte ip[] = {192, 168, 0, 10};   <----- Where can i find this? this is my wifi for the laptop at the moment

What do you mean by “my wifi for the laptop”? That is an IP address, and has nothing to do with how the Arduino or laptop communicates with the router. It is the address that the router assigned to the laptop. You need to configure the router to assign an address for the Arduino, too.

(Your laptop probably uses DHCP to get an IP address, so it knows what address it was given. The Arduino could use DHCP, too, but it might be easier to have the router reserve a static address for the Arduino.)

Can you please check if this code should be working please

No, it shouldn’t. It’s nowhere near complete.

IPAddress server(134, 119, 45, 10); <---- these are just random numbers, how do i find the actual IP?

Use the ping command.

Im using MAMP sorry.

PaulS: You need to configure the router to assign an address for the Arduino, too. Have the router reserve a static address for the Arduino.

PaulS: Use the ping command.

Could you elaborate on how to do both of these please.

Please start from the beginning by Google : arduino ethernet shield tutorial
There will be a lot of tutorials explaining the basic set up of the Ethernet shield.

By the way: the ping command (on your PC) is a basic tool, to see if an IP addresse is active on your net.
Ex:
ping 198.162.1.1 will try to contact the device with IPaddress 192.168.1.1 (could be your router)

The response could be (if address is alive):

Pinging 192.168.1.1 with 32 bytes of data:

Reply from 192.168.1.1: bytes=32 time=2ms TTL=64
Reply from 192.168.1.1: bytes=32 time=2ms TTL=64
Reply from 192.168.1.1: bytes=32 time=3ms TTL=64
Reply from 192.168.1.1: bytes=32 time=4ms TTL=64

Ping statistics for 192.168.1.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 2ms, Maximum = 4ms, Average = 2ms

/ffur

Could you elaborate on how to do both of these please.

Send me your router, so I can see what kind it is.

Im still having trouble with this so i will post everything to do with my projects so far (see attachment). Hopefully you can tell me why my Arduino isn’t connecting to the server :frowning:

fyi: i’m no longer using a router, the ethernet is going straight into the mac (see attachment)

This is my Arduino code:

#include <SPI.h>
#include <Ethernet.h>
#include <Client.h>             //library for client functions

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};    // MAC address
byte ip[] = {192, 168, 0, 200};     // IP of Arduino                                 
byte gateway[] = {192, 168, 0, 1};

IPAddress server(192, 168, 0, 10);

EthernetClient client;

bool connected = false; // whether or not you got a message from the client yet

void setup(void) {
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection:
  Serial.println("Trying to get an IP address using DHCP");
  delay(1000);
  Ethernet.begin(mac);
  Serial.println("Status ");
  analogReference(INTERNAL);

}

void loop(void){

  if(!connected)
{
Serial.println("Not connected");
if (client.connect(server,80))
{
    connected = true;
    Serial.print("Status is ");
    Serial.println("42");
    Serial.println();
    Serial.println("Sending to Server: ");

    client.print("GET /php_get_write.php?state=");
    Serial.print("GET /php_get_write.php?state=");
    client.print("42");
    Serial.print("42");

    Serial.println();
    client.println();
    client.println("HTTP/1.1\r\n");
    Serial.println();
    Serial.println("HTTP/1.1\r\n");
    client.println("Host: localhost\r\n");
    Serial.println("Host: localhost\r\n");
    client.println();
    client.println("User-Agent: Arduino\r\n");
    Serial.println("User-Agent: Arduino\r\n");
    client.println("Accept: text/html\r\n");
    Serial.println("Accept: text/html\r\n"); 
    client.println();
    Serial.println();
    delay(1000);
}
else
{
    Serial.println("Cannot connect to Server");
}
  }
  else
  {
    delay(1000);
    while (client.connected() && client.available())
{
    char c = client.read();
    Serial.print(c);


}
Serial.println();
client.stop();
connected = false;
}
 }

And this is what prints when i type ifconfig:

ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
	options=3<RXCSUM,TXCSUM>
	inet6 ::1 prefixlen 128 
	inet 127.0.0.1 netmask 0xff000000 
	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
	nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
	ether 68:5b:35:81:ff:91 
	inet6 fe80::6a5b:35ff:fe81:ff91%en0 prefixlen 64 scopeid 0x4 
	inet 192.168.0.200 netmask 0xffff0000 broadcast 192.168.255.255
	nd6 options=1<PERFORMNUD>
	media: autoselect (100baseTX <full-duplex>)
	status: active
fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
	lladdr 44:fb:42:ff:fe:a8:54:6e 
	nd6 options=1<PERFORMNUD>
	media: autoselect <full-duplex>
	status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	ether a8:bb:cf:19:b1:3a 
	inet6 fe80::aabb:cfff:fe19:b13a%en1 prefixlen 64 scopeid 0x6 
	inet 192.168.0.10 netmask 0xffffff00 broadcast 192.168.0.255
	nd6 options=1<PERFORMNUD>
	media: autoselect
	status: active
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=60<TSO4,TSO6>
	ether d2:00:1a:85:46:e0 
	media: autoselect <full-duplex>
	status: inactive
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
	ether 0a:bb:cf:19:b1:3a 
	media: autoselect
	status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1452
	ether 5e:44:5e:56:27:a7 
	inet6 fe80::5c44:5eff:fe56:27a7%awdl0 prefixlen 64 scopeid 0x9 
	nd6 options=1<PERFORMNUD>
	media: autoselect
	status: active
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=63<RXCSUM,TXCSUM,TSO4,TSO6>
	ether 6a:5b:35:18:6e:00 
	Configuration:
		id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
		maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
		root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
		ipfilter disabled flags 0x2
	member: en2 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 7 priority 0 path cost 0
	nd6 options=1<PERFORMNUD>
	media: <unknown type>
	status: inactive
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1500
	inet6 fe80::bdf7:d22:9c93:8b1%utun0 prefixlen 64 scopeid 0xb 
	nd6 options=1<PERFORMNUD>

fyi: i'm no longer using a router, the ethernet is going straight into the mac (see attachment)

Why? The mac is just adding an additional layer of complexity to the situation.

so the connection will be: Ethernet into router, and Arduino usb to the mac.

I thought a direct connection to the mac would make it simpler. I'll i connect it to the router then. Can you see why it wont connect?

Where did you get your Ethernet Shield? Can you post a link or look at the chip on the shield to see if it says WizNet W5100? There are several different versions with different Ethernet controller chips on them. The stock Arduino Ethenet library only supports the W5100 so if your shield has a different chip then you would need to install a different library for it to work.

The link to where i bought it - http://www.ebay.co.uk/itm/161481080450

It appears to be the W5100. I have changed the mac address in the code as it is actually stated on the page.

I got the IP of the Arduino through the DhcpAddressPrinter example sketch. I cant see why it isn't working?

Ok, so the Ethernet library is the correct one to use so that's not the cause of your problem.

What does your output on your serial monitor show? Are you shure your server address, which must be the addres of your Mac, are 192 168 0.10 I can see 192.168.0.200 on the en0 interface (from your printout). Your IPAddress are the address of the Arduino Ethernet shield, and must be a free address in your address range. Your Gateway address must be the address of your router. Your Server address must be the address of the device, where your Server application is running.

Insert the correct addresses and try to ping, what you think are your Server address. If successfull, then ping your router (Gateway). If successfull, then ping your Arduino. If successfull your Arduino are connected to your network.

But let's see your output from your monitor.

/ffur

ffur:
What does your output on your serial monitor show?

Trying to get an IP address using DHCP
Status
Not connected
Cannot connect to Server
Not connected
Cannot connect to Server
Not connected
Cannot connect to Server
Not connected

ffur:
Are you sure your server address, which must be the address of your Mac, are 192 168 0.10

System Preferences > network settings > mac (connected via wifi) and it says ‘Wi-Fi is connected to MYHOUSE and has the IP address 192.168.0.4.’

Arduino sketch > DhcpAddressPrinter = 192.168.0.15

Still not working >:(

#include <SPI.h>
#include <Ethernet.h>
#include <Client.h>             //library for client functions

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};    // MAC address
IPAddress ip(192, 168, 0, 15);     // IP of Arduino                                 
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

IPAddress server(192, 168, 0, 4);

EthernetClient client;

bool connected = false; // whether or not you got a message from the client yet

void setup(void) {
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection:
  Serial.println("Trying to get an IP address using DHCP");
  delay(1000);
  Ethernet.begin(mac);
  Serial.println("Status ");
  analogReference(INTERNAL);

}

void loop(void){

  if(!connected)
{
Serial.println("Not connected");
if (client.connect(server,80))
{
    connected = true;
    Serial.print("Status is ");
    Serial.println("42");
    Serial.println();
    Serial.println("Sending to Server: ");

    client.print("GET /php_get_write.php?state=");
    Serial.print("GET /php_get_write.php?state=");
    client.print("42");
    Serial.print("42");

    Serial.println();
    client.println();
    client.println("HTTP/1.1\r\n");
    Serial.println();
    Serial.println("HTTP/1.1\r\n");
    client.println("Host: localhost\r\n");
    Serial.println("Host: localhost\r\n");
    client.println();
    client.println("User-Agent: Arduino\r\n");
    Serial.println("User-Agent: Arduino\r\n");
    client.println("Accept: text/html\r\n");
    Serial.println("Accept: text/html\r\n"); 
    client.println();
    Serial.println();
    delay(1000);
}
else
{
    Serial.println("Cannot connect to Server");
}
  }
  else
  {
    delay(1000);
    while (client.connected() && client.available())
{
    char c = client.read();
    Serial.print(c);


}
Serial.println();
client.stop();
connected = false;
}
 }

The end of your setup section looks a little weird. Your Serial.println will only print the word "Status" ? You are not using DHCP, so your Arduino should have the IP address 192.168.0.15 Have you tried to ping the Arduino from your PC ?

Try a simpler working sketch to see if you have a connection/shield/programming problem. Ex try this tutorial : https://www.arduino.cc/en/Tutorial/WebClient

/ffur

Test code you can try unmodified to see if you get an error code returned from the remote server.

//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from weather server captured in readString 

#include <SPI.h>
#include <Ethernet.h>
String readString;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "api.openweathermap.org"; // myIP server test web page server
EthernetClient client;

//////////////////////

void setup(){

  Serial.begin(9600); 
  Serial.println("client readString test 11/04/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  Serial.println();
  
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /data/2.5/weather?zip=46526,us HTTP/1.1"); //download text
    client.println("Host: api.openweathermap.org");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client.stop(); //stop client
  Serial.println("client disconnected.");
  Serial.println("Data from server captured in readString:");
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println();
  Serial.println("End of readString");
  Serial.println("=======e===========");
  Serial.println();
  readString=""; //clear readString variable

}

ffur: Try a simpler working sketch to see if you have a connection/shield/programming problem. Ex try this tutorial : https://www.arduino.cc/en/Tutorial/WebClient

/ffur

Ive tried this and it appears to work, so i assume my arduino is connected to the internet. I think it may be my server thats causing the problem then.

I will tried the test code provided by zoomkat and it doesn't work. The only parameter i changed is swapping 'api.openweathermap.org' for 'localhost:8888' ???

I’ve the ‘IP address’ part of my code and something is working!!!

I can’t tell where it stops working though??. See attachment to see what i’m now seeing please

Test code you can try unmodified

I will tried the test code provided by zoomkat and it doesn't work. The only parameter i changed is swapping 'api.openweathermap.org' for 'localhost:8888' ???

Did you miss the "unmodified" instruction?

See attachment to see what i'm now seeing please

DO NOT POST ANOTHER DAMNED PICTURE OF TEXT!

Unmodified gives me this:

client readString test 11/04/13
Send an e in serial monitor to test

connected
client disconnected.
Data from server captured in readString:

HTTP/1.1 401 Unauthorized
Server: openresty
Date: Tue, 15 Mar 2016 12:57:18 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 107
Connection: close
X-Cache-Key: /data/2.5/weather?zip=46526,us
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST

{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}

End of readString
=======e===========