I am using ESP32 Development board along with MCP23017 pin Expander and I want to change the status of my relay when user Clicks on touch sensor, for that I am using IRAM_ATTR method, All working fine when I was working with ESP32 GPIO pins but When I code like this
Actually I want an instant reflection on relay status as soon as user clicks on button, and in my code my loop taking some time that's why If I write my digitalwrite code in loop its take some time, so does any one know how to solve this?? Please help me.
Don't.
Firstly, you're switching a relay. There's absolutely no necessity whatsoever to do that from an ISR given the relatively low speed at which relays switch to begin with.
Secondly, you're doing I2C communications from inside an ISR which should never, ever be done, in particular not on an ESP platform - as you have found out. I2C is waaaay too slow to be run from an ISR; handling a full I2C transaction easily takes several milliseconds; an ISR should be kept as short as a couple of dozen microseconds at most.
Just poll the button from your loop() and then set your relay.
If you must absolutely use an interrupt (which you don't, but OK), then only use the ISR to set a flag/boolean and then use that in your loop() to set the relay.
the minimum is to post your complete sketch. Already from the few things you have described I can conclude that you have some mis-conceptions and gaps in your knowledge that just asking for some details will not lead to the solution.
Be the change you want to see in the world
best regards Stefan
Thank you for your reply, As you said I2C is too slow to run from ISR and that's why It's throwing error, but as I said earlier My loop is slow actually I am checking continuously for Internet by ping that's why My loop takes some delay about 1-2 second and that is what I am trying to avoid, so there is any way to do this or I have no any option except looping??
Maybe don't? How often do you really need to ping a web server for updates? What is the acceptable delay between new data appearing on the server and your ESP responding?
It might help if you posted your full sketch so we can pinpoint where unnecessary delays etc. are.
Thanks for your reply, but I know how to create a post and you know what sometimes it doesn't matter, sorry I am being rude but It's fact, rather than pointing on others knowledge try helping them, I would be happy if you could solve my problem.
I haven't posted my entire code just because I have multiple packages included and there are so many lines in it, So rather than posting entire code I just point outed the main code where I am getting error, so normal coder can easily understand and advice me for what's I am doing wrong.
Actually I am using a Firebase library for web server and there I have created a listener which is continuously listening database values, but I found that when board goes offline and if user performs a query then It's acting weird and end up with an error that's why I am pinging and checking for there is internet or not and that's procedure consuming time.
less information more wild guessings:
Ping your ISP instead of google
send an UDP-heartbeat from the other side to check if internet is online
use the second core for this checking
use another ESP32 for checking
Ok, so you had a problem, you patched it up and created a new problem in the process and now that bites you. So perhaps go back to the first problem and try to tackle that.
Like I said, I think there's probably another and better way to keep your program working even under difficult connection conditions, but it's impossible to recommend a solution for an invisible situation. So somehow posting your sketch is really necessary.
You have working code? Post#1 you did not have working code.
Then in post#7 you are stating your code runs slow. If you have working code that runs slow, post it. Otherwise I thought we were still working to fix your ESP32 Exception.