sonoff: wifi-based switch-box running micropython & MQTT

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

sonoff: wifi-based switch-box running micropython & MQTT

Post by torwag » Tue Oct 04, 2016 1:39 pm

Hi,
During my adventure of creating my own smart-home system, which does not rely on any company services, I looked out for all those different wifi-based power switch adapters. I found a rather cheap wifi-based switch-box called sonoff.

https://www.itead.cc/smart-home/sonoff- ... witch.html
Interestingly (and I thing we have to honour this) the manufacture makes no secret out of the fact that they utilise an esp8266 for the wifi functionality and the program logic. Furthermore, the box can be easily open and closed (no glue or easy breakable plastic clips) and the PCB enables user to access the esp8266 directly. I guess such an openness make it valid to post a link here (above), without falling into any advertisement category.
However, using the device, with the manufacture firmware, a connection via the users wifi network to a server will be established. An app on the mobile phone can then connect (via the server) to that switch box and the user and remotely set the switch state.
The benefit of this switch box compared to other solutions:
  • * Included power supply directly from mains
    * Proper (at least from a first view) isolation between main and dc voltage parts
    * Relatively small in size
    * Proper enclosure
Since I dislike the idea, that I have to send data to a server operated by someone in somewhere, I thought I give it a try and install micropython on it.
It worked out very well :D. The board has an unpopulated 5-pin header: 3.3V, RX, TX, GND and GPIO. Connecting a TTL-serial adapter to it, I was able to flash the recent git version of micropython onto it, in the same way as it is done with any other ESP8266 unit. I received a prompt and was able to program the unit.

Hence, right now I am adding the MQTT protocol, which will enable me to operate those switches via my own MQTT broker on my network. I got a first proof of a working MQTT driven power switch via micropython and woahh it was so relaxing to do that in micropython interactively, with a minimum of code. :D
The switch box comes with at least 4 direct accessible functions (there might be even more unused GPIOs but those are more difficult to access).
  • * GPIO14 - free GPIO Pin of the above 5-pin row header (I might want to use this to connect an external switch)
    * GPIO12 - Relay (for 110/230V mains)
    * GPIO13 - Status LED (inverse)
    * GPIO0 - onboard button (inverse) also used for flashing
I will now start to write a more complete program to enable all of the above functions hopefully getting a solid and robust MQTT-client. I will add pictures and code as this progress.

fpp
Posts: 64
Joined: Wed Jul 20, 2016 12:08 pm

Re: sonoff: wifi-based switch-box running micropython & MQTT

Post by fpp » Tue Oct 04, 2016 2:29 pm

Fun project !
Now I understand what is keeping you away from your keyboard controller idea :-)

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

Re: sonoff: wifi-based switch-box running micropython & MQTT

Post by kfricke » Tue Oct 04, 2016 2:54 pm

Cool ...and what a serendipity! Last friday i had 3 of those switches on my mail. My progress is nearly is far as yours, but my thoughts are now on how to implement a "safe" MQTT client on the Sonoff... delaying the minimum switching times (most AC devices dislike fast on/off cycles!) by using a simple FSM and possibly use that FSM for local button debounce.

Have you made yourself some thoughts regarding my "issues"? Maybe we can join forces?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: sonoff: wifi-based switch-box running micropython & MQTT

Post by pythoncoder » Tue Oct 04, 2016 5:19 pm

Nice device! At risk of getting into hot water for "promoting" my scheduler https://github.com/peterhinch/Micropython-scheduler.git I'd argue that with it the problem becomes trivial. Switch debouncing is already implemented with Switch and Pushbutton classes. Safe switching would come down to a trivial thread. Assume that the MQTT interface sets or clears a global boolean demand_state. Initialisation turns the relay off setting demand_state = 0. The thread would then work along lines like this:

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
A more elegant way might be to implement it as a method, eliminating the global, but this illustrates the principle. The only drawback is that the scheduler needs to be installed as frozen bytecode to work on the ESP8266.
Peter Hinch
Index to my micropython libraries.

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

Re: sonoff: wifi-based switch-box running micropython & MQTT

Post by kfricke » Tue Oct 04, 2016 5:29 pm

Thanks for your advice. I Will try to use it then ;)


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

Re: sonoff: wifi-based switch-box running micropython & MQTT

Post by kfricke » Tue Oct 04, 2016 7:31 pm

The newer components even seem to have got the CE certification. Let's hope European re-sellers will soon sell them as products. Let the opensource software IoT revolution begin!

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

Re: sonoff: wifi-based switch-box running micropython & MQTT

Post by torwag » Tue Oct 04, 2016 8:38 pm

Yep, they recently got a new bunch of products. However, it seems not all of them use a esp8366. We have to be brave and order one to dismantle it.
I just got home-assistant running and could switch the LED on an off via mqtt and my mosquito broker. (not using the relay for testing since that would require to connect the board to mains and I am not willing to fry myself). Using CoreOS and docker all the heavy part sits neatly in a single server in the attic :)
Thus the very first proof shows me that this might work out nicely.
However, I believe we are leaving the realm of the esp8266 board subforum. I will start a thread in the project section and link to it

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

Re: sonoff: wifi-based switch-box running micropython & MQTT

Post by torwag » Tue Oct 04, 2016 8:46 pm

To not dilute the esp8266 threads with only remotely related topics, I would like to continue the discussion here:
http://forum.micropython.org/viewtopic. ... 444#p14444

Post Reply