Created
February 21, 2019 04:37
-
-
Save janakiramm/81071cea324581947d9c0dde99881dc7 to your computer and use it in GitHub Desktop.
Google Cloud Function to Control LiFX Smart Bulb
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
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