Weekend Project: Send sensor data from Arduino to MongoDB


Arduino is an open-source electronics platform that can acknowledge and interact with its environment through a variety of sensor types.  It's great for hardware prototyping and one-off projects.

I just got an Arduino Board from our friends at SendGrid, who also gave me a little tutorial in the art of Arduino hacking. Inspired by the tutorial and armed with this new  board, I bought a passive infared (PIR) motion sensor from my local Radio Shack. Now I was ready to play; in particular, I wanted to be able to collect that continuous stream of hardware sensor data into a MongoDB database for logging, trend analysis, system event correlation, etc.

To this end, I created the demo project "mongodb-motion", which I've made public on Github. In the "mongodb-motion" Github repo, you will find an Arudino project that writes motion sensor data to a cloud MongoDB database at MongoLab and sends alerts via email based on certain criteria. I built this demo using Node.js and the MongoLab REST API.

Below, I'll go through exactly what hardware you need to make your own "mongodb-motion" project a success, and how the code actually works.

What You Need

The hardware used in this demo includes: an Arduino UNO R3 and a Parallax PIR motion sensor.

How the Code Works

You can use a variety of motion sensors with the Arduino. In this particular experiment, I used a PIR motion sensor. The PIR motion sensor behaves like a switch, with 'down' events emitted on motion detection and 'up' events a few seconds after motion ceases to be detected.

On the receiving side, I used JohnnyFive, an appropriately named Node.js package that accepts sensor events and sends messages to the Arduino board.

With the two ends set, I'll move on to the project's configuration file. In this demo, I've included a configuration file, config-sample.js, where credentials for the MongoLab REST API and for the email SMTP server can be added. In my case, I used the SendGrid SMTP service.

The configuration file also has two callbacks that determine when an email is emitted, one for each type of event - "detect" and "ceased". I've used this feature to automatically send an email alert if an event timestamp is between 7:00pm and 8:00am, ostensibly when my office should be motionless... I'm out there watching you, office!

Once you've customized this config-sample.js file, be sure to rename it to config.js in order for it to be usable.

If you inspect the project code, you'll notice that the MongoLab REST API is called in the logMsg() function, using an https.request.

Building this little demo has given me some new ideas for hardware hacking the cloud. I hope you give it a try too.

Thanks to the Arduino, Node.js and Javascript communities, and special thanks to Rick Waldon for Johnny Five, SendGrid for the UNO board, and a big shout out to @swiftalphaone for the Waza tutorial.

About benwen

I'm MongoLab's VP of Sales and Marketing. And I'm here to serve our customers' needs for MongoDB hosting in the cloud.

15 Responses to Weekend Project: Send sensor data from Arduino to MongoDB

  1. Swathi 2013/04/04 at 7:12 am #

    This is super cool !

  2. Jenntrus 2017/03/14 at 10:40 am #

    Zac Brown Band is a famous country singer, so don’t miss the possibility to visit Zac Brown Band concerts 2017

  3. Brostrus 2017/03/21 at 8:33 pm #

    Tim McGraw is an acclaimed nation vocalist, so don’t miss the likelihood to visit Tim McGraw concert schedule 2017

  4. Antrus 2017/03/30 at 5:26 am #

    Jason Aldean is a famous country singer, so don’t miss the possibility to visit Jason Aldean concert schedule

  5. mca scam 2017/04/01 at 10:14 am #

    174900 345175This kind of lovely blog youve, glad I found it!?? 491451

  6. Best Best Online News in the World 2017/04/06 at 10:54 am #

    937392 161275Wonderful post, Im seeking forward to hear far more from you!! 620589

  7. Best News Website in World 2017/04/06 at 9:40 pm #

    559966 880291really excellent goodthis post deserves almost absolutely nothing hahaha merely joking: S nice write-up: P 501975

  8. Best Best Online News in the World 2017/04/07 at 5:50 am #

    914495 137588It is difficult to get knowledgeable men and women with this subject, but the truth is could be seen as do you know what you are referring to! Thanks 742544

  9. Best Best Online News in the World 2017/04/07 at 12:14 pm #

    398511 618320I besides believe therefore , perfectly composed post! . 529213

  10. Best Best Online News in the World 2017/04/07 at 1:39 pm #

    600304 871566I saw but yet another thing concerning this on yet another weblog. Youve certainly spent some time on this. Properly done! 358779

  11. 205893 276472U never get what u expect u only get what u inspect 282727

  12. Best Best Online News in the World 2017/04/08 at 9:35 am #

    698474 774579A persons Are usually Weight loss is certainly a practical and flexible an eating strategy method manufactured for those that suffer that want to weight loss and therefore ultimately conserve a a lot much more culture. weight loss 13593

  13. Best Best Online News in the World 2017/04/08 at 12:04 pm #

    314787 956603It is onerous to search out knowledgeable individuals on this subject, nonetheless you sound like you already know what you are talking about! Thanks 288137

  14. weight loss clinic 2017/05/05 at 1:11 pm #

    This piece of writing provides clear idea designed for the new
    users of blogging, that in fact how to do blogging
    and site-building.


  1. Speaking to Arduino with Email - 2013/11/04

    […] it detects motion within a certain time frame. You can find all the details and source code at the MongoLab blog. This implementation also logs data to a MongoDB database via the MongoLab REST […]

Leave a Reply