Logging your energy consumption over time
For now, we built a smart plug that has more or less the same features as a commercial smart plug: it can control a device, measure the power consumption of this device, and also comes with a nice graphical interface. In this section, we are going to go further, and see how we can easily add functions to our project with some lines of code.
As an example, we are going to see how to log the measurements made by the board into a database on the Pi so that those measurements can be recalled later. As the code for this section is really similar to the previous section, I will only highlight the main changes here.
Start by importing the required module for the database:
var Datastore = require('nedb') db = new Datastore();
After that, we define a route to get all the data currently present inside the database:
app.get('/data', function (req, res) { db.find({}, function (err, docs) { res.json(docs); }); });
Inside the measurement loop, we create...