konsumer / tplink-lightbulb

Control TP-Link smart lightbulbs from nodejs
MIT License
190 stars 32 forks source link

Is there a way to create preset "OFF"? #25

Closed Nexen23 closed 6 years ago

Nexen23 commented 6 years ago

I have a lot of power outages so I'm seeking a way to turn it off by default after power gets on. But I can only set preset as its default state which is 1% of light at minimum. Is there a way to change this behavior by command?

konsumer commented 6 years ago

This library doesn't currently have preset support, but it might just be a limitation of the lightbulb firmware: "there must be a brightness setting for a preset". If you had a way to "detect a power outage" you could send off commands to everything, though.

A quick idea, and this may not fit your needs at all and be a totally crazy way to do this: make a computer boot very fast, and when it turns on connect to wifi, and turn all the lights off once. This way when it first receives power (it "detects" power) it will shutoff all the lights. The fastest pi image complete with linux system I could find boots in 18 seconds. There is some discussion about it here. If you don't mind the delay, you could install node & this lib on it, and that would work fine.

This still seems too slow to me, for this use-case. a ESP286 microcontroller comes complete with wifi & tcp/ip stack, and boots onto wifi very fast. You can find them from china for <$5. It wouldn't run this library, but maybe you could adapt the protocol to that chip and call it "tplinkmcu" or something! You'd have to port the decrypt/encrypt functions to lua/c++/other supported language, and make sure you could make UDP requests, and then you can use my lib as a reference for how the messages work (basically badly encrypted JSON messages over UDP, a scan is a broadcast.) By sending messages on boot you could have a kind of "power on preset for the whole network".

It looks like someone got javascript working on ESP286, but if I remember correctly, it has very little space for more program after that. Might be worth experimenting with just copying the encrypt/decrypt stuff over, and trying to get it to do UDP.

konsumer commented 6 years ago

I am guessing that very purpose-built lua would be the best balance of speed, ease of development, and having enough program-space to do it, but if the js stuff works on the chip, that'd be awesome and much easier.

konsumer commented 6 years ago

I thought a bot more about it, and since nodemcu runs lua, you could adapt the decode function I put in the wireshark plugin to encode th messages. If I have a minute today, I'll post something.

konsumer commented 6 years ago

Maybe I'll make a new project for this, as it'd be a cool base for IOT lightswitches and stuff (e.g: "when this battery powered switch is turned on, set these 3 lights to red, blue and green, when it's off switch them all off")

ExperimentalZeros commented 6 years ago

Not to sabotage, but my two cents:

I have been trying to find the time to do exactly what you just mention in your previous post: I have been learning from your code so I can have ESP8266 (running ESPRUINO at this moment) with Analog to Digital converters, as well as “old” iPods (and possibly NODE-JS as a graphical monitor) to send UDP messages down to these type of TP-Link devices to change light colors in each room!

Hopefully you are more successful in finding time!!!

That was all!

On Apr 27, 2018, at 11:50, David Konsumer notifications@github.com wrote:

Maybe I'll make a new project for this, as it'd be a cool base for IOT lightswitches and stuff (e.g: "when this battery powered switch is turned on, set these 3 lights to red, blue and green, when it's off switch them all off")

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

konsumer commented 6 years ago

I started a project here maybe we can work on it, together. It's not at all complete, but I fleshed out the basic interface/tooling that I think would be useful and an alloff/allon example.

konsumer commented 6 years ago

I think this handful of functions would make it a useful library. Basically power is just a wrapper around send, and more advanced commands could be done with send directly, which should take a lua table, turn it into json, encode that, and send it to an ip, wait for return, decode the JSON/encryption, and return it as a lua table. I'm super-busy today, but this should be a decent start, and if I have time, I will add more (like actual encrypt/decrypt/send functions.)

konsumer commented 6 years ago

I'm going to close this issue, and track over on this project. I'm hoping to have enough time to work on it, today.

konsumer commented 6 years ago

@ExperimentalZeros somehow I missed you are using ESPRUINO, so it's JS? Have you found that it has enough memory/cpu to do useful stuff? I might switch to this, if so. Is your project open-source?

ExperimentalZeros commented 6 years ago

Absolutely!

So you can build ESPRUINO on several different type of embedded systems (RPI, ESP, STM32x, etc) with with as much memory as needed (I have not ran out and I Code terribly) to run most anything within IoT tasks.

The developer of ESPRUINO (along with the many enthusiastic collaborators) just “recently” released the UDP library on all platforms (you could say I was bit-banging it and using NODE-RED to glue it all together) but with it - I Imagine - one can literally talk directly to a bulb, or a router with all bunch of bulbs/sockets on it.... without using KASA or NODE.

I don’t think my code works to any standards as very little I put into it, which gave me very little results.

If you get the ESP8266 (or even better the ESP32), putting ESPRUINO in it is no more difficult than installing firmware in other platforms.

Just to go the extra mile: I vouch for this product, both Hardware and Software, as it has helped me a ton finding, patching, and fixing in almost “real-time” on hardware devices where senior software engineers couldn’t.

Let me know your thoughts, and hope I can really find time to get this done.

On May 7, 2018, at 00:51, David Konsumer notifications@github.com wrote:

@ExperimentalZeros somehow I missed you are using ESPRUINO, so it's JS? Have you found that it has enough memory/cpu to do useful stuff? I might switch to this, if so. Is your project open-source?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

konsumer commented 6 years ago

I have several ESP8266. Where is your code?

konsumer commented 6 years ago

@ExperimentalZeros I can't seem to find a working UDP broadcast example for Espruino. I see single clients & servers, but no broadcast. Did you get this working somehow? Where is the code that you mention when you say "code works to any standards as very little I put into it," ? Did you get node-red running on an Espruino somehow (as you mention, "bit-banging", which I'm unsure what you mean in the context of UDP.) You seem to allude to working but ugly code somewhere, but I'm not sure I see how that would work in the way you say you are using it. Can you share your code?