Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // EtherShield webserver demo
- #include "etherShield.h"
- // please modify the following two 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] = { 0x00,0x55,0x58,0x10,0x00,0x24 };
- static uint8_t myip[4] = { 10,1,1,35 };
- int usable_dpins[] = {3,4,5,6,7,8,9};
- int usable_apins[] = {0,1,2,3,4,5};
- #define PORT 80
- #define BUFFER_SIZE 750
- static uint8_t buf[BUFFER_SIZE+1];
- uint16_t plen; // Packet Length
- // The ethernet shield object
- EtherShield es=EtherShield();
- // Add the string str to the buffer one character at a time
- uint16_t add_string(char* str) {
- int i = 0;
- //Loop through each char
- while (str[i]) {
- // Add each char one by one to the buffer
- buf[TCP_CHECKSUM_L_P + 3 + plen] = str[i];
- i++;
- plen++;
- }
- return plen;
- }
- void reset_buffer() {
- //memset(buf,65,BUFFER_SIZE);
- plen = 0; // Set the packet len to 0, so it starts filling back up at 0
- }
- uint16_t http200ok(void) {
- reset_buffer(); // Reset the buffer
- add_string("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
- }
- // prepare the webpage by writing the data to the tcp send buffer
- uint16_t print_webpage(uint8_t *buf) {
- plen=http200ok(); // Add the HTML 200 ok header
- add_string("<html><head><title>Arduino</title></head><body>\n");
- add_string("<h1>Arduino Ethernet Shield</h1>\n");
- for (int i = 0; i < (sizeof(usable_dpins) / sizeof(usable_dpins[0])); i++) {
- int mypin = usable_dpins[i];
- char mystr[40];
- pinMode(mypin, INPUT);
- sprintf(mystr,"<div>Digital Pin #%d is %d</div>\n",mypin,digitalRead(mypin));
- add_string(mystr);
- }
- for (int i = 0; i < (sizeof(usable_apins) / sizeof(usable_apins[0])); i++) {
- int mypin = usable_apins[i];
- char mystr[40];
- sprintf(mystr,"<div>Analog Pin #%d is %d</div>\n",mypin,analogRead(mypin));
- add_string(mystr);
- }
- add_string("</body></html>\n");
- return(plen);
- }
- void setup() {
- // initialize enc28j60
- es.ES_enc28j60Init(mymac);
- // init the ethernet/ip layer:
- es.ES_init_ip_arp_udp_tcp(mymac, myip, PORT);
- }
- int send_tcp() {
- es.ES_www_server_reply(buf,plen); // send web page data
- reset_buffer(); // Empty out the buffer
- }
- void send_404() {
- reset_buffer(); // Clear out any previous buffer
- add_string("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>");
- send_tcp();
- reset_buffer(); // Empty out the buffer
- }
- void loop() {
- while(1) {
- // read packet, handle ping and wait for a tcp packet:
- plen = es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
- // If there is no packet loop around until their is
- if(plen == 0){
- continue; // Restart the while loop
- }
- // Convert the buffer into a char pointer just to check the request
- char* request = (char *)&(buf[plen]); // Cast the buffer as a char* to convert it
- // just one web page in the "root directory" of the web server
- if (strstr(request,"GET /ard.html")) {
- plen = print_webpage(buf);
- } else {
- send_404();
- }
- // Send the packet
- send_tcp();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement