Trouble connecting puck.js to webpage #3369
Replies: 7 comments
-
|
Posted at 2020-04-20 by Robin Mon 2020.04.20 Hi @user110429, what is the result of running the example Puck page at:
Ref 2nd pp. Is the page deployed on a server that supports https: ? Reference this thread starting at #3 post there: |
Beta Was this translation helpful? Give feedback.
-
|
Posted at 2020-04-21 by @gfwilliams Hi - the issue is you've got two places where you can execute JavaScript - in the Web Browser, and in Puck.js itself. You're running code that's meant for Puck.js in the web browser. The usage of the If you want to get this working, use the two-way communication example from https://www.espruino.com/Web%20Bluetooth#two-way-communications Then replace: with: ... and you probably want to change the What's then happening is you're using |
Beta Was this translation helpful? Give feedback.
-
|
Posted at 2020-04-21 by user110429 Hi, I've tried the suggested fix with little success, attached is my code: I've tried this line: With and without the onclick attribute. Whenever it connects to the Puck it follow ups with an error message: ERROR: TypeError: callback is not a function |
Beta Was this translation helpful? Give feedback.
-
|
Posted at 2020-04-21 by Robin Tue 2020.04.21 Would you please point to the code that calls function connectDevice() as I only find one instance in that snippet.
Most likely as the web page isn't maintaining it's reference to the callback function supplied and the click event itself. Knowing your experience level debugging the event model within web pages along the amount of experience with Javascript using the Puck would assist us as I see your profile indicates a recent member to the forums. Debugging this may need several intermediate steps, adding/building internal content until success is achieved. Be patient as this may take some time.
While we applaud your enthusiasm with the attempts so far, it would really help us to understand where the hold up is with the extremely simple 'Try Me' click examples provided in detail within the link in #2 post, especially the link Gordon supplied in #3 post. Try each and please report back where progress ceases. That should provide the insight needed to embed the snippets for your project. |
Beta Was this translation helpful? Give feedback.
-
|
Posted at 2020-04-22 by @gfwilliams I think it's becacuse you effectively just deleted the should be |
Beta Was this translation helpful? Give feedback.
-
|
Posted at 2020-04-22 by @gfwilliams Ok, looks like that wasn't it - there were a few issues but the main one you hit was that you were just calling Try this: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="btnId">Go</button>
<script src="https://www.puck-js.com/puck.js"></script>
<script>
var connection;
document.getElementById("btnId").addEventListener("click", function () {
if (connection) {
connection.close();
connection = undefined;
}
Puck.connect(function (c) {
if (!c) {
alert("Couldn't connect!");
return;
}
console.log("Connected - uploading...");
connection = c;
connection.on("data", function(d) {
console.log("Received "+JSON.stringify(d));
});
connection.write("reset();\n", function () {
// Wait for it to reset itself
setTimeout(function () {
connection.write(`\x10{
var zero = Puck.mag();
var rep = 0;
function onMag(p) {
p.x -= zero.x;
p.y -= zero.y;
p.z -= zero.z;
var s = Math.sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
if (!Boolean(s < 1000)) {
digitalPulse(LED2, 1, 1000);
rep++;
Bluetooth.println(rep);
}
}
Puck.on('mag', onMag);
Puck.magOn();}\n`,
function () { console.log("Ready..."); });
}, 1500);
});
});
});
</script>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
|
Posted at 2020-05-19 by user110429 Hi @robin sorry, I did not see your messages initially. I've tried all the examples and they worked, so thank you for your help. Also @gfwilliams the code worked, thank you for your help. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2020-04-20 by user110429
I'm trying to test my code for Puck.js on a webpage, however I keep getting errors when trying to call the function, such as uncaught reference error when trying to execute the function. Also, errors are been returned when called puck.mag(). I've looked at the puck.html examples and I can't figure out why this is happening.
This is my code:
Beta Was this translation helpful? Give feedback.
All reactions