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.




4 comments:

  1. Hey! Just had one question how do you handle sending three different parameters in the function used with device.on on the agent side? I mean how do these parameters need to be passed in that function? And does the imp recognize each as a separate object?

    ReplyDelete
    Replies
    1. Hi,
      I haven't tried this but I believe you could pass in a single object. The object could be a table or an array containing multiple values.

      You could try populating a table variable like this:

      Device code:

      local current_light = hardware.lightlevel();
      local voltage = hardware.voltage();
      local a_table= {"first_key" : current_light, "second_key" : voltage, "third_key" : true};
      agent.send("motionDetected", a_table);


      AgentCode:

      function handleMotionDetected(a_table)
      {
      myThreeArgumentFunction(a_table.first_key, a_table.second_key, a_table.third_key);

      }

      device.on("motionDetected", handleMotionDetected);

      There is more information on how the tables work here:
      https://electricimp.com/docs/resources/squirrelcrib/



      Delete
    2. Thank you so much for posting this I have spend a few hours trying to get this input to some form of solid data. I tried using the sensor as a analog in and a digital in. I CANNOT get your code to work. I checked and rechecked and using the instructions you gave I cannot get my parallax PIR sensor to work with Electric imp. I know they updated there IDE maybe that has something to do with it. Any pointers?

      Delete
    3. HI David, thanks for your comment. I can confirm that this still works with the current API available on the Electric Imp site. I am still able to get email messages from the electric Imp motion sensor when motion is detected.


      Here is the full source code for both the Agent and Device. You'll need to verify that you put in your own email and mail gun credentials.

      https://github.com/codergirljp/Electric-Imp-Motion-Sensor

      If the problem is in code, hopefully the above will help you find it. The next thing to check is your circuit.

      Delete