Using state machines for MySensors sketch



  • As I have been struggling in the past to coordinate states in my sketches, I recently started using state machines. I'm very happy with the result and thought I might share it here should anyone feel interested.

    The code can be found here:
    https://github.com/freynder/LowPowerButtons

    It uses the Automaton library as a framework to run the state machine logic and let the machines interact.
    https://github.com/tinkerspy/Automaton
    There is a small learning curve but I think it's worth it.

    The example sketch consists of a binary switch sensor that uses 2 state machines:

    • SleepyMachine: Will remain awake during a short time and try to detect any button presses. When a timeout is reached it will turn the device into sleep mode. When waking up it will try to find out why and if a button may have been pressed, it will wait for the button to be handled or return to sleep after a short timeout.
    • MySensorsSender: contains logic to send MySenors messages and handle acknowledgement + implements a retry mechanism with timeouts. The retry mechanism is probably redundant as RFM69 performs retries on the transport level already, but it is available should you want it.
    • Button (provided by the framework): handles button presses and debounce logic.

    All the sketch logic is contained within the state machine logic and their interactions; the main loop is very minimal.

    I also used state machines in another node for eliminating doubles while handling RF communications, I can share that as well if interested.


  • Mod

    @freynder said in Using state machines for MySensors sketch:

    As I have been struggling in the past to coordinate states in my sketches

    What problem did you have?


  • Admin

    I used a partly homemade statemachine in my heatcontroller. I think the original idea for the layout came from @scalz, which I then modified to my liking.

    So, you don't need a large and fancy library to create a basic statemachine.



  • @tbowmo said in Using state machines for MySensors sketch:

    I used a partly homemade statemachine in my heatcontroller. I think the original idea for the layout came from @scalz, which I then modified to my liking.

    I had a look at your code. Looks clean indeed, similar to the state machine in the MySensors transport layer.

    So, you don't need a large and fancy library to create a basic statemachine.

    Agreed. I just prefer to use a framework when possible that takes care of some of the details as the overhead is not very large. The framework also helps running multiple state machines together and let them interact in a structured way.



  • @gohan said in Using state machines for MySensors sketch:

    @freynder said in Using state machines for MySensors sketch:

    As I have been struggling in the past to coordinate states in my sketches

    What problem did you have?

    Running all desired functionality concurrently:

    • handle both buttons (including debounce)
    • sending, waiting for ack and retrying if needed
    • Sleep handling (wakeup, wait for possible activity (not-yet-debounced button, timeouts, go to sleep faster after send instead of waiting for a timeout)
    • perform battery voltage check every x times

    Using the framework I could define these as loosely coupled state machines, not needing to wory about combined states except for well defined interactions between them (triggering events). The battery voltage check was simple enough to forego the state machine but could have been one as well.



  • Maybe off-topic, but those that don't know about Finite State Machines could have a look at these links for a start...
    https://en.wikipedia.org/wiki/Event-driven_finite-state_machine
    https://gamedevelopment.tutsplus.com/tutorials/finite-state-machines-theory-and-implementation--gamedev-11867


Log in to reply
 

Suggested Topics

  • 1
  • 2
  • 10
  • 198
  • 6
  • 1

36
Online

11.4k
Users

11.1k
Topics

112.6k
Posts