Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created August 18, 2020 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/352f186bf49fece4cb3dfb4cfae45cab to your computer and use it in GitHub Desktop.
Save justinyoo/352f186bf49fece4cb3dfb4cfae45cab to your computer and use it in GitHub Desktop.
Remote Controlling Home Appliances Using Raspberry PI and Power Platform
let lirc_node = require('lirc_node');
lirc_node.init();
const remote = (device, command) => {
lirc_node.irsend.send_once(device, command, () => {
console.log(command);
});
}
const remoteControl = {
onSwitchOn: (device) => {
remote(device, "SWITCH_ON");
},
onSwitchOff: (device) => {
remote(device, "SWITCH_OFF");
}
}
module.exports = remoteControl;
let express = require('express');
let remote = require('./remote');
let app = express();
app.get('/remote/switchon', function(req, res) {
let device = req.query.device;
remote.onSwitchOn(device);
let message = {
"message": device + " turned on"
}
res.send(message);
});
app.get('/remote/switchoff', function(req, res) {
let device = req.query.device;
remote.onSwitchOff(device);
let message = {
"message": device + " turned off"
}
res.send(message);
});
app.listen(4000, () => {
console.log("app started !!");
});
module.export = app;
{
...
"scripts": {
"start": "node app.js"
},
...
}
./ngrok http 4000
const axios = require('axios');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
let device = (req.query.device || (req.body && req.body.device));
let power = (req.query.power || (req.body && req.body.power));
let baseUri = process.env.REMOTE__BASE_URI;
let switchOnEndpoint = process.env.REMOTE__ENDPOINTS__SWITCHON;
let switchOffEndpoint = process.env.REMOTE__ENDPOINTS__SWITCHOFF;
let endpoint = power == 'on' ? switchOnEndpoint : switchOffEndpoint;
let requestUri = baseUri + endpoint + "?device=" + device;
let response = await axios.get(requestUri);
context.res = {
// status: 200, /* Defaults to 200 */
body: response.data
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment