Add simple timer example.

This commit is contained in:
mattbk 2023-08-26 20:55:28 -05:00
parent 76ef0b129f
commit 856ada13e8
2 changed files with 44 additions and 2 deletions

View File

@ -16,3 +16,4 @@ monitor_speed = 115200
lib_deps = lib_deps =
me-no-dev/AsyncTCP@^1.1.1 me-no-dev/AsyncTCP@^1.1.1
me-no-dev/ESP Async WebServer@^1.2.3 me-no-dev/ESP Async WebServer@^1.2.3
contrem/arduino-timer@^3.0.1

View File

@ -16,6 +16,7 @@
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <SPIFFS.h> #include <SPIFFS.h>
#include <Preferences.h> #include <Preferences.h>
#include <arduino-timer.h>
// download zip from https://github.com/me-no-dev/ESPAsyncWebServer and install. // download zip from https://github.com/me-no-dev/ESPAsyncWebServer and install.
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
@ -64,6 +65,21 @@ String output27State = "off";
const int output26 = 26; const int output26 = 26;
const int output27 = 27; const int output27 = 27;
// Timers
auto timer = timer_create_default();
auto time_until_start = timer_create_default();
// Example from https://github.com/contrem/arduino-timer#examples
bool toggle_led(void *) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
return true; // keep timer active? true
}
// defaults
// String yourInputString;
// int yourInputInt;
// float yourInputFloat;
void notFound(AsyncWebServerRequest *request) { void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found"); request->send(404, "text/plain", "Not found");
} }
@ -118,6 +134,11 @@ String processor(const String& var){
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
// Timer example, blink main LED
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
// call the toggle_led function every 1000 millis (1 second)
timer.every(1000, toggle_led);
// Initialize the output variables as outputs // Initialize the output variables as outputs
pinMode(output26, OUTPUT); pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT); pinMode(output27, OUTPUT);
@ -138,6 +159,11 @@ void setup() {
} }
#endif #endif
// Read in existing data
// String yourInputString = readFile(SPIFFS, "/inputString.txt");
// int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt();
// float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) { if (WiFi.waitForConnectResult() != WL_CONNECTED) {
@ -176,14 +202,25 @@ void setup() {
} }
Serial.println(inputMessage); Serial.println(inputMessage);
request->send(200, "text/plain", inputMessage); request->send(200, "text/plain", inputMessage);
// // Update data from files
// String yourInputString = readFile(SPIFFS, "/inputString.txt");
// int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt();
// float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
}); });
server.onNotFound(notFound); server.onNotFound(notFound);
server.begin(); server.begin();
} }
void loop() { void loop() {
// Read seconds from file // Timers
time_until_start.tick();
timer.tick();
String yourInputString = readFile(SPIFFS, "/inputString.txt");
int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt(); int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt();
float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
// Blink LED according to seconds entered // Blink LED according to seconds entered
if (yourInputInt > 0) { if (yourInputInt > 0) {
Serial.println("GPIO 26 on"); Serial.println("GPIO 26 on");
@ -195,7 +232,11 @@ void loop() {
output26State = "off"; output26State = "off";
digitalWrite(output26, LOW); digitalWrite(output26, LOW);
delay(yourInputInt * 1000); delay(yourInputInt * 1000);
} } else {
output26State = "off";
}
// // To access your stored values on inputString, inputInt, inputFloat // // To access your stored values on inputString, inputInt, inputFloat
// String yourInputString = readFile(SPIFFS, "/inputString.txt"); // String yourInputString = readFile(SPIFFS, "/inputString.txt");