Posted By

TioSolid on 04/21/11


Tagged

ethernet arduino


Versions (?)

Arduino + ET-MINI ENC28J60


 / Published in: C
 

URL: http://code-all.blogspot.com/2011/01/arduino-ethernet-control-led.html

  1. /*
  2.  * Examples : Arduino Experiment Examples By....Thai Micro Tech
  3.  * Program : Hardware LED
  4.  * Hardware : Arduino 328 and ENC28J60
  5.  * : ET-MINI ENC28J60(Select VCC = +5V)
  6.  * : SDO -> MOSI(PB3) = Digital-11
  7.  * : SDI <- MISO(PB4) = Digital-12
  8.  * : SCK -> SCK(PB5) = Digital-13
  9.  * : CS# -> SS#(PB2) = Digital-10
  10.  * : LED -> Output Pin(PB1) = Digital-9
  11.  * Function : Webserver Control LED
  12.  */
  13.  
  14. #include "etherShield.h"
  15.  
  16. #define ledPin 9 // LED Source Drive ("1" = ON , "0" = OFF)
  17. #define ledPin2 8 // LED Source Drive ("1" = ON , "0" = OFF)
  18. #define ledPin3 6 // LED Source Drive ("1" = ON , "0" = OFF)
  19. #define ledPin4 5 // LED Source Drive ("1" = ON , "0" = OFF)
  20.  
  21. // please modify the following two lines. mac and ip have to be unique
  22. // in your local area network. You can not have the same numbers in
  23. // two devices:
  24. static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
  25. static uint8_t myip[4] = {192,168,1,201};
  26. static char baseurl[] = "http://192.168.1.201/";
  27. static uint16_t mywwwport = 80; // listen port for tcp/www (max range 1-254)
  28.  
  29. #define BUFFER_SIZE 500
  30. static uint8_t buf[BUFFER_SIZE+1];
  31. #define STR_BUFFER_SIZE 22
  32. static char strbuf[STR_BUFFER_SIZE+1];
  33.  
  34. EtherShield es=EtherShield();
  35.  
  36. // prepare the webpage by writing the data to the tcp send buffer
  37. uint16_t print_webpage(uint8_t *buf, byte led_status);
  38. int8_t analyse_cmd(char *str);
  39.  
  40. void setup()
  41. {
  42. /*initialize enc28j60*/
  43. es.ES_enc28j60Init(mymac);
  44. es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
  45. delay(10);
  46.  
  47. /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
  48. // LEDA=greed LEDB=yellow
  49. //
  50. // 0x880 is PHLCON LEDB=on, LEDA=on
  51. // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
  52. es.ES_enc28j60PhyWrite(PHLCON,0x880);
  53. delay(500);
  54. //
  55. // 0x990 is PHLCON LEDB=off, LEDA=off
  56. // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
  57. es.ES_enc28j60PhyWrite(PHLCON,0x990);
  58. delay(500);
  59. //
  60. // 0x880 is PHLCON LEDB=on, LEDA=on
  61. // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
  62. es.ES_enc28j60PhyWrite(PHLCON,0x880);
  63. delay(500);
  64. //
  65. // 0x990 is PHLCON LEDB=off, LEDA=off
  66. // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
  67. es.ES_enc28j60PhyWrite(PHLCON,0x990);
  68. delay(500);
  69. //
  70. // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  71. // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
  72. es.ES_enc28j60PhyWrite(PHLCON,0x476);
  73. delay(100);
  74.  
  75. //init the ethernet/ip layer:
  76. es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
  77.  
  78. pinMode(ledPin, OUTPUT);
  79. digitalWrite(ledPin, LOW); // switch off LED
  80. pinMode(ledPin2, OUTPUT);
  81. digitalWrite(ledPin2, LOW); // switch off LED
  82. pinMode(ledPin3, OUTPUT);
  83. digitalWrite(ledPin3, LOW); // switch off LED
  84. pinMode(ledPin4, OUTPUT);
  85. digitalWrite(ledPin4, LOW); // switch off LED
  86. }
  87.  
  88. void loop()
  89. {
  90. uint16_t plen, dat_p;
  91. int8_t cmd;
  92. int8_t cmd2;
  93. int8_t cmd3;
  94. int8_t cmd4;
  95. static byte led_status = 0;
  96.  
  97. plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
  98.  
  99. /*plen will ne unequal to zero if there is a valid packet (without crc error) */
  100. if(plen!=0)
  101. {
  102. // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address.
  103. if(es.ES_eth_type_is_arp_and_my_ip(buf,plen))
  104. {
  105. es.ES_make_arp_answer_from_request(buf);
  106. return;
  107. }
  108.  
  109. // check if ip packets are for us:
  110. if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0)
  111. {
  112. return;
  113. }
  114.  
  115. if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
  116. {
  117. es.ES_make_echo_reply_from_request(buf,plen);
  118. return;
  119. }
  120.  
  121. // tcp port www start, compare only the lower byte
  122. if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==mywwwport)
  123. {
  124. if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V)
  125. {
  126. es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack
  127. return;
  128. }
  129. if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V)
  130. {
  131. es.ES_init_len_info(buf); // init some data structures
  132. dat_p=es.ES_get_tcp_data_pointer();
  133. if (dat_p==0)
  134. { // we can possibly have no data, just ack:
  135. if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V)
  136. {
  137. es.ES_make_tcp_ack_from_any(buf);
  138. }
  139. return;
  140. }
  141.  
  142. if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0) // GET /?cmd=0, GET /?cmd=1
  143. {
  144. // head, post and other methods for possible status codes see:
  145. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  146. plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK
  147. Content-Type: text/html
  148.  
  149. <h1>200 OK</h1>"));
  150. goto SENDTCP;
  151. }
  152. if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0)
  153. {
  154. plen=print_webpage(buf, led_status);
  155. goto SENDTCP;
  156. }
  157. cmd=analyse_cmd((char *)&(buf[dat_p+5]));
  158. cmd2=analyse_cmd((char *)&(buf[dat_p+5]));
  159. cmd3=analyse_cmd((char *)&(buf[dat_p+5]));
  160. cmd4=analyse_cmd((char *)&(buf[dat_p+5]));
  161.  
  162.  
  163. // 192.168.1.201/?led=0
  164. if (cmd==0) // ?led=0 --> OFF
  165. {
  166. led_status = 0;
  167. digitalWrite(ledPin, LOW); // LED = OFF
  168. }
  169.  
  170. // 192.168.1.201/?led=1
  171. else if (cmd==1) // ?led=1 --> ON
  172. {
  173. led_status = 1;
  174. digitalWrite(ledPin, HIGH); // LED = ON
  175. }
  176.  
  177.  
  178. plen=print_webpage(buf, led_status);
  179. SENDTCP:es.ES_make_tcp_ack_from_any(buf); // send ack for http get
  180. es.ES_make_tcp_ack_with_data(buf,plen); // send data
  181. }
  182. }
  183. }
  184. }
  185. // The returned value is stored in the global var strbuf
  186.  
  187.  
  188. uint8_t find_key_val(char *str,char *key)
  189. {
  190. uint8_t found=0;
  191. uint8_t i=0;
  192. char *kp;
  193. kp=key;
  194. while(*str && *str!=' ' && found==0)
  195. {
  196. if (*str == *kp)
  197. {
  198. kp++;
  199. if (*kp == '\0')
  200. {
  201. str++;
  202. kp=key;
  203. if (*str == '=')
  204. {
  205. found=1;
  206. }
  207. }
  208. }
  209. else
  210. {
  211. kp=key;
  212. }
  213. str++;
  214. }
  215. if (found==1)
  216. {
  217. // copy the value to a buffer and terminate it with '\0'
  218. while(*str && *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE)
  219. {
  220. strbuf[i]=*str;
  221. i++;
  222. str++;
  223. }
  224. strbuf[i]='\0';
  225. }
  226. return(found);
  227. }
  228.  
  229. int8_t analyse_cmd(char *str)
  230. {
  231. int8_t r=-1;
  232.  
  233. if (find_key_val(str,"led"))
  234. {
  235. if (*strbuf < 0x3a && *strbuf > 0x2f) //'0'..'9'
  236. {
  237. // is a ASCII number, return it
  238. r=(*strbuf-0x30); // Convert '0'..'9' to 0..9
  239. }
  240. }
  241. return r;
  242. }
  243.  
  244. uint16_t print_webpage(uint8_t *buf, byte on_off)
  245. {
  246. int i=0;
  247.  
  248. uint16_t plen;
  249.  
  250. // HTML Header
  251. plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK
  252. Content-Type: text/html
  253.  
  254. "));
  255.  
  256. // HTML Body
  257. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<center><p><h1>Thai Micro Tech Test Control</h1>"));
  258. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<center><hr>"));
  259.  
  260. // Start of Form
  261. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<br><form METHOD=get action=\""));
  262. plen=es.ES_fill_tcp_data(buf,plen,baseurl);
  263. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
  264. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h2>Status</h2> "));
  265.  
  266. if(on_off==0)
  267. {
  268. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h1><font color=\"#FF0000\"> "));
  269. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("OFF"));
  270. }
  271.  
  272. else if(on_off==1)
  273. {
  274. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h1><font color=\"#00FF00\"> "));
  275. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("ON"));
  276.  
  277. }
  278.  
  279. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</font></h1><br>"));
  280.  
  281. if(on_off==0)
  282. {
  283. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=led value=1>"));
  284. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"SW1 ON\">"));
  285. }
  286.  
  287. else if(on_off==1)
  288. {
  289. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=led value=0>"));
  290. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"SW1 OFF\">"));
  291. }
  292.  
  293. // End of Form
  294. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</form>"));
  295.  
  296. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</center><hr> <p>Webserver Control By.Thai Micro Tech <a href=\"http://www.thaimicrotech.com\"> Thai Micro Tech <a>"));
  297. return(plen);
  298. }

Report this snippet  

You need to login to post a comment.