Arduino Forum

Community => Exhibition / Gallery => Topic started by: physic.dude on Feb 05, 2011, 04:38 am

Title: My Arduino Web Server v2.0
Post by: physic.dude on Feb 05, 2011, 04:38 am
I share with you my Arduino Web Server V2.0 (Closed)
   [url removed]

(http://farm6.static.flickr.com/5256/5580985241_ccab7e1321.jpg)

I first debuted my Arduino web server in January of 2011, its been serving you all for months and gained thousands of hits over the course of its service.
Sadly, I retired it after 5 months, it was getting a little old.

Thanks to everyone who provided me with help when I needed it and also to the many people who left comments on the site. XD

Here is the latest code from the site:
Code: [Select]
/*
* This is a web server that allows you to host a fully functional html
*  webpage on the World Wide Web.
* Initial coding was done with the help from the many people in the Arduino community.
*  Thanks guys!!
*
* Arduino Setup: You need an Ethernet Shield SD, and and Arduino.
*  Optionally pins 8 and 9 can be used for two LEDs that can indicate traffic.
*
* SD Card Setup: On the SD card, which must be formated as fat, you must have
*  an HTML file with the file name of 'index.htm'. All file names on this card
*  must be written in the old 8.3 file format. In other words, all files must be
*  named with no more then 8 characters for the name, and 3 for the extension.
*
* Note: If you don't know HTML, a good place that I found useful was w3schools
*  http://www.w3schools.com/html/html_examples.asp
*
*/

#include <LiquidCrystal.h>
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <Ethernet.h>
#define BUFSIZ 128
#define greenLEDandBEEP 2
#define redLEDpin 3
boolean led2 = true;
int hits = 0; // Set the number of hits the hit counter will start at.
int units = 0;
int count = 0;
int photocellPin = 2;
int photocellReading;
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);

//Local ethernet setup
byte mac[] = {
 0x90, 0xA2, 0xDA, 0x00, 0x1C, 0x0E };
byte ip[] = {
 192, 168, 1, 80 };
char rootFileName[] = "index.htm";
Server server(8080);

//SD card stuff
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
 PgmPrint("error: ");
 SerialPrintln_P(str);
 if (card.errorCode()) {
   PgmPrint("SD error: ");
   Serial.print(card.errorCode(), HEX);
   Serial.print(',');
   Serial.println(card.errorData(), HEX);
 }
 while(1);
}

void setup() {
 lcd.begin(16, 2);
 lcd.print("Web Server v2.00");
 lcd.setCursor(0, 1);
 lcd.print("Hits:");

 Serial.begin(9600);

 pinMode(greenLEDandBEEP, OUTPUT);
 pinMode(redLEDpin, OUTPUT);

 PgmPrint("Free RAM: ");
 Serial.println(FreeRam());  

 pinMode(10, OUTPUT);              
 digitalWrite(10, HIGH);              

 if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");

 if (!volume.init(&card)) error("vol.init failed!");

 PgmPrint("Volume is FAT");
 Serial.println(volume.fatType(),DEC);
 Serial.println();

 if (!root.openRoot(&volume)) error("openRoot failed");

 PgmPrintln("Files found in root:");
 root.ls(LS_DATE | LS_SIZE);
 Serial.println();

 PgmPrintln("Files found in all dirs:");
 root.ls(LS_R);

 Serial.println();
 PgmPrintln("Done");

 Ethernet.begin(mac, ip);
 server.begin();
}


void loop()
{
 if(count >=500)
 {  
   led2 = !led2;
   digitalWrite(redLEDpin, led2);
   count = 0;
 }
 count +=1;

 char clientline[BUFSIZ];
 char *filename;
 int image = 0;
 int index = 0;

 Client client = server.available();
 if (client) {
   boolean current_line_is_blank = true;

   index = 0;

   while (client.connected()) {
     if (client.available()) {
       char c = client.read();

       if (c != '\n' && c != '\r') {
         clientline[index] = c;
         index++;
         if (index >= BUFSIZ)
           index = BUFSIZ -1;

         continue;
       }

       clientline[index] = 0;
       filename = 0;

       Serial.println(clientline);

       if (strstr(clientline, "GET / ") != 0) {
         filename = rootFileName;

       }
       if (strstr(clientline, "GET /") != 0) {
         if (!filename) filename = clientline + 5;

         (strstr(clientline, " HTTP"))[0] = 0;

         Serial.println(filename);

         if (! file.open(&root, filename, O_READ)) {
           client.println("HTTP/1.1 404 Not Found");
           client.println("Content-Type: text/html");
           client.println();
           client.println("<h2>Error 404</h2>");
           client.println("<s2>The file does not exist.<s2>");
           client.println("");
           break;
         }

         Serial.println("Opened!");
         //File types
         client.println("HTTP/1.1 200 OK");
         if (strstr(filename, ".htm") != 0)
           client.println("Content-Type: text/html");
         else if (strstr(filename, ".css") != 0)
           client.println("Content-Type: text/css");
         else if (strstr(filename, ".png") != 0)
           client.println("Content-Type: image/png");
         else if (strstr(filename, ".jpg") != 0)
           client.println("Content-Type: image/jpeg");
         else if (strstr(filename, ".gif") != 0)
           client.println("Content-Type: image/gif");
         else if (strstr(filename, ".3gp") != 0)
           client.println("Content-Type: video/mpeg");
         else if (strstr(filename, ".pdf") != 0)
           client.println("Content-Type: application/pdf");
         else if (strstr(filename, ".js") != 0)
           client.println("Content-Type: application/x-javascript");
         else if (strstr(filename, ".xml") != 0)
           client.println("Content-Type: application/xml");
         else
           client.println("Content-Type: text");
         client.println();

         int16_t c;
         while ((c = file.read()) >= 0) {
           //Serial.print((char)c); //Prints all HTML code to serial (For debuging)
           client.print((char)c); //Prints all HTML code for web page
         }

         //Hit counter math
         if(units >= 2)
         {
           hits ++;
           units = 0;
         }
         units +=1;
         //End hit counter math

         //Print the hit counters and light value
         lcd.setCursor(6, 1);
         lcd.print(hits); //Print hits for LCD hit counter

         client.print("<html><body>"); //HTML code starts here
         client.print("<P align=\"center\">");
         client.print("Hits since reset: <b>");  
         client.print(hits); //Print hits to client
         client.print("</b><br>");

         photocellReading = analogRead(photocellPin);
         client.print("Light reading: ");
         client.print(photocellReading); //Prints light reading to client

         // A few threshholds
         if (photocellReading < 10) {
           client.print(" - Dark");
         }
         else if (photocellReading < 200) {
           client.print(" - Dim");
         }
         else if (photocellReading < 500) {
           client.print(" - Light");
         }
         else if (photocellReading < 800) {
           client.print(" - Bright");
         }
         else {
           client.print(" - Very bright");
         }

         client.print("</p></body></html>"); //HTML code ends here
         //End hit counter and light value

         file.close();

       }
       else {
         client.println("HTTP/1.1 404 Not Found");
         client.println("Content-Type: text/html");
         client.println();
         client.println("<h2>Error 404</h2>");
         client.println("");
       }
       break;
     }
   }
   digitalWrite(greenLEDandBEEP, HIGH);
   delay(1);
   digitalWrite(greenLEDandBEEP, LOW);
   client.stop();
 }

}

//The End /*


The display is a 16x2 LCD with blue EL back light, thats the HV driver board in the upper right corner. The SD card was rerouted out to the front for easy access. There was a later edition too, a photocell, cleverly hidden next to the LCD's glass frame.

Oh, and if you want to know how I made the case,
arduino.cc/forum/index.php/topic,51138.msg412259.html#msg412259 (http://arduino.cc/forum/index.php/topic,51138.msg412259.html#msg412259)

I have also included a basic HTML site template below.
Title: Re: My Arduino Web Server
Post by: madworm on Feb 06, 2011, 09:06 pm
Any successful hacks yet ?
Title: Re: My Arduino Web Server
Post by: physic.dude on Feb 06, 2011, 10:17 pm

Any successful hacks yet ?


Nope, not that I'm aware of... 
Title: Re: My Arduino Web Server
Post by: keff_in_sg on Feb 15, 2011, 04:57 pm
Hi Physics-Dude,

There are multiple webpages and there is even a bitmap file in the webpage. But I don't see any of this code in the .pde. Maybe the code you placed on the website is for another page?

Cheers,
K
Title: Re: My Arduino Web Server
Post by: physic.dude on Feb 16, 2011, 10:45 pm

Hi Physics-Dude,

There are multiple webpages and there is even a bitmap file in the webpage. But I don't see any of this code in the .pde. Maybe the code you placed on the website is for another page?

Cheers,
K



I'm not sure whats wrong, try refreshing a few times, or try a different web browser like FireFox. Plenty of other people seem to be able access the site just fine, it looks just fine on my end.
Title: Re: My Arduino Web Server
Post by: fkeel on Feb 22, 2011, 06:34 am
website loads and as soon as is finished my firefox tells me "the connection to server was terminated"

hm... too tired to reset cookies and everything, but I usually dont have problems with firefox - maybe there is something on your end you can do, so that people like me, who quickly want to look at some arduino projects before they go to bed and are to lazy to actually think anymore can access the site?
Title: Re: My Arduino Web Server
Post by: physic.dude on Feb 23, 2011, 02:38 am

website loads and as soon as is finished my firefox tells me "the connection to server was terminated"

hm... too tired to reset cookies and everything, but I usually dont have problems with firefox - maybe there is something on your end you can do, so that people like me, who quickly want to look at some arduino projects before they go to bed and are to lazy to actually think anymore can access the site?


Its not on my end, I too use FireFox and it works just fine. Just refresh if it fails to load.
Title: Re: My Arduino Web Server
Post by: karlboll on Feb 23, 2011, 08:25 am
fkeel >>>

Quote
hm... too tired to reset cookies and everything, but I usually dont have problems with firefox - maybe there is something on your end you can do, so that people like me, who quickly want to look at some arduino projects before they go to bed and are to lazy to actually think anymore can access the site?


Did you try ctrl-F5? Website works fine from my end.
Title: Re: My Arduino Web Server
Post by: fkeel on Feb 24, 2011, 06:17 pm


dont know what it is. it loads fine, but as soon as everything loads i get an error, telling me the connection to server was terminated. ... reloading was the first (well, and only) thing I tried... anyway, I still get the error. I will try from my university network when I get around to it - maybe its an issue with the internet provider here.
Title: Re: My Arduino Web Server
Post by: Meinaart on Feb 25, 2011, 04:43 pm
Really cool, nice job!
Title: Re: My Arduino Web Server
Post by: fkeel on Feb 26, 2011, 12:12 am
must have been the internet provider. I am now using a different service and it works fine... cool project :-)
Title: Re: My Arduino Web Server v1.0
Post by: physic.dude on Apr 02, 2011, 08:37 am
Version 2 is up and running!!  XD
http://physicsdude.dyndns.tv/ (http://physicsdude.dyndns.tv/)
Title: Re: My Arduino Web Server
Post by: amacmullen14 on Apr 02, 2011, 02:24 pm

Hi Physics-Dude,

There are multiple webpages and there is even a bitmap file in the webpage. But I don't see any of this code in the .pde. Maybe the code you placed on the website is for another page?

Cheers,
K


I think that all the data for the website is on the sd card.  The code just reads the sd card and passes the data along to the client.
Title: Re: My Arduino Web Server v2.0
Post by: martin_bg on Apr 02, 2011, 07:46 pm
I like the box, can you say a few words on how you made it?
Title: Re: My Arduino Web Server v2.0
Post by: Kaouthia on Apr 02, 2011, 08:07 pm
Quote from: The Arduino
and a number (seen at bottom of the page) increments up on an LCD


I thought about it...

Then read...

Quote from: The Arduino
(Don't abuse it)


and refrained.

Very neat :)
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 02, 2011, 09:26 pm

I like the box, can you say a few words on how you made it?


Ah yes,
The box was originally a '2 Port Data Switch Box for Parallel Printer Ports'. Here is what they look like.
(http://www.rackmount-devices.com/images/018-2474b.jpg)

After being gutted, I marked a spot for the LCD and then spent some time with my Dremel followed by some careful filing. I then drilled some simple holes for the LEDs and switch. The SD card slot was a later edition, that was just some basic Dremel work too. As for the rear face, the original holes where the parallel ports were fitted were used to my advantage and some minor cuts were made to easily fit in the Arduino and shield. After all holes were cut and drilled, I used some 150 grit sand paper to remove the yellow-ish oxidized paint and make the proper brushed texture. Also, the rings around the LEDs are some fancy washers things I had.

As for the inside, its mostly held in place with plastic standoffs and glue.
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 02, 2011, 09:40 pm

Quote from: The Arduino
and a number (seen at bottom of the page) increments up on an LCD


I thought about it...

Then read...

Quote from: The Arduino
(Don't abuse it)


and refrained.

Very neat :)


Actually, I would LOL. I wouldn't mind it really, unless someone starts going all ]:D ]:D ]:D with it.

Title: Re: My Arduino Web Server v2.0
Post by: tytower on Apr 03, 2011, 07:12 am
Well I get the connection terminated too after it seems to start loading so whats causing that ?
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 03, 2011, 02:30 pm

Well I get the connection terminated too after it seems to start loading so whats causing that ?


Ok, I just changed something in the code that might help. Can you, or someone else who previously had the same problem, please try again and tell me what happens?
Title: Re: My Arduino Web Server v2.0
Post by: Celteek on Apr 06, 2011, 03:31 pm
Do you think t could support video streaming even with some bad qulity video?

How does the comment thing works? You don't have any php or sql working with the Arduino so i don't see how it could work...
Title: Re: My Arduino Web Server v2.0
Post by: Kaouthia on Apr 06, 2011, 03:45 pm
Looks like a 3rd party service. :)

htmlcommentbox.com

But, you wouldn't need PHP & MySQL.  He could've (assuming the Arduino can handle it) written something to receive the comments, store them on an SD card, and play them back when the page is loaded.  He didn't, but could've. :)
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 06, 2011, 05:25 pm

Do you think t could support video streaming even with some bad qulity video?

How does the comment thing works? You don't have any php or sql working with the Arduino so i don't see how it could work...


Streaming video? I've tried it, and it works, but it takes forever to load, even with the lowest bitrate. Same with audio too.

The comment thing is run third party from htmlcommentbox.com I dont fully know PHP and MySQL (yet).
Title: Re: My Arduino Web Server v2.0
Post by: MichaelP08 on Apr 06, 2011, 11:27 pm


Well I get the connection terminated too after it seems to start loading so whats causing that ?


Ok, I just changed something in the code that might help. Can you, or someone else who previously had the same problem, please try again and tell me what happens?


Still have problems: Website loads for a few seconds ( also shows parts of the site),  and then firefox shows "connection lost".
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 06, 2011, 11:54 pm
Quote

Still have problems: Website loads for a few seconds ( also shows parts of the site),  and then firefox shows "connection lost".


Sorry, but I have tried multiple Internet Providers on multiple browsers, and never have I been able to recreate or find any problems. If anyone has a possible solution, please tell.
Title: Re: My Arduino Web Server v2.0
Post by: fkeel on Apr 08, 2011, 12:43 pm
Yeah, I had that too, when I was visiting my grandparents... It would load fully, and as soon as it was there, it would tell me "unable to establish connection" or something like that... my guess is, that my grandparents and MichaelP08's providers have some security feature that your server doesnt pass...

but... to be honest, I really dont know anything about these things... I will check it out when I visit my grandparents again though... If you are interested in solving the problem (if us two are the only ones who ever had issues, it really doesnt seem that pressing...) and if there is something specific I can check, tell me and I will try that next tim I have the opportunity.
Title: Re: My Arduino Web Server v2.0
Post by: MichaelP08 on Apr 08, 2011, 02:05 pm
hmm my provider is t-com germany. what browser do your grandparents have? i always get the error @  firefox 3.6
Title: Re: My Arduino Web Server v2.0
Post by: fkeel on Apr 08, 2011, 06:08 pm
its not a browser issue. well, probably not.
i have firefox 3.6 as well, and it works perfectly fine.

i used the same laptopt, same browser etc. at my grandparents - same error, you had. I tried different browser, same error.

*

btw@physicsdude. I am in no way trying to diminish the coolness of your project - I find it your webserver awesome... I feel a bit bad that all the discussion in this thread has settled down to be about an odd bug... I guess its just human nature that instead of pointing out all the things that work, talking about something that doesnt work seems to be more interesting.
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 08, 2011, 07:18 pm
Quote
btw@physicsdude. I am in no way trying to diminish the coolness of your project - I find it your webserver awesome... I feel a bit bad that all the discussion in this thread has settled down to be about an odd bug... I guess its just human nature that instead of pointing out all the things that work, talking about something that doesnt work seems to be more interesting.


I understand and I'm fine with it. I just wish I had a soulition.
Title: Re: My Arduino Web Server v2.0
Post by: arduinomstr on Apr 08, 2011, 11:06 pm
Try using a different browser - I usually use Firefox on Linux and was getting "The connection to the server was reset while the page was loading."

Just downloaded Chromium and the page loads fine! :) Seems like a Firefox problem.
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 09, 2011, 04:14 am
Ok, a new update is out.
By somewhat popular demand, I have added a feature where the server now outputs sensor data (with a photocell)!! XD
Title: Re: My Arduino Web Server v2.0
Post by: tytower on Apr 09, 2011, 01:06 pm
Imusing Firefox 3.6.10
This time it loadedpartof the page and hung on "About server line"
Second time it got through and lost connection
Third it hung again
I have a screenshot of it and will try to attach it here
Title: Re: My Arduino Web Server v2.0
Post by: tjw7 on Apr 14, 2011, 08:06 pm
Just plain awesome!

Congratulations physics-dude, and thanks for sharing!

Cheers,
Title: Re: My Arduino Web Server v2.0
Post by: lumi on Apr 20, 2011, 05:01 am
Wow, very cool  8)
I might think about to build one for myself to have access to my data when I am not at home.
How about to use it as an download server (i mean just some sketches, nothing very big)

BTW, I am using Firefox 4.0 and the page is working fine. Just "About The Server" page is blank gray but no error message. (just fixed this since i blocked scripts  :D :smiley-red:
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 20, 2011, 05:14 am

Wow, very cool  8)
I might think about to build one for myself to have access to my data when I am not at home.
How about to use it as an download server (i mean just some sketches, nothing very big)

BTW, I am using Firefox 4.0 and the page is working fine. Just "About The Server" page is blank gray but no error message. (just fixed this since i blocked scripts  :D :smiley-red:


Thanks!

You can very well use an Arduino web server to download files like sketches and stuff,  but of course it will be fairly slow with anything over 100kb. Although I have successfully downloaded a 6MB file from the server in testing.

And I will try to reassess the problem with the web page's strangeness soon, now that I know it may be related to scripting.
Edit: Ok, I made a change to the pages, so I either made it worse or maybe I fixed it. Please don't hesitate to tell me if a page fails to load, or if you were one of the few who previously encountered a problem, try again to see it its fixed.
Title: Re: My Arduino Web Server v2.0
Post by: lumi on Apr 20, 2011, 08:32 am

Edit: Ok, I made a change to the pages, so I either made it worse or maybe I fixed it. Please don't hesitate to tell me if a page fails to load, or if you were one of the few who previously encountered a problem, try again to see it its fixed.


Thanks for your quick reply. I will build my own one after my holidays  8)

Now after your changes i just can see the "About The Server" page. All other pages are blank.
Maybe i was to quick...pages are loading fine now...
Title: Re: My Arduino Web Server v2.0
Post by: tytower on Apr 20, 2011, 09:30 am
No change forme still loads then immediately resets page
Title: Re: My Arduino Web Server v2.0
Post by: physic.dude on Apr 20, 2011, 11:05 pm
Ok, I made some changes, again.  :smiley-sweat:
Please try again if you experienced the bug, and say whether it works now or not.
Title: Re: My Arduino Web Server v2.0
Post by: tytower on Apr 20, 2011, 11:36 pm
Sorry -No change
Title: Re: My Arduino Web Server v2.0
Post by: lumi on Apr 21, 2011, 07:33 pm
For me all pages are working fine..no problems at all.
Title: Re: My Arduino Web Server v2.0
Post by: tytower on Apr 21, 2011, 08:59 pm

For me all pages are working fine..no problems at all.


What are you looking at it with ?

I am using
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) Gecko/20101005 Fedora/3.6.10-1.fc14 Firefox/3.6.10

And I have AddBlock on
I'll just turn off addBlock in case that interferes with it
No its not that either
Title: Re: My Arduino Web Server v2.0
Post by: lumi on May 16, 2011, 09:28 am
I used Firefox 4.0 and Firefox 3.6 something and IE 8
Title: Re: My Arduino Web Server v2.0
Post by: SteNull on Jan 26, 2013, 08:23 pm
Love this one! Thanks for sharing, I'm gonna try out your code tonight.