3.5 Writing the Web Mailer

Libraries for sending email directly from the Arduino abound. But these all rely on a stand-alone, dedicated email server providing the mail gateway. So even though the mailer code can be compiled into the Arduino sketch, the solution still relies on an intermediary to send messages from the Arduino to the email inbox of the intended recipient(s).

If you have access to an SMTP mail server that you can connect to for outbound message transmission, check out Maik Schmidt’s Arduino: A Quick Start Guide [Sch11]. His book supplies the necessary code and walkthrough on how to make this work. If you don’t have access to a dedicated SMTP gateway, we can use an Internet web hosting service that supports sending email from a PHP script.

For this project, I have chosen a popular, preconfigured PHP-enabled web server with an SMTP outbound gateway, a configuration that popular website hosting companies like Dreamhost.net, Godaddy.com, and others offer to their customers.

The PHP script for sending email consists of only a few short lines of code. First, we will pass two parameters to the server: the type of alert to send and the recorded value of the flex resistor. Then we will compose a mail message containing the recipient’s email address, the subject, and the message contents. Then we will send the email.

WaterLevelNotifier/wateralert.php
  ​<?php​
  // Grab the type of alert to email and
  // the current value of the flex resistor.
  ​$alertvalue = $_GET["alert"];​
  ​$flexvalue = $_GET["flex"];​
  ​​
  ​$contact = 'your@emailaddress.com';​
  ​​
  if ($alertvalue == "1") {​
  ​ $subject = "Water Level Alert";​
  ​ $message = "The water level has deflected the flex
  resistor to a value of " . $flexvalue . ".";​
  ​ mail($contact, $subject, $message);​
  ​ echo("<p>Water Level Alert email sent.</p>");​
  ​ } elseif ($alertvalue == "0") {​
  ​ $subject = "Water Level OK";​
  ​ $message = "The water level is within acceptable levels.
  Flex resistor value is " . $flexvalue . ".";​
  ​ mail($contact, $subject, $message);​
  ​ echo("<p>Water Level OK email sent.</p>");​
  ​ }​
  ​​
  ​?>​

The script calls the built-in PHP mail function that passes three required parameters: recipient(s), subject, and the body of the email. Yes, it’s that simple.

Save the code to a file called wateralert.php in the root web directory of your PHP server. You can test the script by opening your web browser and visiting http://MYPHPSERVERNAME/wateralert.php?alert=1&flex=486. The page should return a Water Level Alert email sent. message in the browser window, and a corresponding email message should appear in the defined recipient’s inbox. If it doesn’t, check your PHP server settings and make sure that your web server is properly configured to use a working email gateway. If you’re still not having luck with the message test, contact your website hosting provider to make sure your hosted solution is correctly configured for PHP email messaging.

By abstracting the delivery mechanism from the logic running in the Arduino, we can easily modify the message recipients and contents.

Now that we have a working message gateway, we can hook up the Arduino to an Ethernet shield so the deflected flex resistor can talk to the rest of the world.

Programming Your Home
cover.xhtml
f_0000.html
f_0001.html
f_0002.html
f_0003.html
f_0004.html
f_0005.html
f_0006.html
f_0007.html
f_0008.html
f_0009.html
f_0010.html
f_0011.html
f_0012.html
f_0013.html
f_0014.html
f_0015.html
f_0016.html
f_0017.html
f_0018.html
f_0019.html
f_0020.html
f_0021.html
f_0022.html
f_0023.html
f_0024.html
f_0025.html
f_0026.html
f_0027.html
f_0028.html
f_0029.html
f_0030.html
f_0031.html
f_0032.html
f_0033.html
f_0034.html
f_0035.html
f_0036.html
f_0037.html
f_0038.html
f_0039.html
f_0040.html
f_0041.html
f_0042.html
f_0043.html
f_0044.html
f_0045.html
f_0046.html
f_0047.html
f_0048.html
f_0049.html
f_0050.html
f_0051.html
f_0052.html
f_0053.html
f_0054.html
f_0055.html
f_0056.html
f_0057.html
f_0058.html
f_0059.html
f_0060.html
f_0061.html
f_0062.html
f_0063.html
f_0064.html
f_0065.html
f_0066.html
f_0067.html
f_0068.html
f_0069.html
f_0070.html
f_0071.html
f_0072.html
f_0073.html
f_0074.html
f_0075.html
f_0076.html
f_0077.html
f_0078.html
f_0079.html
f_0080.html
f_0081.html
f_0082.html
f_0083.html
f_0084.html
f_0085.html
f_0086.html
f_0087.html
f_0088.html
f_0089.html
f_0090.html
f_0091.html
f_0092.html
f_0093.html
f_0094.html
f_0095.html
f_0096.html
f_0097.html
f_0098.html
f_0099.html
f_0100.html
f_0101.html
f_0102.html
f_0103.html
f_0104.html
f_0105.html
f_0106.html
f_0107.html
f_0108.html
f_0109.html
f_0110.html
f_0111.html
f_0112.html
f_0113.html
f_0114.html
f_0115.html
f_0116.html
f_0117.html
f_0118.html
f_0119.html
f_0120.html
f_0121.html
f_0122.html
f_0123.html
f_0124.html
f_0125.html