Skip to content

Commit

Permalink
First version.
Browse files Browse the repository at this point in the history
  • Loading branch information
p1n0-0 committed Jun 14, 2018
1 parent 3362611 commit 4b15478
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 1 deletion.
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,102 @@
# yeelight-api-server
# Yeelight API Server
API to remotely control the bulbs of the manufacturer yeelight.

*Warning: This API only works with the RGB mode.
The API has been tested with the color bulb models only.*

## Installation

It must be installed on a pc or raspberry pi with PHP 7.0 or higher.

The ```composer install``` command must be executed to install the dependencies.

It is necessary to expose the web folder in apache or nginx. Requests must be made to the ```api.php``` file.

## Settings
To begin its use it is recommended to edit the file config.inc.php (Inside the "includes" folder) and establish a unique secret token.

It is also possible to modify the default values and the connection timeout to the bulbs.

## Parameters
The API only supports the *POST* method and the parameters specified as *form-data*.

| Parameters | Default Value | Min Value | Max Value | Description |
| --------------------- | ------------- | :-------: | :-------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ```secretToken``` | | | | Secret token set in the config.inc.php file. |
| ```bulbs``` | ```all``` | | | List of id of the bulbs to perform the operation separated by ```,```. |
| ```action``` | | | | Action to perform. The possible values are: ```get```, ```power```, ```bright```, ```rgb``` ```default``` & ```cycle```. |
| ```power``` | ```on```* | | | Determine if you want to turn the light bulbs on or off. The possible values are: ```on``` & ```off```. |
| ```bright``` | ```100```* | `1` | `100` | Determine the level of brightness. |
| ```rgb``` | ```FFFFFF```* | `000000` | `FFFFFF` | Determine the color of the bulbs. |
| ```effect``` | ```sudden```* | | | Support two values: ```sudden``` and ```smooth```. If effect is ```sudden```, then the color temperature will be changed directly to target value, under this case, the parameter ```effect_duration``` is ignored. If effect is ```smooth```, then the color temperature will be changed to target value in a gradual fashion, under this case, the total time of gradual change is specified in parameter ```effect_duration```. |
| ```effect_duration``` | ```30```* | `30` | `10000` | Specifies the total time of the gradual changing. The unit is milliseconds. |
| ```cycles``` | ```5```* | `1` | `20` | Number of changes to be made. |
| ```cycles_duration``` | ```1000```* | `100` | `10000` | Duration between cycles. The unit is milliseconds. |
_*The default values can be modified in the file config.inc.php_

## Actions

#### `get` - Get information about the light bulbs.
* Required parameters: ```secretToken``` & ```action```.
* Optional parameters: ```bulbs```
* Response:
```
{
"success": true,
"data": [
{
"id": "0x0000000000000000",
"address": "192.168.1.X:55443",
"power": "off",
"rgb": "ff0000",
"bright": "100"
}
]
}
```

#### `power` - Turn bulbs on or off.
* Required parameters: ```secretToken``` & ```action```.
* Optional parameters: ```bulbs```, ```power```, ```effect``` & ```effect_duration```.

#### `bright` - Set brightness of the bulb.
* Required parameters: ```secretToken``` & ```action```.
* Optional parameters: ```bulbs```, ```bright```, ```effect``` & ```effect_duration```.

#### `rgb` - Set color of the bulb.
* Required parameters: ```secretToken``` & ```action```.
* Optional parameters: ```bulbs```, ```rgb```, ```effect``` & ```effect_duration```.

#### `default` - Set current state as the default state.
* Required parameters: ```secretToken``` & ```action```.
* Optional parameters: ```bulbs```

#### `cycle` - Make a cycle animation.
* Required parameters: ```secretToken``` & ```action```.
* Optional parameters: ```bulbs```, ```bright```, ```rgb```, ```cycles``` & ```cycles_duration```.

## License

```
MIT License
Copyright (c) 2018 Francisco Gómez Pino
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "fsgpino/yeelight-api-server",
"description": "API to remotely control the bulbs of the manufacturer yeelight.",
"license": "MIT",
"authors": [
{
"name": "Francisco Gómez Pino",
"email": "fsgpino@gmail.com",
"homepage": "http://www.fsgpino.es"
}
],
"support": {
"issues": "https://github.com/fsgpino/yeelight-api-server/issues"
},
"require": {
"elberth90/yeelight-api-client": "^0.5.0"
}
}
25 changes: 25 additions & 0 deletions includes/config.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

##
## Yeelight-API-Server Settings
##

# Set secret token (This token is necessary to perform operations with the api)
$secretToken = "0000000000000000"; // Change me

# Set timeout to search Yeelight bulb
$timeout = 1; // Time in seconds

# Default values
$default_power = 'on'; // Power status (Support 'on' or 'off')
$default_bright = 100; // Brightness percentage (Minimum 1, Max 100)
$default_rgb = 0xFFFFFF; // Default RGB color (Hex value)
$default_effect = 'sudden'; // Effect (Support 'sudden' = changed directly; or 'smooth' = changed gradual)
$default_effect_duration = 30; // Time in milliseconds for smooth effect (Minimum support duration is 30)
$default_cycles = 5; // Number of default cycles
$default_cycles_duration = 1000; // Time in milliseconds for cycle

# Do not edit from here

# Include response
include_once (__DIR__."/response.inc.php");
83 changes: 83 additions & 0 deletions includes/response.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

##
## Yeelight-API-Server Responses
##

# Setting no-cache & content-type to application/json
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/json');

# Class response
class Response {

public $success = false;
public $data = [];
public $error = null;

function __construct($success = true, $data = null, $error = null) {

$this->success = $success;

if ($data == null)
unset($this->data);
else
$this->data = $data;

if ($error == null)
unset($this->error);
else
$this->error = $error;

}

}

# Class response error
class ResponseError {

public $code = 400;
public $message = "Unknown error";

function __construct($code = 400, $message = "Unknown error") {

$this->code = $code;
$this->message = $message;

}

}

# Function to return ok
function return_ok($data = null) {

// Ignore user abort, set time limit to 0 & ob_start
ignore_user_abort(true);
set_time_limit(0);
ob_start();

// Response
http_response_code (200);
echo json_encode(new Response(true, $data), JSON_PRETTY_PRINT);

// Close connection to continue processing
header('Connection: close');
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();

}

# Function to return error
function return_error($code = 400, $message = "Unknown error") {

// Response
http_response_code($code);
echo json_encode(new Response(false, null, new ResponseError($code, $message)), JSON_PRETTY_PRINT);

}
Loading

0 comments on commit 4b15478

Please sign in to comment.