Giter VIP home page Giter VIP logo

munet's Introduction

muwerk

Dev Docs CMake PlatformIO CI Console Raspberry_Pico

muwerk is a cooperative scheduler with mqtt-like queues.

Dependencies

muwerk relies only on ustd. Check documentation for required platform defines.

Project overview

muwerk provides a cooperative scheduler that allows fixed-intervall task creation on platforms from attiny up to ESP32 and Linux. (Linux support for testing with debuggers and valgrind.)

Tasks can simply be created by:

#define __ESP__ 1  // replace with appropriate platform define
#include "scheduler.h"

ustd::Scheduler sched;

void myTask() {
    // add code for this task here
}

void setup() {
    int tID = sched.add(myTask, "taskname", 50000L);
}

This example creates a task identified by function void myTask() with task-name taskname that is executed every 50ms (50000us). On ESP8266 minimum schedule time is around 50us.

The arduino main look simply needs to contain the line:

void loop() {
    sched.loop();
}

This calls the muwerk scheduler who dispatches the registered tasks.

Statistics

If a message with topic $SYS/stat/get with string-encoded integer N as message is received, the scheduler sends a statistics json object to topic $SYS/stat every N milliseconds. If N is zero, no more stat information is published.

Sample stat json (single output-line from Examples/mac-linux):

{
    "dt" : 500001, "syt" : 57340, "apt" : 347452, "mat" : 10, "upt":2, "mem":2147483647, "tsks" : 2,
        "tdt" : [["task1", 1, 50000, 10, 99240, 7], ["task2", 2, 75000, 7, 34937, 0]]}
Field Explanation
dt µsec since last stat sample
syt time in usec used by OS
apt time in usec used by mwerk tasks
mat time in usec for housekeeping
upt uptime of system in seconds
mem free memory, max. INT_MAX for unixoids
tsks number of muwerk tasks tn
tdt array of tn entries for each task, containing: task-name tname , tid taskID of process, sched_time scheduling time, number of times task was executed during sample time cn, usecs used by this task during this sample sct, accumulated usecs task execution was later than scheduled slt

This example shows a dt=500ms sample, it has two tasks, task1, id=1 was called 10 times, every 50ms, and used average 9.9240ms per call (task-code has approx. 10ms sleep), and task2, id=2 was called 7 times, every 75ms, and used average 4.991ms (5ms sleep in code). Both tasks were always executed as schedules (negligable late-times cn).

See Examples\mac-linux. (Not available on ATTINY platforms, only ATMEGA and better).

For systems that are connected to an MQTT-server (via munet), a python example script mutop shows how to parse the statistical information.

MQTT-like Communications and Architecture Overview

A more complete example is available at blink that shows how tasks can communicate MQTT-style with each other and -- blink a led.

Tasks can communicate with each other using an MQTT-like pub/sub mechanism. On ESP8266 or ESP32 platforms, the internal communication can be exported transparently using munet's interface to the PubSubClient Arduino MQTT library. The scheduler's task information is also exported via MQTT.

See the documentation for more samples on muwerk's task scheduler and pub/sub intertask communication.

               +-------------------------------+
               |            Apps               |  Samples
               +-------------------------------+
                              |
               +-------------------------------+
               |          Mupplets             |  Sensors, IO-libs, reusable functional units
               +-------------------------------+
                     |                |
+------------+       |        +----------------+  Munet: ESP8266 and ESP32 only:
|  Testcode  |       |        |  munet (ESPx)  |  Access point client connection, NTP,
+------------+       |        +----------------+    OTA-update, MQTT (via PubSubClient)
       |             |                |
+----------------------------------------------+
|                   muwerk                     |  Cooperative scheduler and task-to-task
+----------------------------------------------+    MQTT-like communication (pub/sub)
       |             |                |
+----------------------------------------------+
|                    ustd                      |  Minimal implementations of Queue, Map (Dicts),
+----------------------------------------------+    Arrays
       |             |                |
+------------+ +------------+ +----------------+
| Mac, Linux | |Arduino SDK | | ESP8266/32 SDK |  OS and Arduino-Frameworks
+------------+ +------------+ +----------------+

Debugging and Troubleshooting

The Console class allows to bind a serial console to muwerk that allows to inspect message passing, file system (if available) and statistical information. See the mu_console example for more information.

Documentation

Related projects:

  • ustd (micro-stdlib), a minimal implementation of array, vector and map c++ classes that work on all arduino platforms, from 8kb attiny up to ESP32 and Unixoids Mac or Linux (for testing).
  • muwerk (microWerk), a cooperative scheduler and an MQTT-like communication-queue for all arduino devices (attiny up to ESP32 [and Unixoids Mac or Linux for testing])
  • munet, modules for network connectivity for ESP8266 and ESP32 devices, implements Wireless connection to access point, NTP time protocol, OTA over-the-air udpate, MQTT-stack (using [PubSubClient]).
  • mupplets, mupplet-sensor a number of implementations for sensors and io-devices. Mupplets implement processes for muwerk and expose muwerk's pub/sub interface to allow other mupplets or apps to access the mupplet's functionality. Mupplets can be sensor-drivers or or more specialized modules, e.g. clock functionality for a led display.
  • examples show the excellent composability using tasks and messages.

History

  • 0.6.5 (2023-06-02) Support for console command extensions with supplied printer instance
  • 0.6.4 (2022-10-17) Support for ESP32C3, RISC-V.
  • 0.6.3 (2022-08-26) Support update() function for sensor filter params
  • 0.6.2 (2021-02-xx) (not yet released) Scheduler support for rp2040 Raspberry Pico, fix console.h data type.
  • 0.6.1 (2021-02-12)
    • New: numericFunction approximator class: piece-wise linear approximation of a function defined by a set of points (x1,y1), (x2, y2)...(xn,yn) for calibration etc.
    • fix: doctor.h and i2cdoctor.h hat wrong casing for Arduino_JSON.h include.
  • 0.6.0 (2021-01-30) Breaking change for ustd library include: ustd include-files have now ustd_ prefix to prevent name-clashes with various platform-sdks. [queue.h clashed with ESP8266-Wifi, platform.h clashed with RISC-V SDK, hence new names ustd_queue.h and ustd_platform.h etc.]
  • 0.5.5 (2021-01-29) Support for all platforms with Doctor and I2CDoctor.
  • CI (2021-01-28 All supported platforms are build-checked automatically with Github actions.
  • 0.5.4 (2021-01-28) Minor fixes:
    • ustd::Console and ustd::SerialConsole do now honour the USTD_FEATURE_xxx defines.
    • Safe struct inits (no memset())
    • Support for new platforms defined in ustd (new platforms for __ARM__, __RISC_V__)
  • 0.5.3 (2021-01-22) UST_FEATURE_MEM defines used (requires ustd version 0.4.1 or higher), ATtiny T_TASK limited to static functions.
  • 0.5.2 (2021-01-20) library.properties project name repaired, to allow Arduino automatic update.
  • 0.5.1 (2021-01-19)
    • Added readString with length validation to ustd::jsonfile
    • New task ustd::doctor implements a remote diagnostics interface via pub/sub messages.
    • ustd::Console has now a better interface for using other Printer-derived stream objects, ustd::SerialConsole can now be used with alternative serial interfaces
    • Improvements to stability and memory handling on ustd::Scheduler
    • Additional commandline options for mutop
  • 0.5.0 (2021-01-15) Major update including:
    • New Serial Console class that provides a minimal interactive shell that can be extended with custom commands.
    • New portable filesystem functions that provide abstraction between LittleFS and SPIFFS (and in future others).
    • New class for managing JSON files with possibility to access members with an MQTT-topic-like path syntax.
    • New utility classes heartbeat and timeout.
    • New utility functions shift and split for string argument handling.
    • Analysis tool mutop that allows to monitor tasks and resource consumption for mqtt connected systems.
    • $SYS/STAT format expanded to contain uptime, free memory, and per-task infos task-id and schedule-time.
  • 0.4.0 (2021-01-11) Optional serial console for muwerk, file system support for ESP8266 and ESP32.
  • 0.3.2 (2020-12-25) Small platform updates, no functional change.
  • 0.3.1 (2019-11-29) Compile problem with attiny: resetStats() referenced for attiny.
  • 0.3.0 (2019-11-28) Statistical information is no longer flooding serial port, but is published (on demand) to topic $SYS/stat. Use publish to $SYS/stat/get, message body number (as string encoded) to receive stat information every number milliseconds.
  • 0.2.1 (2019-09-19) Functional support for AVRs via ustd::function<>.

References

muwerk is a derivative and lightweight version of Meisterwerk.

munet's People

Contributors

domschl avatar tuxpoldo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

munet's Issues

abort() when using "timeserver" configuration

If I use the "timeserver" in net.json together with the demo project, I get the following error on an ESP32 in the call to configTime:

Reading net.json
/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c:1442 (xQueueGenericReceive)- assert failed!
abort() was called at PC 0x40088869 on core 1

Backtrace: 0x4008c434:0x3ffb1c30 0x4008c665:0x3ffb1c50 0x40088869:0x3ffb1c70 0x4013867a:0x3ffb1cb0 0x40138956:0x3ffb1cd0 0x401278d8:0x3ffb1cf0 0x40127941:0x3ffb1d10 0x4012eb39:0x3ffb1d30 0x4012eb5e:0x3ffb1d50 0x40127737:0x3ffb1d70 0x400db27f:0x3ffb1d90 0x400d49fa:0x3ffb1e00 0x400d4fca:0x3ffb1f50 0x400dbd0b:0x3ffb1fb0 0x40088b7d:0x3ffb1fd0

If I remove the "timeserver" config or move the block containing configTime into the block after WiFi.status() == WL_CONNECTED, the error disappears.

NTP time acquisition fails about 30% of the time

Retries are necessary, some async version of:

  uint8_t  time_retry=0;                                // Counter retry counts time server
  setenv("TZ", "CET-1CEST,M3.5.0/02,M10.5.0/03", 1);
  struct tm initial;                                         // temp struct for checking if year==1970 
  initial.tm_year=70;
  
  while (initial.tm_year == 70 && time_retry < 15) {                 
    configTime(0, 0, "pool.ntp.org");                // get time from NTP server (ESP8266)
    delay(500);
    time_t now = time(&now);
    localtime_r(&now, &initial);
    Serial.print("Time Server connection attempt: ");
    Serial.println(time_retry + 1);
    Serial.print("current year: ");
    Serial.println(1900 + initial.tm_year);
    time_retry++;
  }

  if (time_retry >=15) {
      Serial.println("Connection to time server failed");
  } else {
    time_t now = time(&now);
    localtime_r(&now, &tm);
    strftime (timeshow, sizeof(timeshow), "%H:%M", &tm);
    Serial.print("Successfully requested current time from server: ");
    Serial.println(timeshow); 
  }

error: 'queue' in namespace 'ustd' does not name a type ustd::queue<T_MSG *> msgqueue;

I am using Arduino 1.8.13 because ESP-IDF does not support esp8266 yet, I think. There three esp development enviroments, arduino, pio, and esp-idf.

I copied the code on the README and tried to compile it and I get this error. The arduino IDE does everything for you so I am a little lost, I like cmake better.

Looking at the library source, it seems Ok.
I am lost about this error. Any ideas?

In file included from /home/pi/Arduino/munet/munet.ino:3:0:
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h:209:5: error: 'queue' in namespace 'ustd' does not name a type
ustd::queue<T_MSG *> msgqueue;
^
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h: In constructor 'ustd::Scheduler::Scheduler(int, int, int)':
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h:231:36: error: class 'ustd::Scheduler' does not have any field named 'msgqueue'
: taskList(nTaskListSize), msgqueue(queueSize), subscriptionList(nSubscriptionListSize) {
^
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h: In destructor 'virtual ustd::Scheduler::~Scheduler()':
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h:257:17: error: 'msgqueue' was not declared in this scope
int l = msgqueue.length();
^
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h: In member function 'bool ustd::Scheduler::publish(String, String, String)':
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h:396:20: error: 'msgqueue' was not declared in this scope
return msgqueue.push(pMsg);
^
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h: In member function 'void ustd::Scheduler::checkMsgQueue()':
/home/pi/Arduino/libraries/Muwerk_scheduler_library/scheduler.h:455:24: error: 'msgqueue' was not declared in this scope
while ((pMsg = msgqueue.pop()) != nullptr) {
^
exit status 1
Error compiling for board Generic ESP8266 Module.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.