Where do I get the YeeLight API?


#1

I’m planning to develop an app, where do I get the API for yee light? Is there a guide on how to get started on developing with the api? Does the API include the searching and storing of light bulbs (e.g like a bulb lobby where I can conveniently display and get all light bulbs from)? Or do i have to code my own functions to search for the bulbs in the lan? I’ve read through the documentations and only see the function names and their params but how do i actually get the api?

I have exactly what I wanna implement to the bulbs just that if the API requires me to also do networking stuffs (which I’ve no clue) it could be hard for me to implement my stuffs in.

Would be great if this API has all the necessary networking stuffs done, and all I have to do is to get a list of all light bulbs and their variables exposed through a list/array


#2

Sorry for being unhelpful to your inquiry,but I’d like to ask what that app is about


#3

Sure, the app is to add a music mode similar to that of phillips hue’s disco mode. Allowing the lights to be controlled by the computer’s sound card output/audio spectrum. This way I’d be able to assign various frequencies to each bulbs or possibly a group of bulbs and they’ll all be able to light up based on the frequencies instead of the current one which lights up based on the microphone’s input. (all the bulbs reacts to the same frequency / in synchronization which is really boring).

If there’s an app similar to what I’m looking for already or currently in development, please let me know else I’ll have to try making one myself if I can figure out how to use the api/get the control of the bulbs into my source. If all else fails my last resort would be to get the overpriced* phillips hues.


#4

No app I’ve heard of unfortunately,but the concept sounds interesting,I’d like to see that in action


#5

Same here. If the api provides and handles the connections of the light bulbs I’ll be able to at least attempt to integrate my code in. It would be even better if the yeelight developers provide a source with a user interface template of the networking portion/light bulb lobby (fully working), and all the potential developers would have to do is to add their own features onto it.

Perhaps a video or a step by step guide on how to get the bulbs using the commands may help.


#6

Any info on the api at all? In the windows demo I see all these

but QtNetwork and the other includes are no where to be found in the folder.I’m opening this demo in visual studio and also unable to compile. What editor or preset should i use in visual studio?


#7

#8

I’ve gotten to the part where I can find the bulb, how do I store the bulb for control usage?

Basically how do I do the following?
Can you provide a sample code to do the following:

  1. Parses and validate the response message.
  2. Identify the device, check if the device is already maintained in local storage by
    searching local database by ID.
  3. Display the status to user if necessary.
  4. Follow the information in “LOCATION” header to establish a TCP connection with the
    WiFi smart LED.
  5. After the TCP connection is successfully established, send control message to control
    the smart LED or monitor the status change of the device and display any change to user.

Can you provide a sample code for this?

Here’s what I’m using so far to search and detect the bulb, jus put it in a main class and add the necessary includes

       IPAddress multicastaddress = IPAddress.Parse("239.255.255.250");
        UdpClient udpclient = new UdpClient();
        udpclient.JoinMulticastGroup(multicastaddress);
        IPEndPoint remoteep = new IPEndPoint(multicastaddress, 1982);
        string ssdpMessage = "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1982\r\nMAN: \"ssdp:discover\"\r\nST: wifi_bulb";

        //Console.WriteLine(ssdpMessage);

        byte[] buffer = Encoding.ASCII.GetBytes(ssdpMessage);
        udpclient.Send(buffer, buffer.Length, remoteep);

        IPEndPoint AnyEndPoint = new IPEndPoint(IPAddress.Any, 0);

        while (true)
        {
            Byte[] data = udpclient.Receive(ref AnyEndPoint);
            string strData = Encoding.ASCII.GetString(data);
            Console.WriteLine(strData);
        }

#9

Any help please? I’ve been trying to figure how to get the following:

NOTIFY * HTTP/1.1
Host: 239.255.255.250:1982
Cache-Control: max-age=3600
Location: yeelight://192.168.1.239:55443
NTS: ssdp:alive
Server: POSIX, UPnP/1.0 YGLC/1
id: 0x000000000015243f
model: color
fw_ver: 18
support: get_prop set_default set_power toggle set_bright start_cf stop_cf set_scene
cron_add cron_get cron_del set_ct_abx set_rgb
power: on
bright: 100
color_mode: 2
ct: 4000
rgb: 16711680
hue: 100
sat: 35
name: my_bulb

to display but failed. The documentation isn’t really helpful. If i can get pass the issue of storing the bulbs and setting the brightness to whatever’s in my code, I would be able to progress and begin adding my music analyzer stuffs in. You guys should at least provide a sample code to show how exactly to retrieve the bulbs and store it in a variable… This way it would be so much easier to begin implementing stuffs in…


#10

The spec is pretty clear, but storing and controlling is implementation specific, if you find it’s hard for you, you could google some sample code or library.


#11

I tried googling but couldn’t find the answer I’m looking for so i’m asking here since you guys are the developers… thanks anyway for the helpful comment


#12

Sorry, we only provide spec, but not implementation.


#13

I have an app written in php to control the Yeelight color bulb. The app is based on the Qingdao Yeelink specification. I run it on the Raspberry-Pi. It will run on anything that supports current versions of PHP. It will do things with your light bulb not possible with the phone app.

WW


#14

Here is some Python code to get the first bulb that responds to a search and calls various functions on it:

# Copyright Jan Newmarch
# Berkeley license
import struct
import re
import json

MCAST_GRP = '239.255.255.250'
MCAST_PORT = 1982
SRC_PORT = 5159 # my random port

CR_LF = "\r\n"

def get_ip_port():
  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  sock.bind(('', SRC_PORT))
  sock.sendto("M-SEARCH * HTTP/1.1\r\n\
  HOST: 239.255.255.250:1982\r\n\
  MAN: \"ssdp:discover\"\r\n\
  ST: wifi_bulb\r\n", (MCAST_GRP, MCAST_PORT))
  sock.close()

  sock_recv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                          socket.IPPROTO_UDP)
  # ensure this socket is listening on the same
  # port as the multicast went on
  sock_recv.bind(('', SRC_PORT))
  response = sock_recv.recv(10240)
  sock_recv.close()

  # match on a line like "Location: yeelight://192.168.1.25:55443"
  # to pull ip out of group(1), port out of group(2)
  prog = re.compile("Location: yeelight://(\d*\.\d*\.\d*\.\d*):(\d*).*")
  for line in response.splitlines():
    result = prog.match(line)
    if result != None:
      ip = result.group(1)
      port = result.group(2)
      return (ip, int(port))
  return (None, None)

def sendto(ip, port, command):
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
  sock.connect((ip, port))
  sock.send(command + CR_LF)
  response = sock.recv(10240)
  #print(response)

  # the response is a JSON string, parse it and return
  # the "result" field
  dict = json.loads(response)
  sock.close()
  #print "Response was ", response
  return(dict["result"])

def get_prop(prop, ip, port):
  # hard code the JSON string
  command = '{"id":1,"method":"get_prop","params":["' + prop + '"]}'
  response = sendto(ip, port, command)
  return response

def set_prop(prop, params, ip, port):
  # hard code the JSON string
  command = '{"id":1,"method":"set_' + prop +\
            '", "params":' + params +\
            '}'
  #print(command)
  response = sendto(ip, port, command)
  return response

def set_name(name, ip, port):
   params = '["' + name + '"]'
   response = set_prop('name', params, ip, port)
  return response

def set_power(state, ip, port):
  params = '["' + state + '", "smooth", 500]'
  response = set_prop('power', params, ip, port)
  return response

def set_bright(state, ip, port):
  params = '[' + str(state) + ', "smooth", 500]'
  response = set_prop('bright', params, ip, port)
  return response


if __name__ == "__main__":
  print 'Starting'
  (ip, port) = get_ip_port()
  if (ip, port) == (None, None):
    print "Can't get address of light"
    exit(1)
  print 'IP is ', ip, ' port is ', port

  # sample set commands:
  success = set_power("on", ip, port)
  print 'Power set is', success[0]

  success = set_bright(90, ip, port)
  print 'Brightness set is', success[0]

  name = set_name('bedroom', ip, port)
  print 'Name is ', name[0]

  # sample get commands:
  power = get_prop("power", ip, port)
  print 'Power is', power[0]

  bright = get_prop("bright", ip, port)
  print "Bright is ", bright[0]

  name = get_prop('name', ip, port)
  print "name now is", name[0]

  # getting multiple properties at once.
  # Be careful with the quotes, words need to be separated by "," !
  prop_list = get_prop('name", "power", "bright', ip, port)
  print "Property list is", prop_list

For more info see Ch 35 of my online book IoT for Techies