Hello I am not good at coding but I can get by. What I want to do it to make a relay controller to control 8 relays with my enc28j60 board. I used a sketch from www.nuelectronics.com to control a led. I was trying to modify it to add 1 more led to see if I could do it. Here is my code:
#include “etherShield.h”
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
static uint8_t myip[4] = {192,168,2,215};
static char baseurl=“http://192.168.2.215/”;
static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)
#define BUFFER_SIZE 500
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 22
static char strbuf[STR_BUFFER_SIZE+1];EtherShield es=EtherShield();
uint16_t print_webpage(uint8_t *buf, byte on_off);
uint16_t print_webpage(uint8_t *buf, byte on_off1);
int8_t analyse_cmd(char *str);#define LED_PIN 4
#define LED_PIN1 5void setup(){
/initialize enc28j60/
es.ES_enc28j60Init(mymac);
es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
delay(10);es.ES_enc28j60PhyWrite(PHLCON,0x880);
delay(500);
es.ES_enc28j60PhyWrite(PHLCON,0x990);
delay(500);
es.ES_enc28j60PhyWrite(PHLCON,0x880);
delay(500);
es.ES_enc28j60PhyWrite(PHLCON,0x990);
delay(500);es.ES_enc28j60PhyWrite(PHLCON,0x476);
delay(100);//init the ethernet/ip layer:
es.ES_init_ip_arp_udp_tcp(mymac,myip,80);pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // switch on LEDpinMode(LED_PIN1, OUTPUT);
digitalWrite(LED_PIN1, LOW); // switch on LED
}
void loop(){
uint16_t plen, dat_p;
int8_t cmd;
byte on_off = 1;
byte on_off1 = 1;
plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);/*plen will ne unequal to zero if there is a valid packet (without crc error) */
if(plen!=0){// arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address.
if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){
es.ES_make_arp_answer_from_request(buf);
return;
}// check if ip packets are for us:
if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){
return;
}if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
es.ES_make_echo_reply_from_request(buf,plen);
return;
}// tcp port www start, compare only the lower byte
if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==mywwwport){
if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack
return;
}
if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
es.ES_init_len_info(buf); // init some data structures
dat_p=es.ES_get_tcp_data_pointer();
if (dat_p==0){ // we can possibly have no data, just ack:
if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
es.ES_make_tcp_ack_from_any(buf);
}
return;
}
if (strncmp("GET “,(char *)&(buf[dat_p]),4)!=0){
// head, post and other methods for possible status codes see:
// HTTP/1.1: Status Code Definitions
plen=es.ES_fill_tcp_data_p(buf,0,PSTR(“HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n200 OK
”));
goto SENDTCP;
}
if (strncmp(”/ ",(char *)&(buf[dat_p+4]),2)==0){
plen=print_webpage(buf, on_off);
plen=print_webpage(buf, on_off1);
goto SENDTCP;
}
cmd=analyse_cmd((char *)&(buf[dat_p+5]));if (cmd==2){
on_off=1;
digitalWrite(LED_PIN, LOW); // switch on LED
}
else if (cmd==3){
on_off=0;
digitalWrite(LED_PIN, HIGH); // switch off LED
}
plen=print_webpage(buf, on_off);if (cmd==4){
on_off1=1;
digitalWrite(LED_PIN1, LOW); // switch on LED
}
else if (cmd==5){
on_off1=0;
digitalWrite(LED_PIN1, HIGH); // switch off LED
}
plen=print_webpage(buf, on_off);
plen=print_webpage(buf, on_off);
plen=print_webpage(buf, on_off1);
plen=print_webpage(buf, on_off1);
SENDTCP: es.ES_make_tcp_ack_from_any(buf); // send ack for http get
es.ES_make_tcp_ack_with_data(buf,plen); // send data
}
}
}}
// 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!=’&’ && i<STR_BUFFER_SIZE){
strbuf_=*str;_
- i++;*
- str++;*
- }*
_ strbuf*=’\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;*
}
uint16_t print_webpage(uint8_t buf, byte on_off);
uint16_t print_webpage(uint8_t buf, byte on_off1);
_{_
* int i=0;** uint16_t plen;*
* plen=es.ES_fill_tcp_data_p(buf,0,PSTR(“HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n”));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("Welcome to Arduino Ethernet Shield V1.0
"));** plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
_<form METHOD=get action=""));_
plen=es.ES_fill_tcp_data(buf,plen,baseurl);
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("">"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("REMOTE LED is
“));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(”<font color="#00FF00"> "));*
* if(on_off)
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(“ON”));
_ else*_
* plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(“OFF”));** plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
_") );*_* if(on_off){
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(""));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=“Switch off”>"));
_ }_
_ else {_
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(""));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=“Switch on”>"));
_ }_
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("REMOTE LED is
“));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(”<font color="#00FF00"> "));*
* if(on_off1)
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(“ON”));
_ else*_
* plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(“OFF”));** plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
_") );_
if(on_off1){
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(""));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=“Switch off”>"));
_ }_
_ else {_
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(""));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=“Switch on”>"));
_ }_
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
V1.0 <a href=“http://www.nuelectronics.com”>www.nuelectronics.com"));*
* return(plen);*
}
[/quote]