From 8b7b8c9a524fa205aea1fb752bded27aa13075a4 Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Wed, 13 Jan 2016 09:34:47 +0100 Subject: [PATCH] clean up frontend to arduino communication --- arduino/src/sketch.ino | 17 ++++++++++------- nodejs/public/javascripts/angularApp.js | 4 ++-- nodejs/routes/index.js | 5 ++--- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/arduino/src/sketch.ino b/arduino/src/sketch.ino index bc4c4d3..2350d08 100644 --- a/arduino/src/sketch.ino +++ b/arduino/src/sketch.ino @@ -1,9 +1,10 @@ #include +RCSwitch mySwitch = RCSwitch(); + +int mode = 0; int incomingByte = 0; boolean on = false; -RCSwitch mySwitch = RCSwitch(); -int mode = 0; void setup() { @@ -15,10 +16,11 @@ void loop() { if(Serial.available()) { incomingByte = Serial.read(); - - switch(mode) - { + + switch (var) { + case 126: + if(on) { controlRCOutlets("0FFF0FFF0000"); Serial.println("Light is off."); @@ -28,12 +30,13 @@ void loop() Serial.println("Light is on."); on = true; } + + break; } - } } void controlRCOutlets(const char* sCodeWord) { mySwitch.sendTriState(sCodeWord); delay(1000); -} +} \ No newline at end of file diff --git a/nodejs/public/javascripts/angularApp.js b/nodejs/public/javascripts/angularApp.js index eb905ff..0b37b28 100644 --- a/nodejs/public/javascripts/angularApp.js +++ b/nodejs/public/javascripts/angularApp.js @@ -25,10 +25,10 @@ app.factory('arduino', ['$http', function($http, auth){ }]); app.controller('MainCtrl', ['$scope', 'arduino', function ($scope, arduino){ - $scope.test = 'Hello World!'; $scope.toggleLight = function(){ - + + arduino.toggleLight(); }; diff --git a/nodejs/routes/index.js b/nodejs/routes/index.js index 7daa819..fc06abe 100644 --- a/nodejs/routes/index.js +++ b/nodejs/routes/index.js @@ -10,10 +10,9 @@ router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); -app.get('/toggleLight', function (req, res) { +router.get('/toggleLight', function (req, res) { + serialPort.write(new Buffer('~','ascii')); res.send('switch light'); - serialport.write(new Buffer(0x7E)); }); - module.exports = router;