Skip to content
/ tuyapi Public
forked from codetheweb/tuyapi

🌧 An easy-to-use API for devices that use Tuya's cloud services. Documentation: https://codetheweb.github.io/tuyapi.

License

Notifications You must be signed in to change notification settings

fison67/tuyapi

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TuyAPI 🌧 πŸ”Œ

XO code style Build Status Coverage Status Node Version

A library for communicating with devices that use the Tuya cloud network. These devices are branded under many different names, but if your device works with the TuyaSmart app or port 6668 is open on your device chances are this library will work.

Installation

npm install codetheweb/tuyapi

Basic Usage

Asynchronous (event based, recommended)

const device = new TuyAPI({
  id: 'xxxxxxxxxxxxxxxxxxxx',
  key: 'xxxxxxxxxxxxxxxx',
  ip: 'xxx.xxx.xxx.xxx',
  persistentConnection: true});

device.on('connected',() => {
  console.log('Connected to device.');
});

device.on('disconnected',() => {
  console.log('Disconnected from device.');
});

device.on('data', data => {
  console.log('Data from device:', data);

  const status = data.dps['1'];

  console.log('Current status:', status);

  device.set({set: !status}).then(result => {
    console.log('Result of setting status:', result);
  });
});

device.on('error',(err) => {
  console.log('Error: ' + err);
});

device.connect();

// Disconnect after 10 seconds
setTimeout(() => { device.disconnect(); }, 10000);

Synchronous

const TuyAPI = require('tuyapi');

const device = new TuyAPI({
  id: 'xxxxxxxxxxxxxxxxxxxx',
  key: 'xxxxxxxxxxxxxxxx',
  ip: 'xxx.xxx.xxx.xxx'});

device.get().then(status => {
  console.log('Status:', status);

  device.set({set: !status}).then(result => {
    console.log('Result of setting status to ' + !status + ': ' + result);

    device.get().then(status => {
      console.log('New status:', status);
      return;
    });
  });
});

This should report the current status, set the device to the opposite of what it currently is, then report the changed status. The above examples will work with smart plugs; they may need some tweaking for other types of devices.

See the setup instructions for how to find the needed parameters.

πŸ“ Notes

  • Only one TCP connection can be in use with a device at once. If using this, do not have the app on your phone open.
  • Some devices ship with older firmware that may not work with tuyapi. If you're experiencing issues, please try updating the device's firmware in the official app.

πŸ““ Docs

See the docs.

TODO

  1. Document details of protocol
  2. Figure out correct CRC algorithm

Contributors

Related

Ports

Projects built with TuyAPI

To add your project to either of the above lists, please open a pull request.

forthebadge forthebadge

About

🌧 An easy-to-use API for devices that use Tuya's cloud services. Documentation: https://codetheweb.github.io/tuyapi.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%