Tuesday, January 14, 2014

Electric Imp - Hello World Motion Sensor to Email

I recently decided to re-do a motion sensor project I had previously with an Arduino Board, but this time using the Electric Imp.  The Electric Imp looks just like an SD card, but don't be fooled - it won't work in an actual SD card slot. You need an April board or similar breakout for this little devil.

You might feel a bit nutty if you use one! That's because the Electric Imp - IDE  is through the Electric Imp cloud site with all of the code written in Squirrel. This is great for projects that you want to be Internet accessible.

This Electric Imp IDE makes this project easy!  The IDE runs entirely from their website and which also provides server side processing capability for your Imp projects. First you have to register the Electric Imp and set the network credentials using the blink up procedures found in the Getting Started Guide and Documents.

The Electric Imp Motion Sensor

I wanted to hook up my motion sensor to my Electric Imp an have it Email me when motion was detected.
I also wanted to hook up battery power so it would be wireless.

Here is the full parts list:

  1. Electric Imp ($29.95)
  2. Electric Imp April Board basic breakout (12.95)
  3. PIR Motion Sensor ( $10.99)
  4. Small Perma-Proto board I had on hand ($2.95)
  5. Some wire that I had on hand to make the connections (~$3.00) 
  6. (optional) Small Breadboard for prototyping I had on hand ($5.00)
  7. (optional) Lithium Ion Polymer Battery - 3.7v 2500mAh  ($14.95)
The total cost to make the project (not including the battery) is about $60 + taxes and shipping.

The PIR Motion sensor pin is connected to pin 1 of the Imp, configured for input. The other two pins on the motion sensor just provide power (connected to Vcc and ground). And that's really the whole circuit for the motion sensor.  It's important to use pin 1 for it's wake up feature. This let's the Imp sleep until pin 1 changes, so no looping! The Imp also has built in light detector capability and it can tell you it's voltage supply too. So I decided to have it email me all three when the motion detector is triggered. 

Here is the device code:
motion <- hardware.pin1;
function readPin(){  if(motion.read() == 1)  {     local current_light = hardware.lightlevel();     local voltage = hardware.voltage();          server.log("Motion Detected - Light Level:" + current_light + " - Voltage:" + voltage);     agent.send("motionDetected", "Motion Detected - Light Level:" + current_light + " - Voltage:" + voltage);  } }
motion.configure(DIGITAL_IN_WAKEUP, readPin); 

The line in the device code of the form:
      agent.send(agentMethodToCall, objectToPassToAgentMethod); 
tells the agent code on the server to call the specified method (in this case the motionDetected method).

I wanted my motion sensor to send an email message when motion is detected. On the Arduino I used the serial port to talk to a c# application on my computer that used SMP to send an email. However, I want to take advantage of the server side processing on the Imp for this feature.  This turned out to be very simply if you use a free emailing service such as MailGun. A very complete guide for writing the agent code to send mail using MailGun can be found Using MailGun with Electric Imp.