3.6 Adding an Ethernet Shield
Attach the Ethernet shield to the Arduino by lining up the base pins so that the Ethernet jack is on top and facing the same direction as the Arduino USB jack. Reconnect the wires to the 5V and analog-in 0 (A0) pins found on the Ethernet shield just like you did when these wires were connected to the Arduino.
Do the same for the 10k ohm resistor bridging across the ground (Gnd) and A0 pins. Run your test again and check the values. In my tests, the base value being read was different compared to the Arduino without the Ethernet shield, and yours will likely reflect similar results. Since we’re more interested in the deviation from this base value than the calibration of the actual value itself, it’s important to use the unbent resistor value in the code and then determine how far of a plus or minus deflection from this base value is acceptable before transmitting the alert.
Now that our hardware is network-enabled, we can add the necessary code to our sketch that transmits the flex sensor status to our PHP server.
Coding the Shield
We will programmatically send data via the Ethernet
shield. But we first must include a reference in the sketch to both
the Arduino Ethernet library and its dependency, the Serial
Peripheral Interface (SPI) library.[26] These two libraries contain the
code needed to initialize the Ethernet shield and allow us to
initialize it with network configuration details. Both libraries
are included in the Arduino IDE installation, so the only thing we
need to do is import the SPI.h
and
Ethernet.h
libraries via the
#include
statement. Add these statements
at the beginning of the sketch:
WaterLevelNotifier/WaterLevelNotifier.pde | |
#include
<SPI.h> |
|
#include
<Ethernet.h> |
With the Ethernet library dependency satisfied, we can assign a unique Media Access Control (MAC) and IP address to the shield. While DHCP libraries are available from the Arduino community, it’s easier just to set the shield with a static IP address.
For example, if your home network uses a 192.168.1.1 gateway address, set the address of the shield to a high IP address like 192.168.1.230. If you plan on using this address as a persistent static IP, refer to your home router’s documentation on how to set a static IP range within a DHCP-served network.
WaterLevelNotifier/WaterLevelNotifier.pde | |
// configure the
Ethernet Shield parameters |
|
byte
MAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; |
|
|
|
// replace this
shield IP address with one that resides within |
|
// your own network
range |
|
byte
IPADDR[] = { 192, 168, 1, 230 }; |
|
|
|
// replace with your
gateway/router address |
|
byte
GATEWAY[] = { 192, 168, 1, 1 }; |
|
|
|
// replace with your
subnet address |
|
byte
SUBNET[] = { 255, 255, 255, 0 }; |
|
|
|
// replace this
server IP address with that of your PHP server |
|
byte
PHPSVR[] = {???, ???, ???, ???}; |
|
|
|
// initialize a
Client object and assign it to your PHP server's |
|
// IP address
connecting over the standard HTTP port 80 |
|
Client
client(PHPSVR, 80); |
Assign constants for the static MAC and IP addresses
that will be used by the Ethernet shield. Add the address of your
Internet router to the GATEWAY value, and add your SUBNET value as
well (most home network subnets are 255.255.255.0). The IP address
of your PHP server also has to be declared prior to the sketch’s
setup
routine.
With the constants declared, we can now properly
initialize the Ethernet shield in the setup
section of the sketch.
WaterLevelNotifier/WaterLevelNotifier.pde | |
void
setup() |
|
{ |
|
// for serial window
debugging |
|
Serial.begin(9600); |
|
|
|
// set up on board led on digital pin 13 for
output |
|
pinMode(ONBOARD_LED, OUTPUT); |
|
|
|
// Initialize Ethernet Shield with defined MAC
and IP address |
|
Ethernet.begin(MAC, IPADDR, GATEWAY, SUBNET); |
|
// Wait for Ethernet shield to
initialize |
|
delay(1000); |
|
} |
Note the use of the Ethernet object in Ethernet.begin(MAC, IPADDR, GATEWAY, SUBNET);
. This
is where the Ethernet shield gets initialized with the assigned
Media Access Control (MAC) address and IP Address.
OK, we have a working network connection. Now we can move on to the next step of requesting the appropriate emailer page on your PHP server when the bend thresholds have been exceeded.
Sending a Message
Up to this point, we have told the Arduino to report
the analog values being generated by the flex resistor, initialized
the Ethernet shield to connect the Arduino to our network, and
added stubs for routines to call out to our PHP server script. Now
it’s time to add that routine. We’ll call it ContactWebServer
.
The ContactWebServer
routine will take the same two parameters we captured for the
SendWaterAlert
function, namely
band_value
and bend_state
. Add the ContactWebServer(bend_value, bend_state);
line at
the end of the SendWaterAlert
function,
since we will talk to the designated PHP web server address each
time the flex resistor state changes.
We’re almost done. We just have to write the body of
the ContactWebServer
function. This will
consist of connecting to the PHP web server and printing the
well-formed HTTP GET string to the server. The string will contain
and pass the values of the bend_state
and bend_value
variables. These will
then be parsed on the server side and the PHP function will respond
in kind.
WaterLevelNotifier/WaterLevelNotifier.pde | |
void
ContactWebServer(int bend_value,
int bend_state) |
|
{ |
|
Serial.println("Connecting to the web server to
send alert..."); |
|
|
|
if (client.connect()) |
|
{ |
|
Serial.println("Connected to PHP
server"); |
|
// Make an HTTP request: |
|
client.print("GET
/wateralert.php?alert="); |
|
client.print(bend_state); |
|
client.print("&flex="); |
|
client.print(bend_value); |
|
client.println(" HTTP/1.0"); |
|
client.println(); |
|
client.stop(); |
|
} |
|
else |
|
{ |
|
Serial.println("Failed to connect to the web
server"); |
|
} |
|
} |
It’s time to test the completed sketch. Download it to the Arduino, open up a serial monitor window, bend the flex resistor, and watch the messages. Check your recipient’s inbox for the corresponding email messages. Did you receive the “Water Level Alert” and “Water Level OK” email messages that correspond to the notifications you saw in the serial monitor window? If not, make sure that your Arduino is connected to your home network by pinging the IP address you assigned.
Test the PHP email URL and verify that you receive
an email when you enter http://MYPHPSERVER/wateralert.php?alert=1&flex=486
into your web browser. When everything works as expected, we will
be ready to put the finishing touches on this project and make it
fully operational.