[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] non blocking sleep

Carl Karsten <carl@xxxxxxxxxxxxxxxx> writes:

> When the door opens, the light should stay on for 60 seconds.
> or if the button is pressed, turn the light off 30 seconds later.
> every door open resets the 60 seconds.
>
> (this is the simplified version that I think is the root of my problem)

This is not an MQTT issue, but I can see it comes up often in things
that want to use MQTT.

> I can't do "on, sleep(60), off" because that will block the on_button
> (right?)

If the main control program calls sleep, it will ignore all other inputs
for that time.  So don't do that :-)

> and when the button is pressed, if the door opens again that should disable
> the "off in 30" thing.

A further complexity is humans asking for lights to be in certain states
and automatic rules at the same time.  In this case I think you want a
virtual light controlled by the human and a virtual light controlled by
the rules and the real light to be the OR of those.

> Is there a tutorial page or something I should be reading?

This is really a fundamental programming issue; you are constructing a
finite state machine with timers.

https://en.wikipedia.org/wiki/Finite-state_machine

There are arguably two approaches to this sort of thing, usually called
threads and async.   It will seems that threads are easier, but really
they just look easier and async is actually easier, when you define that
as "work to get a 100% reliable system".

Frameworks that are set up for your problem include

  https://nodejs.org/en/about/

  http://docs.libuv.org/en/v1.x/

  https://nodemcu.readthedocs.io/en/master/ (but this is Lua on ESP8266,
  probably not where you should run the controller)


but really you sound like you are starting down the path of slowly
reinventing a home automation system, so have a look at

  https://www.home-assistant.io/