-
• #2
What code are you using on Noble to do the transmit? And what OS are you on?
I think if Noble isn't transmitting anything it's quite likely it's some issue with your Noble code. There isn't any flow control on Bluetooth LE so Puck.js can't just reject writes. If the write isn't happening it's probably the code that is sending.
Bluetooth LE OS support can be a bit iffy - I know on Linux it's possible to get it into a state where (for instance) notifications no longer work and you have to restart the Bluetooth stack to get anywhere.
-
• #3
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' ) }
-
• #4
function findByUUID(list, uuid) { // console.log(list.length) for (var i = 0; i < list.length; i++) { // console.log('LIST : ', list[i].uuid) if (list[i].uuid == uuid) { // console.log(list[i].uuid) return list[i] } } }
-
• #5
Ahh - this could be related to 'echo'.
So normally, when you write text to Espruino in the REPL it echoes back those characters so you can see what you typed (plus the JS prompt
>
). Probably if you dump the data you're getting fromrxCharacteristic
you'll see a bunch of stuff coming back. It might actually help to remove all the lines to do withrxCharacteristic
if you don't care about the data coming back - that might improve things.The data that can be sent on BLE is about the same as what can be received, so if you keep sending data to Puck.js it'll keep sending those characters back (plus a few) and it ends up spending all its time waiting to send characters. Eventually it runs out of memory and drops some. I'm not sure it explains the connection issues though.
You can manually turn that off by sending
"echo(0);\n"
first (and back on withecho(1)
.Or - what the Web IDE does is:
- Clears the line with Ctrl-C (char code 3)
- Sends character code 16 at the start of each line to turn echo off just for that line
So just adding
"\x03\x10"
to the front of the code you send (and removing any newlines) might well fix it for you? - Clears the line with Ctrl-C (char code 3)
-
• #6
OK thank you i'll try it !
Hi, sometimes when i want to upload buffer code to puck.js, it stuck in middle of uploading.
I use noble.js, i connect to the puck then convert the code splitted every 19 bytes to write each splitted code to puck.
Sometimes the uploads stop and i cannot reconnect to the puck , i must force reset it to be able to reconnect..
sample code :
i have tested with a delay of 100ms and it change nothing..
when the upload works :
the full javascript code :
Thank You!