Doorbells are so old-fashioned. Who wants to rely on user input to tell you when someone is at your front door when you can use a microcontroller to tell you? In this project, we'll be building a pressure sensor that lives under your doormat. When the pressure sensor registers an input over a certain weight, it sends you a text using the Intel Edison and the Twilio API.
Step 1 - Set Up Node on Your Edison
We'll be using Node.js to run our sketch. If you've been reading my other Instructables about the Edison, this step is going to be the same as all the others. If you've already got an Edison with the Yocto image, feel free to skip to the next step.
Full disclosure, I'm working on a Mac, so these instructions will skew that way. To get started, you should have a freshly-flashed Edison. After you board is flashed, you can try to find the IP address and enter all the additional commands, or you can just "npm install bloop" on the machine that you're trying to SSH in from. Bloop is a tool from Rex St. John, and it's an absolute lifesaver when you're working with the Edison. Instead of running "screen /dev/cu.usbserial-XXXXX 115200 -L", all you have to do is run "bloop c" in terminal and it will connect to the Edison it finds on your network. Once you're in, run "configure_edison --setup" to get your wi-fi and user creds defined.
While all this is happening, you can start downloading the Edison Yocto Image from this site. You want the link that says, "Edison Yocto complete image." Once downloaded, you'll need to load the files onto a micro SD card - you can read up on Yocto and how to get those files onto the SD card here. After you load the files, power down your Edison, insert the SD card, and the power it back up. To test your install is working, SSH into your Edison and type "node -v". If that returns the version of Node that you have installed you're good to go. If it says "Command not found," you're going to need to try loading Yocto onto the SD card again, because something went wrong.
Step 2 - Build Your Pressure Sensor
You can always buy a pressure sensor, but it's really easy to make a super-simple version and save yourself a couple of bucks. Hop on over to hiskeyd's Instructable - "How to Make a Ridiculously Cheap Analog Pressure Sensor" - and find out how to make a pressure sensor from a little bit of anti-static foam that a lot of electronic components come shipped in.
I tried using a sprayable version of the rubber coating for the outside of the foam, but it didn't really work. I was getting some weird readings when anything came in contact with the foam, so I put in ziploc bag to help isolate the reading to just the pressure.
Step 3 - Hook Up Your Sensor
This sensor really couldn't be any more simple. Pick one of the wires coming out of your sensor and denote that one as negative. Doesn't matter which one - we just need one to go to ground and one to go to an analog input. Plug your negative lead into ground, and plug your other wire into A0 (analog 0) on your Edison.
We're using the analog inputs instead of the digital inputs because we're going to be measuring resistance from the pressure sensor.
Step 4 - Write Your Code
Here's a link to the code on Github. I would highly recommend that you actually type out the code rather than just copying and pasting it in - you'll learn more that way.
To get started, if you aren't already SSH'd into your Edison, SSH in. Make a directory called "textingDoormat". CD into that directory, and then make a file named "textingDoormat.js". Run "npm install request" and then "npm install twilio". This will download the two external libraries that we'll be using to run our code.
My code on Github has a bunch of comments explaining what each line does. The gist of it is that you're going to tell the Edison to listen for a change in the values coming from the pressure sensor, and then if that change is above a certain threshold, you're going to send the text, wait ten seconds, and then start checking again.
We're sending the text using an awesome service called Twilio. Twilio lets us use an API to send messages through their service. You'll need to sign up for a trial with them in order to make this work - you can do that here. The trial lets you send thousands of texts before you have to decide if you want to pay for your calls, so you should be in the clear for this and many other projects. Once you get your keys, make sure to replace the sections in the code that say [ INSERT WHATEVER HERE ].
Every pressure sensor is going to be different, so you'll need to do a little bit of testing to find out what your threshold should be. On line 42, comment out the code that says, "console.log(guestPresent);". By letting this code run, you're going to log the input from the sensor every time the function runs. Comment out lines 52-62 to prevent Twilio from sending texts while you are testing.
Now you're ready to run your code for the first time. In your SSH window, type "node textingDoormat.js". You'll see a bunch of stuff go through the terminal, like the version of MRAA that you're using. Shortly thereafter, you should see the values start coming in from the sensor. Go ahead and step on your pressure sensor and see what the values go up to. You can determine what you need to set your threshold at based on this number. Once you have your threshold, edit line 45, the bit that checks if "guestPresent" is greater than the threshold. After that, comment out line 42 again, and then get ready to install your sensor.
Step 5 - Install Your Sensor and Try It Out!
Go ahead and slide your pressure sensor under your doormat - make sure to hide your Edison off to the side, and away from the elements. You may need to solder some longer wires on to your pressure sensor to make this work. Run your code again by typing "node textingDoormat.js" in your SSH window, and then have someone go out and stand on it. You should receive a text in under a minute. If your code doesn't work, take a look at the output in your terminal and see what the errors are telling you.