Switch on a Smart Switch using Google Cloud Function (Sonoff)

Nataraj
3 min readFeb 21, 2022

Below is the sample code to operate your Sonoff IoT devices from a cloud function i am sure that same can be done through lambda as well but here i am using Google Cloud Function

Some assumptions

  1. This is using ewelink api’s
  2. You have an account with ewelink
  3. Assuming that you have installed configured the devices (i have used this with basic,mini and powr3) devices.

Sample code to

  1. Get All Devices
  2. Return current temperature and humidity for specified device.
  3. Return current temperature for specified device
  4. Return logged user region.

const ewelink = require(‘ewelink-api’);

powdeviceid = “somedeviceidhere”;
(async () => {
const connection = new ewelink({
email: ‘accountusernamehere’,
password: ‘eWeLinkPasswordhere’,
region: ‘us’,
});

/* get all devices */
const devices = await connection.getDevices();
console.log(devices);
/* get specific devide info */
const device = await connection.getDevice(powdeviceid);
console.log(device);

const status = await connection.getDevicePowerState(powdeviceid);
console.log(status);

if (status.state == “off”) {
/* toggle device */
await connection.toggleDevice(powdeviceid);
}

const channels = await connection.getDeviceChannelCount(powdeviceid);
console.log(channels);

// multi-channel devices like Sonoff 4CH
const multichstatus = await connection.getDevicePowerState(powdeviceid, 1);
console.log(multichstatus);

const usage = await connection.getDevicePowerUsage(powdeviceid);
console.log(usage);

// Return current temperature and humidity for specified device.
const temphumd = await connection.getDeviceCurrentTH(powdeviceid);
console.log(temphumd);

//Return current temperature for specified device.
const temperature = await connection.getDeviceCurrentTemperature(powdeviceid);
console.log(temperature);

//Return current humidity for specified device.
const humidity = await connection.getDeviceCurrentHumidity(powdeviceid);
console.log(humidity);

//Return logged user region.
const region = await connection.getRegion();
console.log(region);

const firmware = await connection.getFirmwareVersion(powdeviceid);
console.log(firmware);

})();

Google Cloud Function

The above code was used in Google Cloud Function

package.json

{

"name": "sample-http",

"version": "0.0.1",

"dependencies": { "ewelink-api": "^3.1.1" }

}

index.js

/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.smartSwitch = (req, res) => {

/*
(async () => {

const connection = new ewelink({
email: ‘accountusernamehere’,
password: ‘eWeLinkPasswordhere’,
region: ‘us’,
});

const login = await connection.login();

const accessToken = login.at;
const apiKey = login.user.apikey
const region = login.region;

})();

(async () => {

const newConnection = new ewelink({
at: accessToken,
region: region
});

const devices = await newConnection.getDevices();
console.log(devices);
let message = req.query.message || req.body.message || ‘Getting the device details ‘ || devices ;
res.status(200).send(message);

})();

(async () => {

const anotherNewConnection = new ewelink({
at: accessToken,
region: region
});

await anotherNewConnection.toggleDevice(‘somedeviceidhere’);

})();

let message = req.query.message || req.body.message || ‘I have toggled the device’ ;
res.status(200).send(message);

*/

const ewelink = require(‘ewelink-api’);
powdeviceid = “somedeviceidhere”;
(async () => {
const connection = new ewelink({
email: ‘accountusernamehere’,
password: ‘eWeLinkPasswordhere’,
region: ‘us’,
});

/* get all devices */
const devices = await connection.getDevices();
console.log(devices);
/* get specific devide info */
const device = await connection.getDevice(powdeviceid);
console.log(device);

let message = req.query.message || req.body.message || ‘Getting the device details ‘ || devices ;
res.status(200).send(message);

const status = await connection.getDevicePowerState(powdeviceid);
console.log(status);

//if (status.state == “off”) {
/* toggle device */
await connection.toggleDevice(powdeviceid);
//}
})();
}

--

--