Advertisement
FIST01

Ethernet_Project

Feb 7th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. #include "etherShield.h"
  2.  
  3.  
  4. // please modify the following two lines. mac and ip have to be unique
  5. // in your local area network. You can not have the same numbers in
  6. // two devices:
  7. static uint8_t mymac[6] = {
  8. 0x54,0x55,0x58,0x10,0x00,0x24};
  9. static uint8_t myip[4] = {
  10. 192,168,1,15};
  11. static char baseurl[]="http://192.168.1.15/";
  12. static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)
  13.  
  14.  
  15.  
  16. #define BUFFER_SIZE 500
  17. static uint8_t buf[BUFFER_SIZE+1];
  18. #define STR_BUFFER_SIZE 22
  19. static char strbuf[STR_BUFFER_SIZE+1];
  20.  
  21. EtherShield es=EtherShield();
  22.  
  23. // prepare the webpage by writing the data to the tcp send buffer
  24. uint16_t print_webpage(uint8_t *buf, byte on_off);
  25. int8_t analyse_cmd(char *str);
  26.  
  27. // LED cathode connects the Pin4, anode to 5V through 1K resistor
  28. #define LED_PIN 4
  29.  
  30.  
  31. void setup(){
  32.  
  33. /*initialize enc28j60*/
  34. es.ES_enc28j60Init(mymac);
  35. es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
  36. delay(10);
  37.  
  38. /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
  39. // LEDA=greed LEDB=yellow
  40. //
  41. // 0x880 is PHLCON LEDB=on, LEDA=on
  42. // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
  43. es.ES_enc28j60PhyWrite(PHLCON,0x880);
  44. delay(500);
  45. //
  46. // 0x990 is PHLCON LEDB=off, LEDA=off
  47. // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
  48. es.ES_enc28j60PhyWrite(PHLCON,0x990);
  49. delay(500);
  50. //
  51. // 0x880 is PHLCON LEDB=on, LEDA=on
  52. // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
  53. es.ES_enc28j60PhyWrite(PHLCON,0x880);
  54. delay(500);
  55. //
  56. // 0x990 is PHLCON LEDB=off, LEDA=off
  57. // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
  58. es.ES_enc28j60PhyWrite(PHLCON,0x990);
  59. delay(500);
  60. //
  61. // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  62. // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
  63. es.ES_enc28j60PhyWrite(PHLCON,0x476);
  64. delay(100);
  65.  
  66. //init the ethernet/ip layer:
  67. es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
  68.  
  69. pinMode(LED_PIN, OUTPUT);
  70. digitalWrite(LED_PIN, LOW); // switch on LED
  71. }
  72.  
  73. void loop(){
  74. uint16_t plen, dat_p;
  75. int8_t cmd;
  76. byte on_off = 1;
  77.  
  78. plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
  79.  
  80. /*plen will ne unequal to zero if there is a valid packet (without crc error) */
  81. if(plen!=0){
  82.  
  83. // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address.
  84. if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){
  85. es.ES_make_arp_answer_from_request(buf);
  86. return;
  87. }
  88.  
  89. // check if ip packets are for us:
  90. if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){
  91. return;
  92. }
  93.  
  94. if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
  95. es.ES_make_echo_reply_from_request(buf,plen);
  96. return;
  97. }
  98.  
  99. // tcp port www start, compare only the lower byte
  100. if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==mywwwport){
  101. if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
  102. es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack
  103. return;
  104. }
  105. if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
  106. es.ES_init_len_info(buf); // init some data structures
  107. dat_p=es.ES_get_tcp_data_pointer();
  108. if (dat_p==0){ // we can possibly have no data, just ack:
  109. if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
  110. es.ES_make_tcp_ack_from_any(buf);
  111. }
  112. return;
  113. }
  114. if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
  115.  
  116. plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
  117. goto SENDTCP;
  118. }
  119. if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
  120. plen=print_webpage(buf, on_off);
  121. goto SENDTCP;
  122. }
  123. cmd=analyse_cmd((char *)&(buf[dat_p+5]));
  124.  
  125. if (cmd==2){
  126. on_off=1;
  127. digitalWrite(LED_PIN, LOW); // switch on LED
  128. }
  129. else if (cmd==3){
  130. on_off=0;
  131. digitalWrite(LED_PIN, HIGH); // switch off LED
  132. }
  133. plen=print_webpage(buf, on_off);
  134.  
  135. plen=print_webpage(buf, on_off);
  136. SENDTCP:
  137. es.ES_make_tcp_ack_from_any(buf); // send ack for http get
  138. es.ES_make_tcp_ack_with_data(buf,plen); // send data
  139. }
  140. }
  141. }
  142.  
  143. }
  144. // The returned value is stored in the global var strbuf
  145. uint8_t find_key_val(char *str,char *key)
  146. {
  147. uint8_t found=0;
  148. uint8_t i=0;
  149. char *kp;
  150. kp=key;
  151. while(*str && *str!=' ' && found==0){
  152. if (*str == *kp){
  153. kp++;
  154. if (*kp == '\0'){
  155. str++;
  156. kp=key;
  157. if (*str == '='){
  158. found=1;
  159. }
  160. }
  161. }
  162. else{
  163. kp=key;
  164. }
  165. str++;
  166. }
  167. if (found==1){
  168. // copy the value to a buffer and terminate it with '\0'
  169. while(*str && *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){
  170. strbuf[i]=*str;
  171. i++;
  172. str++;
  173. }
  174. strbuf[i]='\0';
  175. }
  176. return(found);
  177. }
  178.  
  179. int8_t analyse_cmd(char *str)
  180. {
  181. int8_t r=-1;
  182.  
  183. if (find_key_val(str,"cmd")){
  184. if (*strbuf < 0x3a && *strbuf > 0x2f){
  185. // is a ASCII number, return it
  186. r=(*strbuf-0x30);
  187. }
  188. }
  189. return r;
  190. }
  191.  
  192.  
  193. uint16_t print_webpage(uint8_t *buf, byte on_off)
  194. {
  195.  
  196. int i=0;
  197.  
  198.  
  199. uint16_t plen;
  200.  
  201.  
  202.  
  203. plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
  204. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<center><p><h1>Welcome to the DBM Switch Controller </h1></p> "));
  205.  
  206. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<hr><br><form METHOD=get action=\""));
  207. plen=es.ES_fill_tcp_data(buf,plen,baseurl);
  208. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
  209. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h2> REMOTE SWITCH: </h2> "));
  210. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h1><font color=\"#00FF00\"> "));
  211.  
  212. if(on_off)
  213. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("ON"));
  214. else
  215. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("OFF"));
  216.  
  217. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(" </font></h1><br> ") );
  218.  
  219. if(on_off){
  220. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=3>"));
  221. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Switch off\"></form>"));
  222. }
  223. else {
  224. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=2>"));
  225. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Switch on\"></form>"));
  226. }
  227.  
  228.  
  229. return(plen);
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement