Here is the preliminary code I have to monitor each analog/digital pin and output it via http. I set it up, and queried it once every .2 seconds for more than 200,000 times and never had an issue.
// EtherShield webserver demo
#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] = {0x00,0x55,0x58,0x10,0x00,0x24};
static uint8_t myip[4] = {192,168,5,99};
int usable_dpins[] = {3,4,5,6,7,8,9};
int usable_apins[] = {0,1,2,3,4,5};
#define MYWWWPORT 80
#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];
// The ethernet shield
EtherShield es=EtherShield();
uint16_t http200ok(void) {
return(es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")));
}
// Add the string str to the buffer one character at a time
int add_string(char* str, uint16_t &plen) {
int i = 0;
//Loop through each char
while (str[i]) {
// Add each char one by one to the buffer
buf[TCP_CHECKSUM_L_P + 3 + plen] = str[i];
i++;
plen++;
}
}
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf) {
uint16_t plen; // Packet Length
plen=http200ok();
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<html><head><title>Arduino</title></head><body>\n"));
add_string("<h1>Arduino Ethernet Shield</h1>\n", plen);
for (int i = 0; i < (sizeof(usable_dpins) / sizeof(usable_dpins[0])); i++) {
int mypin = usable_dpins[i];
char mystr[40];
pinMode(mypin, INPUT);
sprintf(mystr,"<div>Digital Pin #%d is %d</div>\n",mypin,digitalRead(mypin));
add_string(mystr, plen);
}
for (int i = 0; i < (sizeof(usable_apins) / sizeof(usable_apins[0])); i++) {
int mypin = usable_apins[i];
char mystr[40];
sprintf(mystr,"<div>Analog Pin #%d is %d</div>\n",mypin,analogRead(mypin));
add_string(mystr, plen);
}
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</body></html>"));
return(plen);
}
void setup() {
// initialize enc28j60
es.ES_enc28j60Init(mymac);
// init the ethernet/ip layer:
es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);
}
void loop() {
uint16_t plen, dat_p;
while(1) {
// read packet, handle ping and wait for a tcp packet:
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
/* dat_p will be unequal to zero if there is a valid
* http get */
if(dat_p==0){
// no http request
continue;
}
// tcp port 80 begin
if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
// head, post and other methods:
dat_p=http200ok();
dat_p=es.ES_fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 OK</h1>"));
goto SENDTCP;
}
// just one web page in the "root directory" of the web server
if (strncmp("/ard.html ",(char *)&(buf[dat_p+4]),9)==0){
dat_p=print_webpage(buf);
goto SENDTCP;
} else {
dat_p=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
goto SENDTCP;
}
SENDTCP:
es.ES_www_server_reply(buf,dat_p); // send web page data
// tcp port 80 end
}
}