johnny-five icon indicating copy to clipboard operation
johnny-five copied to clipboard

¿Remove device event?

Open husqD opened this issue 3 years ago • 1 comments

I am connecting a NodeJS (express) application with an arduino 1. I have an analog sensor that reads the soil moisture, and I use the following code to read the value from the sensor:

var analogSensor10HS = new five.Sensor({
    pin: "A2", //- attached to pin "A2"
    freq: 2000,// - emits data events every xm
    threshold: 0, //  emits change events when the ADC value has changed by +x/-x
});

app.get("/humidity", function (req, res) {
    if (req.query.read==true ) {
      analogSensor10HS.on("change", ()=> {
        console.log(analogSensor10HS.value)
      })
    }else{
       **STOP DEVICE ON CHANGE EVENT**
    }
})

If i access to http:localhost/humidity?read=true, the sensor starts printing values on the display. How can I remove the event to stop printing the values in the console when I access to http:localhost/humidity?read=false ?

husqD avatar Sep 16 '21 10:09 husqD

Hi, you should use analogSensor10HS.removeAllListeners('change') to remove the event listener

3vilcrow avatar Nov 09 '21 15:11 3vilcrow