Intro
As an alternative to using PHP & MySQL, you can use Node.js with Deployd and MongoDB to store data retrieved from a connected home device. JavaScript normally runs on the client machine. With Node.js, you can run JavaScript code directly on your server allowing you to write directly to a database (DB) without PHP.
In this example, we’ll query a Spark Core every 10 minutes and store the temperature data in MongoDB. We can then use Deployd to create a simple API end point to use for requesting historical data. This can be used in combination with a mobile app or web interface to visualize historic temperature data.
Prerequisites
You need to have a basic understanding of Node.js and have a server (or localhost) set up with Node.js and npm installed.
Warning: Do not install Deployd to your public_html directory or put any server side JavaScript files in your public_html directory on a live server. Always put server-side JavaScript files at least one directory above your public_html folder to prevent others from being able to open and view the contents of your files. Unlike PHP, someone can navigate to your JavaScript file and view it’s contents. This is fine for client-side JavaScript but bad for server-side JavaScript that may contain API keys or DB credentials.
You will need to install the following libraries. Remember to use “-g” (global install) when installing via npm so you have access to the libraries in all directories.
- Node.js
- npm Node package manager
- Require.js
- Forever.js Allows your JS file to run ‘forever’
- node-cron Used in combination with Forever.js to run code at a set interval
- Photon Core with hardware and code from this example (or equivalent code)
Deployd
After installing and configuring Deployd, create a table called temperature with fields temperature, humidity and timestamp. Make sure that you are able to successfully read and write information to MongoDB using the Deployd API you just created.
Sample Code
Update the DEPLOYD_PORT, DB_PORT, DEVICE_ID and API_KEY to match your configuration. Put the code at least one folder above your public_html folder in a file called production.js.
// production.js var deployd = require('deployd'); // Interface to the database var request = require('request'); // Make requests to external server var CronJob = require('cron').CronJob; // Execute requests on interval // Set up deployd with mongodb information var server = deployd({ port: DEPLOYD_PORT, env: 'production', db: { host: 'localhost', port: DB_PORT, name: 'deployd' } }); // Start listening for requests server.listen(); server.on('listening', function() { // Store a reference to the Deployd temperature table var temperatureStore = server.createStore('temperature'); // Run every 10 minutes. Write the data to the Deployd table. new CronJob('00 */10 * * * *', function(){ // Make request to the Spark Core request("https://api.spark.io/v1/devices/DEVICE_ID/result?access_token=API_KEY", function(error, response, body) { // Access results as JSON data var result = JSON.parse(JSON.parse(body).result); // Convert the timestamp to seconds var timestamp = new Date().getTime() / 1000; // Write the data as a new row into the Deployd temperature table temperatureStore.insert({temperature: result.data2, humidity: result.data1, timestamp: timestamp}, function(err, result){ if(result) { // Success! } }); }); }, null, true, "America/Los_Angeles"); }); server.on('error', function(err) { console.error(err); process.nextTick(function() { // Give the server a chance to return an error process.exit(); }); });
Start your script with forever.js
Navigate to the folder you put the production.js file and run the following command.
forever start production.js
Conclusion
This code will run until your server is restarted. You can add a command to your server start up to ensure Forever.js starts up after a reboot.
Why Node.js? Because it’s fun! Rather than learning PHP, you can leverage your JavaScript knowledge to write server side code. I haven’t run any benchmarks myself, but from what I’ve read, your server hardware plays a much bigger role in performance than the software solution you use (MongoDB vs. MySQL). Use what you feel most comfortable with. Understanding all available solutions will help you select the best option given your specific project needs.