An Arduino Room Monitoring Web Server

First of all, what’s an Arduino?

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing). Arduino projects can be stand-alone or they can communicate with software on running on a computer (e.g. Flash, Processing, MaxMSP).

Introduction

In this project, we will use Ethernet shield Ethernet Shield V2.0 and Arduino to create a simple web server. Using the Ethernet library, the device will be able to answer a HTTP request with the Ethernet shield. After opening a browser and navigating to the Ethernet shield’s IP address, the Arduino will respond with just enough HTML for a browser to display the data.

1. What do we need?

First, let’s look at the things we need to build this project. Since we’re working on Arduino  platform, an Arduino main board is a must. Ethernet shield is what you need to create an Ethernet project. This Ethernet shield make use of the famous Wiznet W5100 chip which is easy-to-use and powerful. In term of inputs, we use LDR and PIR sensors. White colour super bright LED is used as the light source. We’re able to control the ‘real’ light in the room such as fluorescent tube with some modification to this Arduino platform. However we take out the AC power section to make sure that we focus on Ethernet  capability in this project. We can cover how to control the real world electrical appliances later.

2. Hardware & Schematic

When we look at the Arduino board and Ethernet shield, we will find 4 rows of header pins (female on main board, while male on shield). They’re designed to be stackable. Plug the Ethernet shield on top of the Arduino main board.  Make sure all the pins are aligned and inserted properly to prevent wrong connection. After that connect the sensors and LED as shown in the schematic below. The connection is very straight forward.

First, a 220 ohm resistor is connected in series with the LED and Digital Pin 4 will drive this LED. PIR sensor output digital signal of 5 and 0V. So any digital I/O pin can be used to read the signal. I’m using Digital Pin 3. For LDR sensor, we will read the analog value produced by the voltage divider formed by the LDR and 10k ohm resistor at Analog In A5. The completed hardware will look like the figure below. The hardware setup is really simple, isn’t it? No soldering at all!

You can see that I insert the iron pins of the components into the female header of the Ethernet shield. Sometimes the connections are loose because the iron pins may be too thin for the header. So you may want to extent the pins and plug the components on a breadboard.

3. Let’s sketching!

Sketch refers to Arduino’s program. It’s the unit of code that is uploaded to and running on an Arduino board. Before we get started with the sketch, we need to install Arduino IDE. It can be downloaded from www.arduino.cc. This Arduino sketch is based on the WebServer.pde example with some modifications to suit our application. The example is located at File > Examples > Ethernet > WebServer in Arduino IDE. Shown below is the Arduino IDE opening the ArduiServer.pde sketch.

First of all, we need to include SPI.h and Ethernet.h at the beginning of the sketch. We’ll use the functions in these two libraries later. Each piece of networking equipment has a unique serial number to identify itself over a network, and this is normally hard-programmed into the equipments’ firmware. However with Arduino we can define the MAC address ourselves. If you are running more than one Ethernet shield on your network, ensure they have different MAC addresses by altering the hexadecimal values in the line:

byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };

However if you only have one shield just leave it be. There may be statistically very rare chance of having a MAC address the same as your existing hardware.

Next we need to specify the IP address of the Ethernet shield. Go to the line:

byte ip[] = { 192, 168, 1, 111 };

The 192.168.1.111 is the IP address I use for my TM Wireless ADSL modem (Model: Innacomm W3100), so you can alter it to match your own setup. For example, if your home router’s IP address is 192.168.0.1, you will need to set your Ethernet shield IP to let’s say 192.168.0.53 so that the Ethernet shield is able to talk to the router. Hence, your IP address should look like this:

byte ip[] = { 192, 168, 0, 53};

You can put any number at the forth column. It’s not necessarily to be 111 or 53. Any number will do as long as none of your device connected to the same router is using the same IP address. You may also try pinging your network from a computer connected to the network, and lookup a table. On your computer, click [Start] -> [Run…] and type “cmd” and [Enter]. Type “ipconfig” to find your network address. The network address is found by performing a logical AND operation on your IP address and the subnet mask. You can see I leave this comment at ArduiServer.pde:

//P1 --> { 10, 1, 1, 5 };

That’s the address I use for my P1 Wimax device. It’s working too!

The last thing we need to set for Ethernet configuration is the port number. Port 80 is the default port for HTTP. Here I use 3178 because it’s the last 4 digits of Cytron’s support line:

Server server(3178);

Then we define the variables used in the sketch:

int LED = 3; // led is connected to digital pin 3
int PIR = 2; // PIR sensor is connected to digital pin 2
int LDR = 5; // LDR sensor is connected to analog in 5
int PIRstate = 0; // variable for PIR sensor status
float photocell = 0; // variable for photocell (LDR) analog value
char c = 0; // received data
char command[2] = "\0"; // command
}

We do the initialization process in the setup section:

void setup()
{
Ethernet.begin(mac, ip);
server.begin();
pinMode(LED, OUTPUT);
pinMode(PIR, INPUT);
}
}

After initialization, we write the program in void loop (). Two useful functions that I would like to mention here are:

client.print(data, BASE); // BASE (optional)
client.println(data);

These two functions are used to print data or string on the web server page. client.println() will print an end-of-line character, while client.print() will not. To display a webpage in the browser, we need to use HTML coding. So you can see a lot of HTML syntax in void loop (). To learn about it, you need to find a book that teaches you HTML or google it in the web.

I hope that you have understood the basic coding structure for an Arduino sketch by now. Since detailed comments are given in the sketch, I won’t be explaining the rest of the code here. You may download the sketch from Robot. Head to Toe website.

4. How’s the page look like in the browser?

Once you have made your alterations, upload the sketch to your Arduino Uno, Duemilanove or compatible main board. Now, connect the shield to your router with an RJ45 cable. Power up the Arduino board via USB or external power supply. Then return to your computer, and using your web browser, enter your Ethernet shield’s IP address into the URL address bar. The IP address based on my Ethernet configuration is:

192.168.1.111:3178

Port number 3178 must be included unless you set it to 80 (default HTTP port). The web browser will query the Ethernet shield, which will return the web server page contents preloaded on the Arduino board. This is what you will see from this project:

The LDR reads the light level of the room and show the value as the Light Reading. If there’s no movement detected, the web page shows “No Movement” in green colour. The data will refresh every 5 seconds. By clicking on the LED On and LED Off buttons, you can control the white colour super bright LED connected to the Arduino board. When the LED is turned on, the light reading increases dramatically (from the initial value of 195 to 939). This is because I put the LDR facing the white LED. When the PIR sensor detects movement, the web page will show “Motion Detected!” in red colour.

LED turned on (virtually)

Motion detected!

LED turned on (physically)

5. Making a private IP visible to the Internet

Now you can see how easy it is to send data from your Arduino via an Ethernet network to a web browser. But that is only to a local web browser. That means you can’t access the web page from other network other than your home router network. You can’t access it in any cafe that provides hotspot too. It’s very impractical to monitor your room only when you’re at home isn’t it?

To get around this, we need to set one of your router’s ports to forward incoming messages and connection requests to the Ethernet shield. To do this, open the router’s administrator interface and look for “port forwarding”, “port mapping” or “port filtering”. Usually this option is located in the “Advance” section. (Woot!…You’re an advanced user!) The interface will vary depending on the model of your router, but the settings generally go by one of these names. I’m using Innacomm W3100 Wireless ADSL modem provided by TM. My router interface looks like this:

The figure above shows the outgoing direction has been set and I’m going to set the incoming direction when this print screen was taken. The full instructions to setup Innacomm W3100 Wireless ADSL Modem:

  1. Log into the router administrator page at 198.168.1.1 and insert your username and password. “tmuser” is the default username and password.
  2. Go to “Advance” tab, select “DMZ Settings”. Then check the box ”Enable DMZ” and fill in your IP address in the DMZ Host IP Address”. Mine is 192.168.1.111. Press “Apply Changes” button, then “Commit/Reboot”.
  3. Once you restarted the router, Log in again. Then go to “Advance” tab and “Firewall”.
  4. Press the “Add Rule” button. Key in the following:
  • Rule Action – Allow
  • Direction – Outgoing
  • Protocol – TCP
  • Src IP Address (mine is 192.168.1.111)
  • Src Subnet Mask (mine is 255.255.255.0)
  • Port (mine is 3178)
  • Dst IP Address (mine is 192.168.1.111)
  • Dst Subnet Mask (mine 255.255.255.0)
  • Port (mine is 3178)
  1. You have just completed for “Incoming, TCP”. continue to key in for “Incoming, TCP”
  2. Press “Apply Changes”, then “Commit/Reboot” and that’s it!

Once we have done this, any incoming requests to connect to the router’s public IP address on port 3178 will be forwarded to the Arduino Ethernet shield’s private IP address on the same port. I’m not going to reveal my router’s public IP address here to prevent any hacking activity. However, you can always check your public IP address at http://whatismyipaddress.com. For example, your public IP address to access the Arduino Server may look like this:

124.25.62.179:3178

Just insert the line into your browser’s address bar from anywhere of the world and you are ready to monitor your room! Usually this IP address is not static and it may change when you restart your modem. This depends on the availability when your modem is turned off and powered up again. Although this IP address will remain if you don’t turn off your modem, what if there’s blackout one day?

Okay, here’s another trick to help you with this dynamic IP problem. You can create a domain name to host your dynamic IP address. Many modem provides Dynamic Domain Name System (DDNS) configuration that enable you to update your current public IP address to your domain name hosted online. DynDNS.com is one of the service provider that allows you to create free account, host your IP address and even automatically update it for free. You just have to setup the account in your router’s administrator page if it’s supported like my Innacomm W3100 Wireless ADSL modem.

DynDNS.com domain name setup

Modem domain name setup

I created an account in DynDNS.com and registered a domain name called arduiserver2 and chose dyndns.org as the host. Hence the full domain name for this project will be:

arduiserver2.dyndns.org:3178

This domain name will host the public IP address. Any incoming request to this domain will be translated by DynDNS.com to the current public IP address that points to the Arduino web server.

That’s all for this project. You can download the source code at the attachment. Hope you enjoy building your Arduino web server too! By further enhancing this Arduino room monitoring web server project, a more sophisticated web based home automation system can be built. If you are new to Arduino, always refer to http://arduino.cc/ for getting started guide and examples.

* If you want faster response from the author, please do post your inquiry/comments/feedback to our technical forum as we seldom check the comments section in tutorial site 🙂

Attachments:

  1. Arduino Ethernet Room Monitoring Source Code.zip

Buy

, ,

Related Post

Getting Started with Colour Sensor Module (SN-COLOUR-MOD)

Getting Started with Tilt Sensor (SN-TILT)

Getting Started with Vibration Sensor Module (Normally Off) (SN-VIBRATE-MOD)

Getting Started with IR Distance Click

36 thoughts on “An Arduino Room Monitoring Web Server

  1. hi, i’m doing a kind of web controlling using arduino too
    and i wanna ask bout how to refresh the page to specified address…
    in you example, the page just refresh to the last address which ended by b=2
    how can i refresh the page to http://192.168.xxx.xxx/ without the?b=2
    i’ve tried to change the line code for refreshing
    –> client.println(“”);
    but it doesn’t work…
    may i know how to make it success?
    thanks n wait for your kindly reply

    wynn

  2. This worked great for me using 022, but I am unable to get the led buttons to work using “IDE 1”. I’ve changed the sketch to add “EthernetServer” and “EthernetClient” where the compiler said server and client were not recognized. Are there any other lines that should be changed in the sketch that I’m missing (yes I’m a newbie). Any ideas would be appreciated

  3. Hi, i also facing same problem with greg.. i can monitor PIR signal(i replace with pushbutton) but i cannot turn on and off the LED..

  4. hi. i am new to Arduino+Ethernet. i am working on same project. i connect my Arduino to BSNL dataone ZTE ZXDSL 531B modem. i port forward it. but i am not able to access my Aduino LED over internet. it works fine for local area but not out side the local network. please tell me what is going wrong?

  5. This article gives clear idea designed for the new visitors
    of blogging, that actually how to do blogging and site-building.

  6. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment
    is added I get three e-mails with the same comment.
    Is there any way you can remove me from that service? Cheers!

  7. Guys,

    Is it possible if i stack an Xbee shield on top of the ethernet shield, and achieve wireless communication using Xbee S2?
    The sensor will be on another module of arduino uno+xbee shield+xbee. And the web server host will be arduino uno+ethernet shield+Xbee Shield+Xbee. The readings of the sensor will be transmitted to the web server host and the readings will be displayed on serial monitor.

    Is this possible? Appreciate detailed explanation or any links related.

    Regards,

  8. Yes, it’s possible. Please browse through our tutorials on XBee too. You may discuss about any problem you encounter in our forum too.

  9. Hello! Good evening sir i would like to ask you about our design because i cant access my web controller using other network provider. what i want to tell is that can i access my web controller using other network connection through internet. I need your help sir im using TPLink TL-WR741ND version 4.22 i think its in port forwarding.

  10. Guys,

    byte ip[] = { 192, 168, 1, 111 };

    for this line, if my line IP is 192.168.1.1 , this line should be written as byte ip[] = { 192,168,1,1 ) ? Or it should be different so it wouldn’t clash.

    Regards,

  11. Yes, it should be different from your modem’s IP address. If your modem’s IP address is 192.168.1.1, you should use, for example, 192.168.1.54 instead.

  12. The author included the steps to do exactly what you need in the 5th point – Making a private IP visible to the Internet. And yes the keyword is port forwarding. Although the modems are different but the concept is there. I believe that dyndns.com only gives free trial by now. May be you can have a look at http://www.noip.com/

  13. We’ve updated the source code.

    Only this 3 lines of code has been modified to suit the latest Arduino IDE:
    1. IPAddress ip(192,168,1,111);
    2. EthernetServer server(3178);
    3. EthernetClient client = server.available();

    Everything works fine at our site. Please feedback if you face any problem. Thanks!

  14. Excellent i have been thinking of doing something similar.
    Ive already started with the mac and ip addresses OK.

    Can you please share the sketch code?

    Regards, T

  15. Hi!
    I would like to know if the PIR sensor works when there is a object above it and it doesn’t move.
    The message will continue to be “motion detected”?
    Thanks

  16. buttons are not notworking,pir motion and light sensor are working,some thing wrong in led on off button coding.

  17. Hi, Your page is so good!!
    I want to know how I should include button nbr 2 (on and off) for LED 2?
    Thank you so much!!!

  18. hi great project

    how do i add more buttons to the code i have added the buttons but dont understand the code well enough to finish it please help this bit is ok

    // button functions
    client.println(“”);
    client.println(“Led On”);
    client.println(“Led Off”);
    client.println(“”);
    client.println(“Pump On”);
    client.println(“Pump Off”);

  19. I am using P1 modem(DV-230) I downloaded the code and change the ip to 10.1.1.5 but it doesn’t work. Do I need to change some setting in the modem? And how I make it accessible by internet?

  20. Hi,

    i have buy the Ethernet shield but i cant find the exact code from the example.help please!!

    thanks.

  21. Hi,
    5.Making a private IP visible to the Internet
    Currently my interface of my modem is totally different with the webpage have shown. i have found the port forwarding but the information need to key in is totally different with the page have shown can u guild me make my shield visible to the internet.
    Thanks for helping.

  22. Crear una empresa conlleva, no solo tener ideas, sino más bien asimismo cualidades personales, habilidades interpersonales y determinadas capacidades profesionales.

  23. What i do not realize is in fact how you’re no longer
    really much more neatly-favored than you might be now.
    You’re so intelligent. You already know thus significantly in the case of this subject,
    made me for my part believe it from numerous numerous
    angles. Its like women and men are not interested until it is something to
    do with Woman gaga! Your own stuffs great. All the time handle
    it up!

  24. Suppose I create a dyndns account and configure the router’s DDNS settings, can I just type the host address(eg:myaccount568.dyndns.org) in the browser and connect to the Arduino ?

  25. Remarkablᥱ tɦings here. Ⅰ’m very glad to see your post.
    Thanks a lоt ɑnd I’m looking ahеad to touсh you. Will you kindly drop me a mɑil?

Leave a Reply

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