Hi everyone.
I'm having a rather strange problem: using a Duemilanove Atmega328 with a ENC28J60 ethernetshield, I want to control some relays.
The relays by itself work fine:
void setup() {
pinMode(0, OUTPUT);
}
void loop() {
digitalWrite(0, LOW);
delay(2000);
digitalWrite(0, HIGH);
delay(2000);
}
However, when I put up my webserver, I seem to loose all control over the relays:
// 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] = {0x54,0x55,0x58,0x10,0x00,0x25};
static uint8_t myip[4] = {192,168,1,25};
#define MYWWWPORT 80
#define BUFFER_SIZE 550
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 22
static char strbuf[STR_BUFFER_SIZE+1];
// The ethernet shield
EtherShield es=EtherShield();
uint16_t print_webpage(uint8_t *buf, byte on_off);
int8_t analyse_cmd(char *str);
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("<html><head><title>Home control</title></head><body>"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("Bureau
"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<form METHOD=get action=\"\">"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=bureau value=1>"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"On\"></form>"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<form METHOD=get action=\"\">"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=bureau value=0>"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Off\"></form>"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</body></html>"));
return(plen);
}
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;
}
void setup(){
Serial.begin(9600);
pinMode(0, OUTPUT);
/* Disable SD card */
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
// Initialise SPI interface
es.ES_enc28j60SpiInit();
// initialize enc28j60
es.ES_enc28j60Init(mymac);
// init the ethernet/ip layer:
es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);
Serial.println("Setup ready.");
}
void loop(){
uint16_t plen, dat_p;
int8_t cmd;
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));
cmd=analyse_cmd((char *)&(buf[dat_p+5]));
/* 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("/ ",(char *)&(buf[dat_p+4]),2)==0){
dat_p=print_webpage(buf);
goto SENDTCP;
}
// Nothing happens here...
// Same without the "if"
// The code however is reachable
if (cmd==1){
digitalWrite(1, LOW);
}
else if (cmd==0){
digitalWrite(0, HIGH);
}
Serial.println("this code is reachable!!!");
SENDTCP:
es.ES_www_server_reply(buf,dat_p); // send web page data
// tcp port 80 end
}
}
Any ideas?
Thanks