Electronics · raspberry pi

Raspberry PI for controlling TP-Link POWER

41yh0p1tkyl-_sl1000_

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:

screenshot_20161219-100830

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!

Advertisements

11 thoughts on “Raspberry PI for controlling TP-Link POWER

  1. 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.

    Liked by 1 person

    1. 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!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s