- Fri Jun 30, 2017 8:22 am
#67766
A lot of it will depend on precisely what the code is doing and what else is going on. With the ESP8266/Arduino code a lot of other stuff is running besides your sketch, in the background handling things like the Wifi stack.
As I understand it, the main problem with doing too much within an ISR is that while your ISR code is running other interrupts are disabled. The other background tasks rely on interrupts and wont work properly if they don't happen when expected, so if your ISR is preventing the other interrupt ISRs from getting called then unexpected things could start going wrong - buffers overflowing, Wifi timeouts and disconnecting etc, perhaps Exception 9's and a stack dump
The MQTT client.publish call requires quite a lot of code to be executed under the covers - to make a TCP requests over Wifi and blocks waiting to get back a response. If any of that code is dependent on another ISR it wont work while your ISR has interrupts disabled. You could try re-enabling interrupts within your ISR but I imagine that could quite easily cause other issues.
With the question of why not just do everything in the main loop? I think the main reason is because the main loop only runs sometimes and will be delayed by the other background stuff so if you just check for button presses in the main loop you could miss some button presses whereas using an interrupt for the button then that is less likely.