Skip to content

Instantly share code, notes, and snippets.

@Deses
Created February 14, 2020 18:11
Show Gist options
  • Save Deses/b0d36e3431137c00b0200b954621c462 to your computer and use it in GitHub Desktop.
Save Deses/b0d36e3431137c00b0200b954621c462 to your computer and use it in GitHub Desktop.
const TuyAPI = require('tuyapi');
const device = new TuyAPI({
id: 'XXXXXXXXXXXXXXXXXXXX',
key: 'XXXXXXXXXXXXXXXX'});
let stateHasChanged = false;
// Find device on network
device.find().then(() => {
device.connect();
});
// Add event listeners
device.on('connected', () => {
console.log('Connected to device!');
});
device.on('disconnected', () => {
console.log('Disconnected from device.');
});
device.on('error', error => {
console.log('Error!', error);
});
device.on('data', data => {
//console.log('Data from device:', data.dps['1']);
if (data.dps['1']) {
console.log('The printer is already on!');
} else {
console.log('The printer is off. Turning it on.');
if (!stateHasChanged) {
device.set({set: true});
stateHasChanged = true;
}
}
});
setTimeout(() => { device.disconnect(); }, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment