____________________________________________________________
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!
____________________________________________________________

The story behind:Somebody was asking few days ago on ESP8266.com forum for the easiest way to turn OFF his tube amplifier remotely. A W2MPS (Wifi Web Mains Power Switch) sounds great for this job?

The Question: How? 

Because of the extensive work done before with such modules, the easiest way that I can see it to be done is a classical setup: Zero voltage crossing bilateral Triac driver + Triac combination, as a added module to our CBDB Board.

 What we will need:

  • USB adapter (take a look on Part 1 for details how to connect them together)
  • Main Power Switch module (details below)

 

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!!

Available models that also can be used:
BT 136 600V – 4A
BT 138 600V – 12A
BT 139 600V – 16A
BTA 23 800V – 12A
BTA 22 800V – 10A
BTA 40 800V – 40A
BTA 41 800V – 40A

A typical circuit for use when hot line switching is required:
                                         MPSM schematic – 1

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.

                                               MPSM Schematic – 2

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.

        Connection of the CBDB Board with MPSM Module

MPSM Software

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

1. Define used GPIO pin:

       outpin=3       -- Select IO - GPIO0        gpio.mode(outpin,gpio.OUTPUT)
       gpio.write(outpin,gpio.LOW)

2. Power Switch function, acting based on received command:

       function PwrSW(swstat,payload)
         gpio.mode(outpin,gpio.OUTPUT)
         newstat=string.sub(payload,swstat[2]+1,#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
      end 

3. Send Page function based on request:

 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"><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>')
   else                      
      conn:send(status)
      conn:send('%')
   end
   conn:send('</b><br /><br />')
   conn:send('<form action="/" method="POST">')
   conn:send('<input type="submit" name="cmd1" value="OFF"/>')
   conn:send('<input type="submit" name="cmd1" value="ON"/><br /><br /></form>')
   conn:send('</body></html>')
end

4. Web Server:

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
   conn:on("receive", function(conn,payload)
      --next row is for 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,"cmd1=")}
         --If POST value exist, set LED power
         if swstat[2]~=nil then
            print("Command received: " .. payload)
            PwrSW(swstat,payload)
            sendPage(conn)
         end
      end
   end)
   conn:on("sent", function(conn)
      conn:close()
      print("Connection closed")
   end)
end)  

 Save the code on ESP as ‘web_switch.lua‘, restart ESP and run:   
         =wifi.sta.getip()        -- find the IP Address where your Web Server will be
        dofile("web_switch.lua")  -- Start the Web Server

If this is your first project with a new ESP module that was never used in your WIFI Network, don’t forget to run also (one time only) :

       wifi.setmode(wifi.STATION)
       wifi.sta.config("<YOUR WIFI Network SSID>","<password>")

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

And a short video demonstration :

If you want the Web Power Switch 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:

tmr.now()                     -- 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 the program and reinitialize the Web Server:

                            Step-by-step run for testing

As I said from the beginning, this is a experimental W2MPS module example for a Web Controlled Power ON-OFF switch only. Dimming is another story, but if you are interrested about we can talk about it.

As usual, waiting  your comments and suggestions. If is anybody interested I have available few of them from previous projects and can easily respin more as the parts are already available.


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

AC Dimmer

MPDMv7.5 Arduino IDE driver example

For a more detailed description please take a look at the Part 1: MPDMv7.5 AC Dimmer devboard overview MPDMv7.5 AC Dimmer devboard is also available on Tindie Store A very simple MPDMv7.5 Arduino IDE driver Read more…

AC Dimmer

AC Dimmer DevBoard – MPDMv7.5 – Part 3 – DAC

For a more detailed description please take a look at the Part 1: MPDMv7.5 AC Dimmer devboard overview MPDMv7.5 AC Dimmer devboard ESP Basic program example for the Extra DAC version configuration: address = 96 Read more…

AC Dimmer

AC Dimmer DevBoard – MPDMv7.5 – Part 2

For a more detailed description please take a look at the Part 1: MPDMv7.5 AC Dimmer devboard overview MPDMv7.5 AC Dimmer devboard ESP Basic program example for the basic version configuration: 'write("starttimer",10) cls 'VCNT GPIO Read more…