Hardware Reference
In-Depth Information
Controlling the relay remotely
Now, we are going to build our first interesting application using the system we just as-
sembled. We are going to build an Arduino sketch to control the relay from anywhere with-
in your local network. For example, if your computer is connected via Wi-Fi to your router
and the Ethernet shield is connected to the same router, you will be able to control the relay
via your computer. The advantage of this approach in this section is that even if your Inter-
net connection is down, you will still be able to control the relay.
The application starts by including the correct libraries:
#include <SPI.h>
#include <Ethernet.h>
#include <aREST.h>
We set up the MAC address of the board:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40 };
We also define a default IP address for the board that will be used if DHCP fails:
IPAddress ip(192,168,1,150);
We then create an instance of the aREST library, which will handle the request that comes
to the board:
aREST rest = aREST();
You also need to create an instance of the Ethernet server:
EthernetServer server(80);
In the setup() function of the sketch, we get an IP address using DHCP, as shown in the
following code snippet:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
Search WWH ::




Custom Search