-->
Page 1 of 1

MQTT - How to interrupt loops with MQTT Message(Arduino IDE)

PostPosted: Wed May 03, 2017 8:34 pm
by CzarCastic
I have a NeoPixel strip running of a NodeMCU 12E 8266 Board using MQTT with Node-Red. The setup is pretty simple, I have a Node-Red Dashboard of buttons used to select LED animation effects. Each effect has a button and associated MQTT "mode" number sent as the payload.

The main code has each effect as its own function that gets called depending on MQTT message received. The problem I am running into is that many of the effects have VERY long or even infinite loops and/or delays which prevent the 8266 from receiving the messages from the broker. I thought of a few solutions but wondering if anyone had some experience with this to help me setup the best method:

* I will turn MQTT RETAIN on when I publish the MQTT messages so the broker holds it until I get there, eventually!

IDEAS for interupting loop to check for new MQTT messages:

1) insert: client.loop(); at various points in the loops. I don't know the effects on CPU utilization, loop delay effects or load on the broker with constant callbacks.

2) have MQTT message payload just set a MODE variable instead of actually using the payload value to choose the function to run. Then the effect functions could just check to see if the current MODE variable is the same as the function number/name I assign running. I think this would eliminate the concerns I had about the 'callback' performance. I guess then if the the MODE variable from MQTT is now different from the current then just break out wherever I am running - reset the lights and then execute the new function. I guess this is a WHILE loop or even a CASE option. thoughts?

3) low level interrupts - this is something I think would be the best but I couldn't find much useful information, if there is any such thing, on using some sort of built in hardware interrupt to check for new MQTT messages every x amount of time.

Anyway these are 3 options I came up with - as a n00b, I have to ask: am I close? Anyone have any better ideas??

Thanks!