Introduction: Push to Data.sparkfun.com From an Intel Edison With Node.js
The Intel Edison is really great at doing a lot of things, but one of the things that I was most excited about was its ability to use Node.JS to interact with objects IRL. As a front-end dev and a maker, I'm just trying to keep Atwood's Law true, haha!
The Intel Edison comes with wi-fi ready to roll after minimal configuration, and it's really easy to add Node.js to the platform with Intel's Yocto image. Once we get that up and running, I'm going to show you how to use Node to push data to a stream on data.sparkfun.com.
Step 1: What We're Actually Building
I wanted to keep this Instructable pretty general because of the usefulness of the topic, but we do need something to work towards. I decided to use the Grove Sound Sensor to monitor the sound in my apartment. That big lug of a dog is King - don't let that picture fool you, one of his favorite pastimes is barking at things. I wanted to make something that could set a threshold for the noise, and then help me track his barking patterns to see what he's up to while we're out of the house.
Step 2: Configure Your Edison and Load the Yocto Image
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 3: Hook Up Your Sensor
At this point in the build, you'll need to hook up whatever kind of sensor you're using. I was fortunate enough to receive a Grove Starter Kit from Intel, so I'll be talking about my experience with hooking that up. First pro-tip is that the connector shield is located on the back of the red anti-static foam, under the LCD screen. I may or may not have made it all the way to ordering a connector shield on Amazon before errantly knocking the starter kit case over and realizing that there was a shield already in the kit. It was early, I hadn't made coffee yet.
I 3D printed a case for my Edison using this file. The case uses the screws for the struts to secure the lid, and has two slots for the shield pins to go through. I did have to drill an extra hole in the lid for the ISP pins, but other than that everything fits great.
Once you have your shield attached, grab a connector and attach your sound sensor to the pin marked "A0" (analog 0). Once that is securely connected, go ahead and power up your Edison.
It should be noted that while the Grove Starter Kit is a nice to have, you don't really need to worry about not having it. Get a sensor, solder some wires on, and hook it up straight to the pins on the Arduino - it's all the same thing.
Step 4: Create a Feed on Data.sparkfun.com
Before we start writing the code, we're going to need some place to put all this tasty, tasty data that we'll be collecting. I decided to use data.sparkfun for its ease of use. You'll need to create a feed - follow this link to do that.
While you're setting up your feed, you need to decide what it is that you'll be tracking. You add these items in the fields section. For my build, all I need to track is the level of noise - Sparkfun automatically adds a timestamp for you.
Once you've created your feed, keep the window open so you have easy access to your public and private keys handy - you'll need those in the next step.
Step 5: Set Up Your Dev Environment
Now that your sensor is plugged in and your Edison is powered on again, run "bloop c" from Terminal on your computer to SSH into your Edison. Once you're in, make a new directory for your project in your root folder. Grab the contents of my package.json file, and then run "npm install" from the root directory. This will install Forever, Moment, Moment Timezone, and Request, as well as any dependencies they have.
Step 6: Write Your Code
Here's a link to the code on Github. You can certainly just git clone this onto your Edison, but I think it's worthwhile to type the code out yourself so that you're actually looking at what it does. The JS file is commented pretty thoroughly, but here's a quick pseudo-code rundown of what it's doing:
- Include the MRAA, Request, and Moment libraries
- Declare your sensor
- Declare your sound threshold
- Loop a function that takes a reading from the sound sensor and checks it it's above the threshold
- If the sound is above the threshold, set the current time, then make a call to the data.sparkfun URL
When you look at the code, you'll need to find the spot that says "[YOUR PRIVATE KEY]" and "[YOUR STREAM ID]" and replace those with the info that you got from Sparkfun when you set up your stream.
Step 7: Run Your Code
Once everything is loaded and configured, navigate to the root folder of your project, and run "node whateverYouNamedYourFile.js". If you left the console.logs in, you'll see the data start to come through the terminal. Adjust your threshold to your liking, then make a loud noise and go check to see that it updated on Sparkfun. If you see the data on Sparkfun's website after a refresh, you got it right. If you don't see the new data coming in, double check that you entered your stream's ID, private key, and data fields correctly. Also take a look at the console output after your run node on your file - the logs there may help your debug. Happy hacking!