Arduino Ethernet Web Server With Relay
Arduino Ethernet Web Server With Relay
randomnerdtutorials.com/arduino-ethernet-web-server-with-relay/
This posts shows how to build an Arduino Ethernet web server that controls a relay that is attached to a lamp.
You can access your web server with any device that has a browser and its connected to the same network.
Ethernet shield
The Arduino Ethernet shield connects your Arduino to the internet in a simple way. Just mount this module onto your
Arduino board, connect it to your network with an RJ45 cable and follow a few simple steps to start controlling your
projects through the web.
Note: you must connect an Ethernet cable from your router to your Ethernet shield as shown in the following figure.
Pin usage
When the Arduino is connected to an Ethernet shield, you cant use Digital pins from 10 to 13, because they are
being used in order to establish a communication between the Arduino and the Ethernet shield.
1/8
Relay module
A relay is an electrically operated switch. It means that it can be turned on or off, letting the current going through or
not. The relay module is the one in the figure below.
This particular relay module comes with two relays (those blue cubes).
2/8
COM: common pin
NO: normally open there is no contact between the common pin and the normally open pin. So, when you
trigger the relay, it connects to the COM pin and power is provided to the load (a lamp, in our case).
NC: normally closed there is contact between the common pin and the normally closed pin. There is
always contact between the COM and NC pins, even when the relay is turned off. When you trigger the relay,
the circuit is opened and there is no power provided to the load.
Relating this project, it is better to use a normally open circuit, because we want to light up the lamp occasionally.
Read this tutorial to learn more about using a relay module with the Arduino board.
The connections between the relay and the Arduino are really simple:
Parts required
Heres a complete list of the components you need for this
project:
Code
Copy the following code to your Arduino IDE and before
uploading it to your Arduino board read the Configuring your
3/8
network section below.
/*
* Rui Santos
* Complete Project Details http://randomnerdtutorials.com
*/
#include <SPI.h>
#include <Ethernet.h>
// Client variables
char linebuf[80];
int charcount=0;
void setup() {
// Relay module prepared
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
4/8
// Generates buttons to control the relay
client.println("<h4>Relay 1 - State: " + relay1State + "</h4>");
// If relay is off, it shows the button to turn the output on
if(relay1State == "Off"){
client.println("<a href=\"/relay1on\"><button>ON</button></a>");
}
// If relay is on, it shows the button to turn the output off
else if(relay1State == "On"){
client.println("<a href=\"/relay1off\"><button>OFF</button></a>");
}
client.println("</body></html>");
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
// 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' && currentLineIsBlank) {
dashboardPage(client);
break;
}
if (c == '\n') {
if (strstr(linebuf,"GET /relay1off") > 0){
digitalWrite(relay, HIGH);
relay1State = "Off";
}
else if (strstr(linebuf,"GET /relay1on") > 0){
digitalWrite(relay, LOW);
relay1State = "On";
}
// you're starting a new line
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
5/8
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");
}
}
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,XXX);
Important: you actually might need to replace that variable highlighted in red with appropriate values that are
suitable for your network, otherwise your Arduino will not establish a connection with your network.
Replace the following line with an IP that is available and suitable for your network:
In my case, my IP range is 192.168.1.X and with the software Angry IP Scanner I know that the IP 192.168.1.111 is
available in my network, because it doesnt have any active device in my network with that exact same IP address:
Schematics
Wire you circuit accordingly to the schematic below:
6/8
Demonstration
Your Arduino Web Sever looks like the figure below:
7/8
Heres a demonstration showing what you have at the end of this project:
Wrapping up
With this project you built an Arduino web server that turns a relay on and off.
Now, you can use this project to control any electronics appliance you want.
8/8