[WIP] LAN Control Yeelight using Tasker (DIY Project) - W/O Yeelight app or internet !

[WIP] LAN Control Yeelight using Tasker - WITHOUT Yeelight app NOR INTERNET !


**************************************** DISCLAIMER ****************************************

  • I’m NOT affiliated with Yeelight/Xiomi in anyway, I’m a Yeelight user like you.
  • This project is WORK IN PROGRESS - while it’s useful, expect that somethings might NOT work.
  • Please DO NOT request any ETA for changes/features etc, since i’m doing it out of my FREE WILL & TIME.
    Nontheless, you’re welcome to change/add whatever you want, and welcome share it with us here
  • Want to share it ? please publish a LINK TO THIS THREAD @forum.yeelight.com
  • and MOST IMPORTANT:
  • I am not responsible for bricked devices, any connectivity issue, thermonuclear war, high device wear or alien visits due to any special lightning effects that might happen.
  • Do some research if you have any concerns about features included in this Project - all of the project’s code can be read & modify for your liking by you.
  • YOU are solely responsible for everything that happens to your Yeelight device / Android device & OS / Tasker
    when you’re choosing to use this project.
    If you point the finger at us for messing up your device, I will laugh at you.

IMPORTANT NOTE: IT IS MANDATORY TO ENABLE “LAN CONTROL” in Yeelight official app

  • You only NEED TO DO IT ONCE, then you don’t need to use that app nor have it installed.
  • It is recommended to have Yeelight official app installed, as a “backup plan”, so if something goes wrong with this project, you may have a backup solution available.

Requirements

  • Android OS (tested on Nougat & Oreo, should work for others) on NON rooted device.
  • Tasker app (tested on v5.xx+, should work on old v4.9 as well)
  • Enable “LAN Control” in Yeelight OFFICIAL app ( single time only = set & forget )
  • (OPTIONAL) AutoNotification (Only mandatory if you wish QS Tile to work, tested on v.3.8.4)
  • (OPTIONAL) “SL4A” & “Py4A” to get auto bulb’s IP detection using SSDP.
  • A LOT OF PATIENCE - THIS IS WIP PROJECT !

Features

  • Control your Yeelight WITHOUT internet connection - i.e on LAN only !
  • QS Tile ( Nougat+ only ): able to toggle power (single click), change presets.(double click when on) and change current controled bulb ( double click when off ).
  • basic tasks ( power toggle/change RGB/change bright etc ) with simple input needed ( by using “Perform Task” action ).
    Examples:
    • To Toggle Power or set its power state - it’s one task, either call that task without any input to toggle the power state or add “on” or “off” as input to set the bulb’s power state.
    • change RGB/temperature - it’s one task, only need as input R.G.B value (e.g 211.0.11) or temp (e,g 6500)

Take under considuration

  • My device is rooted, so it’s untested on non-rooted
    nonetheless, non of the actions I use in the project require root so I only assume this works for non-rooted devices as well.
  • I own a single Yeelight RGB Bulb v2(800lm) so I test the project only on that one device, thus:
    • A. Testing the support for multiple bulbs ( i.e multiple IPs ) is impossible for me, although It exist in the project on some degree.
    • B. Since the TCP commands are unanimous between Yeelight devices - the project should work on other Yeelight devices as well, BUT IT MIGHT NOT.

DOWNLOAD

HOW TO INSTALL

  1. Download xml and put in your device.
    ( tasker’s default projects folder is ‘/sdcard/tasker/projects’ )
  • Open tasker -> long click the lowest part of the screen (the projects tray/icons) -> import

HOW TO UPDATE

  • wipe existing project or change its current name -> then import new version like new installation

MUST DO AFTER FRESH INSTALL OR UPDATE !!!

  • A. enter Yeelight IP(s) in ‘Preferences’ task, comma delimited if you input more then 1.
    ( e.i “192.168.0.1**,** 192.168.0.2” )
    • NOTE 1: it is VERY RECOMMENDED that you set STATIC IP to each Yeelight in your router.
    • NOTE 2: if you prefer to work with dynamic IPs - you can use my ‘SSDP auto IP detection method’ - but don’t blame me if you need to use about every hour or so. ( that’s why STATIC IP is advisable ! )
  • B. enter IP nickname(s) ‘Preferences’ task ( default is RGB1 ), PER EACH IP you’ve input in step #1, i.e number of IPs = number of nicknames !
  • C. RUN ‘Preferences’ task ( to update the permanent memory variables )
  • D. (OPTIONAL) TEST: open ‘Examples’ task -> enable each relevant section to test other tasks.
  • D. (OPTIONAL) to get QS TILE working: enable all 3 profiles -> manually run the ‘Tile_Startup’ task to init the QS tile.
    ( it uses Tile #2 of autonotification addon- make sure it isn’t hidden in autonotification’s app’s settings )

FAQ

  • Yeelight bulb(s) not responding to my commands !

    1. MAKE SURE you’ve ENABLE “LAN CONTROL” in Yeelight official app !
    2. open any terminal/shell app and run ‘nc --help’.
      if you’re getting "nc: command not found” → you MUST install BUSYBOX → thus you MUST have ROOT !
  • SSDP auto IP detection issues:

    • Since it’s based on a python script, users MUST INSTALL BOTH
      “SL4A” & “Py4A” apps from THIS SOURCE
    • After installation, open Py4A (PythonForAndroid) app -> click “Install” to download and install python binaries (you must have internet connection)
    • To get auto network detection of bulbs, simply enter “Prefrences” task, enable related lines under “SL4A” anchor and run task ( twice, to set the debug reports flag ‘OFF’).
    • Don’t forget to manually assign a NICK to each IP - this will be handled in future updates.
    • please note that since P4yA works only by reading python files, folders & a file are created under /sdcard/sl4a/scripts.
      if you don’t have /sdcard folder, please let me know what is your root folder where you can store files.

Future plans / Things to do

  • Add UI for everything !
  • Embed better mutiple IP(many devices) supports
    ( many tasks aspects ( like reports ) currently support only single IP. )
  • Presets - save/load/change combination of different bulb actions.
  • Tile double click actions ( when it’s on ) - being able to change the actions of double-click to any preset predefined ( using UI, preferable ).

Change log

  • v0.5b

  • New Task ‘Sleep Timer’ - make an ‘On’ bulb sleep after X min.

    • running task with input X (numeric value) = turn off afer X min ( i.e TCP command cron_add )
    • running task with input ‘Status’ = getting back time left of the timer ( bulb must be ‘on’ ) ( i.e TCP command cron_get )
    • running task w/o input = turn off timer ( i.e TCP command cron_del )
  • v0.4b

  • Task ‘Change color’ - added support for HSV, i.e ‘set_hsv’ TCP command.
    task action is depended on the length of input:

    • 1 number = temperature color, values of 1700-6500
    • 2 numbers (comma delim) = HSV, i.e 1st is hue (0-359), 2nd is saturation (0-100)
    • 3 numbers (comma delim) = RGB color, 1st red, 2nd Green, 3rd Blue - each of values 0-255.
  • Change name of task ‘Parse RGB’ to ‘Parse_24bit_to_RGB’

  • Added task ‘Parse_RGB_to_24BIT’ that gets input of comma delimmited RGB color ( e.g 255.255.255 ) and returns 24BIT numeric color.

    • Added task ‘Set Default’ to set current settings as default ( so that bulb can retain its settings after disconnection from electricity.
      no input is needed, only that the bulb will be toggled on.
    • New task: ‘Color Flow’, support the use of ‘start_cf’ and ‘stop_cf’ TCP commands.
      • When no input passed into task -> it will stop current working flow ( stop_cf )
      • To get flow working, you need to send this array string ( comma delimited ):
        end_mode, Duration, color_mode, value, brightness…Duration2, color_mode2, value2, brightness2
        Example
        2,500,255.0.0,100,1000,0.255.0,100,500,0.0.255,100
        Detailed explanation:
        • end_mode ( “action” ) is the state of the bulb when flow is ended.
          0= restore previous bulb state before flow,
          1= leave last flow state (color+bright),
          2= turn off
          NOTE that you only pass this value ONE TIME ONLY.
          only the other four parameters, you can repeat as many times as you want as long as each time you add these four parameters.
        • Duration= in MS how long each state will stay before switching to the next
          min is 50.
        • color_mode: 1= RGB, 2= Temperature, 7= sleep ( it exist for adding delay w/o change to color * brightness )
        • Value: is the value of the color_mode you chose
          for 1=RGB, input as point delimited ( e.g 255.255.255 ),
          for 2=Temperature, input as a number with value 1700-6500
          for 7= sleep, you can input any number - it will be ignored
        • Brightness: 0-100 ( low to high ) brightness.
  • v0.3b

    • Bulb’s IP auto detection using SSDP ! ( YES, even for NON-ROOTED !)
      Credits (partially based on): [https://github.com/crquan/work-on-sony-apis/blob/master/search-nex.py](credits: https://github.com/crquan/work-on-sony-apis/blob/master/search-nex.py)
      • READ FAQ above for guidelines how to make it work
      • added two new tasks:
        • SSDP_Identify - send SSDP message to broadcast and return reply from bulbs on the lan.
        • SSDP_IP_List - call the above task and return a array of IP taken from that above reply.
        • There are extra two tasks with SSDP name - they my other trials to get this feature work only for rooted device ( because tcpdump must have root, but tasker can’t run it even with root ).
      • QS Tile double click action - Fix few bugs related to IP switching
  • v0.2b

  • QS Tile: when it’s “off”, double tap on Tile toggles to next pre-defined IP, till it loops back to 1st IP.
    ( please note you MUST manually enter the IPs with delimiter “,” using “Prefrences” task. )

  • QS tile status refreshed ( VS real bulb’s status ) when status bar gets open.

    • please note that status bar values are different from one phone/rom to another.
      to add support for your status bar, send me its full %WIN value when it’s open.
  • v0.1b is online

  • ***tested support for ONE YEELIGHT device, although WIP to support many ***

  • task: Toggle power [on|off| non=toogle]

  • task: Change COLOR [R.G.B|Temperature]

  • task: Change Brightness [on,off,non=toogle]

  • task: Parse RGB ( get 24bit coded number -> return R.G.B coded 0-255 format )

  • task: Send Command ( get ID, Method & IP list -> send command through TCP -> return answer from Yeelight )

  • task: Examples - Inc various examples how to use the above tasks

  • task: Preferences - Set IP list, IP nickname & report flag ( flag on = toast message after running each task - good for debugging )

  • QS Tile related:

    • task: Tile Click Single - toggle on/off power while changing QS tile
    • task: Tile Click Double - loops through 3 modes ( red low bright, red high bright, white-color high bright.
    • task: Tile Startup - Initiating QS tile ( after boot etc )
    • 3 QS TILE ‘PROFILES’ to suppport single click, double click, and init after boot.
1 个赞

Screenshots:
SSDP auto IP detection


"Examples" task

I NEED INTERNET BRAIN HELP FOR THE PROJECT
I want to embed in the project the ability to auto-detect the Yeelights IPs.
from the manual released I know that I need to send SSDP command on port 1982.
I managed to do it successfully using linux shell.
in shell 1 I run the following to listen to returned UDP packets on port 1982.

sudo tcpdump -vv -A "dst port 1982 and udp and inbound"

in shell 2 I send multicast request for the bulbs to identify themself back on UDP port 1982:

( printf “M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1982\r\nMAN: “ssdp:discover”\r\nST: wifi_bulb\r\n”; sleep 0.5 ) | nc -w2 -u 239.255.255.250 1982 -p 1982

The issue is that using TCPDUMP is NOT possible WITHOUT ROOT !
I want to avoid that.
I know that there’s a UDP send/receive addon to Tasker - but I prefer not to, since it require to purchase another app just for that…

I found “ngrep”, an app similar to tcpdump - but that is compiled into one binary for android - but again - the root issue !
without root no one can run “chmod +x” on that binary and no one can execute that binary ( since there’s a NOEXEC parmeter on ext-storage ( i.e /sdcard etc… ) )

so bahh… I need help to bypass it so we could capture UDP packets in android’s shell.
P.S - AFAIK, forget Javascript - it’s no possible.

UPDATE 1:
I found “UDP Sender” - a free app with tasker integration, to send UDP packets ( NO listen-to-port ability ).
although I enter Yeelight special SSDP message to be sent as text - it does work.
after I analyzed that issue, the app does not send “0d” hex values of newline ( i.e does not send UTF-8 of “\r” ).
so I copies the hex data sent from ubuntu ( with the command mentioned above ) and its hex value ( its constant for every yeelight device/version/firmware ) are as follows:

4d2d534541524348202a20485454502f312e310d0a484f53543a203233392e3235352e3235352e3235303a313938320d0a4d414e3a2022737364703a646973636f766572220d0a53543a20776966695f62756c620d0a

for massage of:

M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1982
MAN: "ssdp:discover"
ST: wifi_bulb

( don’t forget the last line should have also \r\n like the all of the reset of them )

NOW… the issue still remains finding a working way WITHOUT ROOT to capture the answer sent back from yeelight…

I know of netcat… but at least the version embed in custom rom install on my phone (Nougat) the netcat (nc) command does NOT supports the “-u” switch - so no UDP sending/capturing is possible ( and I can only solve it by install latest busybox - which of course needs ROOT.
Bummer

UPDATE 2:
Eureka ! I think I solve how can non-root users can get SSDP answers ( i.e how to scan for the IP of yeelight with tasker ! )

The BIG PLUS: it works !
( although have not tested on non-root device, since my only device is rooted )

The small downside - inorder for the solution to work, users need to install two FREE APPS named “SL4A” & “Py4A”.
The former allows Tasker to connects with Py4A and it allows Python v2.7 support.

why ?
I made a Python script that sends SSDP multicast request, and when it get answered it paste that answer into the android clipboard ( since there isn’t any direct access to tasker’s LOCAL variables from python script running on android ).

UPDATE 3:
OK, so a SSDP bulb’s IP auto detection is available since v0.3b for non-rooted phones - using python script that runs on SL4A & Py4A. apps.

In the mean time I tried to embed root-only shell based solution ( that works as in android terminal & su of course ) that uses tcpdump ( because of it root is a must ) & netcat commands.
the issue I couldn’t solve is that “run shell” action can’t run tcpdump command - like it’s not installed on the device ( AFAIK, it came with my rom ).
There was another issue with netcat (nc) not supporting the “-u” switch ( i.e UDP connections ), but that was solved by installing latest busybox ( which raises again the need for su/root).

RESERVED 3 for future updates

RESERVED 4 for future updates

RESERVED 5 for future updates

VERSION v0.5b is live with ‘Sleep timer’ function ( i.e the cron_add/get/del TCP command.)

what is the run shell command to turn on yeelights over lan from tasker…
and what r the things needed to do so…i mean ip address and api key?

i’ve updated the OP, please read section “MUST DO AFTER FRESH INSTALL OR UPDATE” inorder to understand how to work with my tasker project.

about how to send shell commands to yeelight - please look here.