need help with enc28j60

Hi, first of all sry for my bad english, i use an ENC28J60 and i want to control 3 led through the WEB. i have the sketch but something strange is happening, take a look on ;

#include "etherShield.h"


// please modify the following two lines. mac and ip have to be unique
// in your local area network. You can not have the same numbers in
// two devices:
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; 
static uint8_t myip[4] = {192,168,1,15};
static char baseurl[]="http://192.168.1.15/";
static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)



#define BUFFER_SIZE 1500
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 200
static char strbuf[STR_BUFFER_SIZE+1];

EtherShield es=EtherShield();

// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf, byte on_off1, byte on_off2, byte on_off3);
int8_t analyse_cmd(char *str);

// LED cathode connects the Pin4, anode to 5V through 1K resistor
#define LED_PIN4  4
#define LED_PIN5  5
#define LED_PIN6  6

void setup(){
  
   /*initialize enc28j60*/
	es.ES_enc28j60Init(mymac);
   es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
   delay(10);
        
	/* Magjack leds configuration, see enc28j60 datasheet, page 11 */
	// LEDA=greed LEDB=yellow
	//
	// 0x880 is PHLCON LEDB=on, LEDA=on
	// enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
	es.ES_enc28j60PhyWrite(PHLCON,0x880);
	delay(500);
	//
	// 0x990 is PHLCON LEDB=off, LEDA=off
	// enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
	es.ES_enc28j60PhyWrite(PHLCON,0x990);
	delay(500);
	//
	// 0x880 is PHLCON LEDB=on, LEDA=on
	// enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
	es.ES_enc28j60PhyWrite(PHLCON,0x880);
	delay(500);
	//
	// 0x990 is PHLCON LEDB=off, LEDA=off
	// enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
	es.ES_enc28j60PhyWrite(PHLCON,0x990);
	delay(500);
	//
  // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
  es.ES_enc28j60PhyWrite(PHLCON,0x476);
	delay(100);
        
  //init the ethernet/ip layer:
  es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
  
 	pinMode(LED_PIN4, OUTPUT); 
 	digitalWrite(LED_PIN4, HIGH);  // switch off LED
        pinMode(LED_PIN5, OUTPUT); 
 	digitalWrite(LED_PIN5, HIGH);  // switch off LED
        pinMode(LED_PIN6, OUTPUT); 
 	digitalWrite(LED_PIN6, HIGH);  // switch off LED
}

void loop(){
  uint16_t plen, dat_p;
  int8_t cmd;
  byte on_off1 = 1;
  byte on_off2 = 1;
  byte on_off3 = 1;

  plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);

	/*plen will ne unequal to zero if there is a valid packet (without crc error) */
  if(plen!=0){
	           
    // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address.
    if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){
      es.ES_make_arp_answer_from_request(buf);
      return;
    }

    // check if ip packets are for us:
    if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){
      return;
    }
    
    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
      es.ES_make_echo_reply_from_request(buf,plen);
      return;
    }
    
    // tcp port www start, compare only the lower byte
    if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==mywwwport){
      if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
         es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack
         return;     
      }
      if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
        es.ES_init_len_info(buf); // init some data structures
        dat_p=es.ES_get_tcp_data_pointer();
        if (dat_p==0){ // we can possibly have no data, just ack:
          if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
            es.ES_make_tcp_ack_from_any(buf);
          }
          return;
        }
        if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){

          	// head, post and other methods for possible status codes see:
            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
            plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
            goto SENDTCP;
        }

hx all for support :wink:

NEXT:

if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
                plen=print_webpage(buf, on_off1, on_off2, on_off3);
            goto SENDTCP;
         }
        cmd=analyse_cmd((char *)&(buf[dat_p+5]));
        
        if (cmd==2){
                on_off1=1;
        	digitalWrite(LED_PIN4, LOW);  // switch on LED
        }
        else if (cmd==3){
                on_off1=0;
        	digitalWrite(LED_PIN4, HIGH);  // switch off LED
        }
        else if (cmd==4){
                on_off2=1;
        	digitalWrite(LED_PIN5, LOW);  // switch on LED
        }
        else if (cmd==5){
                on_off2=0;
        	digitalWrite(LED_PIN5, HIGH);  // switch off LED
        }
        
        else if (cmd==6){
                on_off3=1;
        	digitalWrite(LED_PIN6, LOW);  // switch on LED
        }
        else if (cmd==7){
                on_off3=0;
        	digitalWrite(LED_PIN6, HIGH);  // switch off LED
        }

        plen=print_webpage(buf, on_off1, on_off2, on_off3);
        	
        	   plen=print_webpage(buf, on_off1, on_off2, on_off3);
SENDTCP:  es.ES_make_tcp_ack_from_any(buf); // send ack for http get
           es.ES_make_tcp_ack_with_data(buf,plen); // send data       
      }
    }
  }
        
}
// The returned value is stored in the global var strbuf
uint8_t find_key_val(char *str,char *key)
{
        uint8_t found=0;
        uint8_t i=0;
        char *kp;
        kp=key;
        while(*str &&  *str!=' ' && found==0){
                if (*str == *kp){
                        kp++;
                        if (*kp == '\0'){
                                str++;
                                kp=key;
                                if (*str == '='){
                                        found=1;
                                }
                        }
                }else{
                        kp=key;
                }
                str++;
        }
        if (found==1){
                // copy the value to a buffer and terminate it with '\0'
                while(*str &&  *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){
                        strbuf[i]=*str;
                        i++;
                        str++;
                }
                strbuf[i]='\0';
        }
        return(found);
}

int8_t analyse_cmd(char *str)
{
        int8_t r=-1;
     
        if (find_key_val(str,"cmd")){
                if (*strbuf < 0x3a && *strbuf > 0x2f){
                        // is a ASCII number, return it
                        r=(*strbuf-0x30);
                }
        }
        return r;
}


uint16_t print_webpage(uint8_t *buf, byte on_off1, byte on_off2, byte  on_off3)
{

       int i=0;
    
        
        uint16_t plen;
        
 
        
        plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
    plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<left><img src=\"http://i915.photobucket.com/albums/ac357/mistermpss/beaucetelecomlogo.png\"  ></LEFT> "));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<body bgcolor=\"#000000\"> "));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<font color=\"#FFFFFF\"><FONT FACE=\"arial\"><RIGHT><I><FONT SIZE=1>BTCAD.<U> V1.0 beta</U></h1></FONT></FONT></I> </RIGHT> "));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<FONT SIZE=3> <font color=\"#00FF00\"></h1><a href=\"http://192.168.1.1\">Cliquez ici pour acc&egrave;der &agrave; vos cam&eacute;ras<a></FONT></FONT>"));
    

        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<hr>
<form METHOD=get action=\""));
        plen=es.ES_fill_tcp_data(buf,plen,baseurl);
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h2><font color=\"#FFFFFF\"> S&eacute;lection  </h2> "));
 				plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h1><font color=\"#FFFFFF\"> "));
 
         
        
        if(on_off1){
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=3>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Moteur #1 ON \"></form>"));
          }
         
        else {
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=2>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Moteur #1 OFF\"></form>"));
     }


          
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
<form METHOD=get action=\""));
        plen=es.ES_fill_tcp_data(buf,plen,baseurl);
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
 
        
        

        if(on_off2){
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=5>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Moteur #2 ON\"></form>"));
        }
           
        else {
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=4>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Moteur #2 OFF\"></form>"));
         
         
             }
           
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
<form METHOD=get action=\""));
        plen=es.ES_fill_tcp_data(buf,plen,baseurl);
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
    
       
        
        
        if(on_off3){
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=7>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Moteur #3 ON\"></form>"));
        }
        else  {
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=6>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Moteur #3 OFF \"></form>"));
        }
        
        
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</CENTER><hr><hr><FONT FACE=\"arial\"><FONT SIZE=1>Une connexion internet est requise pour acc&egrave;der &agrave; ce lien:</font><FONT SIZE=5> <p> Assistance en ligne <a href=\"http://www.google.com\">www.beaucetelecom.ca<a></font></FONT>"));
  
        return(plen);
}

Could you elaborate a little on what's going wrong? There's a lot of code to wade through in those two posts, tricky without some idea what we're looking for.

That code, looking at it briefly, looks generally okay... and very similar to the code I put in my (working!) Arduserver.

However: I found that I could add very, very little to the code before weird things happened because the memory or a buffer in the ethernet chip, or SOMETHING was becoming too full.

Cut out anything you can, try code again, add back things a very little at a time.

Then again... I was using an old Arduino clone with very little memory.... or maybe it was a too-small buffer in the ethernet interface?

Hi i convert code from nuelectronics for Arduino-ethernet enc28j60 for 4 led control....and work fine....i want put and analog input how i convert code?Thanks.

#include "etherShield.h"

static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; 
static uint8_t myip[4] = {192,168,1,15};
static char baseurl[]="/";// ADD http://192.168.1.15
static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)



#define BUFFER_SIZE 1500
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 200
static char strbuf[STR_BUFFER_SIZE+1];

EtherShield es=EtherShield();

// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf, byte on_off1, byte on_off2, byte on_off3,byte on_off4);
int8_t analyse_cmd(char *str);
//int8_t sensorPin = A3;  // designate the input pin for a sensor... a lm35 used in example
//int sensorValue=0; // variable to store the value coming from the sensor

// LED cathode connects the Pin4, anode to 5V through 1K resistor
#define LED_PIN4  4
#define LED_PIN5  5
#define LED_PIN6  6
#define LED_PIN7  7

//int temp1_pin = A0;
//int temp=0;

void setup(){
  
   /*initialize enc28j60*/
	
	
  //init the ethernet/ip layer:
  es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
  
 	pinMode(LED_PIN4, OUTPUT); 
 	digitalWrite(LED_PIN4, LOW);  // switch off LED
        pinMode(LED_PIN5, OUTPUT); 
 	digitalWrite(LED_PIN5, LOW);  // switch off LED
        pinMode(LED_PIN6, OUTPUT); 
 	digitalWrite(LED_PIN6, LOW);  // switch off LED
        pinMode(LED_PIN7, OUTPUT); 
 	digitalWrite(LED_PIN7, LOW);  // switch off LED
}

void loop(){
  uint16_t plen, dat_p;
  int8_t cmd;
  byte on_off1 = 1;
  byte on_off2 = 1;
  byte on_off3 = 1;
  byte on_off4 = 1;
  
   on_off1 = digitalRead(LED_PIN4);
   on_off2 = digitalRead(LED_PIN5);
   on_off3 = digitalRead(LED_PIN6);
   on_off4 = digitalRead(LED_PIN7);
   
   //temp= analogRead(temp1_pin);
  //  sensorValue=analogRead(sensorPin);
     //   float voltage=sensorValue*3.3;
      //        voltage/=1024.0;
      //  float tempC =(voltage-0.5)*100; 

  plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);

	/*plen will ne unequal to zero if there is a valid packet (without crc error) */
  if(plen!=0){
	           
    // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address.
    if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){
      es.ES_make_arp_answer_from_request(buf);
      return;
    }

    // check if ip packets are for us:
   
                plen=print_webpage(buf, on_off1, on_off2, on_off3, on_off4);
            goto SENDTCP;
         }
        cmd=analyse_cmd((char *)&(buf[dat_p+5]));
        
        if (cmd==2){
                on_off1=1;
        	digitalWrite(LED_PIN4, HIGH);  // switch on LED
        }
        else if (cmd==3){
                on_off1=0;
        	digitalWrite(LED_PIN4, LOW);  // switch off LED
        }
        else if (cmd==4){
                on_off2=1;
        	digitalWrite(LED_PIN5, HIGH);  // switch on LED
        }
        else if (cmd==5){
                on_off2=0;
        	digitalWrite(LED_PIN5, LOW);  // switch off LED
        }
        
        else if (cmd==6){
                on_off3=1;
        	digitalWrite(LED_PIN6, HIGH);  // switch on LED
        }
        else if (cmd==7){
                on_off3=0;
        	digitalWrite(LED_PIN6, LOW);  // switch off LED
        }
         else if (cmd==8){
                on_off4=1;
        	digitalWrite(LED_PIN7, HIGH);  // switch on LED
        }
        else if (cmd==9){
                on_off4=0;
        	digitalWrite(LED_PIN7, LOW);  // switch off LED
        }
        plen=print_webpage(buf, on_off1, on_off2, on_off3,on_off4);
        	
        	   plen=print_webpage(buf, on_off1, on_off2, on_off3,on_off4);
SENDTCP:  es.ES_make_tcp_ack_from_any(buf); // send ack for http get
           es.ES_make_tcp_ack_with_data(buf,plen); // send data       
      }
    }
  }
        
}
// The returned value is stored in the global var strbuf
uint8_t find_key_val(char *str,char *key)
{
        uint8_t found=0;
        uint8_t i=0;
        
        return(found);
}

int8_t analyse_cmd(char *str)
{
        int8_t r=-1;
     
        if (find_key_val(str,"cmd")){
                if (*strbuf < 0x3a && *strbuf > 0x2f){
                        // is a ASCII number, return it
                        r=(*strbuf-0x30);
                }
        }
        return r;
}


uint16_t print_webpage(uint8_t *buf, byte on_off1, byte on_off2, byte  on_off3,byte on_off4)
{

       int i=0;
    
        
        uint16_t plen;
        
       // sensorValue=analogRead(sensorPin);
        //float voltage=sensorValue*3.3;
          //    voltage/=1024.0;
       // float tempC =(voltage-0.5)*100;      
        
 
        
        plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
   // plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<left><img src=\"http://i915.photobucket.com/albums/ac357/mistermpss/beaucetelecomlogo.png\"  ></LEFT> "));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<body bgcolor=\"#EEEEEE\"> "));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<font color=\"#000000\"><FONT FACE=\"arial\"><RIGHT><I><FONT SIZE=2>REMOTE CONTROL PANEL.<U> V1.0 A.K 25.11.2011 </U></h1></FONT></FONT></I> </RIGHT> "));
   // plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<FONT SIZE=3> <font color=\"#00FF00\"></h1><a href=\"http://192.168.1.1\">Cliquez ici pour acc&egrave;der &agrave; vos cam&eacute;ras<a></FONT></FONT>"));
    
        //plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(temp)
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<hr>
<form METHOD=get action=\""));
        plen=es.ES_fill_tcp_data(buf,plen,baseurl);
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h2><font color=\"#000000\"> SELECT DEVICE </h2> "));
 				plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h1><font color=\"#FFFFFF\"> "));
 
         
        
        if(on_off1){
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=3>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Device #1 ON \"></form>"));
          }
         
        else {
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=2>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Device #1 OFF\"></form>"));
     }


          
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
<form METHOD=get action=\""));
        plen=es.ES_fill_tcp_data(buf,plen,baseurl);
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
 
        
        

        if(on_off2){
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=5>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Device #2 ON\"></form>"));
        }
           
        else {
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=4>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Device #2 OFF\"></form>"));
         
         
             }
           
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
<form METHOD=get action=\""));
        plen=es.ES_fill_tcp_data(buf,plen,baseurl);
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
    
       
        
        
        if(on_off3){
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=7>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Device #3 ON\"></form>"));
        }
        else  {
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=6>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Device #3 OFF \"></form>"));
        }
        
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
<form METHOD=get action=\""));
        plen=es.ES_fill_tcp_data(buf,plen,baseurl);
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
    
       
        
        
        if(on_off4){
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=9>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Device #4 ON\"></form>"));
        }
        else  {
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=8>"));
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Device #4 OFF \"></form>"));
        }
        
  
        return(plen);
}

I would do that thru the REST protocol, much more elegant.

Take a look here: http://blog.thiseldo.co.uk/?p=599

I made a slightly simplified library for this device, you may like to look at this:

My ethernet card enc28j60 no work after 30 or 60 minutes...need restart...no display on web....How i solve this problem?I use external power supply 9volt.Thanks.

antkan:
My ethernet card enc28j60 no work after 30 or 60 minutes...need restart...no display on web....How i solve this problem?I use external power supply 9volt.Thanks.

sounds like the chip is over heating

I use external power supply 9volt.

Is that a 9v battery or a 9v wall power supply?

lorenzop:

antkan:
My ethernet card enc28j60 no work after 30 or 60 minutes...need restart...no display on web....How i solve this problem?I use external power supply 9volt.Thanks.

sounds like the chip is over heating

Possibly the regulator chip, but that's easy to test - it will be burning hot.

Another possibility is running out of RAM.