Has anyone used the ESP8266TimerInterrupt library in an IoT Cloud sketch?

Hello,

I'm trying to use the timer interrupt on an Wemos D1 R2 ESP8622 board but can't get my sketch to compile in the IoT environment.

I'd like to know whether anyone has successfully used the ESP8266TimerInterrupt library in an IoT Cloud sketch to see whether it's a library issue or my code.

I'm using the Arduino ESP8266TimerInterrupt library and following the example from the repository here, which blinks the LED under interrupt control.

When I run the code in a stand-alone configuration it all compiles correctly and works fine. But when I port the example to an IoT Thing I get the compilation error below:

/usr/local/bin/arduino-cli compile --fqbn esp8266:esp8266:d1_mini:baud=921600,dbg=Disabled,eesz=4M,exception=disabled,ip=lm2f,lvl=None____,vt=flash,wipe=none,xtal=80 --libraries /home/builder/opt/libraries/latest --build-cache-path /tmp --output-dir /tmp/560047697/build --build-path /tmp/arduino-build-A24E174DC5127AF93C8D609DAF4D46C6 /tmp/560047697/003_Thing_Platform_ESP8266_sep14a
Using library arduinomqttclient_0_1_6 at version 0.1.6 in folder: /home/builder/opt/libraries/arduinomqttclient_0_1_6
Using library esp8266timerinterrupt_1_6_0 at version 1.6.0 in folder: /home/builder/opt/libraries/esp8266timerinterrupt_1_6_0
/tmp/560047697/003_Thing_Platform_ESP8266_sep14a/003_Thing_Platform_ESP8266_sep14a.ino:52:16: error: expected initializer before 'TimerHandler'
void IRAM_ATTR TimerHandler()
^
/tmp/560047697/003_Thing_Platform_ESP8266_sep14a/003_Thing_Platform_ESP8266_sep14a.ino:53:16: error: expected initializer before 'TimerHandler'
void IRAM_ATTR TimerHandler()
^
Error during build: exit status 1

I've tried with both the browser-based compilers and get the same results. I'm running Edge in Windows 11.

My code is below. Can anyone suggest a fix, please?

#include "thingProperties.h"

//=======================================================================================================
// Copied directly from reference example at
//    https://github.com/khoih-prog/ESP8266TimerInterrupt/blob/master/examples/TimerInterruptTest/TimerInterruptTest.ino
//=======================================================================================================
#if !defined(ESP8266)
  #error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif

// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG         1
#define _TIMERINTERRUPT_LOGLEVEL_     1

// Select a Timer Clock
#define USING_TIM_DIV1                false           // for shortest and most accurate timer
#define USING_TIM_DIV16               false           // for medium time and medium accurate timer
#define USING_TIM_DIV256              true            // for longest timer but least accurate. Default

#include "ESP8266TimerInterrupt.h"

#define BUILTIN_LED     2       // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED

volatile bool statusLed = false;
volatile uint32_t lastMillis = 0;

#define TIMER_INTERVAL_MS       1000

// Init ESP8266 timer 1
ESP8266Timer ITimer;

void IRAM_ATTR TimerHandler()
{
  static bool started = false;

  if (!started)
  {
    started = true;
    pinMode(BUILTIN_LED, OUTPUT);
  }

  digitalWrite(BUILTIN_LED, statusLed);  //Toggle LED Pin
  statusLed = !statusLed;

#if (TIMER_INTERRUPT_DEBUG > 0)
  Serial.println("Delta ms = " + String(millis() - lastMillis));
  lastMillis = millis();
#endif
}
//=======================================================================================================
//=======================================================================================================

void setup() {
  Serial.begin(9600);
  delay(1500); 
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  //=====================================================================================================
  // Copied directly from reference example at:
  //    https://github.com/khoih-prog/ESP8266TimerInterrupt/blob/master/examples/TimerInterruptTest/TimerInterruptTest.ino
  //=====================================================================================================
  Serial.print(F("\nStarting TimerInterruptTest on ")); Serial.println(ARDUINO_BOARD);
  Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
  Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));

  // Interval in microsecs
  if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
  {
    lastMillis = millis();
    Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(lastMillis);
  }
  else
    Serial.println(F("Can't set ITimer correctly. Select another freq. or interval"));
  //=====================================================================================================
  //=====================================================================================================
  
}

void loop() {
  ArduinoCloud.update();
  
}

Try changing

void IRAM_ATTR TimerHandler()

to

void ICACHE_RAM_ATTR TimerHandler()
1 Like

Genius! That works now. Thanks @Deva_Rishi.

Where did you get that one from?

Starting ESP8266 core 3.x.x, ICACHE_RAM_ATTR was changed into IRAM_ATTR to conform with the ESP32 core. ICACHE_RAM_ATTR was deprecated, but i think it still works in the newest core. Unfortunately newer libraries use IRAM_ATTR which doesn't work in the older cores, and i suspect that the IOT thing uses an older core version.

52:16: error: expected initializer before 'TimerHandler'

states where the error is, so it was clear where to look.
btw, you marked your own post as the solution, but i guess that should be my post (not that care i am happy it is solved !)

That's really useful information, thanks for taking the time.

I've corrected the solution tick box.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.