// EtherShield webserver to control HomeEasy devices // Use url http://10.0.0.29/on=x // http://10.0.0.29/off=x // where x= the channel, 0-15 which equate to buttons 1-4 on pos I // 5-8 on pos II etc // For group actions use /gon and /goff // // By Andrew Lindsay December 2009 // http://blog.thiseldo.co.uk #include #include // Set the TX and RX pins you are using #define TXPIN 3 #define RXPIN 4 //This can be the same as an existing remote control, makes setting up the HE302S sockets easier // use the receiver test program from the playground to determine yours. #define REMOTE_ADDRESS 1010123 HomeEasyAdv homeeasy = HomeEasyAdv(RXPIN, TXPIN, REMOTE_ADDRESS ); // 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,0x29}; static uint8_t myip[4] = { 10,0,0,29}; #define MYWWWPORT 80 #define BUFFER_SIZE 750 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 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("Arduino HomeEasy Control")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("

Arduino HomeEasy Control

")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("1 ON  OFF
")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("2 ON  OFF
")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("3 ON  OFF
")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("4 ON  OFF
")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("G ON  OFF
")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); return(plen); } // The returned value is stored in the global var strbuf 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!='&' && i200 OK")); 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; } else if (strncmp("/on=",(char *)&(buf[dat_p+4]),4)==0) { if (find_key_val((char *)&(buf[dat_p+4]),"on")){ int channel = atoi( strbuf ); homeeasy.on( (byte)(channel & 0x000f) ); } dat_p=print_webpage(buf); goto SENDTCP; } else if (strncmp("/off=",(char *)&(buf[dat_p+4]),5)==0) { if (find_key_val((char *)&(buf[dat_p+4]),"off")){ int channel = atoi( strbuf ); homeeasy.off( (byte)(channel & 0x000f) ); } dat_p=print_webpage(buf); goto SENDTCP; } else if (strncmp("/gon ",(char *)&(buf[dat_p+4]),5)==0) { homeeasy.groupOn( ); dat_p=print_webpage(buf); goto SENDTCP; } else if (strncmp("/goff ",(char *)&(buf[dat_p+4]),6)==0) { homeeasy.groupOff( ); 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 end } }