Go Down

Topic: Beginners guide to Arduino Ethernet Shield 2 (Read 24935 times) previous topic - next topic

hydrocontrol

I ran into some pitfalls here, which I wanted to share - maybe it will help others.

I am talking about this one from arduino.ORG
http://www.arduino.org/products/arduino-ethernet-shield-2

I bought it rather accidentally. I thought I was buying a previous version of
Arduino Ethernet shield Revision 3, which is from arduino.CC
https://www.arduino.cc/en/Main/ArduinoEthernetShield

The main difference is that the ORG version uses a newer chip, the W5500.
The CC version uses the W5100.

For differences between the 2 chips, see
http://feilipu.me/2014/11/16/wiznet-w5500-ioshield-a/

The ethernet library in the .CC IDE 1.6.3 does not support the W5500.
The .ORG IDE includes an updated ethernet library, called ethernet2.
However .ORG does not provide a download link on their website, they want you to install the .ORG IDE
But this is not necessary, you can download the library here:
https://github.com/arduino-org/Arduino/tree/Ide-1.7.6-Tian
I downloaded the whole zip and copied the ethernet 2 library into my .CC IDE library folder.

This library
https://github.com/Wiznet/WIZ_Ethernet_Library
might also work, but I haven't tried it.


I connected the ethernet shield to an arduino mega with jumper leads (power and SPI bus), and at first had no luck at all.

1. It turns out the ethernet shield pins 11 - 13, which are the SPI bus on an arduino uno, are NOT CONNECTED on the ethernet shield. You have to use the 2x3pin ICSP header on the ethernet shield instead. On the Mega, it doesn't matter if you use the ICSP or pins 50 - 52 (SPI on the Mega)

2. Pin 10 on the ethernet shield is CS for the W5500, and pin 4 is CS for the micro SD card reader. For some reason, even if there is no micro SD card inserted, the SPI bus is very flakey to the point of outright not working, if you simply leave pin 4 open. It has to be pulled high.

Maybe I would have never discovered these 2 problems, if I would have plugged the shield directly onto the mega. However, the outside pin rows on the shield are longer than the ICSP socket on the shield. So the ICSP header does not plug in properly into the mega, and the SPI connection sporadically drops out.
Solution: shorten the outside pin rows with a side cutter pliers.

Okay, so we have a working setup now.
A good starting point is the webserver sketch, because it allows for some feedback.
On the IDE serial monitor, if you get "server is at 0.0.0.0", then most likely your SPI connection doesn't work.
It means that the IP address write to the W5500 failed, and obviously the subsequent read also.

If the SPI connection doesn't work:
Insert an SD card and try to connect to it. If that doesn't work, maybe your wiring is wrong.
If the SD card SPI works, maybe you have a problem with your ethernet library.
Try removing the SD card again after successfull SPI test, so it doesn't interfere.

The IP adress write will work without a network cable attached.
Once you attach a network cable, the 4 network status LEDs on the shield (100Mbit, LINK, FDX, and ACTIVE) should light up, and also the 2 LEDS on the RJ45 socket.
If not, your cable or your router socket might be defective.
If you connect directly to another PC, and the PC network adapter does not have an auto-crossover function, then you need a crossover network cable.


Now you can try to ping the shield.
If this doesn't work, maybe your IP address is not in the permissible range.
Most probably your router or another PC in your network is set up as DHCP host. The DHCP host normally assigns IP adresses to all attached network devices. In your router settings, a range of IP adresses is reserved for that purpose.
We ignored the DHCP assignment process, and assigned a fixed IP to our shield. However, this IP adress still has to be in the permissible range, because a data packet sent from a PC to an IP adress outside the range will be dropped by the router, because "recipient address not known"
Other things to try:
- did you enter the correct MAC address (sticker on the shield) into the sketch?
- try adding a subnet mask.
See also http://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/basic-web-server/


If the ping works, but the webserver example doesn't, then most likely you once had a functioning SPI connection, and have configured the W5500 correctly. The ping works independently of the arduino MCU - only power to the shield is needed.
However to serve the web page, the MCU is needed.

With me, every time I uploaded a sketch, the webserver was not reachable anymore.
Turns out the communication between W5500 and MCU needs to be reset after a sketch upload.
Two ways to do it:
- cut the power supply by unplugging the USB cable, and plugging it back in.
OR:
- press the 2 reset buttons on the arduino and the shield, and let go SIMULTANEOUSLY. If you first press one reset button, and then the other, it won't work.

Okay, so far. I need to go. I hope it's helpful for someone.
Thomas

http://www.hydroelectronics.net

Bertf

Hi Thomas,

I have the same setup with an Arduino Mega 2560 and Ethernet Shield 2. I use the shield for NTP. Everything works fine but after 303 seconds the arduino freezes. I did it multiple times always when millis() = 303179, 303338, 303184, ... So always after 303 seconds =+/-5min.

Any idea what causes this problem?

Thank you,
Bert

hydrocontrol

#2
Sep 18, 2015, 06:27 pm Last Edit: Sep 18, 2015, 09:06 pm by hydrocontrol
Hi Bertf,

It's probably your code. There are many reasons why code crashes - memory overflow, recursive calls, interrupts messing things up, variables overflowing, unsuitable variable types...

Hints for trouble shooting:
- comment out or delete parts of your code, until the error disappears. The part you deleted before the error disappeared, is the culprit.
- It's not so helpful to know at what time your code crashed, but more helpful to know where in the sketch. Use Serial.print() to to follow each step of your sketch, and the content of the relevant variables.

If you do your homework, explain all the things you have ruled out as cause of the error, and post a complete demo sketch, which is as small as possible, but still shows the error, then the likelyhood will rise that you will find the help you need.

Thomas
http://www.hydroelectronics.net

SurferTim

I like to guess! I might be wrong, but it is fun!

If you are using an int data type instead of unsigned long to store and compare millis(), it will overflow at 32767 and cause problems.

satyamkr80

I am unable to send data to server, when I connect my Arduino uno directly to a router, it works fine when I connect hardware on my laptop Ethernet port and then bridge wi fi connection with it.

Error:
Failed to configure Ethernet using DHCP

hydrocontrol

@satyamkr80:
Does your router or another device in your network actually run a DHCP server service?
I assume you've checked that the cable connection is okay (activity LEDs blinking on ethernet shield and router)?
Have you ruled out that you are by accident using a crossover cable, and this might be the problem?
Every time you upload a sketch, you need to reset both shield and arduino - see my original post.
Maybe you should mention what shield and what example sketch you are using.
See also
http://forum.arduino.cc/index.php?topic=331202.0
http://forum.arduino.cc/index.php?topic=108301.0
In general, it is a good idea to provide as much detail as possible and mention all the things you have already checked, if you are asking for help.

Thomas
http://www.hydroelectronics.net

Alasdair_Lee

Thomas,

Apologies for commenting on and old post but I believe I may be experiencing a problem that you could help with!

I am new to Arduino and have very little experience with things of this type. I have an Uno r3 with the Ethernet Shield 2 and have been working my way through an example from the Exploring Arduino book in which a server is created to provide control of some of the Arduino outputs via a local network.

In the sketch, IP is assigned via DHCP and all seems to be ok as I can see the assigned IP on the serial monitor, however.... when I try to connect to the server my browser does nothing. I can ping the IP address with no problem.

I added a few serial print lines to see if I could see what was going on and it appears that the line 'if (client) {' never seems to be true as the following line 'Serial.println("2");' doesn't occur. I have no idea why this is.

Would greatly appreciate any help! Thanks in advance.

Thanks

Alasdair


Below is a copy of the sketch I am using;

#include <Ethernet2.h>
#include <EthernetServer.h>
#include <EthernetUdp2.h>
#include <SPI.h>

const int BLUE = 5;
const int GREEN = 6;
const int RED = 7;
const int SPEAKER = 3;

int freq = 0;
int pin;


byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x5F, 0x29 };

EthernetServer server = EthernetServer(80);

boolean receiving = false;

void setup() {
  Serial.begin(9600);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  if (!Ethernet.begin(mac))
  {
    Serial.println("Could not configure Ethernet with DHCP");
    return;
  }
  else
  {
    Serial.println("Ethernet configured");
  }

  server.begin();
  Serial.print("Server started.\nLocal IP: ");
  Serial.println(Ethernet.localIP());
}
void loop() {  Serial.println("1");
  EthernetClient client = server.available();
  if (client) {
    Serial.println("2");
    boolean currentLineIsBlank = true;

    boolean sentHeader = false;

 

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        if (receiving && c == ' ') receiving = false;
        if (c == '?') receiving = true;

        if (receiving)
        {
          if (c == 'L')
          {
            Serial.print("Toggling Pin ");
            pin = client.parseInt();
            Serial.println(pin);
            digitalWrite(pin, !digitalRead(pin));
            break;
          }

          else if (c == 'S')
          {
            Serial.print("Setting frequency to ");
            freq = client.parseInt();
            Serial.println(freq);
            if (freq == 0)
            {
              noTone(SPEAKER);
            }
            else
            {
              tone(SPEAKER, freq);
              break;
            }
          }
        }
        if (!sentHeader)
        {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html\n");

          //Red Toggle
          client.println("<form action='' method='get'>");
          client.println("<input type='hidden' name='L' value='7' />");
          client.println("<input type='submit' value='Toggle Red' />");
          client.println("</form>");

          //Green Toggle
          client.println("form action='' method='get'>");
          client.println("<input type='hidden' name='L' value='6' />");
          client.println("<input type='submit' value='Toggle Green' />");
          client.println("</form>");

          //Blue Toggle
          client.println("<form action='' method='get'>");
          client.println("<input type='hidden' name='L' value='5' />");
          client.println("<input type='submit' value='Toggle Blue' />");
          client.println("</form>");

          //Speaker Slider
          client.println("<form action='' method='get'>");
          client.println("<input type='range' name='S' min='0' max='1000' step='100' value='0' />");
          client.println("<input type='submit' value='Set Frequency' />");
          client.println("</form>");

          sentHeader = true;
        }

        if (c == '\n' && currentLineIsBlank) break;

        if (c == '\n')
        {
          currentLineIsBlank = true;
        }

        else if (c != '\r')
        {
          currentLineIsBlank = false;
        }
      }
    }
    delay(5);
  }
}

hydrocontrol

#7
Jan 26, 2016, 07:01 pm Last Edit: Jan 26, 2016, 07:09 pm by hydrocontrol
Hi Alasdair,

sucessful ping does not mean your arduino is talking to your ethernet shield. It only means they used to talk to each other sucessfully once in the past. Try resetting both simultaneously. See my original post.

Code: [Select]
  EthernetClient client = server.available();
  if (client) {

client is declared as a struct of the type "EthernetClient", that will contain something other than Zero if the server is available.
if(0) means the if clause will never be executed.
So, your server is not available. To find out how the code comes to that conclusion, you will have to look into the function .available() and see what happens there.
I'm afraid I've never explored the server example in depth, so I don't have any experience how it works.

Thomas
http://www.hydroelectronics.net

Alasdair_Lee

Thomas,

Thank you for the quick reply, I have since tried other example server sketches and they work fine. Cannot explain why this one does not work.


Alasdair



pert

Since this got bumped I thought I'd mention you can download the Ethernet2 library here: https://github.com/adafruit/Ethernet2
This makes for a smaller download and simple installation in the arduino.cc IDE.

Alasdair_Lee

Thanks for the tip.

Solved my problem, was just missing a line to close the connection. 

apophene

Since this got bumped I thought I'd mention you can download the Ethernet2 library here: https://github.com/adafruit/Ethernet2
This makes for a smaller download and simple installation in the arduino.cc IDE.
Thanks, pert! Closing in on a year later and this is still useful; just saved me the .org IDE download!

pert

Just to update. The Ethernet2 library is now also available for installation via Library Manager:
  • Sketch > Include Library > Manage Libraries...
  • Type "ethernet2" in the "Filter your search..." field.
  • Click on the Ethernet2 entry.
  • Click "Install"
  • Wait for installation to complete.
  • Click "Close"

indy49

#13
Jan 19, 2017, 09:30 pm Last Edit: Jan 19, 2017, 09:42 pm by indy49
Hi,

I admit I'm lost with the different Arduino-cc/org and Ethernet shields with various libraries coming from various locations.

I have an Arduino Uno and an Ethernet Shield V2 (both official I think)

I had downloaded and added a lib named Ethernet2-1.0.3 before I knew there were trouble between cc/org.
I've seen another link (above) to Ethernet2-master.zip but I don't see where to uninstall the first library.
I get the feeling that if I install both, things will be worse...

Still, for the time being, I get trouble with my boards : they stop working after a few hours.
What should I do?

I should add that I've verified memory leaks (with MemoryFree) and there seem to be none.

Thank you for your help and advices...

pert

Ethernet2-1.0.3 is the latest release of the Ethernet2 library. Ethernet2-master is the current development version of the library. Currently the only differences between the two are some minor changes in the documentation. Absolutely no functional difference between them.

Go Up