{"id":13804755,"url":"https://github.com/nrwiersma/ESP8266Scheduler","last_synced_at":"2025-05-13T18:32:46.679Z","repository":{"id":40725966,"uuid":"67204812","full_name":"nrwiersma/ESP8266Scheduler","owner":"nrwiersma","description":"ESP8266 Co-operative Multitasking","archived":false,"fork":false,"pushed_at":"2024-03-17T13:43:34.000Z","size":32,"stargazers_count":150,"open_issues_count":4,"forks_count":34,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-05T04:32:01.240Z","etag":null,"topics":["esp8266","multitasking","nodemcu","scheduler"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nrwiersma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-02T08:22:58.000Z","updated_at":"2025-01-24T03:35:50.000Z","dependencies_parsed_at":"2024-08-04T01:14:01.400Z","dependency_job_id":null,"html_url":"https://github.com/nrwiersma/ESP8266Scheduler","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nrwiersma%2FESP8266Scheduler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nrwiersma%2FESP8266Scheduler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nrwiersma%2FESP8266Scheduler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nrwiersma%2FESP8266Scheduler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nrwiersma","download_url":"https://codeload.github.com/nrwiersma/ESP8266Scheduler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254003441,"owners_count":21997887,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["esp8266","multitasking","nodemcu","scheduler"],"created_at":"2024-08-04T01:00:53.665Z","updated_at":"2025-05-13T18:32:46.276Z","avatar_url":"https://github.com/nrwiersma.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"![Logo](http://svg.wiersma.co.za/github/project?lang=cpp\u0026title=ESP8266Scheduler\u0026tag=co-operative%20multitasking)\n\n[![Build Status](https://github.com/nrwiersma/ESP8266Scheduler/actions/workflows/test.yml/badge.svg)](https://github.com/nrwiersma/ESP8266Scheduler/actions)\n[![arduino-library-badge](http://www.ardu-badge.com/badge/ESP8266Scheduler.svg)](http://www.ardu-badge.com/ESP8266Scheduler)\n\n# Quick Start\n\n## Installing\n\nYou can install through the Arduino Library Manager. The package name is\n**ESP8266Scheduler**.\n\n## Usage\n\nInclude the library in your sketch\n\n```cpp\n#include \u003cScheduler.h\u003e\n#include \u003cTask.h\u003e\n#include \u003cLeanTask.h\u003e\n```\n\nIn your setup function start the scheduler\n\n```cpp\nScheduler.start(\u0026task);\n\nScheduler.begin();\n```\n\nThe scheduler blocks once begun, so the loop function is called by the scheduler BEFORE the first task is runned each time. But it is recommended to avoid using a loop.\n\n## Creating a Task\n\nTasks are classes that should inherit the ```Task``` class. A task can define a ```loop()``` and ```setup()``` function\nmuch as the normal Arduino standard.\n\n```cpp\nclass BlinkTask : public Task {\n protected:\n  void setup() {\n    state = HIGH;\n\n    pinMode(2, OUTPUT);\n    digitalWrite(2, state);\n  }\n\n  void loop() {\n    state = state == HIGH ? LOW : HIGH;\n    digitalWrite(2, state);\n\n    delay(1000);\n  }\n\nprivate:\n  uint8_t state;\n} blink_task;\n```\n\n**IMPORTANT:** Tasks must be declared globally on the stack (not a pointer). Failure to do so will crash your device.\n\nTasks can run ```yield()``` and ```delay()``` like they normally would. These functions yield control to the scheduler\nrather than the ESP8266.\n\n## Creating a LeanTask\n\nLean tasks are classes that should inherit the ```LeanTask``` class. A lean task can define a ```loop()``` and ```setup()``` function\nmuch as the normal Arduino standard.\n\nLeanTask doesn't use ```cont.h``` (runs in the global context), so ```yield()``` works exactly as it would without using the library. This can be useful for saving ram (4k) if the task does not use ```yield()``` to interrupt the task. You can use ```delay()``` at the end of a task to set the interval.\n\n**Good example:**\n```cpp\nclass MemTask : public LeanTask {\n public:\n  void loop() {\n    Serial.print(\"Free Heap: \");\n    Serial.print(ESP.getFreeHeap());\n    Serial.println(\" bytes\");\n\n    delay(10000);\n  }\n} mem_task;\n```\n\n**Bad example. The first ```delay()``` does nothing:**\n\u003cdetails\u003e\u003csummary\u003eSource. DO NOT DO THIS!\u003c/summary\u003e\n\u003cp\u003e\n\n```cpp\nclass PrintTask : public LeanTask {\n protected:\n  void loop() {\n    Serial.println(\"Print Loop Start\");\n\n    delay(5000);\n\n    Serial.println(\"Print Loop End\");\n\n    delay(5000);\n  }\n} print_task;\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\nTasks can run ```yield()``` and ```delay()``` like they normally would. The ```yield()``` function transfers control to the ESP8266, NOT the scheduler. The ```delay()``` function will tell the scheduler that a delay is needed before the next run. If you REALLY need a delay, use ```::delay()```, but this will block the task and the scheduler.\n\n## Tests using Task vs LeanTask\nAll examples have the same logic. To optimize RAM, use ```LeanTask``` (if possible), because each instance of ```Task``` requires 4 kb of RAM.\n| File | Description | Free heap (more is better) |\n| --- | --- | --- |\n| [simple.ino](examples/simple/simple.ino) | 3 Task | 39896 bytes |\n| [lean_simple.ino](examples/lean_simple/lean_simple.ino) | 1 Task, 2 LeanTask | 48168 bytes |\n| [subfile_simple.ino](examples/subfile_simple/subfile_simple.ino) | 1 Task, 2 LeanTask + main loop | 48136 bytes |\n\n[heap_test.ino](examples/heap_test/heap_test.ino):\n| TASK_TYPE | Description | Free heap (more is better) |\n| --- | --- | --- |\n| Task | 12 Task + main loop | 2280 bytes |\n| LeanTask | 12 LeanTask + main loop | 51912 bytes |\n\n## Task methods\n#### bool AbstractTask::isEnabled();\nMethod return the status of the task: enabled (true) or disabled (false).\n\n---\n\n#### AbstractTask(bool _enabled = true, unsigned long _interval = 0);\nConstructor.\n\n---\n\n#### bool AbstractTask::isEnabled();\nMethod return the status of the task: enabled (true) or disabled (false).\n\n---\n\n#### void AbstractTask::enable();\nThe method enable the task.\n\n---\n\n#### void AbstractTask::disable();\nThe method disable the task.\n\n---\n\n#### void AbstractTask::setInterval(unsigned long val);\nThe method sets the task run interval.\n\n---\n\n#### unsigned long AbstractTask::getInterval();\nThe method returns the task run interval.\n\n\n## Scheduler methods\n#### static void SchedulerClass::start(AbstractTask *task);\nAdds a task to the multitasking queue.\n\n---\n\n#### static void SchedulerClass::begin();\nStarts the scheduler. This function is \"blocking\". It should be the last call the ```setup``` function.\n\n---\n\n#### static void SchedulerClass::delay(unsigned long ms);\nCalls ```AbstractTask::delay()``` on the runned task from outside\n\n**IMPORTANT:** You should not call this method outside for LeanTask tasks. It does not make sense.\n\n---\n\n#### static void SchedulerClass::yield();\nCalls ```AbstractTask::yield()``` on the runned task from outside\n\n**IMPORTANT:** You should not call this method outside for LeanTask tasks. It does not make sense.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnrwiersma%2FESP8266Scheduler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnrwiersma%2FESP8266Scheduler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnrwiersma%2FESP8266Scheduler/lists"}