-
• #2
Does it work if you do
Puck.eval('1+1'
- without the semicolon? The idea is it's executed as an expression so a semicolon might confuse matters.Looks like the code was causing an exception, the
U
is probably theU
fromUncaught ...
. I wonder what the best way of reporting that back to the user of the library would be? -
• #3
Aha that's it, thanks Gordon. Working like a charm now for several different expressions. I missed the lack of a semicolon in the example.
-
• #4
Not sure what the best way to report that back to users is.
-
• #5
I'd recommend to catch the syntax error and output the input that caused the syntax error?
In this case the targets error message. -
• #6
I've hit that problem a few times. e.g. right now one of my Puck.write() calls is working but when I follow it with a Puck.eval(), it throws a JSON error which turns out to be due to trying to parse
Bluetooth.println(JSON.stringify(new Date().toString()))
Still trying to figure out what interaction of write() and eval() is causing that to appear as the response to eval(). Doing two eval() in a row is fine. And if I do write(), eval(), eval(), the second eval() is fine so write() is causing some issue.
<html> <head> </head> <body> <script src="https://www.puck-js.com/puck.js"></script> <script> var d = new Date(); var n = Math.round(d.getTime()/1000); var puckCommand = "setTime(" + n + ")\\n"; console.log(puckCommand); </script> <button onclick="Puck.write(puckCommand);">Set The Time</button> <button onclick="Puck.eval('new Date().toString()',function(x) { console.log(x); })">Get The Time</button> </body> </html>
-
• #7
The issue is the
Puck.write
call generates some output on the Puck. Then wheneval
is called, it receives whateverPuck.write
output instead - I guess 'Puck.eval' could prefix the output with something - that would help to differentiate it from the output ofwrite
.One option is to temporarily turn off the console output (echo) for the line you
write
- trypuckCommand = "\x10setTime(" + n + ")\\n"
and I imagine that'll fix it (it's just sending char code 16 at the start of the line) -
• #8
What does the char code do? Disable echo?
I'm getting a different error now:
Uncaught SyntaxError: Got '\' expected EOF
-
• #9
Yes, char code 16 right on the start of a line disables echo for just that line.
Is it possible that you somehow ended up sending the characters
\x10
and not the single character code 16? You could tryString.fromCharCode(16)+
but I'd have thought the code I posted should work. -
• #10
Still no joy. Same error which is very weird.
-
• #11
Do you have your Web Bluetooth page hosted online somewhere that I could take a look at?
I just went to https://www.espruino.com/try.php?page=Puck.js+Web+Bluetooth&n=0, clicked some buttons to get it connected, opened devtools, and then started typing.
Puck.write('LED1.set();\n'); Puck.write('\x10LED1.reset();\n'); // both work Puck.write('LED1.set();\n');Puck.eval("BTN.read()",function(x) { console.log(x); }) // fails - as expected Puck.write('\x10LED1.set();\n');Puck.eval("BTN.read()",function(x) { console.log(x); }) // works fine
-
• #12
Hmm. Typing that in the devtools console works for me too. I must be missing something very obvious.
Code is on: https://conorpuckjs.now.sh/web_bluetooth_set_time.html
-
• #13
So I can replicate the write/eval error in devtools console when \x10 is excluded. But when I include it, it works, as you predicted. So I'm clueless as to why it's not working in the code on the page.
-
• #14
Is it this bit that's not working?
<button onclick="Puck.write('\x10setTime(12345789);\\n')">Set The Time with x10</button>
I think it's to do with escaping in HTML, because you're in a string inside a string - note you have to do
\\n
for newline, not\n
- maybe just doing\\x10
would fix it?... and your
puckCommand
example wouldn't work, but for the other reason! Try changing\\n
to\n
in that one! -
• #15
So the bad news is that \x10 or even \\x10 doesn't work (I've tried so many variations around that). The good news is that changing \n to \n in puckCommand solves the overall problem and eval() happily runs now after write(). Thanks for all the help!
-
• #16
In the
<button
line? The normal\x10
in thepuckCommand
line itself is totally fine though. -
• #17
The final working code was:
<html> <head> </head> <body> <script src="https://www.puck-js.com/puck.js"></script> <script> var d = new Date(); var n = Math.round(d.getTime()/1000); var puckCommand = String.fromCharCode(16)+"setTime(" + n + ")\n"; </script> <button onclick="Puck.write(puckCommand);">Set The Time</button> <button onclick="Puck.eval('new Date().toString()',function(x) { console.log(x); })">Get The Time</button> </body> </html>
I never got \x10 working in the code. As you said, there must be some HTML escaping going on somewhere.
-
• #18
I've now added some queueing functionality to the
puck.js
file at https://espruino.github.io/js/puck.jsPlease could you give it a go? You should now be able to call
Puck.write
andeval
right after each other, and it'll queue theeval
until thewrite
has finished.It'll be a little slower since it waits to receive all of the data from the
Puck.write
command before executing whatever is next, but I think that's probably worth it. For high speed stuff you can still usePuck.connect
. -
• #19
Yup, that worked perfectly. Even with
Puck.write(puckCommand);Puck.eval('new Date().toString()',function(x) { console.log(x); })
Thanks!
-
• #20
hello,
I do realize this is an old topic, but I have what appears the same problem.I am using a RuuviTag as hardware and have setup a test website, which points to the Puck.js library. When I test the code from Post #17 I can send the command and I see in the IDE console the date is printed. However in my Chrome Console, I only receive an empty JSON error.
<BLE> Unable to decode "", got SyntaxError: Unexpected end of JSON input
Puck.js:313
followed by a "null"I have that problem with all WebBluetooth examples I can find. Any idea how to solve this?
-
• #21
What computer are you using this on? If it's Windows then Chrome for Windows currently has a bug where Web Bluetooth only works for sending data, not receiving it...
-
• #22
Hello Gordon
Well, yes I am.
Thank you for your info!However, I can't get this to work on my Android phone (Android 8) and Chrome too.
I changed the get line to an alert statement:<button onclick="Puck.eval('new Date().toString()',function(x) { alert(x); })">Get The Time</button>
in order to get the received data displayed.
The alert just presents me we a message "null" -
• #23
I'm not sure which version of the firmware you have, but maybe try
(new Date()).toString()
just in case?Or just
E.getTemperature()
? -
• #24
Well, now that is working with
E.getTemperature()
The command I posted with the date works when I enter it in the IDE directly, but I can't transmit it (yet).
I'm running Epruino 1.94 on a RuuviTag.Thank you very much. This helped me a lot.
I have Puck.write working fine but Puck.eval is throwing an error on Chrome on Ubuntu.
Same error no matter what command I try, even 1+1.
This is on 1v92.
Am I missing something obvious? Thanks!