With ESP32 (Arduno IDE) what is the best way to replace delay (ms) so as to not interfere with things like WiFi, Bluetooth etc???
THANKS!
Regards, Terry King…In The Woods In Vermont
The one who dies with the most Parts LOSES! WHAT DO YOU NEED??
It depends on what you are doing. You can use the millis() function for timing and/or use interrupts. Adafruit has a good tutorial series.
Hi Terry.
As Steve mentioned, you can use millis().
You can also try to use taskScheduler: https://github.com/arkhipenko/TaskScheduler that uses the milis() function on the background.
Regards,
Sara
Hi Sara,
First MANY Thanks to you and to Rui for the incredible effort you have put into making Arduino and ESP32 understandable and fun for so many people! I write http://ArduinoInfo.Info which took a different approach and has nowhere near the quantity and detail of information that you guys have done.
I will come back here with my results and experiences. I have also messaged Rob Tillart who wrote stopwatch_RT and has written many arduino libraries, to ask similar questions.
I will try TaskScheduler. I love RealTime stuff and wrote a lot in EDL/EDX at IBM 30 years ago, and wrote a working 6502 RTOS. I was trying to avoid the complexity of FreeRTOS. Hmmm…
The project is remote control of generator sets used in many home and RV environments. So, engine starting, stopping, power monitoring etc. We plan to put out a WiFi server that is the user interface: THANKS for your guidance on that stuff!
So, my RealTime Worry is: ESP32 co-hosting Wifi, Server, engine/generator controller state machine, with needed delays and timeout checks. Can you give us any guidance on the response time needed for the WiFi/Server code running in an ESP32 VS other user code??
THANKS! Any comments or suggestions or critique welcome!
Regards, Terry King…In The Woods In Vermont
The one who dies with the most Parts LOSES! WHAT DO YOU NEED??
I don’t believe anybody can give you any guidance there. It would all depend on your use case so you would need to test. WiFi/Server basically monitors for connections. When it get’s a request it checks what it’s for and sends the response. Most of the time it’s idling so you can do a lot. Also remember that the ESP32 has two cores so you can set one to handle HTTP requests and the other to handle everything else.
Hi Steve, Thanks..
I am looking at NOT using regular delay().. I’ll do a lot of testing..
Can you point to example of putting some code in different cores? I need to learn that. And about 10^6 other things 🙂
Better than being Bored and Unemployed…
Regards, Terry
Hi Everyone,
OK, after some discussions I have used Rob Tillart’s “stopwatch_RT” library successfully for doing timing and timeouts for my GeneratorSet engine starting code. See: https://github.com/RobTillaart/StopWatch_RT
After funny compile stuff, I have this example running on ESP32:
// FILE: Stopwatch_RT_Test.ino
// AUTHOR: Rob Tillaart / Terry King
// VERSION: 0.1.1
// PURPOSE: Test timing for engine starting (Fragment)
// DATE: 2021.05.01
// URL: https://github.com/RobTillaart/StopWatch_RT
//
//
/*—–( Import needed libraries )—–*/
#include <StopWatch.h>
/*—–( Declare Constants and Pin Numbers )—–*/
#define MAX_CHOKE_MS 2000
#define MAX_STARTER_MS 10000
//–fragment from main code–
enum { INIT_STATE, // State = 0 Do whatever Initialization needs to be done
STOPPED_NORMAL_STATE, // State = 1
STARTING_MANUALLY_STATE, // State = 2
STARTING_AUTOMATICALLY_STATE, // State = 3
START_SUCCESS_STATE, // State = 4 Might be used for slow choke open and warmup before automatic transfer ?
START_FAILURE_STATE, // State = 5 Send alert ??
RUNNING_STARTUP_STATE, // State = 6 Might be used for warmup before automatic transfer ?
RUNNING_NORMAL_STATE, // State = 7
STOPPING_NORMAL_STATE, // State = 8 Cooldown?
STOPPING_PANIC_STOP_STATE, // State = 9 ALERT??
STOPPING_LOW_OIL,
STOPPED_LOW_OIL,
STOPPING_LOW_FUEL,
STOPPED_LOW_FUEL,
STOPPING_ELECTRICAL_FAULT,
STOPPED_ELECTRICAL_FAULT
} generatorState = INIT_STATE; // Set first state at code start
/*—–( Declare objects )—–*/
StopWatch choke_millis;
StopWatch starter_millis;
//StopWatch sw_secs(StopWatch::SECONDS);
/*—–( Declare Variables )—–*/
bool chokeClosed = false;
bool starterRunning = false;
bool AC_powerDetected = false;
int msChokewasClosed ; // Record the history of this startup event
int msStarterRan ;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(115200);
Serial.println(“TEST OF Stopwatch for engine controls”);
delay(3000);
Serial.println(“ATTEMPTING TO START ENGINE”);
chokeClosed = true;
starterRunning = true;
choke_millis.start();
starter_millis.start();
}//–(end setup )—
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if (chokeClosed)
{
Serial.print(“choke_millis = “);
Serial.println(choke_millis.elapsed());
if (choke_millis.elapsed() >= MAX_CHOKE_MS) // Long enough time
{
Serial.println();
Serial.println(“Opening CHOKE”);
chokeClosed = false;
choke_millis.stop();
}
}
if (starterRunning)
{
Serial.print(“starter_millis = “);
Serial.println(starter_millis.elapsed());
if (starter_millis.elapsed() >= MAX_STARTER_MS)
{
Serial.println();
Serial.println(“STARTER ran too long, failure”);
starterRunning = false;
generatorState = START_FAILURE_STATE;
starter_millis.stop();
while (true) {}; //Example all over…
}
}
// Serial.println();
if (AC_powerDetected) // The generator engine has started
{
generatorState = RUNNING_NORMAL_STATE; // Will open choke, stop starter
msChokewasClosed = choke_millis.elapsed(); // Record this start attempt
msStarterRan = starter_millis.elapsed();
choke_millis.stop();
starter_millis.stop();
}
delay(100);
}//–(end main loop )—
/*—–( Declare User-written Functions )—–*/
//*********( THE END )********
This will work fine for non-blocking delays also. I’ll update when a bunch of code from 3 people gets integrated.
THANKS!
Hi Terry.
Thanks for sharing.
This is definitely a subject that many people are interested in, but there is very little “easy to use” information available.
I also need to search and learn more about subjects related to time.
There isn’t much information available for dual core either. We have this tutorial that is based on the espressif documentation:
I also think that Andreas Spiess has a good video about dual-core: https://youtu.be/k_D_Qu0cgu8
Regards,
Sara