Introduction: You've Got Real Mail. Letterbox, Raspberry Pi, Particle Pi & IfTTT

So What's This About?

Imagine you have ordered a package from Amazon or eBay. You have been waiting for it to arrive but because it's coming by post there is no definite time it will arrive. It's all down to the Postman and their round. You have to just wait, impatiently. Now imagine you pop out and bump into a friend. You want to get back to get the post as soon as it arrives. If you knew that the post hadn't arrived yet you could stay out. Only returning once the post had arrived for sure. How could you know that?

Particle

Particle.io recently sent me confirmation that I'd been accepted on their Raspberry Pi Beta program. I'd signed up to it hoping that I might learn something about IoT, the Internet of Things!

So the first thing I did was follow the instructions on how to set up and played with their samples. I'd never written in an Arduino IDE before, which is how the Particle.io works. So I was a little lost. I've some Python experience and Scratch so I wasn't a complete and utter newbie. But almost.

Having played around for a few hours I thought about what can I possibly put on the Internet of Things from my house. I looked at a temperature sensor I have from CamJam for the Raspberry Pi but found that too complicated. (Still haven't figured it out how to code for it with the IDE). The I thought about a switch of some sort. Simple to read. A nice easy start. But where to put it?

The letterbox!

So the idea is this. The Postman pushes the mail through the letterbox. In doing so the flap lifts slightly and a small mercury tilt switch connects and closes a circuit (or opens it in fact, the coding just needs to be in reverse). Attached to the switch is a Raspberry Pi. This sends a message to it's Particle.io console. IFTTT (If This Then That) is reading the console and if it sees the correct message it then sends me an email via my Gmail account to my phone telling me the post has arrived.

Step 1: What You Need

  • A basic Raspberry Pi setup consisting of:

Raspberry Pi 3 ( or a Pi 2 with internet access). Also an 8Gb Micro SD Card and a 2 amp power supply.

OR

A Particle Pi Starter kit which automatically has access to the Particle Beta Program.

  • A mouse, keyboard and screen to help in setting up the Pi.
  • A small tilt mercury switch.
  • An LED.
  • 2 resistors. (I had a 330 ohm but it would probably work with a small one).
  • Some small core wire that will be unobtrusive attached to your letterbox.
  • A small breadboard to wire everything up. (But this could be lost and everything soldered and wired together once it works).
  • Some male to male and female to male wires for use with the breadboard.
  • Another computer to view the Particle website to program your Raspberry Pi.
  • An account with Particle.io (currently on invitation but apply and hopefully you won't have to wait as long as I did).
  • An account with IfTTT.

Step 2: The Raspberry Pi

Raspberry Pi

I'd suggest a new install of the operating system for the Raspberry Pi. Not essential but from now on every time you switch on the Pi it will become part of the Particle Cloud unless to uninstall the software. So maybe having an SD card just for the Particle Cloud might be a good idea.

Instructions for installing the operating system Raspbian Jessie are everywhere but I'd get them from the RaspberryPi.org website. The downloads are all there too. Just follow their instructions. I did have the Pi already connected to the Wifi so using the Particle instructions for this were not needed. If you can view a webpage on the Pi then you are ready to go.

I also had the VNC server activated in the Raspberry Pi configuration. The beauty about this is that running the VNC viewer from another computer or from the VNC app on my phone I can properly shut down the Pi when I wish.

Step 3: Particle Cloud

Particle

Particle have all kinds of interesting things on their website. But for this it is the Raspberry Pi Beta we are interested in. You'll need to sign up for it and once you get a confirmation email that you have been accepted you are good to go. It did take a few months before I got my email but hopefully they will be getting quicker. The Raspberry Pi and Particle is still in Beta so things may not work 100% but I've not found any difficulties yet. But nothing I'm doing is very advanced!

Once the you have an SD card loaded with the Raspberry Pi operating system, Pixel, then hopefully you have the Pi up and running. Get it connected to the internet by Wifi or ethernet. Then the instructions on the Particle website will guide you through installing their firmware. This is just one small line typed into the terminal. And everything is downloaded and installed. You will be asked to sign in to your account on the Particle website (on your other computer not the Pi) and the Pi will complete set up and log in itself.

The Particle firmware (as they call it) can be installed using a Terminal on the Pi. It is just one line of code. Their install page even has a video showing you how it should look. Installing from a terminal is the easiest way if you do not know how to use SSH. Particle do explain how to do this if you wish to, so I won't repeat it here. SSH is not needed later anyway.

This will turn your Pi into part of the Internet of Things. It will automatically connect to Particle on boot and run the Tinker script. If you download the Particle app then you can now tinker with the Pi using the Tinker script that is now running. With this you can read and write to the GPIO pins from your mobile. This requires no setting up at all, nothing, it just works! Connect an LED to ground via a resistor and Pin 12, tap the D9 pin on the app and the LED lights. Amazing!

At this point the Pi no longer needs the mouse, keyboard and screen. So you can shut it down and remove these.

Step 4: Connect the Sensor

So for this first use of Particle I needed to get some tilt switches. These are very small mercury filled bulbs. When tilted one way the switch is closed the other open. When attached to the back of the letterbox flap they will be permanently closed and as the flap lifts as the post is delivered the switch will open. So the code reflects this, monitoring when the switch opens and sending the signal onwards.

It is a very simple circuit to build. The longest part was soldering the tilt switch to the wire and then soldering some legs onto the other end to make it easy to plug into the breadboard.

The image should show everything you need to rebuild it. Switch you Pi off at this point and once built you can restart it.

Pin Connections.

One side of the tilt switch goes to the 5v Pin 2 (GPIO18 and D9 Particle) of the Pi. This is the red wire in the image.

Other side of switch via a resistor goes to Pin 6 of the Pi which is Ground. This is the green wire in the image.

Also from the same side of the switch a lead goes to Pin 12 (GPIO18 or D9 Particle). This is the blue wire in the image.

The LED, which only acts as an indicator and is not essential to the build, connect the short leg via a resistor to the Ground (green wire again) and the long leg to Pin 11 (GPIO17 or D1 Particle) is the yellow wire.

Step 5: Time to Code Particle

So now the Pi has all the wires attached and has been restarted. It should now be live on Particle and ready to program.

Head to the IDE on Particle. The image here shows the actual code for the project. So copy this into the IDE. Remeber this is my first Arduino style programming so it may not be up to too much critique. But it works.

The first two lines assign the two GPIO to the Particle numbering. The I set the tilt switch condition to high as the switch will be closed in the letterbox flap being downwards when closed.

In setup we let letterbox be an input from the switch and the led an output to light it up.

Then to start things off when the script first runs, just to show me it has started, the led lights up for a second (delay(1000)). The the Pi sends a message to Particle to show it's ready in the Particle console. (Though this hasn't always appeared if the Pi doesn't get online fast enough.

The rest is the main code which reads the switch. If it goes LOW (or open) a message is sent to Particle Consol then there is a short delay to stop multiple messages being sent and the LED lights up too just to show that the switch has been opened. This is useful when trying to obtain the best tilt for the switch when installing it.

int letterBox = D9;     // connect leg of tilt sensor to GPIO18 - Pin 12
int led = D1;           // connect LED to GPIO17 - Pin 11
int buttonState = HIGH;    // initialise start conditions of switch, HIGH is off
void setup() {
  pinMode(letterBox, INPUT);
  pinMode(led, OUTPUT);
    
    // flash led to confirm all working
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  Particle.publish("Ready for mail");
}
void loop() {
  // read the state of the pushbutton value
  buttonState = digitalRead(letterBox);
  if (buttonState == LOW) {
    // turn LED on
    digitalWrite(led, HIGH);
    // publish to console
    Particle.publish("LetterBox", "You have mail");
    delay(5000);
    digitalWrite(led, LOW);
  } 

  else {
    // turn LED off
    digitalWrite(led, LOW);
  }
}

Once all copied in click the arrow to the top left to get your code checked. If all is well then click the flash button above the tick and the code will be flashed to the Pi. And that is it! The code will start quite quickly which can be confirmed by the LED lighting up for a second. By going to the Particle Console the messages saying that You have mail can be viewed. So now we have to organise the message to be sent to tell you you have mail.

Step 6: If This Then That

The final part of the puzzle is to get a message sent to you. For that we use If This Then That. Sign up for an account if you don't have one. Then to start creating an app click the arow next to your user name when signed in.

Then, it's not so obvious, click the big + next to This on the page which says If + this then that. Then you get a Choose a Service on the next page. We only need a Particle one so just start to type that in the search box and then click it.

You should now get a request to allow IFTTT to access your Particle account. Click the appropriate parts. On the next page, Choose Trigger, click the last option New Event Published. Now comes the part where we tell IFTTT what to listen for.

The first field has "Letterbox", the second "You have mail", just as in the code. Your Pi should be in the last box already or if you have more than one Particle device choose the Pi. Now click Create App and you return to the main page but the + is now the Particle.

Next click on the + again next to this. I chose Gmail to notify me. Search for it and it will appear. Click Gmail, then select action (there is only one option) and the email form appears. Fill these in as the message that you would like to have when the post arrives together with your Gmail address. I just took out the word New from the message pane and left it at that. Then click Create Action. Click Finish on the next page and you are all done. It may take an hour or so to properly work but you are now up and running.

Finale

I hope you like this Instructable. It is my first so may be missing something but I'll try and correct it in light of any feedback. I was surprised how easily everything fits together. In fact the only problem I've had is not getting a trigger when the door opens or closes. But it does work.

Comments

author
Swansong (author)2017-02-02

I wish I had one of these! It drives me crazy never knowing when they're going to come.