----------------------------------------------------------------------------------------------------------------------------
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 encourage you to play arround!. The
author take no responsibility for any injury or
death resulting, directly or indirectly, from your inability to
appreciate the hazards of household mains voltages.
The circuit diagrams are as accurately as possible, but are offered with no
guarantees whatsoever.
There is no guarantee that this design meets any Rules which may be in force in your country so please check before your local rules/regulations.
For any new requests please feel free to use as usual: tech at esp8266-projects.com.
If you want to order MAINS POwer Dimmer/Switch bare PCBs only, you can also do it directly at Dirty PCBs, our preferred PCB House:
-----------------------------------------------------------------------------------------------------------------------------
As I promised in the last article about the new MAINS Dimmer / Switch Module, Today we will continue with designing the Web interface access.
What we will need:
- CBDB Board or any other ESP8266 you may like
- USB adapter (take a look on Part 1 for details about the USB Adapter
- Main Power Dimmer/Switch module (MPDMv3)
![]() |
| MAINS Power Dimmer / Switch Module - MPDMv3 - Connections |
![]() |
| CBDB Evolution DevBoard |
Because I don't like to have flying MAINS wires on my workdesk I have created a temporary separate MPDMv3 Module Box, containing all the MAINS part of the story :).
![]() |
| Isolated MAINS unit - keep your fingers away from the Deadly zone ! |
Remember, Safety First! And also more relaxing knowing that is no Russian Roulette wire game on progress on my table. Shit happens all the time, but at least let's try to reduce the chances to hit badly the fan too often :)
![]() |
| Live Testing on progress |
In the Box above is enough space for all the parts going inside, including ESP8266 module, power supply, Dimmer Module, Choke, etc, but at this stage I find it easier to connect them together like that.
MPDMv3 Web Server Software
For programming CBDBv2 Board and uploading the driver and the software we will continue to use the LuaUploader as before.
1. Define used GPIO pin:
outpin=7 -- Select Triac Command pin - GPIO13
gpio.mode(outpin,gpio.OUTPUT)
gpio.write(outpin,gpio.LOW) -- Triac OFF
inpin=6 -- Zero crossing detector input - GPIO12
gpio.mode(inpin,gpio.INT,gpio.PULLUP) -- attach interrupt to ZCD
2. Zero Cross Detector function and Triac command
For a more detailed explanation how Zero cross detection circuit works please take a look at the previous MPDMv3 article.
function zero_cross()
dt = 76*dim
--print("Zero cross detected!")
stat = "ON"
tmr.delay(dt) -- Firing delay time calculated above
gpio.write(outpin,gpio.HIGH) -- Triac ON - Zero cross detected
tmr.delay(100) -- Triac ON - Propagation time
gpio.write(outpin,gpio.LOW) -- Triac OFF - let's be sure it's OFF before next cycle :)
tmr.wdclr()
return stat
end
3. WEB Server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(conn,payload)
--debugging output only
-- print(payload)
if (string.find(payload, "GET / HTTP/1.1") ~= nil) then
--print("GET received")
sendPage(conn)
else
swstat={string.find(payload,"cmd=")}
--If POST value exist, set power switch status
if swstat[2]~=nil then
--print("Command received: " .. payload)
PwrSW(swstat,payload)
sendPage(conn)
end
end
end)
conn:on("sent", function(conn)
conn:close()
conn = nil -- clear and allow the garbage collector to free the memory
--print("Connection closed")
end)
end)
4. Send page function based on received requests
function sendPage(conn)
conn:send('HTTP/1.1 200 OK\n\n')
conn:send("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"5\">")
conn:send('<!DOCTYPE HTML>')
conn:send('<html>')
conn:send('<head><meta content="text/html; charset=utf-8">')
conn:send('<LINK href="http://www.instructables.com/files/orig/FGU/L48I/I98BK1X6
/FGUL48II98BK1X6.css"')
conn:send('rel="stylesheet" type="text/css">')
conn:send('<title>ESP8266 - Power Switch Controller</title></head>')
conn:send('<body><center><h1>ESP8266 MAINS Power Dimmer Controller</h1></center>')
conn:send('<br /><br />')
conn:send('<form action="/" name="dimmer" oninput="outputUpdate(cmd.value)"
method="POST">')
conn:send('<font color=red><b>ON </b></font><input style="width:550px; height:50px"
type="range" name="cmd" id="cmd" value="')
conn:send(status)
conn:send('" min=1 max=128 step=1 />')
conn:send('<font color=red><b>OFF</b></font></form>')
conn:send('<br><center>Dimmer Value: <b>')
conn:send(status)
conn:send(' pts</b></center>')
conn:send('<script type="text/javascript">')
conn:send('function outputUpdate(dim) {document.querySelector("#cmd").value = dim;')
conn:send('document.forms[0].submit();}</script>')
conn:send('</body></html>')
end
CSS file created for this example is just for fancy decorations. If you don't like it you can very easy change the style attributes or add local ones.
5. Check received Dimmer value and update status
function PwrSW(swstat,payload)
gpio.mode(outpin,gpio.OUTPUT)
newstat=string.sub(payload,swstat[2]+1,#payload)
status = tonumber(newstat)
print("Dimmer Value:" .. status)
end
4. Main code
status = 60 -- around 50% - choose your desired start value.
newstat = 60
outpin=7 -- Select Triac Command pin - GPIO13
gpio.mode(outpin,gpio.OUTPUT)
gpio.write(outpin,gpio.LOW) -- Triac OFF
inpin=6 -- Zero crossing detector input - GPIO12
gpio.mode(inpin,gpio.INT,gpio.PULLUP) -- attach interrupt to ZCD
gpio.trig(inpin,"up",zero_cross) -- ZCD interrupt attached - trigger on falling edge
print(wifi.sta.getip()) -- print our new MPDMv3 WebServer IP
For testing, just save the code on ESP as 'dimserver.lua', restart ESP and run:
dofile("dimserver.lua") -- Start the Dimmer Listening WebServer
=wifi.sta.getip() -- find the IP Address where your Web Server will be
Open your favorite Web browser and type your new MPDMv3 Web Server IP address. If all ok, should look something like below :
![]() |
| MPDMv3 Web Interface |
If you want the MPDMv3 software to start automatically when your CBDB module starts or reboots, then you neet to create and add some lines in your 'init.lua' file:
dofile("dimserver.lua") -- Start automatically the Dimmer Listening WebServer
Save the code on ESP as 'init.lua', restart ESP. It should reboot and restart automatically the program.
A short video presentation, testing the Web Interface:






















