Skip to content

Instantly share code, notes, and snippets.

@janakiramm
Created February 21, 2019 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janakiramm/81071cea324581947d9c0dde99881dc7 to your computer and use it in GitHub Desktop.
Save janakiramm/81071cea324581947d9c0dde99881dc7 to your computer and use it in GitHub Desktop.
Google Cloud Function to Control LiFX Smart Bulb
exports.bulb = (event, callback) => {
const https = require('https')
// Access the payload
const msg = event.data;
const data = Buffer.from(msg.data, 'base64').toString();
const token = 'Bearer <put your LiFX API token here>'
console.log(data);
// Build the LiFX request
const options = {
hostname: 'api.lifx.com',
port: 443,
path: '/v1/lights/all/state',
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
'Authorization': token
}
}
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
process.stdout.write(d)
})
})
req.on('error', (error) => {
console.error(error)
})
req.write(data)
req.end()
console.log("Success!");
callback();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment