I tried your sketch with the following, on an ESP32 and it worked fine. void parpadeoLed() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); } I suspect on the ESP8266 the delay statement is using the same timer as Ticker.h so it doesn't work. As an … Answer from red_car on forum.arduino.cc
🌐
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
Ticker.h - esp32 library that calls functions periodically · (similar to Ticker.h for esp8266) · Copyright (C) 2017 Lyudmil Vladimirov · · This program is free software: you can redistribute it and/or modify · ...
Author   sglvladi
Discussions

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
c++ - arduino (esp8266/esp32) ticker callback class member function - Stack Overflow
Can someone help me with the following code ? I have a custom class and I want to define a callback for the ticker the function onTickerCallback(). It compiles and runs on ESP8266 but not on ESP32.... 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
🌐
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
🌐
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…
🌐
TechTutorialsX
techtutorialsx.com › home › esp32 › esp32: ticker library
ESP32: Ticker library - techtutorialsx
August 7, 2021 - How to get started with the Ticker library, using the ESP32 and the Arduino core
🌐
Everything ESP8266
esp8266.com › viewtopic.php
Arduino Ticker use example - Everything ESP8266
The complete fill of the array disables ticker in it's callback routine. // A print button does a serial print of the array data. The print button could be eliminated if // it was desired to print the data upon completion of filling the array. #include <ESP8266WiFi.h> #include <Ticker.h> //---------------------------------------------------- #include <Button.h> //https://github.com/JChristensen/Button const int startSW = 4; // const int printSW = 0; // #define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
🌐
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
Find elsewhere
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");
}
🌐
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:
🌐
techtutorialsx
techtutorialsx.wordpress.com › 2021 › 08 › 30 › esp32-ticker-light-sensor-readings
ESP32 Ticker: Light Sensor readings – techtutorialsx
August 30, 2021 - A site about programming, IoT and technology. Find tutorials for many different projects. Posted on August 30, 2021August 30, 2021 by antepher · In this tutorial we will check how to periodically obtain measurements from an ambient light sensor, using the ESP32 and the Ticker library.
🌐
Brentwoodfoundation
brentwoodfoundation.org › 2gbml6dux › esp32-ticker-example.html
Esp32 ticker example
In this diagram, a high speed channel has been attached to a certain high speed timer module. So I decided to write some code that makes it easy for me to use the ESP32’s hardware timers. h> #include <Ticker. Ticker library for esp32. The article includes a DIY project to program an ESP32 ...
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 › bertmelis › Ticker-esp32
GitHub - bertmelis/Ticker-esp32: Ticker library for the ESP32
March 12, 2019 - Ticker library for the ESP32. Contribute to bertmelis/Ticker-esp32 development by creating an account on GitHub.
Starred by 18 users
Forked by 9 users
Languages   C++ 100.0% | C++ 100.0%
🌐
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.
🌐
Git
git.liberatedsystems.co.uk › jacob.eva › arduino-esp32 › src › commit › fa61b3bffedc0c5b65de69c1a550c7c07d437faa › libraries › Ticker › src › Ticker.h
arduino-esp32/Ticker.h at fa61b3bffedc0c5b65de69c1a550c7c07d437faa - 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 ·
🌐
GitHub
github.com › sstaub › Ticker
GitHub - sstaub/Ticker: Ticker library for Arduino · GitHub
Advice: for use with ESP boards and mbed based Arduino boards like Arduino Nano RP2040 Connect and Raspberry Pi Pico (using the official Arduino core) the TickTwo library https://github.com/sstaub/TickTwo is recommanded avoiding name conflicts. The Arduino Ticker Library allows you to create easily Ticker callbacks, which can call a function in a predetermined interval.
Starred by 209 users
Forked by 36 users
Languages   C++
🌐
GitHub
github.com › mike-rankin › ESP32_Stock_Ticker
GitHub - mike-rankin/ESP32_Stock_Ticker: ESP32 based multiple stocks ticker · GitHub
ESP32 based multiple stocks ticker. Contribute to mike-rankin/ESP32_Stock_Ticker development by creating an account on GitHub.
Starred by 39 users
Forked by 3 users
Languages   C 98.8% | C++ 1.2%
🌐
techtutorialsx
techtutorialsx.wordpress.com › 2021 › 08 › 07 › esp32-ticker-library
ESP32: Ticker library - techtutorialsx - WordPress.com
August 7, 2021 - In our example we will cover how to setup a function to execute periodically and another to fire only once, after a time interval. As such, we will create two Ticker objects, one for each use case. ... Moving on to the Arduino setup, we will first take care of opening a serial connection. The functions that will be triggered by the Ticker objects will make use of it to print a different message each.
🌐
Instructables
instructables.com › circuits › arduino
ESP32 LED Matrix WIFI Ticker Display : 24 Steps - Instructables
March 21, 2023 - ESP32 LED Matrix WIFI Ticker Display: ESP32 powered LED matrix displaying real-time news, weather, stock, date, time, barometer data, menu and web interface with notification. Notes Jan 2023 The API code will need updating before this project will work with internet based se…