-
Update : working now!
Reading the code from @Zakaton and @Gordon's response I added a
characteristic.handle_cccd = 0x0038;
statement.c.handle_cccd = 0x0038; return c.startNotifications();
Now the Notifications are coming through.
Device connected: A4:C1:38:4E:75:A0 public Service: ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6 Characteristic: ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6 C: BluetoothRemoteGATTCharacteristic: { "uuid": "ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6", "handle_value": 54, "handle_decl": 53, "properties": { "broadcast": false, "read": true, "writeWithoutResponse": false, "write": false, "notify": true, "indicate": false, "authenticatedSignedWrites": false } } Done! temp: 2484 Hum: 53 temp: 2487 Hum: 53 temp: 2486 Hum: 53
Thanks for the inspiration.
-
-
Hi,
I have the same error as the topic starter, not sure if my problem is related, I'm rather new to Espruino/BLE so it may be a "user error".
Here's my code, and the output it produces. It seems to run OK until the point when Notifications are enabled:
var d; var out=""; function getData(){ print("Getting data now"); g.drawString("Getting data now",0,60); NRF.connect("A4:C1:38:4E:75:A0 public").then(function(device) { print("Device connected:",device.device.id); out=device.device.id; g.drawString(out,0,80); d=device; return d.getPrimaryService("EBE0CCB0-7A0A-4B0C-8A1A-6FF2997DA3A6"); }).then(function(s) { out=s.uuid; g.drawString(out,0,105); if(s) { print("Service:",s.uuid); } else { print("S-error"); } return s.getCharacteristic("EBE0CCC1-7A0A-4B0C-8A1A-6FF2997DA3A6"); }).then(function(c) { print("Characteristic:",c.uuid); print("C:",c); out=c.uuid; g.drawString(out,0,130); c.on('characteristicvaluechanged', function(event) { buf =E.toString(event.target.value.buffer); g.drawString(buf,0,155); print(buf); print(event.target.value.buffer); // For 26.0 degrees and 54% is like : new Uint8Array([47, 10, 54, 98, 10]).buffer b=event.target.value.buffer; temp=b[1]*256+b[0]; hum=b[2]; print("temp:",temp, " Hum:",hum); }); // c.on return c.startNotifications(); }).then(function() { console.log("Done!"); }); } // end getData function setWatch(function(){ print("Bangle setwatch on TOP button activated"); g.clear(); g.setFont("Vector",15); getData(); },BTN ,{ repeat:true,debounce:25 } ); /* WEBIDE OUTPUT _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams > Bangle setwatch on TOP button activated Getting data now Device connected: A4:C1:38:4E:75:A0 public Service: ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6 Characteristic: ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6 C: BluetoothRemoteGATTCharacteristic: { "uuid": "ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6", "handle_value": 54, "handle_decl": 53, "properties": { "broadcast": false, "read": true, "writeWithoutResponse": false, "write": false, "notify": true, "indicate": false, "authenticatedSignedWrites": false } } Uncaught Error: Unhandled promise rejection: CCCD Handle not found > */
The NrfConnect output for the Notification is below.
I know the handle for the notification is 0x0038. When I connect to the device using RPI-gatttool using this handle it starts Notifications immediately.:
pi@raspberrypi3:~/Mydata/python/Xiaomi $ gatttool -b a4:c1:38:4e:75:a0 --char-write-req --handle='0x0038' --value="0100" --listen Characteristic value was written successfully Notification handle = 0x0036 value: c5 09 3a 4e 0a Notification handle = 0x0036 value: c4 09 39 4e 0a
Do I need to specify this handle specifically before using
startNotifcations
, and if so, how do I do that?Any help is welcome.
-
Thanks for the extensive explanation, sounds like a perfect addition for the reference. It's clear for me now.
Coming from other languages it feels "natural" to start with an "open" command, so that's the reason why I chose
Storagefile
in the 1st place.This says it all!
Normal Storage functions are what I'd recommend you use for basically everything unless you intend to append textual data (eg a log file). They're much more efficient to write and read.
-
When I started using Espruino a few weeks ago, I was "fresh" re. Espruino. I found the extensive description really confusing. So I just picked one.
Now being a somewhat "less fresh" user I still wonder why there are 2 different file types. I read and reread the documentation, and still have no idea why the differences. It's probably me .....
Just making sure, my posts were not meant as a gripe, but an attempt to add to the discusion that @MaBe kicked of.
-
Tested my code once more to verify that both
news.txt
andnews.txt\1
are recognised.
IT DOES NOT :Ovar Storage = require("Storage"); l=Storage.list(); print(l); function exists(name){ return Boolean(Storage.open(name,"r").addr); } print(exists("news.txt")); print(exists("news.txt\1")); print(exists("nonews.txt")); print(exists("ABC.txt"));
Output is:
[
"news.txt\1",
"weath0.txt\1",
"weath1.txt\1",
"stocks.txt\1",
"messages.txt\1",
"ABC.txt"
]
true
false
false
falseThe mix of the 2 storage versions does not feel like a blessing anymore :)
-
When I check for files without an added chunk number (f.i. file ABC.txt) it is recognised perfectly with your code, output is:
[
"news.txt\1",
"weath0.txt\1",
"weath1.txt\1",
"stocks.txt\1",
"messages.txt\1",
"ABC.txt"
]
existsAs @fanoush explained there are 2 methods to store a file, I happened to use the files with the added chunk number.
-
Fair enough @MaBe but
news.txt\1
is not recognised either.s = require('Storage'); print(s.list()); filename = 'news.txt\1'; if (s.read(filename)!==undefined) { print("exists"); } else { print("does not exists"); }
Output is:
[
"news.txt\1",
"weath0.txt\1",
"weath1.txt\1",
"stocks.txt\1",
"messages.txt\1"
]
does not exists -
OK, thanks, but all of my files have a .txt extension, and those are not recognised by your code :(
s = require('Storage'); print(s.list()); filename = 'news.txt'; if (s.read(filename)!==undefined) { print("exists"); } else { print("does not exists"); }
Output is:
[
"news.txt\1",
"weath0.txt\1",
"weath1.txt\1",
"stocks.txt\1",
"messages.txt\1"
]
does not exists -
This works for me. It's based on the fact that
addr
is zero when a file does not exist. When a file existsaddr
is not zero but points to an address in Storage.var Storage = require("Storage"); l=Storage.list(); print(l); function exists(name){ return Boolean(Storage.open(name,"r").addr); } print(exists("news.txt")); print(exists("nonews.txt"));
The output is:
[
"news.txt\1",
"weath0.txt\1",
"weath1.txt\1",
"stocks.txt\1",
"messages.txt\1"
]
true
false -
Great, I was just looking for some "file exists" code ;-)
The suggestions work OK for filenames without extension like "ABC". But I want to check for file "news.txt" and all variants report "false/missing".
This is my code
s = require('Storage'); file = { name : 'news.txt', exist : 0}; print(s.list()); // variant I if (s.list().find(e => (e == file.name))) { file.exist = 1; console.log("I",file.name,"exists"); } else { file.exist = 1; console.log("I",file.name,"is missing "); } // variant II if (s.read(file.name)) { file.exist = 1; console.log("II",file.name,"exists"); } else { file.exist = 1; console.log("II",file.name,"is missing "); } // variant III print("III",s.list(/^file.name/)); // variant IV print("IV",s.read(file.name)!==undefined); // variant V print("V",s.read(file.name));
Output is
`[
"news.txt\1",
"weath0.txt\1",
"weath1.txt\1",
"stocks.txt\1",
"messages.txt\1",
"ABC",
"news"
]
I news.txt is missing
II news.txt is missing
III [ ]
IV false
V undefined`
-
This is a working example I have come up with with your help, maybe it's of any use to others.
/* Example of an Espruino Object that fires an event when the button on pin D17 is pressed. */ var P8 = { init : function(){ setWatch(function(){ P8.emit("button","D17"); },D17 ,{ repeat:true,debounce:25 } ); } }; // end of P8 Object // ============================= test of P8 object =============== P8.init(); P8.on('button', function(btn){ print("Button",btn, "press detected");});
Output of the script when the button is pressed:
Button D17 press detected
In this example the init function needs to be called at start of the main script. Not sure if there is a way to move this init into the Object, but this works for me.
-
-
I tried the script on my Bangle:
- uploaded the script to the Bangle using WebIDE
- disconnected the Bangle from the WebIDE
- Using NrfConnect on iPhone reveals the Bangle, connected to it, and it shows the advertised services. Writing to
78290002.....
works OK. - To make sure I disconnected the Bangle from NrfConnect, and reconnected. The data I had written was displayed as I sent it. I GUESS this means that the data is written to the Bangle succesfully.
Maybe this helps?
- uploaded the script to the Bangle using WebIDE
-
-
-
@gordon. YES, a simple example like in my previous post would have helped me getting this working. And YES, adding such an example to the doc for on/emit is the perfect place.
Thanks.
-
Based on the tip from @d3nd3-o0 I changed the example in message #1 to:
var eventEmitter = new Object(); //Create an event handler: var myEventHandler = function () { console.log('I hear a scream!'); }; //Assign the event handler to an event: eventEmitter.on('scream', myEventHandler); //Fire the 'scream' event: eventEmitter.emit('scream');
The result:
I hear a scream!
So that works!
So, there is no need in Espruino to
require
anevents
module, instead you just create anobject
, and use that asevent emitter
.Thanks to @d3nd3-o0 for guiding me in the right direction!
-
-
-
Hi,
I would like to create my own event handlers similar like the Bangle event handlers, f.i. the "step" handler.
Bangle.on('step', function(up) { print("step was made"); });
Tried this example from https://www.w3schools.com/nodejs/nodejs_events.asp
var events = require('events'); var eventEmitter = new events.EventEmitter(); //Create an event handler: var myEventHandler = function () { console.log('I hear a scream!'); }; //Assign the event handler to an event: eventEmitter.on('scream', myEventHandler); //Fire the 'scream' event: eventEmitter.emit('scream');
But this results in the error:
I read in the Espruino documentation here http://www.espruino.com/Reference#l_Object_emit about Object.emit and Object.on but can not find any reference or example on how to setup an eventEmitter in Espruino.
I'm rather new to Espruino/Javascript so maybe there's an obvious answer. Can someone point me in the right direction?
-
Yes, in this case it seems it's coming from node coming from the Espruino Hub on Arduino.
But it's not PC-only, as my selfcoded BLE-peripheral on an ESP32 has the
unhandled promise....
too :-(And the Bangle code from post #4 has no "code" at all, it's just a httprequest, so thta's not it either.
BTW Espruino code on a bare NRF52832 (version 1V97) i.c.w. the Espruino Hub also has the same error.
Beats me!
-
As an intermezzo I tried the Bangle.js App Loader from https://github.com/espruino/BangleApps.
Wanted to use it as a "simple" file uploader, but so far I can see the current code does not support that.
As an experiment I copied the
cmdInstallApp
function, slashed it, and created:function cmdUploadFile(fileName, deviceAddress) { bangleSend(fileName, deviceAddress).then(() => process.exit(0)); }
When I executed it I got the dreaded 'Unhandled Prmise Rejection`, there's no escaping it ;-)
iMac19-9:bin gerard$ node apploadergwr.js upload heartOnScreen.js f3-2c-dc-06-01-95 Initialising Notifications <snip> UART: Discovering... UART: Found device: f3-2c-dc-06-01-95 Bangle.js 0195 UART: Connecting to f3-2c-dc-06-01-95 UART: BT> Connecting UART: BT> Connected UART: Disconnected (node:79575) UnhandledPromiseRejectionWarning: Disconnected but not complete (node:79575) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:79575) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Not sure if this error message is interesting for you, but here it is.
-
I always think it's ME, not this time ;-)