Skip to content

Instantly share code, notes, and snippets.

@e-neko
Forked from kolontsov/nespresso.js
Created October 22, 2017 00:59

Revisions

  1. @kolontsov kolontsov created this gist Oct 22, 2017.
    40 changes: 40 additions & 0 deletions nespresso.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const noble = require('noble');
    const SERVICE_UUID = '06aa1910f22a11e39daa0002a5d5c51b';
    const TEMPERATURE = {low: '01', medium: '00', high: '02'};
    const VOLUME = {ristretto: '00', espresso: '01', lungo: '02', americano: '04', hotwater: '05', recipe: '07'};
    let brew_cmd = (vol, t)=>`0305070400000000${TEMPERATURE[t]}${VOLUME[vol]}`;

    let process_char = char=>{
    if (char.uuid=='06aa3a41f22a11e39daa0002a5d5c51b')
    {
    char.write(Buffer.from(AUTH_KEY, 'hex'));
    console.log('Auth done');
    }
    else if (char.uuid=='06aa3a42f22a11e39daa0002a5d5c51b')
    {
    char.write(Buffer.from(brew_cmd('lungo', 'high'), 'hex'));
    console.log('Brew done');
    }
    };

    console.log('Waiting for device');

    let discovered = 0;
    noble.on('discover', machine=>{
    if (discovered++)
    return;
    console.log(`Coffee machine found: ${machine.advertisement.localName} (${machine.address}), id=${machine.id}`);
    noble.stopScanning();
    machine.connect(err=>{
    console.log('Connected')
    // service/char discovery not needed, just write to appropriate characteristics
    machine.discoverServices([], (err, services)=>
    services.forEach(srv=>srv.discoverCharacteristics([], (err, chars)=>chars.forEach(char=>process_char(char)))))
    });
    });

    noble.on('stateChange', state=>{
    if (state=='poweredOn')
    return void noble.startScanning([SERVICE_UUID], true);
    noble.stopScanning();
    });