Hi Peter,
I got your MQTT-as working on a standard sonoff device.
Even more, I was able to use your aswitch and Delay_ms functions to create a proper asynchronous function for the (on board) press button. Having a device as as small as sonoff and even three possible trigger with a single button is really nice.
With a little hardware hack, one can free another GPIO (albeit there is now already a two channel version). This enables one to switch another (lower power DC device) or read in a sensor.
Right now I am really happy with the solution. I need to give it some "real world" testing to see how it behaves in case of network failures but overall it looks very good. I added mqtt_as and the uasyncio libs as froozen bytecode to the firmeware.
That gives me still enough space for quite a bit of logic:
DONE:
long button press: turns on resp. off the device
double button press: turns the device on (or resets the timer) and set a timer to be off in 5 min
each of those are accompanied with a proper MQTT message
receiving MQTT messages and turn on or turn off the device
MAYBE:
I could go with a the idea to send a timer information in addition via MQTT, then I would not need to turn the device off, but receive it is switched off. That would save sending an additional mqtt message and might have some safety advantages as there wil be no situation where the device keeps on, due to a lost network in the meantime. I will investigate this.
A overall watchdog. The device should turn off, as soon as the watchdog runs out of time, e.g. due to some network problems
Just to give you an idea of the application:
I installed an irrigation system in my greenhouse. To operate it, I have to switch on a 230V pump and open a valve, as the pump is usually be used for a few other watering tasks. If this works out reliable with a single sonoff unit, and some wiring for the 12V valve, I am pretty happy. It will be added to my home assistant server and in the future maybe some humidity and water sensors will be added as well.
Finally one small challenge remains:
Starting the esp, I get the following error from time to time, dropping back in REPL.
Code: Select all
Waiting for network to come up
WebREPL daemon started on ws://192.168.4.1:8266
WebREPL daemon started on ws://0.0.0.0:8266
Started webrepl in normal mode
b'dd2e0700'
Connected to broker
Traceback (most recent call last):
File "main.py", line 101, in <module>
File "main.py", line 99, in <module>
File "uasyncio/core.py", line 132, in run_until_complete
File "uasyncio/core.py", line 121, in run_forever
File "uasyncio/core.py", line 85, in run_forever
File "uasyncio/core.py", line 129, in _run_and_stop
File "main.py", line 70, in main
File "mqtt_as.py", line 354, in connect
File "mqtt_as.py", line 202, in _connect
File "mqtt_as.py", line 118, in _as_read
OSError: [Errno 103] ECONNABORTED
As you can see, I took care already that the network is up and ready. Only then I go on and start the MQTT client.
I thought I have a race condition between some of your code and the time it takes to set-up the wifi network.
Calling import main from the REPL again, works always.
Any idea on that?
Finally also a big thanks to @pfalcon who worked on the underlying uasyncio function... GREAT JOB!