0

I am running a local website with NodeJS on my Pi and I want to control a lamp via an 433MHz sender connected to the Pi. I have already written a bash script which works perfectly to control the lamp but I can't find a way to execute it through the HTML site on the node server. So I am asking whether it is even possible because it doesn't really seem secure to me and how can I implement it in javascript as a function or only on the server side ?

3
  • It is possible to do it server-side, but not client-side, which I think is what you're asking by wanting to "execute it through the HTML site"... Commented Dec 15, 2019 at 20:50
  • i would take a look at socket.io or you could build a express api Commented Dec 15, 2019 at 21:38
  • Yes it is possible with ajax and it is secure if you make it secure. Commented Dec 15, 2019 at 21:57

1 Answer 1

1

there are many ways you could go about doing something like this but one way would be creating an api so your website can make api calls to the server to turn the light on and off

so here is a simple example

HTML

<html>
    <head>
            <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    </head>
    <body>
        <form>
            <input type="button" Value="on" onclick="turnOn()">
            <input type="button" value="off" onclick="turnOff()">
        </form>
    </body>
</html>
<script>
function turnOn(){
$.ajax({
    url: 'http://localhost:3000/api/light/on',
    type: "GET",
    dataType: "json",
    success: function (data) {
        console.log(data);
    },
    error: function (error) {
        console.log(`Error ${error}`);
    }
});
}

function turnOff(){
$.ajax({
    url: 'http://localhost:3000/api/light/off',
    type: "GET",
    dataType: "json",
    success: function (data) {
        console.log(data);
    },
    error: function (error) {
        console.log(`Error ${error}`);
    }
});
}

</script>

SERVER

//server.js
const app = require('express')();
const server = require('http').createServer(app);
var cors = require('cors');
const shell = require('shelljs')
var port = 3000;


server.listen(port, () => console.log(`API server running on  ${port}!`))
app.use(cors())

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html')
})

app.get('/api/light/:status', (req, res) => {
    var status = req.param('status')
    if(status == 'on'){
        shell.exec('./turn-light-on')
        res.json({success:"light turned on"})
    }else{
        shell.exec('./turn-light-off')
        res.json({success:"light turned off"})
    }
})

all you would have to do is change the file that shell.exec() calls for it to work

this app reqires

  • express
  • cors
  • shelljs

to be installed just npm install the names as listed above

Sign up to request clarification or add additional context in comments.

2 Comments

It works out perfectly thank you so much for your answer it was super helpful!
no problem man if this answered all of your questions could you mark it as the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.