Skip to content

Examples

Jim edited this page Jul 24, 2024 · 6 revisions

DemoTemplate

This Arduino program demonstrates functionalities for an ESP8266, ESP32, or Raspberry Pi Pico W (with WiFi) using libraries like ElegantOTA, TaskSched, TelnetSpy, and PubSubClient. Here's a breakdown of its key features:

Network Connection and Management:

  • Connects to a specified WiFi network (SSID and password are defined in the code).
  • Implements a retry mechanism if the initial connection attempt fails.
  • Obtains the IP address once connected and logs it.

Web Server:

  • Starts a basic web server that serves a simple message ("Hi! I am ESP8266.") when a client connects to the root path (/).

Over-the-Air (OTA) Updates:

  • Integrates the ElegantOTA library for receiving firmware updates wirelessly.
  • Provides callbacks for handling different stages of the OTA update process (start, progress, and end).

MQTT Communication:

  • Connects to an MQTT broker (mosquitto.org in this example) using the PubSubClient library.
  • Publishes a message to a topic ("test/connection") with the device's IP address upon successful connection.
  • Regularly checks the connection status and attempts to reconnect if the connection is lost.

Task Scheduling:

  • Utilizes the TaskSched library to manage and schedule different tasks within the program.
  • Defines multiple tasks for various purposes:
    • Connecting to the network
    • Waiting for network connection
    • Connecting to the MQTT broker
    • Waiting for MQTT connection
    • Performing actions if connected (potentially user-defined logic)
    • Handling reconnection attempts for both network and MQTT
  • The runForever task continuously checks the network and MQTT connection status and triggers reconnection attempts if necessary.

Telnet Support:

  • Includes the TelnetSpy library to allow users to connect and interact with the device using a Telnet client.
  • Provides welcome messages and disconnection notifications for Telnet sessions.

Serial Communication:

  • Uses the Serial object for logging information and debugging purposes.
  • Prints messages throughout the program's execution to track different stages and events.

Overall, this program demonstrates a versatile setup for an ESP-based device with features for network connectivity, web server functionality, OTA updates, MQTT communication, task scheduling, Telnet support, and informative serial communication.

SkedBlink1

This Arduino program demonstrates basic task scheduling using the TaskSched library to control LED blinking. Here are its key features:

LED Blinking:

  • Blinks an LED connected to the LED_BUILTIN pin (adjust if needed for your board).
  • Toggles the LED on and off every second for a total of 20 iterations (40 seconds).

Task Scheduling:

  • Utilizes the TaskSched library to create and manage two tasks:
    • turnLedOn: Turns the LED on, prints a message with the current time (OnTime), and sets the interval for the next execution (2 seconds).
    • turnLedOff: Turns the LED off, prints a message with the current time (OffTime), sets the interval for the next execution (initially 1 second, then switches to 2 seconds), and restarts the turnLedOn task.
  • Tasks are scheduled with specified intervals and total iterations.
  • The scheduler.addTask function adds tasks to the scheduler.

Serial Communication:

  • Uses Serial communication to print messages for debugging and monitoring.
    • Prints "Turn on" and "Turn off" messages for each task execution.
    • Prints the current time ("OnTime" and "OffTime") when turning the LED on or off using millis() and formatting milliseconds as a string.
    • Prints the scheduler's status information using scheduler.displayStatus(true).

Loop Function:

  • The loop function simply calls the scheduler.run() function to execute the scheduled tasks.

Limitations:

  • The program includes commented-out code for validating even/odd second blinks, which is not implemented in the current version.
  • The program doesn't explicitly handle the end condition (20 iterations) for the LED blinking.

Overall, this program provides a basic example of task scheduling and LED blinking using the TaskSched library.

SkedBlink2

This Arduino program demonstrates a more advanced use of task scheduling and LED blinking using the TaskSched library, with the ability to dynamically change LED behavior through serial commands. Here's a breakdown of its features:

LED Blinking and Task Switching:

  • Aims to turn an LED on and off every second for 20 iterations.
  • Achieves this by using a single task (t) that dynamically switches its callback function between turnLedOn and turnLedOff within the same task object.
  • turnLedOn turns the LED on, sets the callback to turnLedOff, and names the task "Off."
  • turnLedOff turns the LED off, sets the callback to turnLedOn, and names the task "On."
  • This approach avoids creating separate tasks for turning the LED on and off, reducing memory usage.

Task Management:

  • Defines three tasks:
    • t: The main task that alternates between turnLedOn and turnLedOff.
    • t1: A dummy task that runs four times every second (for demonstration purposes).
    • t2: Another dummy task that runs every two seconds (demonstrates different constructor options).
  • Uses scheduler.addTask to add tasks to the scheduler.

Serial Communication and Dynamic Control:

  • Allows sending commands through the serial monitor to retrieve the scheduler's status.
  • Parses incoming serial data using string manipulation functions (strtok) to identify commands.
  • If the command starts with "status," it retrieves and prints the scheduler's status information.
  • It demonstrates two different methods of enumerating the elements of a list, specifically the list of tasks that the scheduler maintains.

Additional Points:

  • The program includes comments explaining the usage of different TaskSched library functions and constructors.
  • It demonstrates dynamic memory allocation using new for the temporary buffer (buf) used during serial data parsing.
  • A safety check is implemented to limit the length of received serial data to avoid potential buffer overflows.

Overall, this program showcases a more intricate approach to task scheduling and LED control, along with the ability to interact with the program through serial commands.

Here's a sample output from sending "status" and "list" to the SkedBlink2.ino app:

137 Found status

Task On, Enabled? 0, Diff 01:09.816, Interval 00:01.000, RunIm 0\\
Task On1, Enabled? 0, Diff 01:05.543, Interval 00:01.000, RunIm 0
Task On2, Enabled? 0, Diff 01:29.292, Interval 00:02.000, RunIm 0

137 Found status

Task On, Enabled? 0, Diff 10:41.680, Interval 00:01.000, RunIm 0
Task On1, Enabled? 0, Diff 10:37.407, Interval 00:01.000, RunIm 0
Task On2, Enabled? 0, Diff 11:01.156, Interval 00:02.000, RunIm 0

The first field in the output is the name, the value of the enabled flag is shown next followed by the interval between the last time the task was run and the millisecond timer. If the task had been enabled that interval would never have been greater than the interval value. The interval is show as MM:SS.MSE as produced by the formatMS() function. The last field is the boolean value of the run immediately flag.

137 Found list

This output was produced by asking the getTasks function
to provide a list of the tasks.  It then prints the names
of each task.
Name=Off
Name=On1
Name=On2
This output was produced by using the read function of the SimpleList class
to return a task.  We use the task getName function to print the name.
Name=Off
Name=On1
Name=On2

Look at the source for the SkedBlink2.ino to see how these different outputs were structured. If we run these last two tests multiple times, the first example will give the same results each time. The second one will only give you results the first time. After that the "buffer" is empty. To get it to give you more results, you need to issue a "rewind()" function before starting the reads.

Clone this wiki locally