ENC28J60

Best Library for this shield ? Is there a not stateless library for TCP/IP like other ethernet shield ?

Best Library for this shield ?

EtherCard

Is there a not stateless library for TCP/IP like other ethernet shield ?

No. TCP/IP on the Wiznet chips is done in hardware.

I try to find it...

I have a small problem: i need code for open a socket and send analog input status.
With classical web socket (ethershieldlib) i send syn+ack but terminal tcp ip do not receive the flush of buffer until them put data (keep alive packet).
Is there a solution for a simple socket response ?
I repeat what happned now:

1- telnet 192.168.0.47 80
2- terminal connect to arduino
3- i press any key on terminal [THE PROBLEM I DO NOT NEED THIS]
4- terminal receive VOLTAGE and TEMPERATURE from arduino (STRING: “V=0.14;T=14.64;OK”) then logout

I need step 1-2-4 without 3 !!!

Another question: this code can work in a GW routed LAN ? I think yes beacuse response is based on MAC and IP of incoming packet, then MAC is a GW mac and IP is the correct remote IP, isn’t it ?

THX Silvano


My code:

#include “etherShield.h”
#include <stdlib.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,0,47};
static char baseurl=“http://192.168.0.47/”;
static uint16_t mywwwport_L =80; // listen port for tcp/www LO byte (max range 1-254)
static uint16_t mywwwport_H =0; // listen port for tcp/www HI byte (max range 1-254)
int sensorPin = 0;

// or on a different port:
//static char baseurl=“http://10.0.0.24:88/”;
//static uint16_t mywwwport =88; // listen port for tcp/www (max range 1-254)
//

#define BUFFER_SIZE 500
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 22
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);
int8_t analyse_cmd(char *str);
void setup(){
// Serial.begin(9600);
/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,mywwwport_L);

}

void loop(){
uint16_t plen, dat_p;
int8_t cmd;

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;
}
// Serial.println(“Avvio PACCHETTO PER ME”);
// Serial.println((int)buf[TCP_DST_PORT_H_P]);
// Serial.println((int)buf[TCP_DST_PORT_L_P]);
// tcp port www start, compare only the lower byte
if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==mywwwport_H&&buf[TCP_DST_PORT_L_P]==mywwwport_L){
// Serial.println(“Avvio Porta OK”);
if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
// Serial.println(“Avvio Porta SYN”);
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){
// Serial.println(“Avvio Porta ACK_V”);
es.ES_init_len_info(buf); // init some data structures
if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
// Serial.println(“Avvio Porta FIN_V”);
es.ES_make_tcp_ack_from_any(buf);
return;
}
// Serial.println(“Invio Risposta”);
SENDTCP:
plen=print_webpage(buf);
es.ES_make_tcp_ack_from_any(buf);
es.ES_make_tcp_ack_with_data(buf,plen); // send DATA
// Serial.println(“Risposta-Inviata”);
}
}
}
}
// The returned value is stored in the global var strbuf

uint16_t print_webpage(uint8_t *buf)
{
uint16_t plen;

//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage ) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
char v[50];
char t[50];

int decimale;

decimale=(int)((voltage-(int)voltage)*100);
sprintf(v,“V=%0d.%d”,(int)voltage,decimale);

decimale=(int)((temperatureC-(int)temperatureC)*100);
sprintf(t,";T=%0d.%d;OK\n",(int)temperatureC,decimale);

// Serial.println(voltage);
// Serial.println(temperatureC);

//plen=es.ES_fill_tcp_data_p(buf,0,PSTR(""));
int i =0;
while (v*) {*

  • buf[TCP_CHECKSUM_L_P+3+plen]=v[i++];*
  • plen++;*
  • }*
    i=0;
    while (t*) {*
    * buf[TCP_CHECKSUM_L_P+3+plen]=t[i++];
    _
    plen++;_
    _
    }_
    return(plen);
    _
    }*_

The EtherShield and EtherCard libraries have one disadvantage that they are single packet libraries. Connection is dropped after a packet is sent.

There is a new library for the ENC28J60, the uIP port for NaNodes. Check it out here https://github.com/sde1000/NanodeUIP

I haven't tested it myself, but the examples seem quite much like what you need. Give it a test.