B4J Library jYeelight

This is a simple library that allows you to control the 15$ Xiaomi´s YeeLight WiFi RGB LED bulbs (real alternative to Phillips Hue) through your LAN.
So far only tested with the RGB version (v1) but maybe it works also with the white bulb and strips.

You need to enable “Lan Control” in the App.

Yeelight
  • setPower (state As Boolean, transistion As Int)
    Set the Bulbs state
    state: switch the Bulb On or Off
    trasition: Fading time
  • sendCommand (cmd As String)
    Send the Raw Command
    cmd: JSON string
  • setBrightness (value As Int, transistion As Int)
    This method is used to change the brightness
    value: target brightness from 0 to 100
    trasition: Fading time
  • setRGB (color As Int, transistion As Int)
    This method is used to change the color
    color: target color from 0 to 16777215
    trasition: Fading time
  • setColorFlow (count As Int, action As Int, expression As String)
    This method is used to o start a color flow
    count: total number of visible state changing before color flow stopped. 0 means infinite Loop.
    action: is the action takenafter the flow Is stopped.
    0 = State before the color flow started.
    1 = Stay at the state when the flow Is stopped.
    2 = turn off after the flow Is stopped.
    expression: expression of the state changing series
    Every state contains Duration, Mode, Value, Brightness
    Duration = Gradual change time Or Sleep time, in milliseconds, minimum value 50.
    Mode = 1 –color, 2 –color temperature, 7–Sleep.
    Value = RGB value when mode is 1, CT value when mode is 2, Ignored when mode 7
    Brightness= Brightness value, -1 Or 1 ~ 100.Ignored when mode Is 7.
    When this value Is -1, brightness in this tuple Is ignored
    (only color Or CT change takes effect)
  • stopColorFlow()
    This method is used to stop a running color flow.
  • setDefault()
    This method is used to save current state of smart LED in persistent memory.
    So If user powers off And Then powers on the Bulb again (hard Power reset),
    the Bulb will show last saved state.
  • Toggle()
    This method is used to toggle the Bulbs power state.
  • setSleep (minutes As Int)
    Switch the bulb off after X minutes.
  • setName (name As String)
    This method is used to name the device.
    The name will be stored on the device and
    reported in discovering response.
  • stopSleep()
    Stops the Sleeptimer
  • setKelvin (kelvin As Int, transistion As Int)
    This method is used to change the color temperature
    kelvin: target color temperature. Range Is 1700 ~ 6500 (k)
    trasition: Fading time
  • setHSV (hue As Int, sat As Int, transistion As Int)
    This method is used to change the color
    hue: ranges from 0 to 359
    sat: ranges from 0 to 100
    trasition: Fading time
  • setMusicMode (active As Boolean)
    This Mode puts the Bulb in Client mode and connects to the library TCP Serversocket
    In this Mode no property will be reported and no message quota Is checked.
    In Servermode(Musicmode Off) the bulb only accepts 144 comands per Minute (4 x 60 x 60%)
    After set the Musicmode to True you can send any Command as usual.
  • Disconnect
    Disconnects from the Bulb
  • Discover
    Discover all Yeelights in your network
    'he "discoverComplete" event will be raised when discover is finished

More Features will be added soon!

The following code turns on the lamp and starts a custom colorflow

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim light As Yeelight
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
 
    light.Initialize(Me,"yee")
    light.Connect("192.168.178.20")
 
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub yee_Connected(Successful As Boolean)
    If Successful then
        light.setPower(True,500)
        light.setColorFlow(0,1,"1000,2,2700,100,500,1,255,10,5000,7,0,0,500,2,5000,1")
    end if
End Sub

Sub yee_Response(Message As String)
    Log(Message)
End Sub


Version 1.0
First Upload
Version 1.2
added MusicMode
Version 1.3
control multiple Bulbs
Version 1.4
small improvements
Discover all Yeelight based Lights such Color, Mono (White), Strip, Bedside Lamp
 

Attachments

  • jYeelight 1.4.zip
    14.2 KB · Views: 566
Last edited:

Blueforcer

Well-Known Member
Licensed User
Longtime User
Version 1.2
MusicMode added

With MusicMode it is possible to send unlimited Commands to the Bulb.
So you can change the Colors pretty fast. In Regular mode the bulb only accepts 144 comands per Minute (4 x 60 x 60%). In the following Video I read the Pixelcolor of the Mouseposition and send the RGB Value to both Bulbs.
Because i only use RGB Values instead of HSB (dont know how) i cannot change the Brightness (For Black etc) . You can find this Demo in the Attachment


Next Step is a Ambilight Server written in B4J
 

Attachments

  • MusicMode.zip
    20.1 KB · Views: 555

Blueforcer

Well-Known Member
Licensed User
Longtime User
Version 1.3
Now it is possible to control different Bulbs with different Commands.
This gives huge possibilities such a awesome Ambilight Server ;) .
This Server control 2 seperate Bulbs from the given Capture Areas.
I will publish the Ambilight Server if its finished.
For more information take a look at "Share my Creation"

p.s: it was unpossible to find a great and colorful 32:9 Video. So i use an 16:9 Video.
Since you can change the both capture areas via Drag&Drop, it is easy get the Areas you want.

 
Last edited:

sz4t4n

Member
Licensed User
Longtime User
I try to connect to my yeelight bulb (no RGB just white) but not success.
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
I try to connect to my yeelight bulb (no RGB just white) but not success.
Do you activate the LAN control function in the Yeelight App?

Screenshot_20180827_173053_Yeelight_1.jpg

a>
 
Last edited:

sz4t4n

Member
Licensed User
Longtime User
i cant find that option. I search in yeelight app and in "mi home" app

edit: Ok it works fine now but after i connect this bulb again to Mi home app this setting is false again and i cant connect. So it can be used only with BJA or Mi home not together.

Anyway your lib works good with normal white yeelight bulb.

thanks
 
Last edited:

sz4t4n

Member
Licensed User
Longtime User
ok i was my mistake. It is possible to use this bulb in Mi home app and B4J app in the same time :)

Now its time to make some app with your lib :)

one again thanks :)
 

sz4t4n

Member
Licensed User
Longtime User
Small app that helps you to find all yeelight bulbs IP's in yours network
Can someone check if it works with rgb bulb?
upload_2018-8-27_20-56-11.png
 

Attachments

  • scan.jar
    473.4 KB · Views: 494

sz4t4n

Member
Licensed User
Longtime User
hmm strange. Can you tell me whats MAC address of yours bulbs?

I use "arp - a" in shell to find all devices in network and then i'm looking for MAC address which starts "34-ce-00-88" because all my bulbs starts witch this numbers ( i thought all bulbs starts with it...)
 

sz4t4n

Member
Licensed User
Longtime User
now it should works with RGB too. I'll read this specification.
 

Attachments

  • ping.jar
    473.4 KB · Views: 583

Blueforcer

Well-Known Member
Licensed User
Longtime User
Now it works but I dont think its always the same MAC. because i have 3 similar RGB bulbs with 2 different MAC. Maybe the strips also have another Mac
With SSDP we could discover all informations about the devices. Can you try my code above? Maybe my router block this broadcast message
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
Version 1.4

-small improvements
-Discover all Yeelight based Lights such Color, Mono (White), Strip, Bedside Lamp in your Network

Start a Discover as follows, The "discoverComplete" event will be raised when discover is finished and returns a Map with all discovered Lights.

Note:
Due Broadcast behavior its only possible to find the Lights if you connected via WiFi or your Router supports Multicast


B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim yee As Yeelight
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
 
    yee.Initialize(Me,"yee",1)
    yee.Discover
    yee.Connect("192.168.178.20")
End Sub

Sub yee_Connected(Successful As Boolean)
    yee.setPower(True,5000)
End Sub

Sub yee_discoverComplete(Lights As Map)
    For Each k As String In Lights.Keys
        Log(k & " : " & Lights.Get(k))
    Next
End Sub

B4X:
192.168.178.20 : Color
192.168.178.26 : Color
192.168.178.30 : Color
192.168.178.34 : White
 
Last edited:

CGP

Member
Licensed User
Longtime User
Thanks @Blueforcer for this lib!

I'm trying to add to my project but I found an incompatibility with jOkHttpUtils2_NONUI and OkHttp library.

Can you check it?

Thanks
 

Attachments

  • Test.zip
    1.1 KB · Views: 534

cliv

Member
Licensed User
Longtime User
1. After i connect, how i check the state of a bulb (ON or OFF)?
2. Can you post a sample how to control multiple Bulbs?
3. Do you have a library version for B4A (maybe B4X)?
 

MaxRosa

Member
Licensed User
Longtime User
Você pode converter a biblioteca para B4A? Quero adicionar suporte ao controle de de lâmpadas Yeelight a um dos meus apps. | Can you convert the library to B4A? I want to add support for Yeelight lamp control to one of my apps.
 

MaxRosa

Member
Licensed User
Longtime User
1. After i connect, how i check the state of a bulb (ON or OFF)?
2. Can you post a sample how to control multiple Bulbs?
3. Do you have a library version for B4A (maybe B4X)?
I made my own code to control the Yeeligh bulbs in B4A.
 
Top