-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sensor disable only works once. #1839
Comments
The unexpected behavior in your code could be due to the asynchronous nature of the setTimeout functions and the fact that the sensor's data event is still being processed after you disable it. This might lead to an interference where the sensor is disabled, but the event handler continues to log values. To address this, you can use a flag to track the sensor's state and prevent logging data when it's disabled. Here's an updated version of your code:
|
@R-holmes10 Thanks for replying! I do agree that it's easy to circumvent this bug, but I truly believe that it is a bug. I'll provide another example, attempting to prove that this is not a problem with the asynchronous nature of the setTimeout function, and that there's reason to believe it's not due to the event still being processed. In this example I'm using a web socket to communicate between an HTML-page and the node server running johny-five, using the "ws" library (https://www.npmjs.com/package/ws) The html page has a simple button, which sends a toggle signal to the server using the web socket. Node server:
HTML file:
Here's the resulting console log: 40 |
I appreciate your detailed explanation. Now I believe that this can be
handled by the development team of the johnny-five.
…On Tue, Jan 2, 2024 at 5:39 PM Tsteinroesland ***@***.***> wrote:
@R-holmes10 <https://github.com/R-holmes10> Thanks for replying!
I do agree that it's easy to circumvent this bug, but I truly believe that
it is a bug.
I'll provide another example, attempting to prove that this is not a
problem with the asynchronous nature of the setTimeout function, and that
there's reason to believe it's not due to the event still being processed.
In this example I'm using a web socket to communicate between an HTML-page
and the node server running johny-five, using the "ws" library (
https://www.npmjs.com/package/ws)
The html page has a simple button, which sends a toggle signal to the
server using the web socket.
The server responds by enabling or disabling the sensor.
Node server:
const { Board, Sensor } = require("johnny-five");
const WebSocket = require("ws");
const http = require("http");
const board = new Board({
port: "COM4",
});
board.on("ready", () => {
const moistureSensor = new Sensor({
pin: "A0",
freq: 250,
});
moistureSensor.on("data", (value) => {
// Log the value only if the sensor is enabled
console.log(value);
});
// Create an HTTP server
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("WebSocket server is running");
});
const PORT = 3001;
server.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
// Create a WebSocket server by passing the HTTP server object
const wss = new WebSocket.Server({ server });
wss.on("connection", (ws) => {
console.log("Client connected");
ws.on("message", (message) => {
message = JSON.parse(message);
console.log("Received message: ", message);
if (message.topic === "sensor") {
if (message.enabled) {
console.log("Enabling sensor");
moistureSensor.enable();
} else {
console.log("Disabling sensor");
moistureSensor.disable();
}
}
});
// Event handler for WebSocket connection closing
ws.on("close", () => {
console.log("Client disconnected");
});
});
});
HTML file:
<!DOCTYPE html>
<html lang="en">
<script>
let toggle = true;
const socket = new WebSocket("ws://localhost:3001");
const buttonClicked = function () {
toggle = !toggle;
socket.send(JSON.stringify({ topic: "sensor", enabled: toggle }));
};
</script>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button onclick="buttonClicked()">Toggle!</button>
</body>
</html>
Here's the resulting console log:
40
40
Client connected
40
39
39
39
39
39
39
39
Received message: { topic: 'sensor', enabled: false }
Disabling sensor
Received message: { topic: 'sensor', enabled: true }
Enabling sensor
38
37
37
37
37
36
Received message: { topic: 'sensor', enabled: false }
Disabling sensor
36
36
36
[... Keeps running the callback function]
—
Reply to this email directly, view it on GitHub
<#1839 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A2B7JU4535BMOQEMA4N6CH3YMP2GBAVCNFSM6AAAAABBGOSBX2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZTHE2DQNRQHE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Original bug posted in 2019: #1520
I'm also seeing this issue with a very simple approach using setTimeouts.
Code to reproduce:
Console:
The text was updated successfully, but these errors were encountered: