Introduction: Arduino Powered Connected Mailbox for TfCD
This device will send you a notification when the mailman delivers you letters (or spam :p). This guide will show you step by step how the system was build and provide all the code to make it work.
We've used the following components:
- Arduino (Uno)
- WiFi shield (Sparkfun ESP8266)
- a WiFi network
- Some wires
- A web server with php
- A way to power the system (usb power brick, adapter, hamster on a wheel.. )
Step 1: Wire Up the Switch
The first step is to mount the switch next to the mailbox in such a way it is pressed when a letter is pushed though the mailflap. We've used a micro switch with is readily available with a long lever. If your lever has holes you could used some nails but hot-glue or ducktape should also work fine.
Step 2: Explaining the System
The Arduino runs a code that requests a web server to send a notification. The service used for the notification is Pushover which has an app for both the iPhone and Android.
The WiFi shield used for this project is incapable of establishing a secure HTTPS connection. Pushover however requires HTTPS for access to their system. An intermediate server is used to forward the request by the Arduino to Pushover. The Arduino requests a webpage over HTTP which in turn starts script that makes the actual request to Pushover.
Step 3: Setting Up Pushover
After installing the Pushover app and registering we need a couple of things to setup the intermediate server:
1. Log in to the website and save your User Key
2. Create a Pushover application by filling in this form. Only a name is required, don't worry about the rest.
3. Copy the API token/key from the next page.
Step 4: Deploying the Intermediate Server Script
Get the code for the intermediate server script here and open it in a text editor.
- Change APP_TOKEN and USER_KEY with the values saved in the previous step like shown below. Leave the " quotation marks in place around the values or you will break the script.
- Change the message to be shown in the app for a notification, we've used "You've got mail".
<?php curl_setopt_array($ch = curl_init(), array( CURLOPT_URL => "https://api.pushover.net/1/messages.json", CURLOPT_POSTFIELDS => array( "token" => "2AWLKJ3AKDJ323jj", "user" => "Awj32bla23JWW2", "message" => "You've got mail!", ), CURLOPT_SAFE_UPLOAD => true, )); curl_exec($ch); curl_close($ch); ?>
Save the script under an applicable name like sendNotification.php and upload it to your website. This website must be capable to run php scripts. There are plenty free options available if you don't have one.
Try out of the script works by going to http://yourwebsiteadress.com/sendNotification.php... If you received a notification on your phone, congrats, you're almost done!
Step 5: Program the Arduino
The Arduino must learn how to talk to the WiFi shield, to do this follow these steps to install the library for the ESP8266 shield.
Our device uses an customized script that comes with the library. Get the script here, save it on your computer and open it in the Arduino IDE program.
All the values that you have to change are at the top.
- Change the NETWORK-NAME to your wifi network name
- Change WIFI-PASSWORD to your wifi password
- Change mywebsite.com to your web site address where the intermediate script is
- Change http://mywebsite.com/sendNotification.php to where your script is
- Host: mywebsite.com to your website address
You are now ready to upload the script!
Step 6: Install Your Device
With the intermediate script setup and the program uploaded to your Arduino all that is left is to connect your switch to the 5V pin and pin 2 and fire it up. When you press the switch a notification should appear on your phone within a few seconds.
If there is no message on your phone open up the serial monitor in the Arduino IDE to see if there are any error messages. Sometimes it takes a few tries for the WiFi shield to connect successfully.