Hi everyone.
I'm using a NodeMCU ESP8266 with the Arduino IDE, and for the code I'm writing I need an interrupt, but I can't make it work.
This is the general code that I'm using to test the interrupt behaviour/problems in the ESP8266:
#define ICACHE_RAM_ATTR
const int interruptPin = 14;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
ICACHE_RAM_ATTR;
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE);
Serial.begin(115200);
}
void ICACHE_RAM_ATTR handleInterrupt() {
Serial.println("Interrupt Detected");
}
void loop() {
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
Serial.println("..."); //Just for testing purposes.
}
At first, I was having the issue "ISR not in IRAM", I've corrected it with the "ICACHE_RAM_ATTR".
Now my problem is that, apparently, the ESP8266 is resetting continiously with code 2 (reset pin), and output this:
"
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld
"
I've tried all the things suggested in forums, datasheets, blogs...nothing seems to work, maybe I am doing something wrong.
I have tried the following things:
- Changing library version;
- Changing interrupt pins;
- Changing ICACHE_RAM_ATTR position in the code;
- Changing USB cable;
- Changing USB port on my computer, 2.0 or 3.0 (yeah I know it's pointless but you never know...);
- Changing Interrupt method (CHANGE, RISING, FALLING);
- Crying ON the ESP8266 and praying that it will work one day (joking...kind of);
Any suggestion on how I can solve this frustrating problem?
void ICACHE_RAM_ATTR handleInterrupt() {
Serial.println("Interrupt Detected");
}
you can not use Serial within an interrupt, try
#define ICACHE_RAM_ATTR
const int interruptPin = 14;
volatile bool intfired=false;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
ICACHE_RAM_ATTR;
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE);
Serial.begin(115200);
}
void ICACHE_RAM_ATTR handleInterrupt() {
intfired=true;
}
void loop() {
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
if (intfired) {
Serial.println("Interrupt Detected");
intfired=false;
}
}
It returns the same exact error:
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld
What else do you have connected and how are you powering the ESP ?
For testing why my interrupts do not work, i have connected only the usb so it's powered via the usb of the computer, to eliminate external problems.
OK, so i had to do a full test (including upload) and came to the conclusion that :
-First your program (as you posted it initially) doesn't compile, since you re-defined //#define ICACHE_RAM_ATTRThen i had to swap the pin nrs a little, since i don't have a nodeMCU but just an ESP-01 and on my uploader i do have a switch on pin 0 (with a hwPullup), and a led on pin 2 (active LOW or i won't get the correct bootmode), and this code works !
//#define ICACHE_RAM_ATTR
const int interruptPin = 0;
volatile bool intfired=false;
void setup() {
pinMode(2, OUTPUT);
pinMode(interruptPin, INPUT);
//ICACHE_RAM_ATTR;
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
Serial.begin(115200);
Serial.println();
Serial.print("Built in LED is pin : ");
Serial.println(LED_BUILTIN,DEC);
Serial.println("Interrupt Test Program");
}
void ICACHE_RAM_ATTR handleInterrupt() {
intfired=true;
}
void loop() {
digitalWrite(2, LOW);
delay(500);
digitalWrite(2, HIGH);
delay(500);
if (intfired) {
Serial.println("Interrupt Detected");
intfired=false;
}
}
For me, using the Arduino IDE 1.8.8, it compile only with the code I have uploaded originally. With yours, I need the #define ICACHE_RAM_ATTR to be uncommented in order to make it compile. Same problem: resetting over and over and over. I will just try to change esp8266 at this point...
EDIT 1:
I changed ESP8266 and have the same exact issue
What ESP core are you using ?
and what if you just leave ICACHE_RAM_ATTR out altogether ?
void handleInterrupt() {
intfired=true;
}
works for me.
I'm using the 2.5.2 library, I tried the 2.5.1, 2.5.0, 2.4.2, 2.4.1
I think i am still on 2.4.2, but have you tried with my pin numbers ? using the 'Boot' button as a trigger ?
I can't try anything, because the board keeps resetting itself even with nothing connected on it (except, obviously, the usb cable).
EDIT 1:
I tried using Arduino IDE 1.8.10 on a different computer, same errors.
By the way, programs with NO INTERRUPTS in it work perfectly. I'm starting to think that nodeMCU ESP8266 have some issues with interrupts, maybe in the libraries or in the hardware configuration itself.
CyberBarby:
I can't try anything, because the board keeps resetting itself even with nothing connected on it (except, obviously, the usb cable)
So you can't upload anything ? not even blink ? well then my support seems rather pointless. Back to basics, disconnect all, drop the ICACHE_RAM_ATTR,
ICACHE_RAM_ATTR;btw what do you think this does once ICACHE_RAM_ATTR is undefined ?
Use the pin nrs i use, it should just compile and not reset itself (if it does not with blink, if it does with blink it's obviously not good)
Ok I've found the problem.
(THIS CODE WORKS!!!)
//#define ICACHE_RAM_ATTR
const int interruptPin = 0;
volatile bool intfired=false;
void setup() {
pinMode(2, OUTPUT);
pinMode(interruptPin, INPUT);
//ICACHE_RAM_ATTR;
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE);
Serial.begin(115200);
Serial.println();
Serial.print("Built in LED is pin : ");
Serial.println(LED_BUILTIN,DEC);
Serial.println("Interrupt Test Program");
}
ICACHE_RAM_ATTR void handleInterrupt() { //THE PROBLEM IS THIS LINE!
intfired=true;
}
void loop() {
digitalWrite(2, LOW);
delay(500);
digitalWrite(2, HIGH);
delay(500);
if (intfired) {
Serial.println("Interrupt Detected");
intfired=false;
}
}
If I put:
void ICACHE_RAM_ATTR handleInterrupt()
the code REQUIRE a:
#define ICACHE_RAM_ATTR
AND a:
ICACHE_RAM_ATTR;
before any attachInterrupt().
If one of the two lines, or both of them, are not present, the code will not compile correctly and will ouput: "handleInterrupt' was not declared in this scope".
If I put:
ICACHE_RAM_ATTR void handleInterrupt()
the code and the interrupts works perfectly and no resets will occur.