ENC28J60 Libraries

can anyone tell me where I can find the Libraries for a ENC28J60 sheild?

Thanks, Yonu

The ENC28J60 is not officially supported by Arduino. The two best libraries are EtherCard and UIPEthernet.

https://github.com/jcw/ethercard https://github.com/ntruchsess/arduino_uip

The UIPEthernet is compatible with the Arduino Ethernet library, the EtherCard has its own functions.

Thanks the UIPEthernet lib worked great now I have a slightly related question I am trying to get this code smaller it is using 99% of dynamic memory and I still have several lines to write I am hoping that someone here may be able to make suggestion to thin it out.

Thanks,
Yonu

#include <UIPEthernet.h>
//#include <EEPROM.h>
int pinButton1 = 17;
int pinButton2 = 16;
int pinButton3 = 15;
int pinButton4 = 14;
int led = 4;
int led2 = 5;
int led3 = 6;
int led4 = 7;
int led5 = 8;
long time = 0;
long debounce = 500;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x28 };   //physical mac address
byte ip[] = { 192, 168, 1, 109 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")

EthernetServer server(80);                             //server port     
String readString;
char serv[] = "www.google.com";    // name address for Google (using DNS)
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);pinMode(led3, OUTPUT);pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);pinMode(pinButton1, INPUT);pinMode(pinButton2, INPUT);
  pinMode(pinButton3, INPUT);pinMode(pinButton4, INPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}
void loop() {
  //toggles
  //stateButton = digitalRead(pinButton);  
  if(pinButton1 == HIGH && millis() - time > debounce) {
    if(digitalRead(led == HIGH)) {
     // stateRelay = LOW; 
      digitalWrite(led, LOW);
    } else {
       //stateRelay = HIGH;
       digitalWrite(led, HIGH); 
    }
    time = millis();
    }

  if(pinButton2 == HIGH && millis() - time > debounce) {
    if(digitalRead(led == HIGH)) {
     // stateRelay = LOW; 
      digitalWrite(led2, LOW);
    } else {
       //stateRelay = HIGH;
       digitalWrite(led2, HIGH); 
    }
    time = millis();
    }

  if(pinButton3 == HIGH && millis() - time > debounce) {
    if(digitalRead(led3 == HIGH)) {
     // stateRelay = LOW; 
      digitalWrite(led3, LOW);
    } else {
       //stateRelay = HIGH;
       digitalWrite(led3, HIGH); 
    }
    time = millis();
    }

  if(pinButton4 == HIGH && millis() - time > debounce) {
    if(digitalRead(led4 == HIGH)) {
     // stateRelay = LOW; 
      digitalWrite(led4, LOW);
    } else {
       //stateRelay = HIGH;
       digitalWrite(led4, HIGH); 
    }
    time = millis();
    }            
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          Serial.print(c);
         }
         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H2>Master Suite</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off LED</a>
");   
           client.print(digitalRead(led));
           client.println("
");     
           client.println("<a href=\"/?button2on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button2off\"\">Turn Off LED</a>
");   
           client.print(digitalRead(led2));
           client.println("
"); 
           client.println("<a href=\"/0?button3on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button3off\"\">Turn Off LED</a>
");   
           client.print(digitalRead(led3));
           client.println("
"); 
           client.println("<a href=\"/?button4on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button4off\"\">Turn Off LED</a>
");   
           client.print(digitalRead(led4));
           client.println("
");     
           client.println("<a href=\"/?button5on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button5off\"\">Turn Off LED</a>
");   
           client.print(digitalRead(led5));
           client.println("
");                
           client.println("</BODY>");
           client.println("</HTML>");
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);
              if (client.connect(serv, 80)) {
                 Serial.println("connected");
                // Make your API request:
                client.println("GET /sendmsg?user=test&password=********&api_id=3539384&to=********&text=hello HTTP/1.1");
                client.println("Host: api.clickatell.com/http/sendmsg");
                client.println("Connection: close");
                client.println();
              } 
              else {
                    // kf you didn't get a connection to the server:
                    Serial.println("connection failed");
                   }
           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button2on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button2off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button3on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button3off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button4on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button4off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button5on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button5off") >0){
               digitalWrite(led, LOW);
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

Put all unchanging (constant) variables and text in Flash memory instead of RAM by using the F() macro

#include <UIPEthernet.h>
//#include <EEPROM.h>
const byte pinButton1 = 17;
const byte pinButton2 = 16;
const byte pinButton3 = 15;
const byte pinButton4 = 14;
const byte led = 4;
const byte led2 = 5;
const byte led3 = 6;
const byte led4 = 7;
const byte led5 = 8;
long time = 0;
const long debounce = 500;
const byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x28 };   //physical mac address
const byte ip[] = { 192, 168, 1, 109 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")

EthernetServer server(80);                             //server port    
String readString;
const char serv[] = "www.google.com";    // name address for Google (using DNS)
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);pinMode(led3, OUTPUT);pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);pinMode(pinButton1, INPUT);pinMode(pinButton2, INPUT);
  pinMode(pinButton3, INPUT);pinMode(pinButton4, INPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print(F("server is at "));
  Serial.println(Ethernet.localIP());
}
void loop() {
  //toggles
  //stateButton = digitalRead(pinButton);  
  if(pinButton1 == HIGH && millis() - time > debounce) {
    if(digitalRead(led == HIGH)) {
      // stateRelay = LOW;
      digitalWrite(led, LOW);
      } else {
      //stateRelay = HIGH;
      digitalWrite(led, HIGH);
    }
    time = millis();
  }
  
  if(pinButton2 == HIGH && millis() - time > debounce) {
    if(digitalRead(led == HIGH)) {
      // stateRelay = LOW;
      digitalWrite(led2, LOW);
      } else {
      //stateRelay = HIGH;
      digitalWrite(led2, HIGH);
    }
    time = millis();
  }
  
  if(pinButton3 == HIGH && millis() - time > debounce) {
    if(digitalRead(led3 == HIGH)) {
      // stateRelay = LOW;
      digitalWrite(led3, LOW);
      } else {
      //stateRelay = HIGH;
      digitalWrite(led3, HIGH);
    }
    time = millis();
  }
  
  if(pinButton4 == HIGH && millis() - time > debounce) {
    if(digitalRead(led4 == HIGH)) {
      // stateRelay = LOW;
      digitalWrite(led4, LOW);
      } else {
      //stateRelay = HIGH;
      digitalWrite(led4, HIGH);
    }
    time = millis();
  }            
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {  
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          Serial.print(c);
        }
        //if HTTP request has ended
        if (c == '\n') {          
          Serial.println(readString); //print to serial monitor for debuging
          client.println(F("HTTP/1.1 200 OK")); //send new page
          client.println(F("Content-Type: text/html"));
          client.println();    
          client.println(F("<HTML>"));
          client.println(F("<HEAD>"));
          client.println(F("</HEAD>"));
          client.println(F("<BODY>"));
          client.println(F("<H2>Master Suite</H2>"));
          client.println(F("
"));  
          client.println(F("<a href=\"/?button1on\"\">Turn On LED</a>"));
          client.println(F("<a href=\"/?button1off\"\">Turn Off LED</a>
"));  
          client.print(digitalRead(led));
          client.println(F("
"));    
          client.println(F("<a href=\"/?button2on\"\">Turn On LED</a>"));
          client.println(F("<a href=\"/?button2off\"\">Turn Off LED</a>
"));  
          client.print(digitalRead(led2));
          client.println(F("
"));
          client.println(F("<a href=\"/0?button3on\"\">Turn On LED</a>"));
          client.println(F("<a href=\"/?button3off\"\">Turn Off LED</a>
"));  
          client.print(digitalRead(led3));
          client.println(F("
"));
          client.println(F("<a href=\"/?button4on\"\">Turn On LED</a>"));
          client.println(F("<a href=\"/?button4off\"\">Turn Off LED</a>
"));  
          client.print(digitalRead(led4));
          client.println(F("
"));    
          client.println(F("<a href=\"/?button5on\"\">Turn On LED</a>"));
          client.println(F("<a href=\"/?button5off\"\">Turn Off LED</a>
"));  
          client.print(digitalRead(led5));
          client.println(F("
"));                
          client.println(F("</BODY>"));
          client.println(F("</HTML>"));
          delay(1);
          //stopping client
          client.stop();
          //controls the Arduino if you press the buttons
          if (readString.indexOf("?button1on") >0){
            digitalWrite(led, HIGH);
            if (client.connect(serv, 80)) {
              Serial.println(F("connected"));
              // Make your API request:
              client.println(F("GET /sendmsg?user=test&password=********&api_id=3539384&to=********&text=hello HTTP/1.1"));
              client.println(F("Host: api.clickatell.com/http/sendmsg"));
              client.println(F("Connection: close"));
              client.println();
            }
            else {
              // kf you didn't get a connection to the server:
              Serial.println(F("connection failed"));
            }
          }
          if (readString.indexOf("?button1off") >0){
            digitalWrite(led, LOW);
          }
          if (readString.indexOf("?button2on") >0){
            digitalWrite(led, HIGH);
          }
          if (readString.indexOf("?button2off") >0){
            digitalWrite(led, LOW);
          }
          if (readString.indexOf("?button3on") >0){
            digitalWrite(led, HIGH);
          }
          if (readString.indexOf("?button3off") >0){
            digitalWrite(led, LOW);
          }
          if (readString.indexOf("?button4on") >0){
            digitalWrite(led, HIGH);
          }
          if (readString.indexOf("?button4off") >0){
            digitalWrite(led, LOW);
          }
          if (readString.indexOf("?button5on") >0){
            digitalWrite(led, HIGH);
          }
          if (readString.indexOf("?button5off") >0){
            digitalWrite(led, LOW);
          }
          //clearing string for next read
          readString="";  
          
        }
      }
    }
  }
}

And try to find a way to replace String object with char as String will fragment your memory and could lead to instability/crashing issues after the sketch has been running for a while.

ok I made the suggested changes but as soon as I start adding in the code for the 1-wire temp sensors I go over the amount of sram any ideas

here is the current code
Thanks,
Yonu

New Text Document.txt (12.7 KB)

Is it for an Arduino Uno or Nano or Leonardo ? You better continue with an Arduino Mega 2560 or Arduino Due.

If you want to continue with the Arduino Uno, you need to get rid of the String object as Riva wrote. You also need to replace the same code with a single function, and you even might need to remove some parts from the sketch.

With the 'same code', I mean code blocks that do almost the same. In your sketch that is the code for the buttons. It is 10 times the same functionality.

Some Arduino users on this forum say that you should not use String objects in an Arduino Uno at all. Not even when there is enough memory.

Did you accidently drop a '0' here ?

    \"/0?button3on\"\"

As a very last option, it is possible to enable the compiler -flto option, by changing or adding some files (but I forgot which files that are). That might save a few extra bytes. However, if you keep the String objects, the -flto option will not be enough.

I am using a nano as size is a factor.

OK on the functions is there any chance of a example sorry I am still learning. And I am working on getting rid of the strings.

Thanks, Yonu

Well I got the functions wrote to replace ALL THE BUTTON PRINTS Ands it dropped it the 65% in use so hopefully that frees up enough space

well I got everything in at 94%PROGMEM and 82%SRAM

Should I expect problems in the long term at that close to full?

Thanks, Yonu

The flash/code/PROGMEM is no problem. But 82% sram, and also using String objects can be a problem. That is indeed something that could make it unreliable when it is running for some time.

String objects are dynamically allocated and released every time something changes. The ram could get fragmented : https://learn.adafruit.com/memories-of-an-arduino/solving-memory-problems

With ARM Cortex processors, like the M0+ (Arduino Zero) or the M3 (Arduino Due), it is not a problem. They have 32kbyte and 96kbyte sram. That is why some users say that you should not use String objects at all with an Arduino Uno or Nano.

ok guess I will try to remove the strings.

thanks, yonu

ok I need some basic input here I decided to got have a look at the older ETHER_28J60 libs to see if I can get the code a bit smaller. However I can't get it to compile the new sketch on anything above IDE 1.0 is this normal or am I doing something wrong?

Thanks, Yonu

That is not the way to go. You better use EtherCard and UIPEthernet, because they work. Using a Arduino Mega 2560 board is the best.

Did you replace the String objects with old string functions ? Can you show your sketch ? With the String objects, you compare with a string in sram:

  if (readString.indexOf("?button4off") >0){

With the old string functions, even the string to compare with can be in flash. For example with strstr_P(). The ..._P functions are an extra for the Arduino, they refer to strings in flash.

You mention ETHER_28J60. If you use something, please give a link to it.

OK I will give that a shot with the string function. And here is the library I am referring to. I am sure that a mega would work better but I barely have room in the enclosure for the nano and enc28j60.

http://www.tweaking4all.com/hardware/arduino/arduino-enc28j60-ethernet/

Yonu

ok I am messing with the strstr_P() but I will be honest and say I am confused on how to use it

Thanks, Yonu

well I found out I could disable UDP in UIPEthernet that got me down to 75% SRAM but I still don'tr understand the strstr_P() stuff.

Thanks, Yonu

The old string functions are : strlen(), strstr(), strchr(), and so on. For some of those, there are functions with the ..._P to allow a string in flash. First use the normal string functions, and afterwards try to change a few into the ..._P version.

The old string functions: A common reference: http://www.cplusplus.com/reference/cstring/ The specific reference of the compiler : http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

The extra ...P versions : http://www.nongnu.org/avr-libc/user-manual/groupavr_pgmspace.html (scroll one or two pages down)

I have seen that page about the ETHER_28J60, and it's not bad at all, I like it. Did you download the newest version from Github ? That should compile. However, I prefer the more used UIPEthernet.

i haven't tried the github version but I need a web client and best I can tell it doesn't have a clients so I am going to have to make the UIPEthernet lib work on this project. I will look at the string pages you linked.

Thanks, Yonu