An Arduino Room Monitoring Web Server

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 various 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 communicate with software 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 what we need to build this project. Since we’re working on the Arduino platform, an Arduino main board is a must. Ethernet shield is what you need to create an Ethernet project. This Ethernet shield makes 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 can control the ‘real’ light in the room such as fluorescent tubes with some modifications to this Arduino platform. However, we take out the AC power section to ensure that we focus on Ethernet capability in this project. We can cover how to control 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 the main board, while male on the 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 straightforward.

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 the 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 extend the pins and plug the components into a breadboard.

3. Let’s sketch!

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:

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

However if you only have one shield just leave it be. There may be a 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:

  1. 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:

  1. 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:

  1. //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:

  1. Server server(3178);

Then we define the variables used in the sketch:

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

We do the initialization process in the setup section:

  1. void setup()
  2. {
  3. Ethernet.begin(mac, ip);
  4. server.begin();
  5. pinMode(LED, OUTPUT);
  6. pinMode(PIR, INPUT);
  7. }
  8. }

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

  1. client.print(data, BASE); // BASE (optional)
  2. 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 athttp://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 in the attachment. I 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 a faster response from the author, please do post your inquiry/comments/feedback to our technical forum as we seldom check the comments section in the tutorial site 

ATTACHMENTS:

  1. Arduino Ethernet Room Monitoring Source Code.zip