Last time, we displayed the brightness data from the light sensor using analog input on the Arduino. Now, we know how to process both input and output with the Arduino! Today, I’d like to take it a step further and connect the Arduino to a network. By enabling network capabilities on the Arduino, the project possibilities open up. Despite its small size, it can become a web server! This time, I’d like to make a program that turns the Arduino into a web server using a module called the Ethernet shield. This will allow us to connect a LAN cable, and enable networking on the Arduino.
I explained last time that if the light sensor is put in a dark space, its resistance goes up, and if it’s put in a bright place, the resistance goes down.
If a light sensor is connected to a network, you can transmit the measurements of the light sensor over the network! By integrating this Arduino web server with a PHP or Ruby program, it becomes possible to develop simple programs that measure the length of daytime for each day or determine whether someone’s still at the office based on the office lighting, or program bots that interact with the Twitter API to regularly tweet the current brightness.
There are many modules called shields for the Arduino, which can be used to easily extend the available set of features. In addition to the Ethernet Shield that we’ll use today, there’s an accelerometer as well as many other shields you can use without having to build a circuit from scratch.
Google search for “Arduino shield”
Today, we’ll use Ethernet Shield R3. The Ethernet shield lets you connect your Arduino to a network. There’re several samples using the Ethernet shield included in the Arduino sketchbook. So, we will use them to run Arduino as a server.
First, let’s connect the Ethernet shield to the Arduino.
(Arduino products are always in such cool-looking box…)
It looks like this once it’s been unboxed.
Let’s get connect the Arduino.
Place the Arduino body on the bottom, and connect the Ethernet shield’s feet to the appropriate slots.
The Arduino with the Ethernet shield connected. Some of the legs stick out, but that’s okay.
We’re now done connecting the Arduino unit to the Ethernet shield. You may have connecting issues if you bend any of the feet when you’re removing or inserting the shield, so be careful.
Now we’ll get into the development of the program. The development isn’t hard. For this one, we’ll use the WebServer sample program for the Ethernet section of the sketchbook.
[File]-[Examples]-[Ethernet]-[WebServer]
This is a sample program that allows us to use the Arduino as a server. Once we access the Arduino’s server, we will find the analog pin input data displayed in HTML.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Specify the MAC address that's on the Ethernet shield IPAddress ip( 192,168,0,177); //Specify the IP address to assign to the Arduino server EthernetServer server(80); //Specify the port that the server will use (Since it will be HTTP, you can leave it as 80) void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } Ethernet.begin(mac, ip); //Here we attempt to connect by Ethernet shield server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { EthernetClient client = server.available(); //Monitor whether there is an external connection to the server if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == 'n' &amp;&amp; currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println(); client.println(""); client.println(""); // output the value of each analog input pin for (int analogChannel = 0; analogChannel &lt; 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println(""); } client.println(""); break; } if (c == 'n') { // you're starting a new line currentLineIsBlank = true; } else if (c != 'r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disonnected"); } } |
There’s two places where you need to modify the program:
MAC address
A MAC address is like a unique product number that differs among each piece of hardware. The Ethernet shield that we’re using today should have the MAC address written on the shield itself or on the box that it came in. Refer to it and modify the code, prefixing the address with “0x”.
Here it’s written on the back of the Ethernet shield.
IP address
Set the IP address based on the network configuration of the current computer that you’re using. Usually there’s a router when you’re running a network at home, in which case you can find out what the computer’s IP address by using the method below, then assign an IP address to the Arduino by picking one that doesn’t clash with other devices on the network. On a Mac, you can check the [Network] section of the [System Preferences], or enter “ifconfig” in the [Terminal].
On Windows, open a [command prompt] and enter “ipconfig”.
Today, I have the computer and Arduino connected to the router by LAN cable as shown below. There can be various differences in network setups, so please make sure to check your own environment setup.
Once you configured the MAC and IP address, let’s compile the program and upload it.
Once you’ve uploaded the program to your Arduino unit, let’s go ahead and enter the URL based on the IP address that you configured earlier in your browser. Since I set it to 192.168.0.177, I entered “http://192.168.0.177/” on the browser. And the values of the analog input were displayed.
This program is designed to refresh the browser every 5 seconds and display the analog input value, but since there’s nothing else connected to the Arduino, the input value is going up and down. By linking here the circuit to the light input sensor that we created last time, we can monitor the data from the light sensor every time we access the server.
Here the circuit with the analog input that we made last time
Once the circuit is done, we connect it to the Arduino. We only want to use the analog input #0 this time, so let’s modify our program. In the part of the code beginning with “for” around line 36, the input value is monitored by specifying the analog input number. This block starting with “for” is called a “for statement,” and it’s used whenever we want to evaluate something repeatedly.
for (initial value ; condition for looping ; action done after every loop){ what’s done if the condition is true }
Here, it is obtaining the input values by looping through analog channels 0 through 5 in order. Today, we’re only interested in #0, so we can either remove the “for” loop, or have it complete after one cycle.
1 2 3 4 5 6 7 8 9 10 |
//Run the contents of the loop whenever the variable analogChannel(initial value set to 0) is below 6. At the end of each loop, add 1 to analogChannel for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println(" "); } |
Pattern 1: Since we only want the value of channel 0, modify the “for” loop’s condition
1 2 3 4 5 6 7 8 9 |
for (int analogChannel = 0; analogChannel < 1; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println(" "); } |
Pattern 2: Write out the code without a “for” statement
1 2 3 4 5 6 7 |
int sensorReading = analogRead(0); //Specify 0 in analogRead client.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println(" "); |
Modify the program using one of the two patterns above and run it.
Now the brightness value from the light sensor is displayed on the server. After that, you can run a different program on a separate PC that periodically access the server, reads the brightness data, and uses it as input. By using these Arduino external module “shields,” you are able to come closer to making what you want to create sooner, without having to create a circuit from scratch. Next time, we’ll try to make an even more practical project using the Arduino and its Ethernet shield! Don’t miss it!