Hi,
I'll share this to others who just start playing with ENC28J60 ethernetshield.
Reasons :
- I received so many help/enlighment from others
- This Shield is not Official , so there is no "standardize" Library for it.
Credits goes to :
- Andy , http://blog.thiseldo.co.uk/?p=344 , For his ENC28J60 Arduino Library.
- Trystan , emoncms | Archived Forum.
- PaulS, Arduino Forum , Tonns of enlighment.
- Gus , Structured EEPROM access with Arduino/AVRs – Project Gus
Ok , Here we go.
Important note : I made a small modification of Andy's library. It's a good library, but do not "end user" to make change of HOSTNAME (need to be hardcoded at sketch level). I don't want this, so I made small modification.
Here it is :
Modification to --> ip_arp_udp_tcp.c
1. Look for --> #if defined (WWW_client) , around line# 1310
2. Look for --> //GET , around line# 1316
3. Commen out all :
len=fill_tcp_data_p(bufptr,0,PSTR("GET "));
len=fill_tcp_data_p(bufptr,len,client_urlbuf);
if( client_urlbuf_var )
len=fill_tcp_data(bufptr,len,client_urlbuf_var);
// I would prefer http/1.0 but there is a funny
// bug in some apache webservers which causes
// them to send two packets (fragmented PDU)
// if we don't use HTTP/1.1 + Connection: close
len=fill_tcp_data_p(bufptr,len,PSTR(" HTTP/1.1\r\nHost: "));
len=fill_tcp_data_p(bufptr,len,client_hoststr);
len=fill_tcp_data_p(bufptr,len,PSTR("\r\nUser-Agent: EtherShield/1.6\r\nAccept: text/html\r\nConnection: close\r\n\r\n"));
4. Replace with :
len=fill_tcp_data(bufptr,0,client_urlbuf_var);
Arduino sketch.
A. Main Sketch
/*
WE USE MODIFIED VERSION OF Andy's Library ver 1.6
*/
#include <EtherShield.h>
//#include <EEPROM.h>
#include<string.h>
//Structured EEPROM--START
#include <avr/eeprom.h>
#define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block(dst_p, (void *)offsetof(__eeprom_data, eeprom_field), MIN(dst_size, sizeof((__eeprom_data*)0)->eeprom_field))
#define eeprom_read(dst, eeprom_field) eeprom_read_to(&dst, eeprom_field, sizeof(dst))
#define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block(src_p, (void *)offsetof(__eeprom_data, eeprom_field), MIN(src_size, sizeof((__eeprom_data*)0)->eeprom_field))
#define eeprom_write(src, eeprom_field) { typeof(src) x = src; eeprom_write_from(&x, eeprom_field, sizeof(x)); }
#define MIN(x,y) ( x > y ? y : x )
//const int buflen = 32;
/*
* __eeprom_data is the magic name that maps all of the data we are
* storing in our EEPROM
*/
struct __eeprom_data {
byte magic;
byte mac[6];
byte ip[4];
byte gw[4];
byte srv[4];
char host[40];
char callfile[20];
byte conflen[10];
char nodeid[10];
char nodepin[10];
};
//Structured EEPROM--STOP
//----ETH VARS-------------------------------
#define PORT 80 // Ndak bisa di rubah
byte magic;
byte mac[6] = {0x54,0x55,0x58,0x10,0x00,0x25};
byte ip[4] = {192,168,10,231};
byte gw[4] = {192,168,10,1};
byte srv[4] = { 192,168,10,232 }; // Get pachube ip by DNS call
char host[40]="telemetry.bino.int";
char callfile[20]="/post.php?" ; // Set your own feed ID here
byte conflen[10]={0,0,0,0,0,0,0,0,0,0};
char nodeid[10]="mynodeid";
char nodepin[10]="mynodepin";
byte mymagic=0x0a;
uint8_t resend=0;
int8_t dns_state=0;
uint32_t timetosend=millis();
uint16_t dat_p;
long lastDnsRequest = 0L;
int plen = 0;
EtherShield es=EtherShield();
#define BUFFER_SIZE 550
uint8_t buf[BUFFER_SIZE+1];
//-------------VARS to SEND------------------------------
int sendvarlen = 6;
char sendvarname[6][5]={"s0","s1","s2","s3","s4","s5"};
char sendvarval[6][10]={"0","0","0","0","0","0"};
//-------------------------------------------------
//----FOR Menuing----
boolean inmenu = false;
boolean waitentry=false;
void setup(){
Serial.begin(9600);
eeprom_read(magic, magic);
if (magic != mymagic)
{
delay(3000);
Serial.print("FIRST BLOOD !!!");
magic = mymagic;
saveall();
}
readall();
delay(100);
es.ES_enc28j60Init(mac);
//init the ethernet/ip layer:
es.ES_init_ip_arp_udp_tcp(mac, ip, PORT);
// init the web client:
es.ES_client_set_gwip(gw); // e.g internal IP of dsl router
es.ES_client_set_wwwip(srv);
}
void loop()
{
//Do this in any condition
dns_state=2;
// handle ping and wait for a tcp packet - calling this routine powers the sending and receiving of data
// If we have IP address for server and its time then request data
ethReady();
//---------------------------
if (!inmenu)
{
if (Serial.available())
{
if (Serial.read() == 'c')
{
printmenu();
}
}
else
{
domainproc();
}
}
else
{
if (Serial.available())
{
domenu();
}
}
}
void domainproc()
{
if(millis() - timetosend > 1000) // every 10 seconds
{
timetosend = millis();
readsensor();
ethSend();
}
}
void readsensor()
{
for (int i = 0 ; i < sendvarlen ; i++)
{
itoa(analogRead(i),sendvarval[i],10);
}
}
Oppss ... I need to post 2 other part seperately