Top Banner
Home Automation Done completely with Javascript
15

Home automation with javascript

Jan 24, 2018

Download

Technology

Arnav Gupta
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Home automation with javascript

Home AutomationDone completely with Javascript

Page 2: Home automation with javascript

The BasicsComponents1.A controller/driver (Power Electronic Switch)2.Logic circuit for controller/driver3.Software interface for logic circuit4.Server-Client, or Socket based model (the API)5.User Interface

Page 3: Home automation with javascript

What we will useController : BeagleBone Black

A microcomputer board (like Raspberry Pi), with multiple pinouts, running on an ARM processor. Regular Debian/Ubuntu Linux OS. Language (HW control): BoneScript

A Node.js plugin developed especially for boards like BBB which provides a JS interface to turn current to the pins of a BBB on or off (or read the current for input PINS). BoneScript is similar to the Arduino language.

Page 4: Home automation with javascript

What we will useLanguage (API) : Socket.io or Node.js REST

If we directly access a webpage hosted on the BBB, we can use socket.io, or we can create a RESTful API on the BBB. Language (UI): AngularJS

We can use any MVC or pure JS too. This is the frontend for the given REST API on the BBB to control the states of the switches.

Page 5: Home automation with javascript

How switching works Power Electronics – Controlling Electrical

appliances with Electronics Common switches - BJT, MOSFET, TRIAC,

DIAC A 5V-1A cirtuit can control a 220V-15A circuit.

Page 6: Home automation with javascript

How switching works

Page 7: Home automation with javascript

BoneScriptSetting a PIN output to high current

var b = require('bonescript');b.pinMode('USR0', b.OUTPUT);b.digitalWrite('USR0', b.HIGH);

Page 8: Home automation with javascript

BoneScriptRead input current from a PIN

var b = require('bonescript');b.pinMode('P8_19', b.INPUT);b.digitalRead('P8_19', printStatus);function printStatus(x) {

console.log('x.value = ' + x.value);console.log('x.err = ' + x.err);

}

Page 9: Home automation with javascript

BoneScriptBlink a LED

var b = require('bonescript');var led = "USR3";var state = 0;

b.pinMode(led, 'out');toggleLED = function() {

state = state ? 0 : 1;b.digitalWrite(led, state);

};

timer = setInterval(toggleLED, 100);

stopTimer = function() {clearInterval(timer);

};

setTimeout(stopTimer, 3000);

Page 10: Home automation with javascript

Basic home automation with Bonescript

Connect LEDs on some of the PINS of the BBBCreate a webpage on the BBBMake buttons on the page. Each button calls a digitalWrite function corresponding to connected LEDs.On page load read PIN states and updates button accordingly. Red color for off, Green for on (or something similar).Connect the BBB to WLAN/Internet. Access IP of BBB from Laptop/Mobile, click the buttons and watch the LEDs go ON and OFF. *** Make a video, or show live demo of this if WiFi available ***

Page 11: Home automation with javascript

Why this isn’t practically applicable ?

HAVE TO open the particular webpage on the particular IP. Who remembers URLs and IPs ?No App for mobile, have to do via browser. Clients cannot be built for devices that do not have JS support.

Page 12: Home automation with javascript

Solution : REST ApiWe will create a simple REST API using Node.js and Express 4. var express = require('express');var app = express();

app.get('/switch/fan/:state', function(req, res) {if (req.params.state === 1)

// digitalWrite high to the required PINelse

// digitalWrite low to the required PIN});app.listen(8080);

Page 13: Home automation with javascript

Solution : REST ApiWe make REST calls from the client to turn the fan off or on http://bbb.ip/switch/fan/0 -- **to turn off the fan**

http://bbb.ip/switch/fan/1 -- ** to turn on the fan**

Now we can make whatever client we want (MVC client app, or Android/iOS app, or even a command line based noGUI app).

Page 14: Home automation with javascript

AngularJS frontend Create buttons with asynchronous calls to rest

API Responsive UI + Material/Bootstrap themes.

Such design, much awesome. Can also turn into Android/iOS clients using

Ionic etc.

Page 15: Home automation with javascript

GPIO/I2C run through General Purpose I/O – Kernel interface to

control currents over sysfs. Some info on why using C/C++ or shell to

actually control the currents is more efficient than Python/Node.