Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
740 views
in Technique[技术] by (71.8m points)

iot - How can I retrieve the returned value of a timer ID using the Timer.set() js timer in Mongoose OS?

Using the Mongoose OS JS API for Timers (https://mongoose-os.com/docs/mongoose-os/api/core/mgos_timers.h.md) I am unable to work out how to get the returned value of the timer ID, so I can use it in Timer.del().

Below is my snippet of code that fires a timer when an MQTT message arrives and ideally cancels the timer on another received message. As you can see I have put in the variable timer instead of null, and have tried using it to call Timer.del().

I have tried to define the timer variable in the global scope, but that hasn't worked. I have also tried to use "return timer" etc.

Can someone please help me with this?

// Interrupt cycle
MQTT.sub('cp/interrupt', function(conn, topic, msg) {
    let status = JSON.parse(msg);
    if (status === 1) {
        Timer.del(timer)
        GPIO.blink(12, 0, 0);
        let relreturn = JSON.stringify('interrupt cycle off');
        MQTT.pub("cp/tp1", relreturn);
    } else if (status === 0) {
        Timer.set(1000, Timer.REPEAT, function() {
            let relreturn = JSON.stringify(ADC.read(0));
            MQTT.pub("cp/tp1", relreturn);
        }, timer);
        GPIO.blink(12, 12000, 3000);
        let relreturn = JSON.stringify('interrupt cycle on');
        MQTT.pub("cp/tp1", relreturn);
        print('Relay 4 is ON (', msg, ')');
    }
    return true;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...