I'm on Raspbian Stretch, Here the code from my app :
if (puckJSnoble) { puckJSnoble.connect() puckJSnoble.once('connect', () => { puckJSnoble.discoverAllServicesAndCharacteristics(function ( error, services, characteristics ) { var txCharacteristic = findByUUID(characteristics, NORDIC_TX) var rxCharacteristic = findByUUID(characteristics, NORDIC_RX) console.log("passage connected"); if (txCharacteristic && rxCharacteristic) { rxCharacteristic.subscribe(function () { try { var writeData = () => { splitTheScript(scriptInit) setTimeout(() => { txCharacteristic.write( stringToBuffer(splittedScript), false, function (error) { if (error) { console.log( 'error writing data in puck - init : ', error ) } if (splitFinished == true) { rxCharacteristic.unsubscribe(error => { // console.log('unsubscribe : ', error) puckJSnoble.disconnect(error => { console.log(error) }) }) splitFinished = false } else { writeData() } } ) }, delayWrite) } if (!reseted) { txCharacteristic.write( stringToBuffer('reset()\nsave()'), false, function (error) { puckJSnoble.disconnect(error => { console.log(error) }) } ) } else { writeData(); } } catch (e) { console.log('BT> SEND ERROR ' + e) } }) } }) }) puckJSnoble.once('disconnect', () => { console.log('PASSAGE disconnect') if (reseted) { noble.startScanning([], true) } else { reseted = true; initPuck(); cbStopScanning(); } }) } // Converts a string to a Buffer function stringToBuffer(str) { // console.log(str); var buf = new Buffer(str.length) for (var i = 0; i < buf.length; i++) { buf.writeUInt8(str.charCodeAt(i), i) } console.log('buffer : ', buf) return buf } // split the script every 19 byte (device's limit..) to write data in puck.js function splitTheScript(data) { var writeData = [{ data: data, blockSize: 19 }] var d var split = writeData[0].nextSplit || { start: 0, end: writeData[0].data.length, delay: 0 } // Otherwise split based on block size if (split.end >= writeData[0].blockSize) { split = { start: 0, end: writeData[0].blockSize, delay: 0 } } else { splitFinished = true } // Only send some of the data if (writeData[0].data.length > split.end) { if (split.delay == 0) split.delay = 50 d = writeData[0].data.substr(0, split.end) writeData[0].data = writeData[0].data.substr(split.end) if (writeData[0].nextSplit) { writeData[0].nextSplit.start -= split.end writeData[0].nextSplit.end -= split.end if (writeData[0].nextSplit.end <= 0) writeData[0].nextSplit = undefined } } else { d = writeData[0].data writeData[0].data = '' writeData[0].nextSplit = undefined } // actually write data scriptInit = writeData[0].data splittedScript = d delayWrite = split.delay console.log( 'Sending block ' + JSON.stringify(d) + ', wait ' + split.delay + 'ms' ) }
@Christof550 started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I'm on Raspbian Stretch,
Here the code from my app :