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:
- 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);
}
- 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 Overflowc++ - arduino (esp8266/esp32) ticker callback class member function - Stack Overflow
ESP32 stock ticker with sensor data
Error in defining Ticker in ESP32 library - Arduino Stack Exchange
How to use Ticker correctly
Videos
Made an ESP32 stock ticker that uses the free Finnhub api and json to grab stock prices. Had room on the board and threw down sensors I had in my library. It’s open source and can work on sharing the code, pcb & parts if there is interest.