All Projects → samuraitruong → yeelight

samuraitruong / yeelight

Licence: MIT license
The nodeJS client library for controlling yeelight over LAN

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to yeelight

homebridge-yeelight-platform
Homebridge plugin for Yeelight Lights supporting Scenes/Moods/Color Flow/Custom Presets/Music Flow/Night Mode
Stars: ✭ 53 (+1.92%)
Mutual labels:  yeelight, yeelight-devices
yeelight-cli
a low level, cross-platform command line client for Yeelight
Stars: ✭ 43 (-17.31%)
Mutual labels:  yeelight, iot-device
Smart-Lights-Yeelight
Control Yeelight Devices quickly from your Mac OS Tray
Stars: ✭ 51 (-1.92%)
Mutual labels:  yeelight, yeelight-devices
node-yeelight-wifi
node js package for xiaomi yeelight bulbs
Stars: ✭ 49 (-5.77%)
Mutual labels:  yeelight
mi-lamp-re
💡 Reverse Engineering Notes for the Yeelight Bedside Lamp (BLE)
Stars: ✭ 35 (-32.69%)
Mutual labels:  yeelight
Pedalino
Smart wireless MIDI foot controller for guitarists and more.
Stars: ✭ 105 (+101.92%)
Mutual labels:  iot-device
yeelight-hand-controller
With Yeelight Hand Controller you can turn on and off your Yeelight just with a finger snap and change the brightness intensity raising or lowering your hand.
Stars: ✭ 41 (-21.15%)
Mutual labels:  yeelight
Basecamp
An Arduino library to ease the use of the ESP32 in IoT projects
Stars: ✭ 251 (+382.69%)
Mutual labels:  iot-device
yeelight.io
Node.js library for controlling Xiao Mi Yeelight
Stars: ✭ 19 (-63.46%)
Mutual labels:  yeelight
YeelightController
Control your Xiaomi Yeelight from your OSX Device
Stars: ✭ 39 (-25%)
Mutual labels:  yeelight
uplexa
uPlexa: Incentivizing the mass compute power of IoT devices to form a means of anonymous blockchain payments.
Stars: ✭ 46 (-11.54%)
Mutual labels:  iot-device
esphome-components
ESPHome components
Stars: ✭ 62 (+19.23%)
Mutual labels:  yeelight
hass-miio-yeelink
Xiaomi Miio Yeelink/Yeelight devices for Home Assistant
Stars: ✭ 140 (+169.23%)
Mutual labels:  yeelight
esphome-yeelight-ceiling-light
Esphome custom firmware for some Yeelight Ceiling Lights
Stars: ✭ 81 (+55.77%)
Mutual labels:  yeelight
python-yeelightbt
Python library for Yeelight's bedside (btle) and candela lamps
Stars: ✭ 77 (+48.08%)
Mutual labels:  yeelight
goodbye-mihome
Web UI and plugins to manage Xiaomi MiHome gateway, sensors and smart devices.
Stars: ✭ 92 (+76.92%)
Mutual labels:  yeelight
Miio
Control Mi Home devices, such as Mi Robot Vacuums, Mi Air Purifiers, Mi Smart Home Gateway (Aqara) and more
Stars: ✭ 1,669 (+3109.62%)
Mutual labels:  yeelight
iosynth
IoSynth is IoT device/sensor simulator and synthetic data generator.
Stars: ✭ 21 (-59.62%)
Mutual labels:  iot-device
iot-workshop
A complete IoT Workshop
Stars: ✭ 42 (-19.23%)
Mutual labels:  iot-device
theCore
theCore: C++ embedded framework
Stars: ✭ 76 (+46.15%)
Mutual labels:  iot-device

YEELIGHT AWESOME

The node api to control yeelight devices using wifi network TCP/UDP npm version

CI

Package Quality Mocha Test Report

Test Coverage Report

Installation

npm install yeelight-awesome

Get Started

Setup your light

You need to enable "LAN Control" on the phone App make the light discoverable over LAN network. Open the phone app, go to the light setting and toggle LAN control on.

Reset your device Use physical switch to turn on - off the light 5 time, give the 1 second break in the between of the onn/off. You probably need to do reset device and reset up it with phone app if the discover doesn't work properly.

Discover the light

Before you can control the light, you need to discover it unless you know the light's IP

// typescript
import { Discover, IDevice } from 'yeelight-awesome';
const discover = new Discover({ port: 1982, debug: true }, logger);
discover.once('deviceAdded', (device: IDevice) => {
  // using device action
});

// make sure you call this
discover.start();
// javascript
const y = require('yeelight-awesome');
const discover = new y.Discover({
  port: 1982,
  debug: true,
});

discover.once('deviceAdded', (device) => {
  const yeelight = new y.Yeelight({
    lightIp: device.host,
    lightPort: device.port,
  });

  yeelight.on('connected', () => {
    yeelight.setRGB(new y.Color(123, 99, 65), 'smooth', 5000);
  });
  yeelight.connect();
});

discover.start();

There is know issue with the yeelight device that discover method doesn't work at the first time attemp. In this case we can:

  • reset device and resetup -see instruction above

  • make the connection using the light IP without discover method, after the first connection successful. the light now will response to discover method

  • from version 1.0.6, there is fallback option to using Ip scan if discover failed. this fallback option is turn on by default.

    const discover = new Discover({ fallback: true });
    
    discover.start();
  • Use IP scan method (this method will expect to take few seconds)

    const discover = new Discover({});
    
    discover
      .scanByIp()
      .then((devices) => console.log('scan finished: ', devices));
    
    discover.on('deviceAdded', (device: IDevice) => {
      console.log('found device', device);
    });

Control the light

To control the light, you need to know the IP of the light, if not sure, using the discover above to find details, after you have details you can make connection to the light and control it

import { Yeelight, Color } from 'yeelight-awesome';

const yeelight = new Yeelight({ lightIp: device.host, lightPort: device.port });
yeelight.on('connected', () => {
  yeelight.setRGB(new Color(66, 87, 23), 'smooth', 5000);
});
yeelight.connect();

Handle Events

The yeelight awesome using Event Emitter pattern, so that you can hook up into the event to get & process data. bellow are list of event

  • commandSuccess: This event emit on every command successful
  • set_power
  • toggle
  • set_default
  • start_cf
  • stop_cf
  • get_prop
  • set_scene
  • set_ct_abx
  • set_rgb
  • set_hsv
  • set_bright
  • cron_add
  • cron_get
  • cron_del
  • set_adjust
  • set_music
  • set_name
  • adjust_bright
  • adjust_ct
  • adjust_color

event data. each event will emit with below data

interface IEventResult {
  action: CommandType;
  command: Command;
  result: ICommandResult;
  success: boolean;
}

Use Promise

see below example

const discover = new Discover({ debug: true }, logger);
discover
  .start()
  .then((devices) => {
    const device = devices[0];
    logger.info('found device: ', devices);
    const yeelight = new Yeelight({
      lightIp: device.host,
      lightPort: device.port,
    });

    yeelight.connect().then((l) => {
      l.toggle().then(() => {
        logger.info('The light has been toggled');
        // you need to call disconnect and destroy when you finish
        l.disconnect();
        discover.destroy();
      });
    });
  })
  .catch((e) => {
    logger.error(e);
    discover.destroy();
  });

All the method has promise support. example:

// yeelight will always running on port 55443
const yeelight = new Yeelight({ lightIp: '192.168.1.101', lightPort: 55443 });
yeelight.once(CommandType.SET_NAME, (data) => {
  logger.info('Can also capture the event data when it ran successful', data);
});

yeelight.once('commandSuccess', (data) => {
  logger.info('commandSuccesss fire everytime the command finish', data);
});
yeelight.connect().then(function (light) {
  light.setName('Bedroom1_light');
});

Logger

You can pass any logger in the constructor of Discover/Yeelight class. In our example we use winston library to write a log.

to write your own logger, you need implement the logger with below ILogger interface

interface ILogger {
  info: (message: string, data?: any) => void;
  error: (message: string, data?: any) => void;
  debug: (message: string, data?: any) => void;
  log?: (message: string, data?: any) => void;
}

If logger not being passed to the constructor, the console.log will be used. by using default Logger below

const defaultLogger: ILogger = {
  debug: (message: string, obj: any) => console.debug,
  error: (message: string, obj: any) => console.error,
  info: (message: string, obj: any) => console.info,
  log: (message: string, obj: any) => console.log,
};

Samples

Here are a full sample of set color flow

// Typescript
import {
  Discover,
  IDevice,
  StartFlowAction,
  FlowState,
  Yeelight,
  logger,
} from 'yeelight-awesome';

const discover = new Discover(
  { port: 1982, asPromise: true, debug: true },
  logger,
);
discover.once('deviceAdded', (device: IDevice) => {
  logger.info('found device: ', device);
  const yeelight = new Yeelight({
    lightIp: device.host,
    lightPort: device.port,
  });

  yeelight.on('connected', () => {
    yeelight.startColorFlow(
      [
        new FlowState(2000, 2, 2700, 100),
        new FlowState(2000, 1, 255, 50),
        new FlowState(2000, 7, 1500, 30),
        new FlowState(2000, 2, 5000, 45),
        new FlowState(2000, 2, 3000, 25),
      ],
      StartFlowAction.LED_STAY,
    );
  });
  yeelight.connect();
});

discover.start();

Or javascript

const discover = new Discover({ debug: true }, logger);
discover
  .start()
  .then((devices) => {
    const device = devices[0];
    logger.info('found device: ', devices);
    const yeelight = new Yeelight(
      { lightIp: device.host, lightPort: device.port },
      logger,
    );

    yeelight.connect().then((l) => {
      l.startColorFlow([
        new FlowState(2000, 2, 2700, 100),
        new FlowState(2000, 1, 255, 50),
        new FlowState(2000, 7, 1500, 30),
        new FlowState(2000, 2, 5000, 45),
        new FlowState(2000, 2, 3000, 25),
      ]).then(() => {
        logger.info('Start color flow finish');
        // you need to call disconnect and destroy when you finish
        l.disconnect();
        discover.destroy();
      });
    });
  })
  .catch((e) => {
    logger.error(e);
    discover.destroy();
  });

Please refer to [https://github.com/samuraitruong/yeelight/tree/master/samples] (https://github.com/samuraitruong/yeelight/tree/master/samples)

NOTE: The example was written in typescript so you need to use ts-node to run them.

ts-node samples/filename.ts

API

Yeelight awesome has implement all the support function for yeelight device, Just in case the device has new function that not covert by the API, you can use the sendCommands function with your own data structure to make a request to the light

Please refer to [https://samuraitruong.github.io/yeelight/] (https://samuraitruong.github.io/yeelight/) for all details document

  • NOTE: This library has been test and confirm working on Yeelight YLDP02YL AC220V RGBW E27 Smart LED Bulb To run the example, you need to install ts-node
npm install ts-node -g
then execute the ts-ndoe instead of node with the typescript file

ts-node sample/start-flow.ts

WIP

  • Adding error handling event/successful event
  • Enhancement documents and add defined type for typescript
  • Unit test & Test Coverage for the main code

Issues & Feedbacks

Please use github issue page if you encounter with any problem or want to give a feedback, feel free to post an issue on github page

Bug fixs & Enhancements

Feel free to fork and pull request the new feature that you make/or bug you fix. Thanks

Release Notes

  • 1.0.10 : Added typescript declaration
  • 1.0.8 : Fix the repeat count of startColorFlow function (start_cf)
  • 1.0.7 : Added set_shv function
  • 1.0.6 : added support IP scan method fallback
  • 1.0.3: Added support promises
  • 1.0.0-1.0.0.2 : The very first initial , include all test and working function
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].