#include "etherShield.h" /*infrared sensor setting*/ #define INFRARED_IN 3 #define LED_STATUS 5 #define ENABLE_EXTERNAL1_INTERRUPT() ( EIMSK |= ( 1<< INT1 ) ) #define DISABLE_EXTERNAL1_INTERRUPT() ( EIMSK &= ~( 1<< INT1 ) ) // please modify the following 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,0x33}; static uint8_t myip[4] = {192,168,1,89}; static uint16_t my_port = 1200; // client port // client_ip - modify it when you have multiple client on the network // for server to distinguish each ethershield client static char client_ip[] = "192.168.1.89"; // server settings - modify the service ip to your own server static uint8_t dest_ip[4]={192,168,1,4}; static uint8_t dest_mac[6]; enum CLIENT_STATE { IDLE, ARP_SENT, ARP_REPLY, SYNC_SENT }; static CLIENT_STATE client_state; static uint8_t client_data_ready; static uint8_t syn_ack_timeout = 0; #define BUFFER_SIZE 500 static uint8_t buf[BUFFER_SIZE+1]; EtherShield es=EtherShield(); void setup(){ /*initialize enc28j60*/ es.ES_enc28j60Init(mymac); es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz delay(10); /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ // LEDA=greed LEDB=yellow // // 0x880 is PHLCON LEDB=on, LEDA=on // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00); es.ES_enc28j60PhyWrite(PHLCON,0x880); delay(500); // // 0x990 is PHLCON LEDB=off, LEDA=off // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00); es.ES_enc28j60PhyWrite(PHLCON,0x990); delay(500); // // 0x880 is PHLCON LEDB=on, LEDA=on // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00); es.ES_enc28j60PhyWrite(PHLCON,0x880); delay(500); // // 0x990 is PHLCON LEDB=off, LEDA=off // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00); es.ES_enc28j60PhyWrite(PHLCON,0x990); delay(500); // // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10); es.ES_enc28j60PhyWrite(PHLCON,0x476); delay(100); //init the ethernet/ip layer: es.ES_init_ip_arp_udp_tcp(mymac,myip,80); // intialize varible; syn_ack_timeout =0; client_data_ready = 0; client_state = IDLE; // infrared sensor initialization pinMode(LED_STATUS, OUTPUT); // infrad digitalWrite(LED_STATUS,LOW); pinMode(INFRARED_IN, INPUT); ENABLE_EXTERNAL1_INTERRUPT(); // tigger at INT1 rising edge EICRA = 0x0c; SREG|=1<1 new seq,seqack with data plen, // tcp data length dest_mac, dest_ip ); return; } // after AVR send http request to server, server response by send data with PSHACK to AVR // AVR answer by send ACK and FINACK to server if ( buf [ TCP_FLAGS_P ] == (TCP_FLAG_ACK_V|TCP_FLAG_PUSH_V) ) { plen = es.ES_tcp_get_dlength( (uint8_t*)&buf ); // send ACK to answer PSHACK from server es.ES_tcp_client_send_packet ( buf, 80, // destination port 1200, // source port TCP_FLAG_ACK_V, // flag 0, // (bool)maximum segment size 0, // (bool)clear sequence ack number plen, // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data 0, // tcp data length dest_mac, dest_ip );; // send finack to disconnect from web server es.ES_tcp_client_send_packet ( buf, 80, // destination port 1200, // source port TCP_FLAG_FIN_V|TCP_FLAG_ACK_V, // flag 0, // (bool)maximum segment size 0, // (bool)clear sequence ack number 0, // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data 0, dest_mac, dest_ip ); return; } // answer FINACK from web server by send ACK to web server if ( buf [ TCP_FLAGS_P ] == (TCP_FLAG_ACK_V|TCP_FLAG_FIN_V) ) { // send ACK with seqack = 1 es.ES_tcp_client_send_packet( buf, 80, // destination port 1200, // source port TCP_FLAG_ACK_V, // flag 0, // (bool)maximum segment size 0, // (bool)clear sequence ack number 1, // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data 0, dest_mac, dest_ip ); client_state = IDLE; // return to IDLE state client_data_ready =0; // client data sent } } }