Controlling a led over the Internet using Arduino and Ethernet Shield
Ethernet Shield enables us to connect Arduino to an ethernet network and build projects which send and receive information via local network and internet. The device, along with its control library, allows the design of network applications easily. There is no need to worry about the complicated signaling schemes in ethernet networks since all control and connection are made by library routines. After the connection is established, we may just read or write data through it, similarly to a conventional serial connection.
In the following, we show an example of a project of a web page that allows you to turn on or turn off a digital port of the Arduino via the local network or the internet. In a practical application, the project could be used to control a lamp, serving as a good “Hello World” application for home automation with Arduino. In this example, the idea is to focus on the design of the software required for the application so that we will consider the connection of led to the Arduino port and show one of the most expensive ways to light a led.
In this project, we used the Ethernet Shield based on the Wiznet W5100 chip.This chip provides all the TCP/IP stack transparently, turning the project very simple since there is no need to worry about the signaling schemes and controls for TCP/IP. It is worth to notice that there are other ethernet shields, simpler and cheaper, using chips that require to control the TCP/IP signaling via software implementation in the Arduino sketch.
The hardware configuration for this project is very simple: just connect the Ethernet Shield to the Arduino and connect a led in series with a 1 KOhm resistor between pin 6 of the Shield and ground (GND).
The idea is to access the IP address associated with the Arduino using a browser and get a page with a link to turn on the led and another to turn it off. For this, we used the following code:
#include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 1, 235 }; EthernetServer server(80); String readString; int Pin = 6; void setup(){ pinMode(Pin, OUTPUT); Ethernet.begin(mac, ip); server.begin(); } void loop(){ EthernetClient client = server.available(); if (client) { while (client.connected()) { if (client.available()) { char c = client.read(); if (readString.length() < 100) { readString += c; } if (c == '\n') { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("<HTML>"); client.println("<BODY>"); client.println("<H1>Control LED</H1>"); client.println("<hr />"); client.println("<br />"); client.println("<a href=\"/?ledon\">Turn on led</a>"); client.println("<a href=\"/?ledoff\">Turn off led</a><br />"); client.println("</BODY>"); client.println("</HTML>"); delay(1); client.stop(); if(readString.indexOf("?ledon") > 0) { digitalWrite(Pin, HIGH); } else { if(readString.indexOf("?ledoff") > 0) { digitalWrite(Pin, LOW); } } readString=""; } } } } }
To control the Ethernet Shield, the Ethernet library is used . The communication between Arduino and the Shield is made using the SPI protocol, hence the need to import the SPI library as well. Remember that communication via SPI is done via pins 10, 11, 12 and 13 on the Arduino Uno, which cannot be used in the design.
The configuration of the server running on port 80 is made by lines
EthernetServer server(80); Ethernet.begin(mac, ip); server.begin();
where the MAC and IP addresses are defined by the vectors mac
and ip
created in lines 4 and 5.
After the initial setup, the program waits for a client connection and, when it detects it, it reads the characters sent to the server regarding the GET request made by the client though the browser.
The characters are received one by one and are stored sequentially in the ReadString
variable. A conditional is used to limit the number of characters received at 100. At each character received, a test is made to check if it is an end of line (\n
, an “ENTER”). In this case, a successful response is sent to the request of the client (HTTP/1.1 200 OK
), containing an HTML page with links to the turn on and turn off the led.
The links to turn on and turn off the led use the query strings ?ledon
and ?ledoff
so that the server can identify the command from the client. After responding to the request, a test is made to check if the address requested by the client contains one of these query strings, by checking the variable ReadString
for the presence of the sequences ?ledon
or ?ledoff
. From this information, the led is turned on or off, and finally, the variable ReadString
is restarted.
This way, it is possible to control the led from an access point to the LAN. It is also possible to control it via internet, configuring a port forward of the port 80 in the router to the IP associated with the Arduino and accessing the page using the public IP provided by the internet provider. However, it is worth mentioning that this setting does not take into account the any security issues, allowing anyone with the public IP to control the led via internet.
Comments