Home

Up: Smart Plug

Previous: Smart Plug Prototype 0.2

Smart Plug Prototype 0.3

The next phase of usefullness is turning the light off. In this next version, we keep track of whether the light is on or off, and toggle it upon receiving a TCP connection.

var b = require('bonescript');
var net = require('net');
var LEDS = [
  'USR0',
  'USR1',
  'USR2',
  'USR3'
];
var state = b.LOW;
LEDS.forEach(function (LED) {
  b.pinMode(LED, b.OUTPUT);
  b.digitalWrite(LED, state);
});
var server = net.createServer(function(connection) {
  console.log('Received connection');
  state = Number(!Boolean(state));
  LEDS.forEach(function (LED) {
    b.digitalWrite(LED, state);
  });
  connection.end();
});
server.listen(9999, function () {
  console.log('Ready for commands');
});

This works, but it's fairly crude. What would be better is defined protocol beyond just listening for connections.

Next: Smart Plug Prototype 0.4

Creative Commons License