Page 2 of 3

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Thu Oct 06, 2016 3:09 pm
by kfricke
torwag wrote:I still struggle to get mqtt.publish working. Still have no clue why I get this strange error message :(
Bear with me, i only did some subscribe test so far. My sensor nodes do use an older version of umqtt (from the time it had its own branch and not landed in the master branch of micropython-lib).
I hope to find some time for further prototyping on saturday.

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Fri Oct 07, 2016 9:31 am
by pythoncoder
torwag wrote:... One second might be rather long for our case. Is there also any metric about resources? Maybe one is lighter then the other?

I still struggle to get mqtt.publish working. Still have no clue why I get this strange error message :(
I haven't tested uasyncio on the ESP8266 but I would expect it to be more lightweight. On the ESP mine has to be compiled as frozen bytecode. To give a rough guide, I have an application comprising a few modules and running half a dozen threads using my scheduler with MQTT. When running there is about 12KiB of RAM free.

As for mqtt.publish, I'm afraid I can't help. It works fine here.

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Sun Oct 09, 2016 8:32 am
by kfricke
torwag wrote:...
I still struggle to get mqtt.publish working. Still have no clue why I get this strange error message :(
While implementing my microthreading based scheduler i can not confirm your problem. I am using the payload, formatting it into another string and do republish it with no hassle.

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Sun Oct 09, 2016 10:35 am
by pythoncoder
@torwag Re publish, I suggest you post some code that produces the error. The function takes three mandatory positional args, the first of which is self - the MQTTClient instance. Thus if c is such an instance, the following should work:

Code: Select all

c.publish(b'my_topic', b'my_message')
Have you tried the example program example_pub_button.py from the library?

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Tue Oct 11, 2016 10:54 pm
by torwag
Ok, I solved the problem, which was a wired one, due to an mix of an older micropython version and some inabilities on my side to manage git correctly. Now it is working.
I wrote a little script, which subscribes to for different topics (just for testing) and enables publish and subscribe on those topics. This all seems to work rather well.
Thus, I will try the scheduling part now.
Thanks again for the help and discussion.

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Tue Oct 11, 2016 11:27 pm
by kfricke
Got it working with Pythoncoder's microthreading scheduler. Implementing the local button and debouncing this is still missing. I will of course commit this to a Github repository the next days!
Sonoff-Switch_Working.png
Sonoff-Switch_Working.png (68.96 KiB) Viewed 8892 times
IMG_20161012_012429.jpg
IMG_20161012_012429.jpg (160.2 KiB) Viewed 8892 times

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Wed Oct 12, 2016 7:10 am
by kfricke
I know it is difficult without seeing the code, but I am on the way to work now and got no time yesterday to publish the code.

The scheduler is used to pool the MQTT broker in one thread and additionally "loose some time" in another one to let the ESP RTOS do it's work. How do I build a delay into this, so having received a MQTT message can introduce some hysteresis for the relay interval? This one's of course needs to be combined with the local switch, which needs to be debounced and forced into the same hysteresis (the switch should also toggle the relay).

Any directions for implementing this asynchronous are very welcome!

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Wed Oct 12, 2016 4:12 pm
by pythoncoder
Given that you're using my scheduler there are already classes for debouncing switches and pushbuttons (the latter is a switch with support for events like long presses and double clicks) - just use one of those.

Providing a minimum on or off duration for the relay should be trivial, and I mentioned the following approach in a recent thread on this topic. Assuming you have a global variable demand_state, and relay is a Pin instance, I suggest something on these lines:

Code: Select all

def relay_control(relay):
   global demand_state
   yield
   while True:
      if relay() != demand_state:
         relay(demand_state)
         yield 0.5  # Minimum on/off duration

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Wed Oct 12, 2016 10:36 pm
by kfricke
Got a few spare minutes to try your suggestions.

After getting the head around the yield directive and the cooperative micro-threading it simply works like a charm. But your code example does miss a 'yield' in the case when nothing needs to be switched.

https://github.com/kfricke/micropython-sonoff-switch

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Posted: Thu Oct 13, 2016 5:58 am
by pythoncoder
D'oh, you are of course right :oops: