Make a water pump that can be turned on and off over HTTP using only JavaScript.
Warning Do not try this at home without the help of an electronics expert. Solid state relays switch high voltage and can cause bodily harm if used improperly.
I have been looking for a way to do hobby electronics projects using JavaScript running on microcontrollers. You can control an Arduino remotely from a laptop over USB with libraries like johnny-five, but you can't keep your code running after you close your laptop since the Arduino can't run JavaScript.
The Tessel is a microcontroller that runs Node code on the board. The team at Tessel wrote a JS to Lua compiler and then they run Lua directly on the Tessel hardware. It's not the fastest thing in the world, but it opens up most of npm to run on a wi-fi enabled, USB powered microcontroller the size of an Arduino.
For this project I used four components. It takes about an hour to put together:
I would also recommend using a junction box/electronics enclosure for housing the SSR along the lines of this one.
The key is the solid state relay. For background on how these works check out this excellent article, but the short version is that they let you use a small voltage (3.3V or 5V such as those available in "digital out" pins in microcontrollers) to turn on and off high voltage circuits such as 110V or 220V (wall outlets).
Instead of using an SSR directly as I did you could also purchase a PowerSwitch Tail, which is a pre-made, safely housed relay enclosure that comes to you already built.
To build the 'circuit', I simply cut the dimmer slider portion off of the Lamp Dimmer, stripped the ends off of the wires, and connected them to the SSR instead. Where there used to be a dimmer slider the circuit there is now the SSR.
The flow of electricity is roughly: Wall -> Dimmer -> SSR -> Pump. The tessel is connected to the SSR and can turn it on and off at will.
Again, I can't stress enough that you should really not try this on your own unless you have someone who is experienced with Solid State Relays and 110V/220V electricity.
Here's a video of the system in action:
The tessel is running a small HTTP server written in Node that turns the G3 GPIO pin on or off. When it is on it outputs 3.3V to the SSR, which tells the SSR to turn on voltage, which turns on the pump.
var tessel = require('tessel')
var http = require('http')
var pin = tessel.port['GPIO'].pin['G3']
var server = http.createServer(function (req, res) {
console.log(req.url)
if (req.url === '/on') return on(req, res)
if (req.url === '/off') return off(req, res)
return res.end('<a href="/on">/on</a> or <a href="/off">/off</a>\n')
})
server.listen(80, function(err) {
if (err) console.log('error!' + err)
console.log('http listening')
})
function on (req, res) {
console.log('turning on')
pin.write(1)
res.end('turned on\n')
}
function off (req, res) {
console.log('turning off')
pin.write(0)
res.end('turned off\n')
}
Save the above code as a file called server.js
, plug your Tessel over USB, and run tessel push server.js
to upload the server onto your Tessel. You can run tessel logs
to view the HTTP server logs while your tessel is connected over USB.
Once you have the Tessel configured to join your home Wi-Fi (using the tessel wifi
CLI command) it will then always automatically connect a few seconds after powering up, every time. You can put it anywhere in your house, power it over USB, and it will start up it's HTTP server and be accessible over your local network.
You could definitely use other microcontrollers instead of the Tessel for this. I like the Tessel, however, because it is the simplest way to run Node code on hardware, and they also make add-on hardware modules that are easy to use. For example, you could use the Raspberry Pi for this project, but it would be more complicated as you would have to configure Linux. I just prefer to keep things easy!
Happy hacking! Questions? Open an issue in this repository