GithubHelp home page GithubHelp logo

thamerthn / broadlink-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rudestan/broadlink-api

0.0 0.0 0.0 62 KB

Broadlink devices API implementation (RM, RM3 Pro plus etc.)

PHP 100.00%

broadlink-api's Introduction

Broadlink API PHP7 library

A PHP 7 library for controlling IR and Wireless 433Mhz controllers (e.g. RM Devices), Smart Plugs (e.g. SP2/SP3) from Broadlink. The protocol refer to: mjg59/python-broadlink

Original code refer to: ThePHPGuys/broadlink.

What are the differences with original implementation from "ThePHPGuys"?

  • Added full support of RM3 Pro Plus (learning mode, receiving and sending commands), so it became fully usable withing the api
  • Added support of SP2/SP3 Devices (power on/off, nightlight on/off, get state of power/nightlight)
  • Code was refactored and logic a bit simplified
  • Code was reformatted and cleaned to be able to easily understand the flow

Usage

Discover devices

use BroadlinkApi\Device\NetDevice; 

$discovered = (NetDevice::create())->discover();

The code will produce an array with Instances of corresponding Authenticatable (extended from AbstractAuthenticatableDevice::class) devices or/and with Instances of UnknownIndetifiedDevice::class in case no or some Unknown devices found.

Authorize device (get the cipher key)

To control previously discovered RM device it must be Authenticated. Let's say the first device in the $discovered array is an instance of RMDevice class (or any other that extended from AbstractAuthenticatableDevice::class). The code will look like the following:

use BroadlinkApi\Device\AuthenticatableDeviceInterface;
use BroadlinkApi\Exception\ProtocolException;

/** @var AuthenticatableDeviceInterface $device */
$device = $discovered[0];

try {
    $device->authenticate();
} catch(ProtocolException $e) {
    echo $e->getMessage();
}

Set device to learning mode

After RM device got Authenticated we can set it to learning mode for receiving commands from any remote control. The following code will set the device to learning mode in case the device class is instace of RMDevice.

use BroadlinkApi\Device\Authenticatable\RMDevice;

if ($device instanceof RMDevice) {
    try {
        $device->enterLearning();
    } catch(ProtocolException $e) {
        echo $e->getMessage();
    }
}

Receive last learned command from the Device

Once the RM Device is in learning mode we can receive the last learned command. We also need to wait until the command arrives, so example code might look like this:

$command = null;

while(true) {
    $command = $device->getLearnedCommand();       
    sleep(1);
    
    if ($command !== null) {
        break;
    }
}

var_dump($command->toArray());

An instance of Packet::class will be returned once the command will be received.

Send command to the Device

After receiving a Packet from RMDevice::getLearnedCommad() it can be converted to an array by calling Packet::toArray() method. Converted array can easily be stored for example in json file or in the DB and later can be reused to reproduce this command with the Device. To send previously learned command the following code can be used:

use BroadlinkApi\Exception\ProtocolException;

try {
    $device->sendCommand($command);
} catch (ProtocolException $e) {
    echo $e->getMessage();
} 

Other usage example

If the IP and Mac address of the device is already known (for example it was previously saved) as well as the command (eg. loaded from JSON) the device can be easily used to trigger this command. Possible code might look like the following:

use BroadlinkApi\Device\Authenticatable\RMDevice;
use BroadlinkApi\Exception\ProtocolException;
use BroadlinkApi\Packet\Packet;

// Let's assume that $commandJson is json encoded array with received command 
$commandCode = json_decode($commandJson, true);
$command = Packet::fromArray($commandCode);

$rmDevice = new RMDevice('192.168.1.1', '77:0f:71:b9:5e:82');

try {
    $device->authenticate();
    $device->sendCommand($command);
} catch(ProtocolException $e) {
    echo $e->getMessage();
}

Use cases

The following API can be used for example to connect Google Home or Alexa together with Broadlink RM Pro for example through Raspberry PI as proxy hub. Or another way of usage can be creation of some web widget for Android instead of using limited official Broadlink Application. This API extends the limits of the Device and it becomes much more interesting.

broadlink-api's People

Contributors

rudestan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.