Code Monkey home page Code Monkey logo

hs100's Introduction

hs100

A tool for using TP-Link HS100/HS103/HS105/HS110/HS300 wi-fi smart plugs. You can turn them on and off, reboot them, and so on. You can even set them up without using TP-Link's app (see Initial Setup).

Tested to work on Linux, OSX, IRIX, and Windows under WSL.

Loosely based on pyHS100 and research from softScheck.

Usage

hs100 <ip> <command>

Commands:

  • associate <ssid> <key> <key_type>: set wifi AP to connect to. get your key_type by doing a scan
  • info: get device information including device IDs
  • emeter: realtime power consumption (works with HS110 and HS300)
  • factory-reset: reset the plug to factory settings
  • off: turn the power off
  • on: turn the power on
  • reboot: reboot the plug
  • scan: scan for nearby wifi APs (probably only 2.4 GHz ones)
  • set_server <url>: set cloud server to <url> instead of TP-Link's
  • Alternatively, you can supply a JSON string to be sent directly to the device. Note that the JSON string must be quoted, like so: hs100 <ip> '{"system":{"set_relay_state":{"state":1}}}'

Use with Outlet Strips (such as HS300)

The emeter, off and on commands can be used on the individual outlets of multiple outlet strips.

  • use the info command to get the ID string of the outlet you want to control
  • append the ID string to the command line for emeter, off or on like so: hs100 <ip> on 80062947BE0A1339DC7914B75B24A6A51FB6977E02

Initial Setup

According to TP-Link, initial setup of the plugs is performed by installing their "Kasa" app on your smartphone (free account required), and using its setup tool. This sucks and I do not recommend it. Instead, follow these alternative instructions.

You want to get the plug into the "blinking amber and blue" state, in which it will spin up its own AP and await commands. If you have a brand new plug, then it should do this automatically. Otherwise, hold down one of the buttons (depending on your model) for about 5 seconds, until its light blinks amber and blue.

You should see a wifi AP called "TP-Link_Smart Plug_XXXX" or similar. Connect to this AP. You will be given an IP of 192.168.0.100, with the plug at 192.168.0.1.

Issue the following commands to the plug:

  • Factory reset the plug to get rid of any settings from a previous owner: hs100 192.168.0.1 factory-reset. You will be disconnected from its wifi AP. Once the factory reset is done (usually a few seconds), reconnect to the plug's AP.
  • Disable cloud nonsense by setting a bogus server URL: hs100 192.168.0.1 set_server localhost
  • Scan for your wifi AP using hs100 192.168.0.1 scan. Find your AP in the list and note its key_type; you will need this to associate.
  • Associate with your AP using hs100 192.168.0.1 associate <ssid> <password> <key_type> . Your key_type is a number that indicates the kind of wifi security that your AP is using. You can find it by doing a wifi scan (see previous step).

If the light turns solid amber, then it was unable to associate-- factory reset the plug and try again. Otherwise, the light on your plug will change first to blinking blue, then to solid blue indicating that it has successfully connected to your AP.

Build

Ubuntu instructions You will need build-essential installed. Then run make. This will produce a hs100 binary which you can use. E.g. ./hs100 192.168.0.1 off

Todo

  • better error checking
  • plug discovery

This program is very basic. Patches welcome!

hs100's People

Contributors

atom-bomb avatar daemon32 avatar damionw avatar decke avatar jkbenaim avatar kushagharahi avatar mikeryan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hs100's Issues

howto typo

The last command:
Associate with your AP using hs100 192.168.0.1 <key_type> . Your key_type is a number that indicates the kind of wifi security that your AP is using. You can find it by doing a wifi scan (see previous step).
should be
Associate with your AP using hs100 192.168.0.1 associate <key_type> . Your key_type is a number that indicates the kind of wifi security that your AP is using. You can find it by doing a wifi scan (see previous step).

Great program by the way and thanks for sharing it

Windows version

Make a single .exe version of the tool for Windows systems

naming HS100

Hi

Just wanted to ask if it would be possible to add a function to name the HS100 and/or give it a fix IP?

Regards

Compilation error on struct addrinfo: comms.c:110:18: error: storage size of ‘hints’ isn’t known

It seems the compilation is broken on my machine since the introduction of #1, commit b246731

I can compile successfully. when I revert this commit.

Do I (or the README) miss something ?

$ make
cc -std=c99 -Os   -c -o hs100.o hs100.c
cc -std=c99 -Os   -c -o json-builder.o json-builder.c
cc -std=c99 -Os   -c -o comms.o comms.c
comms.c: In function ‘hs100_send’:
comms.c:110:18: error: storage size of ‘hints’ isn’t known
  struct addrinfo hints, *res;
                  ^~~~~
comms.c:122:19: error: ‘AI_NUMERICSERV’ undeclared (first use in this function)
  hints.ai_flags = AI_NUMERICSERV; // numeric port
                   ^~~~~~~~~~~~~~
comms.c:122:19: note: each undeclared identifier is reported only once for each function it appears in
comms.c:124:6: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
  if (getaddrinfo(servaddr, "9999", &hints, &res) != 0)
      ^~~~~~~~~~~
comms.c:127:19: error: dereferencing pointer to incomplete type ‘struct addrinfo’
  sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                   ^~
comms.c:148:2: warning: implicit declaration of function ‘freeaddrinfo’ [-Wimplicit-function-declaration]
  freeaddrinfo(res);
  ^~~~~~~~~~~~

With old commit:

$ git revert b246731
[master d357d61] Revert "resolve hostname using getaddrinfo"
 1 file changed, 15 insertions(+), 18 deletions(-)
$ make
cc -std=c99 -Os   -c -o comms.o comms.c
cc -std=c99 -Os   -c -o handlers.o handlers.c
cc -std=c99 -Os   -c -o json.o json.c
cc -lm  hs100.o json-builder.o comms.o handlers.o json.o   -o hs100

($?=0)

current_ma, voltage_mv and total_wh not internally consistent

I have a v1 version of the KP125 energy monitoring plug that seems to work fine with hs100 -- i.e., it turns on/off and the emeter functionality responds.

However, some of the emeter numbers are not consistent with the Kasa app.
For example:

# hs100 192.168.1.13 emeter
{"emeter":{"get_realtime":{"current_ma":280,"voltage_mv":123141,"power_mw":19871,"total_wh":1374,"err_code":0}}}

Now by Watts law, P=IR but P=19871mw and IR = 280 * 123.141 = 34554
What am I missing here?

Support HS300 power strip

The hs100 tool can only turn the strip on or off completely. It should be able to turn on or off each plug individually.

Haufenweise Fehlermeldungen

Hallo ich bekomme unregelmäßig aber mehrmals täglich die folgenden Fehlermeldungen:

`instance system.adapter.hs100.0 terminated with code 6 (UNCAUGHT_EXCEPTION)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[4]: at process.processTimers (node:internal/timers:512:7)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[4]: at listOnTimeout (node:internal/timers:569:17)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[4]: at Timeout._onTimeout (/opt/iobroker/node_modules/tplink-smarthome-api/lib/network/tcp-socket.js:59:36)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[4]: 192.168.123.105:9999 {"emeter":{"get_realtime":{}}}
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[4]: [Waschmaschine] device.send() Error: TCP Timeout after 20000ms
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[3]: at process.processTimers (node:internal/timers:512:7)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[3]: at listOnTimeout (node:internal/timers:569:17)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[3]: at Timeout._onTimeout (/opt/iobroker/node_modules/tplink-smarthome-api/lib/network/tcp-socket.js:59:36)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[3]: 192.168.123.105:9999 {"emeter":{"get_realtime":{}}}
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[3]: TCP 192.168.123.105:9999 Error: TCP Timeout after 20000ms
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[2]: at process.processTimers (node:internal/timers:512:7)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[2]: at listOnTimeout (node:internal/timers:569:17)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[2]: at Timeout._onTimeout (/opt/iobroker/node_modules/tplink-smarthome-api/lib/network/tcp-socket.js:59:36)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[2]: 192.168.123.105:9999 {"system":{"get_sysinfo":{}}}
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[2]: TCP 192.168.123.105:9999 Error: TCP Timeout after 20000ms
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[1]: at process.processTimers (node:internal/timers:512:7)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[1]: at listOnTimeout (node:internal/timers:569:17)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[1]: at Timeout._onTimeout (/opt/iobroker/node_modules/tplink-smarthome-api/lib/network/tcp-socket.js:59:36)
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[1]: 192.168.123.105:9999 {"system":{"get_sysinfo":{}}}
host.iobroker-produktiv
2023-11-26 18:38:32.981 error Caught by controller[1]: TCP 192.168.123.105:9999 Error: TCP Timeout after 20000ms

hs100.0
2023-11-26 18:38:32.937 info terminating

hs100.0
2023-11-26 18:38:32.435 warn Terminated (UNCAUGHT_EXCEPTION): Without reason `

Der Adapter wurde dann von IoBroker gestoppt und muss manuell gestartet werden.

product list update

I can confirm this worked to setup HS103 plugs and HS220 dimmer switch. Thanks for this!

New Release

It would be great if you could create a 1.2 release because many interesting commits have happened since the last one.

failed to send command

Am I doing something wrong? I've tried in opensuse and ubuntu
When I try ./hs100 scan 192.168.86.1 I immediately get the error "failed to send command"

error

what does this means?

`

hs100.0 2023-02-18 09:22:29.514 error TCP Timeout after 10000ms192.168.178.34:9999 {"emeter":{"get_realtime":{}}}
hs100.0 2023-02-18 09:22:29.513 error Error: TCP Timeout after 10000ms192.168.178.34:9999 {"emeter":{"get_realtime":{}}} at Timeout._onTimeout (/opt/iobroker/node_modules/tplink-smarthome-api/lib/network/tcp-socket.js:59:36) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7)
hs100.0 2023-02-18 09:22:29.513 error unhandled promise rejection: TCP Timeout after 10000ms192.168.178.34:9999 {"emeter":{"get_realtime":{}}}
hs100.0 2023-02-18 09:22:29.508 error Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
hs100.0 2023-02-18 04:26:29.114 error TCP Timeout after 10000ms192.168.178.34:9999 {"emeter":{"get_monthstat":{"year":2023}}}
hs100.0 2023-02-18 04:26:29.114 error Error: TCP Timeout after 10000ms192.168.178.34:9999 {"emeter":{"get_monthstat":{"year":2023}}} at Timeout._onTimeout (/opt/iobroker/node_modules/tplink-smarthome-api/lib/network/tcp-socket.js:59:36) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7)
hs100.0 2023-02-18 04:26:29.113 error unhandled promise rejection: TCP Timeout after 10000ms192.168.178.34:9999 {"emeter":{"get_monthstat":{"year":2023}}}
hs100.0 2023-02-18 04:26:29.109 error Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.