Skip to content

Instantly share code, notes, and snippets.

@dmattia
Last active September 23, 2021 19:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmattia/99c85c5968e1e06adf6beb5adb300655 to your computer and use it in GitHub Desktop.
Save dmattia/99c85c5968e1e06adf6beb5adb300655 to your computer and use it in GitHub Desktop.
PetTutor Feed dispense
// external
import noble from '@abandonware/noble';
noble.on('stateChange', async (state) => {
if (state === 'poweredOn') {
await noble.startScanningAsync();
}
});
noble.on('scanStart', () => {
logger.info(`Began scanning.`);
setTimeout(async () => {
await noble.stopScanningAsync();
}, 1000 * 100);
});
noble.on('discover', async (peripheral) => {
if (!peripheral.advertisement.localName === 'PTFeeder') {
return;
}
await peripheral.connectAsync();
const { services } =
await peripheral.discoverAllServicesAndCharacteristicsAsync();
// Find the service to talk to
const service = services.find(
({ uuid }) => uuid === 'b0e6a4bfccccffff330c0000000000f0',
);
if (!service) {
throw Error('Could not find service');
}
console.info('Found service!');
// Find the characteristic to talk to
const characteristic = service.characteristics.find(
({ uuid }) => uuid === 'b0e6a4bfccccffff330c0000000000f1',
);
if (!characteristic) {
throw Error('Could not find service');
}
console.info('Found characteristic!');
// Dispense a treat for a good dog
await characteristic.writeAsync(Buffer.from([0]), true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment