The implementations of the two Ticker.h files are a bit different. On ESP8266, the once method expects type "callback_function_t" where callback_function_t is defined as:

typedef std::function<void(void)> callback_function_t;

On ESP32, it expects type "callback_t" which is defined as:

typedef void (*callback_t)(void);

In your code example, std::bind provides the std::function type that is expected. This is not the case for the ESP32 Ticker.h, it expects a function pointer. You have two options:

  1. Instead of having the onTickerCallback function part of the Test class, just create a free function for the callback. (Note that this is only acceptable if the callback doesn't need to be part of the Test class).
#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void callbackFunction() {
  Serial.println("Hello");
}

void Test::start(){
  ticker.once(5, callbackFunction);
}
  1. Create a free function that takes an instance of test and use this to call your function. (Note this will also require onTickerCallback to be public, no real way around this that I can think of).
#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void callbackFunc(Test* testInstance) {
  testInstance->onTickerCallback();
}

void Test::start(){
  ticker.once(5, callbackFunc, this);
}

void Test::onTickerCallback() {
  doSomething();
}

void Test::doSomething() {
  Serial.println("Hello");
}

Bonus: After thinking about it for a second, you could use a lambda instead of creating the function (note you need to have a + sign before the lambda in order for it to work as a function pointer). This would look like:

#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void Test::start(){
  ticker.once(5, + { testInstance->onTickerCallback(); }, this);
}

void Test::onTickerCallback() {
  doSomething();
}

void Test::doSomething() {
  Serial.println("Hello");
}
Answer from Dan on Stack Overflow
🌐
GitHub
github.com › espressif › arduino-esp32 › blob › master › libraries › Ticker › src › Ticker.h
arduino-esp32/libraries/Ticker/src/Ticker.h at master · espressif/arduino-esp32
Ticker.h - esp32 library that calls functions periodically · · Copyright (c) 2017 Bert Melis. All rights reserved. · Based on the original work of: Copyright (c) 2014 Ivan Grokhotkov.
Author   espressif
🌐
GitHub
github.com › sglvladi › Ticker_ESP32 › blob › master › Ticker.h
Ticker_ESP32/Ticker.h at master · sglvladi/Ticker_ESP32
esp32 library that calls functions periodically (similar to "Ticker.h" for esp8266) - Ticker_ESP32/Ticker.h at master · sglvladi/Ticker_ESP32
Author   sglvladi
Discussions

c++ - arduino (esp8266/esp32) ticker callback class member function - Stack Overflow
The implementations of the two Ticker.h files are a bit different. On ESP8266, the once method expects type "callback_function_t" where callback_function_t is defined as: Copytypedef std::function callback_function_t; On ESP32, it expects type "callback_t" which is defined as: More on stackoverflow.com
🌐 stackoverflow.com
ESP32 stock ticker with sensor data
Next, someone is selling an algorithm that predicts stock prices based on humidity over temperature More on reddit.com
🌐 r/esp32
59
122
October 11, 2023
Error in defining Ticker in ESP32 library - Arduino Stack Exchange
https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h#L36 · And that's why it works on ESP8266 and not on ESP32 -- either adapt the Ticker library or rework your own code to not use member functions of classes. More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
June 6, 2020
How to use Ticker correctly
Hi, using esp8266 i am trying to make a led turn on only for 0.5 seconds every 10 seconds, for that i am using Ticker but it seems that there is some error because the led turns on only a few milliseconds, it is almost imperceptible, could someone tell me how correct this? More on forum.arduino.cc
🌐 forum.arduino.cc
15
0
August 11, 2022
🌐
GitHub
github.com › Pedroalbuquerque › Ticker-ESP32 › blob › master › Ticker-ESP32.h
Ticker-ESP32/Ticker-ESP32.h at master · Pedroalbuquerque/Ticker-ESP32
Ticker equivalente to ESP8266 to make code ESP32 campatible - Ticker-ESP32/Ticker-ESP32.h at master · Pedroalbuquerque/Ticker-ESP32
Author   Pedroalbuquerque
🌐
TechTutorialsX
techtutorialsx.com › home › esp32 › esp32: ticker library
ESP32: Ticker library - techtutorialsx
August 7, 2021 - In this post we are going to learn how to get started with the Ticker library, using the ESP32 and the Arduino core.
🌐
GitHub
github.com › Pedroalbuquerque › Ticker-ESP32
GitHub - Pedroalbuquerque/Ticker-ESP32: Ticker equivalente to ESP8266 to make code ESP32 campatible
# Ticker.h This lib mimics the Ticker library from ESP8266 package to make code compatible with ESP32 for this library to work properly it should be installed in ESP32 package folder assuming ESP32 package has been installed a folder named ...
Author   Pedroalbuquerque
Top answer
1 of 1
7

The implementations of the two Ticker.h files are a bit different. On ESP8266, the once method expects type "callback_function_t" where callback_function_t is defined as:

typedef std::function<void(void)> callback_function_t;

On ESP32, it expects type "callback_t" which is defined as:

typedef void (*callback_t)(void);

In your code example, std::bind provides the std::function type that is expected. This is not the case for the ESP32 Ticker.h, it expects a function pointer. You have two options:

  1. Instead of having the onTickerCallback function part of the Test class, just create a free function for the callback. (Note that this is only acceptable if the callback doesn't need to be part of the Test class).
#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void callbackFunction() {
  Serial.println("Hello");
}

void Test::start(){
  ticker.once(5, callbackFunction);
}
  1. Create a free function that takes an instance of test and use this to call your function. (Note this will also require onTickerCallback to be public, no real way around this that I can think of).
#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void callbackFunc(Test* testInstance) {
  testInstance->onTickerCallback();
}

void Test::start(){
  ticker.once(5, callbackFunc, this);
}

void Test::onTickerCallback() {
  doSomething();
}

void Test::doSomething() {
  Serial.println("Hello");
}

Bonus: After thinking about it for a second, you could use a lambda instead of creating the function (note you need to have a + sign before the lambda in order for it to work as a function pointer). This would look like:

#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void Test::start(){
  ticker.once(5, + { testInstance->onTickerCallback(); }, this);
}

void Test::onTickerCallback() {
  doSomething();
}

void Test::doSomething() {
  Serial.println("Hello");
}
Find elsewhere
Top answer
1 of 1
4

ESP32 ticker attach functions take pure function pointers with at max one argument type -- not a std::function/ std::_Bind_helper that std::bind produces.

See code and types at https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h#L38.

For the ESP8266 however, they do use a std::function:

https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h#L36

And that's why it works on ESP8266 and not on ESP32 -- either adapt the Ticker library or rework your own code to not use member functions of classes.

Edit: You can easily rewrite your code to use a static helper function with one argument (the watchdog object itself) which then just calls the member function of that object. For this to work, the called function must be public. So a rewrite like

static void watchdog_timer_triggered_helper(myIOT32* watchdog) {
   //needs to be public!
   watchdog->_feedTheDog();
}

void myIOT32::_feedTheDog()
{
  _wdtResetCounter++;
  if (_wdtResetCounter >= _wdtMaxRetries)
  {
    sendReset("Dog goes woof");
  }
}
void myIOT32::_startWDT()
{
  wdt.attach(1, &watchdog_timer_triggered_helper, this); // Start WatchDog
}

void myIOT32::sendReset(char *header)
{
  char temp[150];

  sprintf(temp, "[%s] - Reset sent", header);

  if (useSerial)
  {
    Serial.println(temp);
  }
  if (strcmp(header, "null") != 0)
  {
    pub_msg(temp);
  }
  delay(1000);
  ESP.restart();
}

Since both the ESP32 and ESP8266 versions implement the

    template<typename TArg>
    void attach(float seconds, void (*callback)(TArg), TArg arg)

templated function this should work on both platforms now.

🌐
GitHub
github.com › espressif › arduino-esp32 › blob › master › libraries › Ticker › examples › TickerBasic › TickerBasic.ino
arduino-esp32/libraries/Ticker/examples/TickerBasic/TickerBasic.ino at master · espressif/arduino-esp32
#include <Arduino.h> #include <Ticker.h> · #ifndef LED_BUILTIN · #define LED_BUILTIN 13 · #endif · · Ticker flipper; · int count = 0; · void flip() { int state = digitalRead(LED_BUILTIN); // get the current state of GPIO1 pin · digitalWrite(LED_BUILTIN, !state); // set pin to the opposite state ·
Author   espressif
🌐
PlatformIO
platformio.org › lib › show › 1938 › Ticker-esp32
bertmelis/Ticker-esp32: Ticker library for ESP32
April 26, 2021 - This library acts as a compatibility layer to add Ticker functionality as seen in the ESP8266 core for the Arduino environment. As such the same function calls can be used on ESP32 as on ESP8266. There are a number of differences and usage hints:
🌐
GitHub
github.com › bertmelis › Ticker-esp32
GitHub - bertmelis/Ticker-esp32: Ticker library for the ESP32
March 12, 2019 - This library acts as a compatibility layer to add Ticker functionality as seen in the ESP8266 core for the Arduino environment. As such the same function calls can be used on ESP32 as on ESP8266. There are a number of differences and usage hints:
Starred by 18 users
Forked by 9 users
Languages   C++ 100.0% | C++ 100.0%
🌐
GitHub
github.com › bertmelis › Ticker-esp32 › blob › master › src › ESP32Ticker.h
Ticker-esp32/src/ESP32Ticker.h at master · bertmelis/Ticker-esp32
March 12, 2019 - Ticker.h - esp32 library that calls functions periodically · · Copyright (c) 2017 Bert Melis. all rights reserved · Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
Author   bertmelis
🌐
GitHub
github.com › bertmelis › Ticker-esp32 › blob › master › src › ESP32Ticker.cpp
Ticker-esp32/src/ESP32Ticker.cpp at master · bertmelis/Ticker-esp32
January 21, 2018 - #include "ESP32Ticker.h" · Ticker::Ticker() : _timer(nullptr) {} · Ticker::~Ticker() { detach(); } ·
Author   bertmelis
🌐
techtutorialsx
techtutorialsx.wordpress.com › 2021 › 08 › 30 › esp32-ticker-light-sensor-readings
ESP32 Ticker: Light Sensor readings – techtutorialsx
August 30, 2021 - The tests shown below were performed on a ESP32-E FireBeetle board, also from DFRobot. The first thing we will do is including the Ticker.h library.
🌐
Git
git.liberatedsystems.co.uk › jacob.eva › arduino-esp32 › src › commit › 273196d7e67c489c0ae3efc322059f750877f479 › libraries › Ticker › src › Ticker.h
arduino-esp32/Ticker.h at 273196d7e67c489c0ae3efc322059f750877f479 - arduino-esp32 - Git - Liberated Embedded Systems
arduino-esp32/libraries/Ticker/src/Ticker.h · Bert Melis 8e29347d4f Add Ticker library (#1057) ... * Add Ticker library * Fix CI (#1) remove LED_BUILTIN in examples · 2018-03-04 20:34:03 +01:00 ·
🌐
Wokwi
wokwi.com › projects › 398021638946585601
ejercicio 28 libreria ticker h led - Wokwi ESP32, STM32, Arduino Simulator
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } #include <Ticker.h> Ticker cambiarLed; #if defined(ESP8266) int led = LED_BUILTIN; #elif defined(ESP32) int led = 12; int button1 = 35; int button2 = 34; #endif boolean estado = false; void FuncionLed() { estado = !estado; digitalWrite(led, estado ?
🌐
PlatformIO
registry.platformio.org › libraries › sstaub › Ticker
sstaub/Ticker: A library for creating Tickers which can call…
The world's first package and project dependency management solution for embedded development
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to use Ticker correctly - Programming - Arduino Forum
August 11, 2022 - Hi, using esp8266 i am trying to make a led turn on only for 0.5 seconds every 10 seconds, for that i am using Ticker but it seems that there is some error because the led turns on only a few milliseconds, it is almost i…
🌐
Arduino
arduino.cc › reference › en › libraries › ticker
Ticker | Arduino Documentation
August 11, 2022 - You can change the number of repeats of the callbacks, if repeats is 0 the ticker runs in endless mode. Works like a "thread", where a secondary function will run when necessary. The library use no interupts of the hardware timers and works with the micros() / millis() function.
🌐
GitHub
github.com › espressif › arduino-esp32 › pull › 1057 › files
Add Ticker library by bertmelis · Pull Request #1057 · espressif/arduino-esp32
Ticker.h - esp32 library that calls functions periodically · Copyright (c) 2017 Bert Melis. All rights reserved. Based on the original work of: Copyright (c) 2014 Ivan Grokhotkov.
Author   espressif