2
I’m having some questions as a beginner and I wanted to help you guys out. I created a Home Automation, based on Microcontroller + Node.js + Angular.js.
Angular.js communication with the microcontroller is OK. However, I will also do the reverse. In which the MIC will send a POST command to Node.js, more or less with this parameter:
router.post('/Saidas', function(req,res) {
saida = req.body.atuador;
estado = req.body.estado;
final = saida + estado;
res.send('Enviado = Saida: ' + saida + '; Estado: ' + estado);
});
And later I will send to the site in HTML, via Angular.js with this command:
router.get('/SaidasSite', function(req,res){
res.send(final);
});
**Above is in the.js code paths to Node.js.
However, in my Angular.js, I made the following code:
<script>
var app = angular.module('Saidas',[]);
app.controller('Status', function($scope, $http, $interval) {
$interval(function(){
var request = $http.get('/SaidasSite');
request.success(function(data) {
if (data='QTD1LEDON') {
$('#QTD1LAMP').bootstrapToggle('on');
} else (data='QTDLEDOFF'){
$('#QTD1LAMP').bootstrapToggle('off');
} else (data='QTD2LEDON'){
$('#QTD2LAMP').bootstrapToggle('on')
} else { $('#QTD1LAMP').bootstrapToggle('on'); }
})
.error(function(data){
console.log('Error: ' + data);
});
}, 5000);
/* O segundo parâmetro "5000", diz que a função deve ser repetida a cada
5000 milisegundos (5 segundos) */
});
</script>
My idea is as follows: When I send the "QTD1LEDON" command to the POST/Output command, it sends the command to Node.js and later the site will receive the GET command with this value (GET/Saidassite) and trigger a button according to the string you received.
Ex: QTD1LED ON button triggered -> QTDLEDON -> Active Button "Toogle Bootstrap".
I’m not finding the best way to do this. This process is not working. In this code, it always triggers the "QTD1LEDON".
Would there be a better way to fix this code or deploy another process? Thank you!