Friday, April 17, 2015

P2 - WIFI Web Power Switch for MAINS - MPSM v.2 DevBoard - ESP8266


New released MPDMv4 (MAINS Power Dimmer) Driver Board !



WARNING!! You will play with LIVE MAINS!! Deadly zone!! 

If you don't have any experience and are not qualified for working with MAINS power I will not ecourage you to play arround!


    Remember the story about the WIFI MAINS Power Switch module

    Because of the high interest in the subject, a new dedicated Dev Board has been born MPSMv2, a small ESP8266 DevBoard with integrated MAINS Power Switch! 

   Yes, it's finally here, arrived safely from the PCB factory and you can see it below. I know a lot of you are waiting it already, but please be patient few more days. None of the already requested ones will leave the premises without a proper 24 hour test.
   For any new requests please feel free to use as usual: tech at
     If you want for your own experiments to order CBD v2 EVO bare PCBs only, you can also do it directly at Dirty PCBs, our preferred PCB House:

MPSM v2 Schematic

    Theory of operation remain the same so please take a look at the previous Article about MAINS Power Switch for deeper explanations.

     I don't know if this it's the smallest MAINS Power Switch with integrated WIFI and direct Web interface access but if is not, it's definitely closer to be at only around 25x50mm :)

    As you can see from the PCB picture above you have even a full ESP8266 pinout access thru 2 breadboard friendly expansion slots for future projects development.

   Finally,  the received result from the PCB Factory, a long awaited moment:

MPSM v2  PCB Batch


      If you want to have a separate Breadboard friendly ESP8266 adapter with integrated 3.3V Power Supply, you can cut the board in 2 independent working parts, the ESP07/12 adapter and the MAINS Triac Switch.

    It is designed in such way that no harm will be done by the cut to the functioning of the circuit. You can use the cut MAINS Power Switch part directly even with your ARM, PIC, Arduino, whatever MCU you like this days without any problems as long as it has a GPIO pin capable to drive the MOC Optocoupler LED.

MPSM v2  - separate usage ESP8266 adapter and MAINS Switch

ESP8266 ESP-07/12 Adapter

      And the final result, a fresh baked one, ready for testing:

MPSM v2 - full asembled

MPSM v2 - full asembled - bottom

   Before going further and start running some tests few considerations to be done:
  • Do NOT use it without proper Knowledge about MAINS circuits !
  • Do NOT use it without a proper FUSE on MAINS line! 
  • This is NOT a Toy!

     What we will need:
  • MPSM DevBoard from above
  • USB adapter (take a look on Part 1 for details how to connect them together)  
  • NodeMCU firmware (download latest - can use the floating point version for now) 
  • NodeMCU Flasher ( firmware programmer for NodeMCU DEVKIT) 
  • For programming MPSM v2 DevBoard and uploading the software we will continue to use the LuaUploader as before. 

    Flashing the MPSM NodeMCU Firmware is a straigth away process:
Programming MPSM v2 Board

  •  Connect MPSM Board with the USB Adapter, Set the PROG jumper (YELLOW) in the  Programming mode position (closed) and power on 
  • Start NodeMCU Flasher. Choose you USB adapter corresponding port
  • Add from Config Menu latest previously downloaded firmware. It must start from 0x0000. Disable anything else. 
  • Go back on Operation tab. Power off your MPSM Board. Press FLASH Button. power ON quick MPSM Board. It will be recognised and will start flashing. Give it a second try if necessary.
  • When finished succesfully A green OK checkmark will appear
  • Power Off   MPSM Board, Remove yellow jumper. Power back ON. A NodeMCU programmed MPSM Board is ready for action :)

    For a more detailed explanation for NodeMCU Firmware flashing please take a look at CBDB Board Firmware Flashing discussion

 To quick run a test we will use the code snippets provided by LuaUploader at start-up. Select the piece of code that you want to run and press "Execute Selection" button.

  • To quick setup your WIFI network - If this is your first project with a new ESP module that was never used in your WIFI Network, don't forget it - one time only :
            -- One time ESP Setup --
            wifi.sta.config ( "YOUR_WIFI_SSID" , "PASSWORD" ) 

  • For the Triac Optocoupler Command testing we will use the "Blinky" part of the code and if all ok it will blink LED2 with the choosen rate "dly".
          Run the modified code from below:

          -- Blink using timer alarm --
          timerId = 0 -- we have seven timers! 0..6
          dly = 500 -- milliseconds
          ledPin = 1 -- 1=GPIO5  - allocated pin for Triac opto command
          ledState = 0
          tmr.alarm( timerId, dly, 1, function()
                ledState = 1 - ledState;
                 gpio.write(ledPin, ledState)

  Now you can connect you MAINS powered lightbulb and you can see it playing the "Blinky" game too :).  Use proper heatsink for power disipation for your Triac.

    Power Switch function and WEB Server software are the same as in previous article about CBDB WIFI MAINS power switch  just change the used GPIO Triac opto command pin from outpin=3 to outpin=1 to properly reflect our new MPSM v2 Board setup

For a MAINS Power Dimmer Module, take a look at the new MPDMv3 Module


As many of you asked for a BOM list, please find it below:

Part Value Device
Package Description

R1 560 R-EU_R1206
R1206 RESISTOR, European symbol
R2 10k R-EU_R1206
R1206 RESISTOR, European symbol
R3 10k R-EU_R1206
R1206 RESISTOR, European symbol
R4 10k R-EU_R1206
R1206 RESISTOR, European symbol
R5 10k R-EU_R1206
R1206 RESISTOR, European symbol
R6 470 R-EU_R1206
R1206 RESISTOR, European symbol
R7 470 R-EU_0207/10
0207/10 RESISTOR, European symbol
R8 390 R-EU_0204/7
0204/7 RESISTOR, European symbol
R9 470 R-EU_R1206
R1206 RESISTOR, European symbol
R10 39/1W R-EU_0309/10
0309/10 RESISTOR, European symbol

C1 100n C-EUC1210
C1210 CAPACITOR, European symbol
C2 100u C-EUC1210
C1210 CAPACITOR, European symbol
C3 0.1u/400V C-EU075-032X103
C075-032X103 CAPACITOR, European symbol

IC1 REG1117 REG1117
SOT223 1A LDO Positive Regulator


OK1 MOC3041M MOC3041M
DIL06 6-Pin DIP ZCD Optoisolator Triac Driver
T1 BT137 BT137-600
TO220BV TRIAC - BT137/600 - NXP

1X2MTA AMP connector

ESP12 ESP-12 ESP8266_ESP-12



NeoMatrixJR said...

What would it take to add power usage sensing to something like this? What was your total cost for the BOM? (What does it cost per switch to build one?)

Unknown said...

To be able to calculate Power usage you will need to add you favourite solution for MAINS Voltage and Current reading. Plenty of standard solutions are available, from voltage dividers+CT current sensors, to Hall effect ones and dedicated IC's. Please keep in mind that you want RMS Voltage, as been an AC Power line.

The cost is heavily dependent of the desired precision/accuracy and chosen solution

Please DO NOT forget that we are talking here about MAINS LINE! DEADLY ZONE!

BobC said...

Wish List:

1) Dimming (but would greatly increase radiated noise under higher loads)

2) Support an HA-friendly protocol such as MQTT (already in NodeMCU?)

3) Hardware change: Line powered (

4) Use Case: Smart Light Switch. Wire in place of a wall switch, then connect de-energized wall switch to GPIO to permit local control (edge-triggered state toggle, not level-triggered). (Note: This would likely place the MPSM board in the fixture or its box, since most light switches only interrupt one side of the line. And the GPIO would not need to be opto-isolated since it is not connected to AC.) Supporting 2 GPIO would permit installation in multi-switch lighting.

5) If needed, switch to native ESP8266 SDK ( or to support the above wish-list.

6) Sell the line-powered hardware on Tindie (or somewhere, for beer/burrito fund) as: Board only, kit, assembled (w/ or w/o ESP8266)


PS: With updated hardware in-hand, I should be able to handle the software (with community help for testing).

Unknown said...

Hi BobC,

Nice wishlist :)

Some of things are easy to be done, some complicated because of different Countries Rules / Regulations / Laws regarding MAINS power lines and power distribution.
It's easier this days to end in Jail than you can imagine :))

1. Take a look here: At least a choke is needed for EMI Suppression.

2. MQTT protocol will be not a very complicated thing to add.

3. yes, stay around for some news about soon :). The solution from the attached Microchip Appnote is not what are you looking for, for many reasons. but noted, MAINS line powered. We are running some test already.

4. Yes, in case you are ready to pay around 25-30$/piece. To have it wall mounted in the way you suggest you need to have a fully certified product for MAINS lines. Be prepared for a budget of many 000's after a 9 :). It's a longer and more painful process than you might think. Been there ,done that. Scary movie.
Might be an easier way to use it is as a wall socket Plug-in ? Might be enough for your applications?

5. It's working ok with native SDK undet Eclipse and also in Arduino IDE:

6. Noted. If will be interest about why not.

PS: waiting your oppinons

Unknown said...

Hi Tracker, I have BTA41-800B Can i use your circuit to drive a 3HP water pump with out replacing any other component...? thanks

Unknown said...

If you are talking about a 2.2kW Engine, it's a NO NO as the MPSMv2 PCB was NOT designed for such high current!!

Main target application for MPSMv2 is lighting, where usually power is way lower, as 40 up to 500W.

If you want a heavy duty WIFI switch for your pump, please contact us directly for details at: tech at

Krogo said...

is it possible that you provide a list of used parts for this board?

Unknown said...

BOM list added to the end of the article

Landi said...

which 1117 do you use?
REG1117A Adj./1A SOT223-3
REG1117 Adj./800mA SOT223-3

Unknown said...

LD1117 from ST :

Landi said...

yes, but i did not see why you use a adjustable!?
In my opinion it is also ok to use a fixed 3,3V regulator, so i can pass the capacicators.

Unknown said...

I would like to purchase the board MPSM v2 but that worked with triac bta41. How do I buy it?

Unknown said...

Hi João, for more details you can contact me at: tech at esp8266-projects dot com.

Dreamer said...

Hi Tracker, does the switch power the board esp board as well? (Whether switched on or off) (Mains that is))

Unknown said...

Hi Dreamer,
You need to use a separate +5vcc power supply, it has the 3.3Vcc regulator only on it.

john anast said...

2 questions:
what is the polarity (+-) of the LED3MM?
I connected the 3.3 power out of the USB2serial as the power source of th board but it does not seem to work. The blue Led of the ESP lights up for a sec and then the PC says that the device is not recognized. Is there anything you sujest ?
Thank you

Unknown said...

As you have a Voltage regulator on the board, you need a +5Vcc power input to make it work properly.
If you want to use a external +3.3V then you should not use the voltage regualtor, you should remove it and solder a bridger across Vin->Vout. The should work OK with 3.3V

Regarding LED, is wired as in the schematic above, as a normal LED. Loos in your LED datasheet to find your LED Anode and Catode pinout. SMD LED's are notorius for non-standard marking of the pins.

Rajeev (राजीव) said...

Hi TrackerJ,

After assembling, I realized that if the load is very low, there is always a residual (leakage) current trough triac. Have you used this schematic in real life conditions with same module powering different kind and level of loads (within the triac's limits - say up to 400W)... Upon using a small LED light load there is a leakage current which caused the LEDs to remain lit, albeit dimly. For incandescent lamps - it works well. For inductive loads, there is a little humming sound (perhaps caused by leakage trough triac). This seems to be common issue with triac based switching and you need to put a bleeder resistor in parallel with the load. Is there any practical alternative or best to use relays for on/off operations?

john anast said...

Finally all the parts are here. I made it (Yes!!) It works with arduino IDE. I can turn on and Off the led on pin 5. How can I test if the triac works OK before I connect it to the mains? I hope it is not a stupid question. I have worked only with relays until now. It is my first with triacs.

Unknown said...

Hi John,

You might replace the MAINS input voltage with a lower one, like 24VAC and measure the output or just use a small 24V lightbulb (or 2x12V in series if easier to find).

You can even try with 12VAC but I still recommend 24VAC.

john anast said...

Thank you!!! It works (even with 12Volts) Let's write some code now :)

rumbero71 said...

Hi, what about cooling the triac on higher loads like -say- 1000w. Datasheet indicates several watts to be dissipated. Did you try higher loads and which cooling block did you use ? Thanks Markus

Unknown said...

Hello Unnknown,

It depends on so many factors, from the load type to the total power and MAINS line voltage, that I cannot give you a universal heatsink size good for.

For under 100W, can work even without, but I suggest some.

1000W means a bit over 4A/240VAC.
I would suggest you to read the following app notes, will help you a lot to properly understand the story behind:

1. App Note - how to calculate the power dissipation
2. App note - thermal management precautions for handling and mounting -

As a rule of thumb, bigger is better, keep it as cool as possible with a corresponding heatsink size.

Unknown said...

And yes, with some precautions (extra reinforcing for MAINS lines, busbar, heatsink, etc) I've run it around 2000W (8A). Resistive load (Owen heating elements). The Triac used was a 24A one.

Post a Comment