The TP-Link HS100 has a nifty little library that allows devices on the same network to control them using a tiny amount of code. This article assumes you know how to setup a raspberry pi, and that you have access to its linux terminal, either through SSH or locally via a keyboard and mouse.
The first step is to make sure your raspberry pi is setup on the same network as the outlet itself. Then, login to your router to discover the TP-Link outlet IP addresses.
It can be difficult to tell which device is the outlet. I figured it out by using the “Kasa” app, and looking at the MAC addresses of the outlets here:
Note the MAC address then login to your router, and find the device with the matching MAC. Note its IP address.
Now to control the device, all you need to do is install NodeJS.
Installing NodeJS:
See my other article for that… then come back.
Installing NodeJS on a Raspberry PI
Installing the hs100-api library…
Then install the library: hs100-api
git clone https://github.com/plasticrake/hs100-api.git
Then… since you know the IP address, this is all the NodeJs code you need!
const Hs100Api = require('./hs100-api'); const client = new Hs100Api.Client(); const lightplug = client.getPlug({host: 'YOUR PLUG IP HERE'}); lightplug.getInfo().then(console.log); // Makes sure the plug is found. var last = true; setInterval(() => { last = !last; lightplug.setPowerState(last); }, 1000);
If everything goes well – that light, the one plugged into the tp-link, should be blinking on and off!
These tp-links can be hard to find at Christmas though – so check out the same program for the Wemo Outlets!
Now, you’ll want to keep that blink program running forever! So check out – running processes forever on your raspberry pi!
i want to just be able to tunr it off and on via script not cycle it. so i removed some of your code. my code looks like this:
const Hs100Api = require(‘./hs100-api’);
const client = new Hs100Api.Client();
const lightplug = client.getPlug({host: ”});
lightplug.setPowerState(true);
The issue i have is that it doesn’t exit. i do a node powerOn.js and i have to hit ^c to get back to the prompt. is there a way to tell it to just return to the prompt once the power is cycled on.
LikeLiked by 1 person
The reason it doesn’t exit is due to the event-based nature of this library. To exit on setting the light value, listen for the event and exit the process:
const Hs100Api = require(‘./hs100-api’);
const client = new Hs100Api.Client();
const lightplug = client.getPlug({host: ”});
lightplug.getInfo().then(console.log);
lightplug.setPowerState(true);
lightplug.on(‘power-on’,() => { process.exit(); });
Hope that helps!
LikeLike
Great article. Could I use something similar to power cycle my WiFi router if my internet goes down?
LikeLike
Thanks! If the router is how you talk to your PI ( same wifi ) you might have trouble since it’s the device passing traffic to the PI and outlet.
This may still be beneficial to you though as it could be just the outside connection being difficult. If it is – then yes!
There is another thing you can do too. Checkout my article on digitalLogger – that device doesn’t require wifi and still let’s you safely control wall power.
One idea – have the pi constantly checking the internet with a ping against 8.8.8.8 – if it ever can’t reach – cycle the internet.
Another idea – make your wifi, provide it’s OWN access point (it won’t have internet access – but it will provide local device comms). Then – when your main one goes down – simply connect to the “utility wifi” (your pi) and use *that* to control the outlet. I have an article that might help for that called MacSplat. But basically you want the pi to be a Wifi AP.
As an aside – I created a speach recognition project that controlled the wifi to force the brothers off of their games at bed times or meals. If they heard me shout “Expeliarmus!!!” They knew they were booted.
It’s all fun – enjoy your pi! It’s an amazing little computer.
LikeLike