ESP8266 MPSM V.2 DevBoard - MAINS WIFI Web Power Switch




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!

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!


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!

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 :)

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Schematic and Theory of Operation

Main Power Switch Module (MPSM)

General considerations:

For resistive loads ON-OFF only jobs (no dimming required) a winning combination is between MOC304X for Triac driver (Zero voltage crossing one) and a good quality Triac, decently sized by the power needs. A 25A one usually fits mostly of the needs in a domestic project, with proper heatsink for power disipation. For standard home lighting, 8A is 99% of the times more than enough for one channel. If your house is a Castle with huge chandeliers, probably not :).

Triacs are the most common used semiconductor devices for power control and switching applications. Such power control circuits can be used to remotelly switch power to electrical devices or to switch power automatically when parameters such as temperature or light intensities go beyond preset level.

BT1XX class from NXP never dissapointed. Be sure they are genuine, from a trustable source. I know, shit happens in the supply chains even to big houses from time to time but, as a good practice, a good traceability for the parts you are using is desirable.

And don't forget :LIVE MAINS Switching!!

A typical circuit for use when hot line switching is required:

In this circuit the “hot” side of the line is switched and the load connected to the cold or neutral side. The load may be connected to either the neutral or hot line.
This type of circuit from above is good enough for resitive loads only. When a TRIAC controls inductive loads, the mains voltage and the load current are not in phase. To limit the slope of the reapplied voltage and ensure right TRIAC turn-off, usually is used a "snubber circuit" connected in parallel with the TRIAC. This circuit can also be used to improve TRIAC immunity to fast transient voltages.

The 100Ω resistor and 0.01μF capacitor are for snubbing of the triac and is often, but not always,necessary depending upon the particular triac and load used, generally for inductive ones.

BT137-600 Datasheet
MOC304X Datasheet

Step 2: PCB Details

  As you can see from the pictures below MPSMv2 DevBoard PCB was designed with great flexibility in mind.

You have even a full ESP8266 pinout access thru 2 breadboard friendly expansion slots for future projects development.

   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, Atmel, 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.

Step 3: What We Will Need

  • MPSM DevBoard presented in this article
  • USB adapter (take a look on CBDB Part 1 for details how to connect them together)
  • NodeMCU firmware (download latest - I recommend you to use the floating point version)
  • 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.

If your Firewall/AV have reported strange behaviours of NodeMCU Flasher, might be a good idea to read about the callback issue. Looks like nothing harmful, just annoying.

Step 4: Firmware

Flashing the MPSM NodeMCU Firmware is a straigth away process:

  • 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

Step 5: Testing MPSMv2 Board

To do quick run 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.

  • WIFI network Setup - 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)

If all OK Now you can carefully power off all and connect you MAINS powered lightbulb. Repeat the testing stepts from above and you can see it also playing the "Blinky" game too :). Use proper heatsink for power disipation for your Triac.

Step 6: MAINS Power Switch Software

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

For programming MPSMv2 Board and uploading the driver and the software we will continue to use the LuaUploader as before.

  1. Define used GPIO pin:
       outpin=1       -- Select Triac command PIN - GPIO5

  2. Power Switch function, acting based on received command:
    function PwrSW(swstat,payload)
       status = newstat
       if newstat=="ON"  then gpio.write(outpin,gpio.HIGH) return end
       if newstat=="OFF" then gpio.write(outpin,gpio.LOW) return end

  3. Send Page function based on request:
    function sendPage(conn)
       conn:send('HTTP/1.1 200 OK\n\n')
       conn:send('<!DOCTYPE HTML>')
       conn:send('<head><meta content="text/html; charset=utf-8"><style>input{width: 100px; height: 100px;}</style>')
       conn:send('<title>ESP8266 - Power Switch Controller</title></head>')
       conn:send('<body><h1>Power Switch Controller</h1>')
       conn:send('Status: <b>')
       if (status == "ON") then      conn:send('<B><font color=red>ON</font></B>')
       elseif (status == "OFF") then    conn:send('<B><font color=green>OFF</font></B>')
       conn:send('</b><br /><br />')
       conn:send('<form action="/" method="POST">')
       if (status == "ON") then  conn:send('<input type="submit" style="background-color:red" name="cmd1" value="OFF"/><br /><br />') 
       elseif (status == "OFF") then  conn:send('<input style="background-color:green" type="submit" name="cmd1" value="ON"/>')

  4. Web Server:
       conn:on("receive", function(conn,payload)
          --next row is for debugging output only
          if (string.find(payload, "GET / HTTP/1.1") ~= nil) then
             print("GET received")
             --If POST value exist, set LED power
             if swstat[2]~=nil then
                print("Command received: " .. payload)
       conn:on("sent", function(conn)
          print("Connection closed")

Save the code on ESP as 'web_switch.lua', restart ESP and run:

  =wifi.sta.getip()        -- find the IP Address where your new Web Server will be
  dofile("web_switch.lua") -- Start the Web Server

Open your favorite Web browser and type your new Web Server IP address. If all ok, should look something like below :

   If you want the Web Power Switch software to start automatically when your MPCMv2 Board starts or reboots, then you neet to create and add some lines in your 'init.lua' file:                      -- for debug only, you can skip it
wifi.sta.getmac()              -- for debug only, you can skip it        
wifi.sta.getip()               -- for debug only, you can skip itnode.heap()
dofile("web_switch.lua")       -- needed to start Web Server for command input

  Save the code on ESP as 'init.lua', restart ESP. It should reboot and restart automatically the program and reinitialize the Web Server:


   This is a easy to build experimental WIFI MAINS Power Switch Dev Board example for a Web Controlled Power ON-OFF switch only. Dimming is another story and we will talk about in a separate Article maybe soon :).

  • If you want to order bare PBC's only, PCB Factory Direct link: MPSMv2 PCB
  • For any other inquiries and requests use tech at

If you choose to use the MPSM project hardware and/or software to do whatever experimentation you might imagine, you do so ENTIRELY AT YOUR OWN RISK.

The software and hardware designs are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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!

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    17 Discussions


    3 years ago

    Please excuse me as Im new to electronics. Can the power that is being switched on/off be used to power the esp8266? If yes, when the power has been switched off, does the esp still have power?

    4 replies

    Reply 3 years ago

    Hi wgreybe,

    It can be used if you have also a backup battery or something else to power the board as long as MAINS line is powered off.


    Reply 3 years ago

    Thanks. Is there a simple cost effective means of doing this? In the particular case of where Id like to use it, there is a 12v gate motor battery which probably is too much. In trying to standardize my build, is there a cost effective means to use either mains and/or battery. However keeping it relative small as I'd like to use this on lights, plugs etc Thanks for your help.


    Reply 3 years ago

    If you have MAINS AC there then why to not put a standard cheap SMPS 5V PSU somewhere BEFORE the AC MAINS SW that will be used for the load. Wiring it in that way, even when you Switch OFF the MAINS Load power will be available for your ESP board.

    What do you want to switch ? What's your load and how is looking the wiring there?


    Reply 3 years ago

    As mentioned, I'm pretty new to this, so when you advise me, it means google :-). I'm attempting to open and close an electric gate using the esp8266. There is 230v mains power as well as a 12v batter backup.

    Having said the above, I would like to potentially use the completed project on light and power switches. But if I have to make different ones, then so be it. I guess I'm learning, its just taking so much time to get such little done :-)


    3 years ago

    hi , thanks for sharing the great job , however I was wondering that if have any plan to rewrite the project in Arduino IDE instead of Lua environment ,


    3 years ago

    I'd like to install this device remotely, near the load that is being switched. Does anyone have an easy way to generate Vcc from the mains? It can be done with a bridge rectifier, a resistor ladder, and a linear regulator, but the ESP8266 needs upwards of 200 ma when it's in transmit mode and the resistor ladder has to dissipate too much heat in order to provide a satisfactory voltage divider function to get down to a max of 35 volts feeding the regulator.

    2 replies

    Reply 3 years ago

    Because ESP8266 needs over 200mA for working properly with WIFI enabled the only option that I can see is to use a small external PSU. A small SMPS, as a phone charger for example or any similar in power size, will suffice.


    Reply 3 years ago

    I agree, but most of them are potted, so they need a bulky socket. I guess I'll be doing some destructive disassembly.


    3 years ago

    Hi, I'm trying to make this project, but I use ESP8266-01 module. The problem is, why the ESP8266 chip getting very hot after a minutes working?


    3 years ago

    Thank you for great info , but i am facing problems to create server and i am using a proxy internet connection . please solve my problem.
    This is .... I need your help with the following problem:the device which needs to connect via a proxy to the internet. I can't set a proxy at the device directly.


    3 years ago

    This is a great Project. I would love to buy this board ready to go with all the parts installed since I do not yet have the equipment to do SM. Do you sell these boards like that. I looked at you other links to Hackaday but did not see anything there.

    1 reply

    Reply 3 years ago

    Thank you, cburg.

    For more details about, please take a look at "Step7:Conclusions" . You will find there the link to the original article with all the related details, PCB source, etc, and for ready made boards and other options just drop your inquiries and requests at tech at


    3 years ago

    Very good. I cannot wait to try to make it myself. There are several devices that I could use it. Just waiting for capital controls in Greece to end so that I can order parts from ebay


    4 years ago

    thanks for sharing. I tried something similar, but I couldn't make it work. The difficult part for me was to make the triac work. Mine worked for 30v AC, but when I tried it to 120v AC the circuit didn't work. How do you calculate the triac resistance? There is no equation in the datasheet that could help. Thanks!