Configuring the smart plug
We are now going to configure the Raspberry Pi so it behaves like a smart plug. As usual, we'll use Node.js to code the software that will control our Raspberry Pi Zero board.
We start by importing all the required modules for the project:
var mcpadc = require('mcp-spi-adc'); var express = require('express'); var app = express(); var piREST = require('pi-arest')(app);
Note that we are using the mcp-spi-adc
module here, which will allow us to easily read data from the MCP3008 chip.
Next, we define the channel to which the current sensor is connected:
var channel = 5;
We also set the value of the load resistance we are using for the sensor:
var resistance = 10;
This will allow us to calculate the actual current flowing through the sensor later on.
So far, you might have noticed that we don't measure the voltage in this project. Indeed, even if we could add another circuit to measure the voltage, we can simply set it in the code:
var voltage = 230; // Europe
Note that you will...