-
Mon 2020.01.20
I like the improved, reordered layout of the code block in revison at #9 post. Nicely done example for others requiring MQTT.
ref last pp. of post #7
It should be noted that, L6 and L15 will execute within microseconds of each other, not 2 seconds apart, attempting to force Espruino into potential WiFi setup conflict. This can be proven using the debugger and WebIDE to reveal this fact.
EDIT: Tue 2020.01.21 clarification regarding Gordon's comment in #11 at least one person is actually reading and following along ;-) Edits crossed paths and left out the qualifier: 'should the 'connected' event L15 occur near simultaneously after the first L6 setTimeout() interval has started' This would then be attempting to set up the AP at the approximate same time the server is attempted to be set up. A lot to do with WiFi simultaneously! Although that reference wasn't the perfect example, my point shouldn't be overlooked. Two seconds is not passing waiting for L8 and then L14. During development, I get around this by using prime numbers with trailing '000' so they most likely won't overlap and as to more easily track which timer trips and in which order. Having identical interval values will cloud that understanding.
The code block suggestion that @Gordon graciously whipped together in post #8 was what I implied in the pseudo code reference in 2nd to last pp. in #7 post.
Here is an excellent explanation:
Although your setup may work satisfactory, using the logic layout in Gordon's code block and tweaking the timeouts, should allow for a solid working state in around a second or so, and not the ten seconds currently required.
Thanks for the help. It does seem that the wifi.on('connected' event is only triggered once, and only on wifi.connect and not wifi.startAP (based on login inside the event handler).
Just from a design perspective, it does make sense to do certain things if and only if the wifi actually made a successful connection, so that's why I've stuck with keeping things inside the handler. Also, now with the into delays, everything has been working 100% of the time.
Here's what I finally settled on (relevant functions only):