Hello, I'm hoping I've posted this in the right section as I'm having problems getting my project working with the Ethershield, though I fully expect it to be my coding that's the problem.
My project is fairly simple in theory. I want to be able to use my Arduino 2009 as a web server and a controller. I want to be able to connect to my Arduino over the net and see a web page that tells me if my front gates are open or closed I then want to have a button on the web page that would then let me open or close the gates. I've had a few goes at getting this working and my present effort produces a webpage and has buttons on it and when I click the button it loads up a different web page. So in effect I have a home page an open page and a close page. The problem is the physical outputs of the Arduino don't come on when I press the web button even though it seems to load up the correct page. I've pasted my sketch below in the hope that someone will point out my silly mistakes and get me going again.
Thanks in anticipation
// EtherShield gate controller
#include "etherShield.h"
// Random mac address
// IP that shouldn't clash with anything else on the LAN
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
static uint8_t myip[4] = {192,168,1,12};
#define MYWWWPORT 80
#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];
int OpenPin = 3; // pin used to send the open signal to the gate motors
int ClosePin = 4;// pin used to send the closed signal to the gate motor
// 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")));
}
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
uint16_t plen;
plen=http200ok();
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("Gate Controller Thingy"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
Welcome to a version that doesn't work
"));plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
-- this should be online -- ")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(" Control the gate here")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(" some sort of button goes here for open close etc")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(""));
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);
pinMode (OpenPin, OUTPUT); //set open pin to ouptut
pinMode (ClosePin,OUTPUT);//set close pin to output
//on first run set both the open and close outputs to logic zero to prevent unknown states
digitalWrite(ClosePin,LOW);// set the close output to logic zero
digitalWrite(OpenPin,LOW);//set the open output to logic zero
}
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("200 OK
"));
goto SENDTCP;
}
// three web page in the "root directory" of the web server
if (strncmp("/ard.html ",(char *)&(buf[dat_p+4]),2)==0){
dat_p=print_webpage(buf);
goto SENDTCP;
}
else if (strncmp("/ard2.html ",(char *)&(buf[dat_p+4]),2)==0){
digitalWrite(ClosePin,LOW);// set close output to logic zero
digitalWrite(OpenPin,HIGH);//set open output to logic one
dat_p=print_webpage(buf);
goto SENDTCP;
}
else if (strncmp("/ard3.html ",(char *)&(buf[dat_p+4]),2)==0){
digitalWrite(ClosePin,HIGH);//set close output to logic one
digitalWrite(OpenPin,LOW);//set open output to logic zero
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
401 Unauthorized
"));goto SENDTCP;
}
SENDTCP:
es.ES_www_server_reply(buf,dat_p); // send web page data
// tcp port 80 end
}
}
The code on the below page uses a different setup, but it does turn a pin on/off and produce a web page. You might want to try it
It's not the same Ethernet Shield as I've got but maybe with my limited skills I could get that sketch to work with the EtherShield one.
Thanks
One obvious problem that pops out is that the counts in your strncmp() calls are wrong: you're only comparing the first two bytes in the strings.
It's probably too closely tied to the "official" Ethernet shield's library, but you might at least get some useful ideas from the Webduino library (do a forum search for it: it was announced here).
Ran
Give me your address and I'll post you a packet of peanuts.
You've found my problem (well one of them).
I've increased the characters being compared and yippee it now works, switching on the open and closed LEDs at will. This is what happens when I copy bits of code here and there not really knowing what I'm doing.
Thanks to all that have assisted.
![]()