SmartHome with sonoff, esp8266, mqtt and micropython

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
User avatar
kfricke
Posts: 310
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby kfricke » Thu Oct 06, 2016 3:09 pm

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.

User avatar
pythoncoder
Posts: 1382
Joined: Fri Jul 18, 2014 8:01 am

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby pythoncoder » Fri Oct 07, 2016 9:31 am

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.
Peter Hinch

User avatar
kfricke
Posts: 310
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby kfricke » Sun Oct 09, 2016 8:32 am

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.

User avatar
pythoncoder
Posts: 1382
Joined: Fri Jul 18, 2014 8:01 am

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby pythoncoder » Sun Oct 09, 2016 10:35 am

@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?
Peter Hinch

torwag
Posts: 167
Joined: Fri Dec 13, 2013 9:25 am

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby torwag » Tue Oct 11, 2016 10:54 pm

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.

User avatar
kfricke
Posts: 310
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby kfricke » Tue Oct 11, 2016 11:27 pm

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 591 times
IMG_20161012_012429.jpg
IMG_20161012_012429.jpg (160.2 KiB) Viewed 591 times

User avatar
kfricke
Posts: 310
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby kfricke » Wed Oct 12, 2016 7:10 am

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!

User avatar
pythoncoder
Posts: 1382
Joined: Fri Jul 18, 2014 8:01 am

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby pythoncoder » Wed Oct 12, 2016 4:12 pm

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
Peter Hinch

User avatar
kfricke
Posts: 310
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby kfricke » Wed Oct 12, 2016 10:36 pm

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

User avatar
pythoncoder
Posts: 1382
Joined: Fri Jul 18, 2014 8:01 am

Re: SmartHome with sonoff, esp8266, mqtt and micropython

Postby pythoncoder » Thu Oct 13, 2016 5:58 am

D'oh, you are of course right :oops:
Peter Hinch


Return to “Hardware Projects”

Who is online

Users browsing this forum: No registered users and 2 guests