Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
/ YeePHP Public archive

💡 A simple PHP library to control your Yeelight RGB lights (currently in development)

Notifications You must be signed in to change notification settings

SharkEzz/YeePHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💡 YeePHP

Yeelight-PHP ?

It's a very simple PHP library to allows you to communicate with your Yeelight RGB lights.

This library is all based on the official documentation from Yeelight.

Requirements

  • PHP 7.4 or up (tested with PHP 8.0)
  • PHP sockets extension
  • PHP JSON extension
  • Yeelight local network control enabled on your lights

How to install

  • Require the library using Composer: composer require sharkezz/yeephp
  • Require the composer autoloader (if it is not done yet)
  • Instantiate a new Yeelight object
  • Use it!

How does it work ?

Yeelight-PHP use PHP sockets to communicate with your lights, that make it fast and reliable!

It can as well be used in your 'from scratch' project or in the framework of your choice!

Currently implemented methods

This list is temporary, expect more methods to come

  • Set the color (RGB) of the light
  • Set the light brightness
  • Set the light name
  • Set the light power (on of off)
  • Get light infos (partially completed)
  • Color Flow control
  • Proper error handling

Examples

Imagine that the Yeelight has the IP 192.168.0.15 and is using the default port (55443), in order to change the light color, you can do this:

Change the light color
<?php

require_once './vendor/autoload.php';

use SharkEzz\Yeelight\YeePHP;

// Create a new instance of Yeelight-PHP
$yeelight = new YeePHP('192.168.0.15');

// Set the color of the light, notice that all the 'set' methods are fluent, it means that you can use them in chain !
$yeelight->setColor(0xFF0000)
    ->commit(); // The commit method has to be called at the end to send the commands to the light

Change the light brightness
<?php

require_once './vendor/autoload.php';

use SharkEzz\Yeelight\YeePHP;

$yeelight = new YeePHP('192.168.0.15');

$yeelight->setBrightness(50)
    ->commit();

Change the color and the brightness in one line of code
<?php

require_once './vendor/autoload.php';

use SharkEzz\Yeelight\YeePHP;

$yeelight = new YeePHP('192.168.0.15');

$yeelight->setBrightness(50)
    ->setColor(0x00FF00)
    ->commit();

Toggle the light
<?php

require_once './vendor/autoload.php';

use SharkEzz\Yeelight\YeePHP;

$yeelight = new YeePHP('192.168.0.15');

// Set the light off
$yeelight->setPower('off')->commit();

// Set the light on
$yeelight->setPower('on')->commit();

Set the light name
<?php

require_once './vendor/autoload.php';

use SharkEzz\Yeelight\YeePHP;

$yeelight = new YeePHP('192.168.0.15');

$yeelight->setName('YeelightLoremIpsum')->commit();

Working with a different port

All Yeelight RGB lights can be accessed by default on the port 55443, if your light doesn't use this port, no problems!

You can set your light port in the constructor like this:

<?php

require_once './vendor/autoload.php';

use SharkEzz\Yeelight\YeePHP;

$yeelight = new YeePHP('192.168.0.15', 12345);

API

All the methods are documented in the class and in the interface, here it is a global reminder:

Name Parameters Return type Description
__construct string $ip int $port null Class constructor
isConnected x bool Return true if the light is reacheable on the local network
isOn x bool Return true if the light is turned on
getIP x string Return the current light IP adress
getPort x int Return the current light port
getBrightness x int Return the current light brightness between 0 and 100
getColor x string return an hexadecimal representation or the light color
getName x string Return the current light name
toggle x self Toggle the light state
setColor int $hexColor self Set the light color, must be an hexadecimal string (eg: 0xFFFFFF)
setBrightness int $amount self Set the light brightness, $amount must be between 0 and 100
setName string $name self Set the light name
setPower string $power self Set the light power (off or on), $power must be "on" or "off"
commit x bool Send all the jobs (previous commands) to the light, return true if the changes has been applied
disconnect x bool Close the current opened socket to the light, return true if the socket has been closed, false otherwise

Testing

If you want to test this library, make sure you have a working Yeelight RGB light in your local network with LAN control enabled.

You juste have to set the $ip variable in the LightTest.php file in the tests folder.

Then just run the PHPUnit executable: ./vendor/bin/phpunit

Credits

Official Yeelight documentation

Inspired by Yeelight-PHP from itskenny0

About

💡 A simple PHP library to control your Yeelight RGB lights (currently in development)

Resources

Stars

Watchers

Forks

Languages