ntruchsess / arduino_uip

UIPEthernet: A plugin-replacement of the stock Arduino Ethernet library for ENC28J60 shields and breakout boards. Full support for persistent (streaming) TCP-connections and UDP (Client and Server each), ARP, ICMP, DHCP and DNS. Build around Adam Dunkels uIP Stack. Further developed version can be found on https://github.com/UIPEthernet/UIPEthernet
489 stars 211 forks source link

ENC28J60 Web server from a SD card does not work using the UIPEthernet library #87

Closed rubborto closed 9 years ago

rubborto commented 10 years ago

Hi all I'm trying to build a server using an ITEAD ENC28J60 module to serve a page stored in the sd card. The shield works fine using the UIPEthernet library when serving a simple text page, but goes into chaos when trying to combine the ethernet function with the sd card reading. I'm simply getting stucked on the setup as the code goes into an infinite loop with no apparent reason. I have moved the jumper on the board as explained in the shield documentation, so CS pin for the ethernet function is now on pin 8, while sd card is on pin 9. I have changed the ENC28J60_CONTROL_CS to pin 8 on file ENC28j60network.h. When using the simplest setup code to start up the server (with no sd card initialization), the server starts up fine and responds correctly. However when including the sd.h library and the sd card initialization code, the setup code goes into a strange loop and there it remains. Any help from your experience will be very welcome!

Please see here below the first part of the code:

#include <UIPEthernet.h>
#include <SD.h>

byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };                                       
byte ip[] = { 192,168,1,18 };

EthernetServer server(80);

Sd2Card card;
const int chipSelect = 9;  
File webFile;

void setup() 
{
    Serial.begin(9600);     

    Serial.println("connecting...");  
    Ethernet.begin(mac,ip); 
    delay(50),
    server.begin();

    delay(1000);  
    Serial.println(Ethernet.localIP());
    Serial.println(Ethernet.subnetMask());
    Serial.println(Ethernet.gatewayIP());
    Serial.println(Ethernet.dnsServerIP());

    delay(1000);
    Serial.println("Initializing SD card...");
    if (SD.begin(chipSelect))  Serial.println("init with SD.begin: SUCCESS - SD card initialized.");
    else   Serial.println("init with SD.begin: ERROR - SD card initialization failed!");

}
ntruchsess commented 10 years ago

By 'strange loop' you mean you get the same output on serial over and over again? If so it could be that there's not enaugh ram for both SD and UIPEthernet (as SD requires quite a lot and calling SD.begin() already opens the volume and reads mbr etc..) UIPEthernet requires more ram than stock ethernet too. On which model of arduino does this fail? Do you have a mega256 to try out? I have to admit I never tried UIPEthernet with an SD card but from my understanding both libraries should set their individual CS-pin to high after each call and UIPEthernet does make sure hardware-SS is correctly set to output, and both initialize SPI-speed to Cpu-clock/2, so I'd expect they should friendly coexist.

rubborto commented 9 years ago

Thanks Norbert for your reply. I'm working on an Arduino Uno board. By strange loop I mean the serial continues to print "connecting...", "initializing..." and so on infinitely. The SD card is apparently initialized correctly, at least this. Any help will be welcome. Thanks again

ntruchsess commented 9 years ago

that is a boot-loop, the arduino is constantly restarting. Most likely caused by shortage of memory that leads to corrupted (overwritten by heap) stack. If this is the case the only thing you can do to resolve this is use an arduino that has more memory (mega2560). Another possible cause is lack of current if both the enc28j60 and the sd-card are powered by the onboard 3.3V regulator.

rubborto commented 9 years ago

thanks again Norbert for your support. Actually the shield is stacked on top of the arduino, so I assume it's powered correctly. I will double check if I can power it separately, but from a first look at the tech docs does not seem possible. I don't really see the point to market this kind of hw if it cannot work properly. It has given me a big headache and what a big loss of time. I should have bought the original Arduino W5100 module...

consolacion commented 2 years ago

that is a boot-loop, the arduino is constantly restarting. Most likely caused by shortage of memory that leads to corrupted (overwritten by heap) stack. If this is the case the only thing you can do to resolve this is use an arduino that has more memory (mega2560). Another possible cause is lack of current if both the enc28j60 and the sd-card are powered by the onboard 3.3V regulator.

I know this is a very old and even closed thread, but I still like to weigh in, just in case someone has the same problems. memory is indeed at a premium and the UIPethernet library surely guzzles lots of it, but if that would be the problem here, there are a few things to try:

Should you really be pressed for memory, there is a way to free up about 5K of Flash: Go to your /../sketchfolder/libraries/UIPEthernet-master/utility/uipethernet-conf.h and open the uipethernet-conf.h file. in that file you will see the following section:

define UIP_CONF_UDP 1

define UIP_CONF_BROADCAST 1

define UIP_CONF_UDP_CONNS 4

If you set UIP_CONF_UDP to ‘0’ you will save 5kB flash, by disabling UDP. However, if you use DHCP to connect to your router, you cannot disable UDP as the DHCP connection requires UDP. In that case you still can gain a bit of memory by reducing the UIP_UDP_CONNS. An example of using a fixed address is this:

include

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //the IP address for the shield: byte ip[] = { 192, 168, 1, 120 };

void setup() { Ethernet.begin(mac, ip); } void loop() {}