Created
August 18, 2020 03:31
-
-
Save justinyoo/352f186bf49fece4cb3dfb4cfae45cab to your computer and use it in GitHub Desktop.
Remote Controlling Home Appliances Using Raspberry PI and Power Platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
... | |
"scripts": { | |
"start": "node app.js" | |
}, | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./ngrok http 4000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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