Pinterest Stumbleupon Whatsapp
Ads by Google

arduino web serverA while ago, I showed you how to setup an internet control system for your Arduino How To Set Up Arduino Web Control Without An Ethernet Shield How To Set Up Arduino Web Control Without An Ethernet Shield For the last few weeks, I handed over control of the mood lighting in my studio to viewers during the live broadcast of Technophilia Podcast - you can see the results of that in the... Read More – but it had to stay connected to a computer through USB in order to maintain the internet connection. If you have an Ethernet shield though, that’s not necessary  Today we’re going to look at setting up an Arduino as a basic webserver so we can host our own device control website.

You will need:

Setting up

The Ethernet shield uses pins 10 through 13 for controlling the network connection, so leave those out of your project; pin 4 is also used for the SD card, but we won’t be using that today.

First things first – open up the File->Examples->Ethernet->WebServer that’s provided within the Arduino IDE. Find the lines referring MAC and IP address.

arduino web server

The MAC address for mine was found on the box – yours may be on the shield itself. Adjust the first line of values – you need to leave the preceding 0x bit though. Technically speaking, it doesn’t really matter what you put here so long as you don’t have any conflicting values on your subnet – but even so, this was the addressed assigned by the manufacturer, so we should probably stick with that. Just know that if you get it wrong, it will still work.

Ads by Google

arduino webserver example

Next up is the IP address. We want a specific fixed address so we can setup port forwarding properly – the shield is capable of getting an address from DHCP, but that’s only useful if it’s merely going to be used as a client, not a server. Fill in an IP address that’s unused and memorable on your local network, your router IP, and 255.255.255.0 (the subnet). If none of this is making sense to you, go read our free and thorough guide to home networking DOWNLOAD Connected: Your Complete Guide To Home Networking DOWNLOAD Connected: Your Complete Guide To Home Networking Are you setting up your home network, but you don't know where to start? Start here with "Connected: Your Complete Guide To Home Networking". By MakeUseOf author Matt Smith, this guide outlines everything you need... Read More .

Port Forwarding

Edit the line that says EthernetServer server(number), and change the number to 8081. This will setup our server to listen on port 8081 in case your ISP is blocking web traffic on the traditional port 80.

Upload the demo at this point and test it out from a local machine. You will of course need to plug in the Ethernet cable too. Keep the USB plugged in too, as we need it for power – you can replace that with a 9V power supply later, but we’re still testing.

Noob-tip: if you get this error, it means you’ve mistakenly put a capital O instead of a 0 in the MAC address area. There is no O in hex-values! If you really are new, it may be worthwhile checking out our beginner’s Arduino guide!

arduino webserver example

To access the Arduino, type in the address and port number directly from a browser.

arduino webserver example

You should see something like that – so far so good. Unfortunately, this is only accessible on the local network right now, so head on into your router’s port configuration page to set up a redirect from port 8081 to the same port on your Arduino’s IP (See: What is port forwarding? What Is Port Forwarding & How Can It Help Me? [MakeUseOf Explains] What Is Port Forwarding & How Can It Help Me? [MakeUseOf Explains] Do you cry a little inside when someone tells you there’s a port forwarding problem and that’s why your shiny new app won’t work? Your Xbox won’t let you play games, your torrent downloads refuse... Read More )

arduino web server tutorial

Test this out from a mobile 3G internet device with Wifi disabled; remember to use your public IP address, not your local network address. Your router should be able to tell you this, or just ask Google “what is my ip“.

arduino web server

Understanding the output

The page being served in the demo is reading values from the analog pins – but since there’s nothing connected there, you’re going to get nonsense. Try connecting a light sensor (photo resistor) to port A0 and +5v, with a 10k resistor also on A0 and gnd (this is a voltage divider, since the analog pins can only read voltage, and not resistance) just to confirm the server is actually reading values. Notice that when light intensity changes, the values on the other pins actually change too – this is because they’re all unconnected.

Getting Interactive

Reading these values is all well and good, but making the jump to controlling an Arduino from here is quite a leap, and outside the scope of this tutorial today. Instead of re-inventing the wheel, I’ll point you towards Arduino forum user hari who wrote a basic API for interacting with digital pins. Using this code (I’ve modified it to work with the latest Ethernet library), connect the long lead of an LED (anode) to pin 8, and the short lead to pin 7 (with a suitable resistor). You can then use URLs of the form http://ip:port/digitalWrite/8/1 to turn on the LED, and http://ip:port/digitalWrite/8/0 to turn it off, and read analog values with analogRead/0.

This example from bildr will also teach you how to control a sequence of LEDs by reading the GET request.

You might also like to checkout this example which outputs a form on the page to control a blinkm RGB LED, though you could easily adapt it for an RGB  strip such as the dynamic ambient lighting project Build Your Own Dynamic Ambient Lighting For A Media Center Build Your Own Dynamic Ambient Lighting For A Media Center If you watch a lot of movies on your PC or media center, I'm sure you've faced the lighting dilemma; do you completely turn off all lights? Do you keep them on full blast? Or... Read More  we built a while back.

Well, I had fun building these, so I hope you did too. If you’ve made a library which makes serving up pages and controls simple, please do get in touch in the comments.

  1. asha
    July 27, 2016 at 4:37 am

    i am using an arduino-uno,but i can't access my web page.i had already assign ip address to arduino. when i run arduino(ctrl+shift+m),it displays output.But when i access my web page,it will display "you can't" reached to this web page.

    • James Bruce
      July 27, 2016 at 7:03 am

      This might be a dumb question, but did you just arbitrarily pick an IP address, or did you make sure it was actually an address possible on your local network? Your router IP address also needs to be correct. After that, access the IP and port directly on your local network, then move onto port forwarding.

  2. Onyx
    June 8, 2016 at 9:10 pm

    Could this be used to create a public Minecraft server? And do need to pay something monthly to do this?

    • James Bruce
      June 9, 2016 at 7:27 am

      No. Arduino isn't actually a computer, so it can't run Minecraft. It can serve up data to a web interface, but nothing more. You could use a $35 Raspberry Pi to run a Minecraft server though: https://pimylifeup.com/raspberry-pi-minecraft-server/

  3. sri
    June 4, 2016 at 12:39 pm

    My Webserver is working fine with local IP and local network with ethernet cable.

    working with port forwarding/public IP when using other network connection to mobile as mentioned in the article.

    doesnt work in my mobile or laptop when using the Wifi/Wireless router in same network with local IP.Please let me know where i am going wrong.

    • James Bruce
      June 4, 2016 at 12:42 pm

      Your Wi-Fi network traffic is being firewalled from your Ethernet traffic. The router is either faulty, or not configured to join the two.

  4. panagiotis
    July 9, 2013 at 2:44 pm

    i want to ask how can i make my ip adress public?
    in local network it works fine but when i am gonna get in from my phone it returns nothing...
    if anyone can help please reply...

    • James T
      July 9, 2013 at 3:27 pm

      Did you read the whole article? The last section on port forwarding specifically addresses this. Basically, you don't make the IP of your Arduino public - you simply tell your router to forward all public requests to the Arduino, using port forwarding config page from your router.

  5. husin
    July 1, 2013 at 2:19 am

    i need help in this code because when emergency occure by 4(high) i can't back to previous state .......how can i do this????

    //ARDUINO 1.0+ ONLY
    //ARDUINO 1.0+ ONLY

    #include
    #include
    boolean reading = false;

    ////////////////////////////////////////////////////////////////////////
    //CONFIGURE
    ////////////////////////////////////////////////////////////////////////
    byte ip[] = { 192, 168, 16, 71 }; //Manual setup only
    //byte gateway[] = { 192, 168, 1, 1 }; //Manual setup only
    //byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only

    // if need to change the MAC address (Very Rare)
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

    EthernetServer server = EthernetServer(80); //port 80
    ////////////////////////////////////////////////////////////////////////

    void setup(){
    Serial.begin(9600);

    //Pins 10,11,12 & 13 are used by the ethernet shield

    pinMode(2, INPUT);// SWICH
    pinMode(3, OUTPUT); //LAMP
    pinMode(4,INPUT);// EMERGANCY
    pinMode(6,INPUT);

    Ethernet.begin(mac);
    //Ethernet.begin(mac, ip, gateway, subnet); //for manual setup

    server.begin();
    Serial.println(Ethernet.localIP());

    }

    void loop(){

    digitalWrite(6,LOW);
    if((digitalRead(4)==LOW)||(digitalRead(6)==HIGH))
    {
    if(digitalRead(2)==HIGH)
    {
    digitalWrite(3,HIGH);
    delay(5000);
    digitalWrite(3,LOW);

    }

    }

    checkForClient(); // listen for incoming clients, and process qequest.

    }

    void checkForClient(){

    EthernetClient client = server.available();

    if (client) {

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
    if (client.available()) {

    if(!sentHeader){
    // send a standard http response header
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println();
    sentHeader = true;
    }

    char c = client.read();

    if(reading && c == ' ') reading = false;
    if(c == '?') reading = true; //found the ?, begin reading the info

    if(reading){
    Serial.print(c);

    switch (c) {
    case '4':
    //add code here to trigger on 2
    triggerPin(4, client);
    break;
    case '6':
    //add code here to trigger on 3
    triggerPin(6, client);
    break;

    }

    }

    if (c == '\n' && currentLineIsBlank) break;

    if (c == '\n') {
    currentLineIsBlank = true;
    }else if (c != '\r') {
    currentLineIsBlank = false;
    }

    }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:

    }

    }

    void triggerPin(int pin, EthernetClient client){
    //blink a pin - Client needed just for HTML output purposes.
    client.print("Turning on pin ");
    client.println(pin);
    client.print("");

    if( pin, HIGH )
    {
    digitalWrite(4 , HIGH );
    digitalWrite(6, HIGH );}

    }

    • Lionel Too
      October 2, 2015 at 2:37 pm

      If I had webserver host, IP is not 192.xxx, So How do I connect arduino to website?

  6. Doc
    April 8, 2013 at 6:37 pm

    "Give Your Arduino Project It’s Own Mini-Webserver, With An Ethernet Shield"

    *its* #corrections

  7. echantrea
    April 8, 2013 at 11:11 am

    Greate information. Which one is better if compare Arduino with Raspberry Pi?

    • muotechguy
      April 8, 2013 at 11:33 am

      That's not a comparison that can be made. What are you wanting to make? For a simple webserver, probably the Pi. For more complex electronic projects, the Arduino by far.

  8. Ben J
    April 8, 2013 at 3:49 am

    I was just thinking to myself that the Raspberry Pi would be a more elegant alternative to this. But this is well worth the read though. Good article.

  9. Chris Marcoe
    April 8, 2013 at 12:49 am

    Wouldn't it be easier and cheaper to do this with a a Raspberry Pi? I'm not really sure because I've never used a Arduino, but i do have a Pi.

    • Ben J
      April 8, 2013 at 3:50 am

      My thoughts exactly.

    • muotechguy
      April 8, 2013 at 6:35 am

      In the context of merely setting up a mini-webserver, certainly. But then you don't have the Arduino; the power of this is really in integrating a web server to existing Arduino projects. The Pi is great for mini-computing, but not really for electronics prototyping.

    • Chris Marcoe
      April 8, 2013 at 3:00 pm

      As for the addition of a web-server, that makes sense. I was thinking of if you were to start from scratch to make one.

  10. Oklahoma_Mike
    April 7, 2013 at 11:14 pm

    I'm sure that if I ever actually purchase one of these I'll find out they're incredibly useful.

Leave a Reply

Your email address will not be published. Required fields are marked *