Advertisement
Guest User

Untitled

a guest
May 30th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. // EtherShield webserver demo
  2. #include "etherShield.h"
  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] = { 0x00,0x55,0x58,0x10,0x00,0x24 };
  8. static uint8_t myip[4] = { 10,1,1,35 };
  9.  
  10. int usable_dpins[] = {3,4,5,6,7,8,9};
  11. int usable_apins[] = {0,1,2,3,4,5};                                                                                                              
  12.  
  13. #define PORT 80                                                                                                                            
  14. #define BUFFER_SIZE 750
  15.                            
  16. static uint8_t buf[BUFFER_SIZE+1];                                                                                
  17. uint16_t plen; // Packet Length
  18.  
  19. // The ethernet shield object
  20. EtherShield es=EtherShield();                                                                                                                    
  21.  
  22. // Add the string str to the buffer one character at a time
  23. uint16_t add_string(char* str) {
  24.   int i = 0;
  25.  
  26.   //Loop through each char
  27.   while (str[i]) {
  28.     // Add each char one by one to the buffer
  29.     buf[TCP_CHECKSUM_L_P + 3 + plen] = str[i];
  30.     i++;
  31.     plen++;
  32.   }
  33.  
  34.   return plen;
  35. }
  36.  
  37. void reset_buffer() {
  38.   //memset(buf,65,BUFFER_SIZE);
  39.   plen = 0; // Set the packet len to 0, so it starts filling back up at 0
  40. }
  41.  
  42. uint16_t http200ok(void) {
  43.   reset_buffer(); // Reset the buffer
  44.   add_string("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
  45. }
  46.  
  47. // prepare the webpage by writing the data to the tcp send buffer
  48. uint16_t print_webpage(uint8_t *buf) {
  49.   plen=http200ok(); // Add the HTML 200 ok header
  50.  
  51.   add_string("<html><head><title>Arduino</title></head><body>\n");
  52.   add_string("<h1>Arduino Ethernet Shield</h1>\n");
  53.  
  54.   for (int i = 0; i < (sizeof(usable_dpins) / sizeof(usable_dpins[0])); i++) {
  55.     int mypin = usable_dpins[i];
  56.  
  57.     char mystr[40];
  58.     pinMode(mypin, INPUT);
  59.     sprintf(mystr,"<div>Digital Pin #%d is %d</div>\n",mypin,digitalRead(mypin));
  60.     add_string(mystr);    
  61.   }
  62.  
  63.   for (int i = 0; i < (sizeof(usable_apins) / sizeof(usable_apins[0])); i++) {
  64.     int mypin = usable_apins[i];
  65.  
  66.     char mystr[40];
  67.     sprintf(mystr,"<div>Analog Pin #%d is %d</div>\n",mypin,analogRead(mypin));
  68.     add_string(mystr);
  69.   }
  70.  
  71.   add_string("</body></html>\n");
  72.  
  73.   return(plen);
  74. }
  75.  
  76. void setup() {
  77.   // initialize enc28j60
  78.   es.ES_enc28j60Init(mymac);
  79.  
  80.   // init the ethernet/ip layer:
  81.   es.ES_init_ip_arp_udp_tcp(mymac, myip, PORT);
  82. }
  83.  
  84. int send_tcp() {
  85.   es.ES_www_server_reply(buf,plen); // send web page data
  86.   reset_buffer(); // Empty out the buffer
  87. }
  88.  
  89. void send_404() {
  90.   reset_buffer(); // Clear out any previous buffer
  91.   add_string("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>");
  92.    
  93.   send_tcp();
  94.   reset_buffer(); // Empty out the buffer
  95. }
  96.  
  97. void loop() {
  98.   while(1) {
  99.     // read packet, handle ping and wait for a tcp packet:
  100.     plen = es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
  101.  
  102.     // If there is no packet loop around until their is
  103.     if(plen == 0){
  104.       continue; // Restart the while loop
  105.     }
  106.  
  107.     // Convert the buffer into a char pointer just to check the request
  108.     char* request = (char *)&(buf[plen]); // Cast the buffer as a char* to convert it
  109.  
  110.     // just one web page in the "root directory" of the web server
  111.     if (strstr(request,"GET /ard.html")) {
  112.       plen = print_webpage(buf);
  113.     } else {
  114.       send_404();
  115.     }
  116.  
  117.     // Send the packet
  118.     send_tcp();
  119.   }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement