ESP8266 Projects Blog
Home of CBDB / MPDMv4 /SmartMon Development boards (ESP-12/ESP-07).
Guidelines and ESP8266 programming examples using LUA, Eclipse and Arduino IDE, ESP Basic and many more!
Part 4 of the MPRSx8 + ESPEasy Tutorial - After having all the bits & pieces in place, ESPEasy Formware properly uploaded and configured, now is time for the Domoticz setup and testing the Control interface!
Part 3 of the MPRSx8 + ESPEasy Tutorial - This time we will take a deeper look on the ESPEasy Firmware configuration and preparing the MPRSx8 Board for Domoticz integration:
Part 2 of the series, ESPEasy Firmware Upload tutorial using a simple standard USB-to-Serial Adapter:
The story behind: Somebody was asking if is really possible to upload firmware with a simple USB to serial adapter, as the ones that are available all over the place. As you can see from the above step-by-step tutorial it is possible and is working very smooth. You are loosing the autoreset and autoupload capabilities of the USBProg board, but as long as you are doing rarely the firmware upload is not so bad. On the other hand, if you want to use it with Arduino IDE or anything else that means frequent upload then I think you will start looking after that functions sooner or later, I think more sooner than later :)
As this time we will talk about the Software side we will design a very simple driver for our board that will include also a interactive Web command interface for the MPRSx8 Home Automation Dev Board Relays.
To keep things simple, we will just add in our Web Interface 8 ON/OFF Buttons and one general OFF one, from where we can turn ON/OFF all the Relays switches.
i2c.begin(address) ss = 0 xor 255 'XOR - Bit masking for the desired I/O pins i2c.write(ss) i2c.end() button "x1", [5] ' Button for Relay 1 button "x2", [6] button "x3", [7] button "x4", [8] button "x5", [1] button "x6", [2] button "x7", [3] button "x8", [4] ' Button for Relay 8 button "OFF", [9] ' General OFF for all the Relays wait
2. General OFF for all the Relays subroutine
[9] i2c.begin(address) ss = 0 xor 255 'XOR - Bit masking for the desired I/O pins i2c.write(ss) i2c.end() wait
3. Subroutines for each Button
[1] i2c.begin(address) ss = ss xor 1 'XOR - Bit masking for the desired I/O pins i2c.write(ss) i2c.end() wait
[2] i2c.begin(address) ss = ss xor 2 i2c.write(ss) i2c.end() wait
[3] i2c.begin(address) ss = ss xor 4 i2c.write(ss) i2c.end() wait
[4] i2c.begin(address) ss = ss xor 8 i2c.write(ss) i2c.end() wait
[5] i2c.begin(address) ss = ss xor 16 i2c.write(ss) i2c.end() wait
[6] i2c.begin(address) ss = ss xor 32 i2c.write(ss) i2c.end() wait
[7] i2c.begin(address) ss = ss xor 64 i2c.write(ss) i2c.end() wait
[8] i2c.begin(address) ss = ss xor 128 i2c.write(ss) i2c.end() wait
In the Web editor interface Save your program as "test_MPRSx8_1.bas" and Run it.
If all OK the result should look as below:
If you want your program to start automatically at reboot/power ON then just Save it as "default.bas" and also from Settings Tab enable the "Run default.bas at startup".
Be aware that at start-up/reboot, it is a delay before your program will start automatically.
The main goal of the project is to create a AC Mains Light dimmer that can automatically control the level of light and keep in in the desired set interval without any external intervention.
Why would you be intrested in such a setup? Well, if you have any interest in hydroponics, photography, or anywhere where you need some sort of constant flood light lamps with precise light intensitity level setup then might be a good idea to take a look :) What we will need:
BH1750FVI Module . Can be ordered in packs of ONE, FIVE or even TEN from the corresponding links. My 10 one was backordered but received it quite quickly.
For programming and uploading the driver and the software we will continue to use the LuaUploader as before.
Connection with the ESP8266 nEXT EVO Board is very easy, as MPU6050 GY-521 Module connector
is fully compatible with the nEXT Bus connector. Depending on how to you
choose you socket type, you can install it on TOP or Bottom of the ESP8266 nEXT EVO Board :
BH1750FVI Module directly connected to the ESP8266 nEXT EVO Board
And the whole project setup, with MPDVv4 AC Dimmer connected on ESP8266 GPIO13 (pin 7):
MPDM v4 + nEXT EVO + BH1750FVI
Closer look, MPDMv4 connected at ESP8266 GPIO13 (Pin7) for PWM control
function set_light() read_input(dev_addr) ll = light - hst --lower level lh = light + hst --upper level if (lux < ll) then p=0 i = i - step if (i<0) then i = 0 end pwm.setduty(7, i) end if (lux >lh) then p=0 i = i + step if (i>880) then i = 880 end pwm.setduty(7, i) end --print("Auto Level Adjust : "..i) --print(ll) --print(lh) if (ll< lux and lux < lh) then if (p<1) then p=1 end if (p<2) then print(string.format("\nLight Level: %0.2f lux",lux)) print("Auto Level Adjust : "..i) p=2 end end end
Main Program --PWM testing for MPDMv4 AC Dimmer Board
For any new orders/requests please feel free to use as usual: tech at esp8266-projects.com. MPDMv4 Boards are also available on Tindie: AC MAINS Dimmer - MPDMv4
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.
One of the main problem that people has been complained about was the fact that a simple MAINS Power Dimmer/Switch unit like the basic one presented last year is using sometime too much resources from a already overloaded application MCU/processor:
MPDMv3 - MAINS Dimmer/Switch with Phase detection
One of the scenarios, directly related with ESP8266 is about the limited timers capabilities that you have. For example, if you want to implement a proper web server to directly serve your webpages from ESP8266 you are automatically using one timer. If you want to start using the second one for your ZCD processing ...well...it's becoming very tricky. Very.
Don't uderstand me wrong, a bigger, better MCU can do it probably very well,
but even then if you have the chance to free some resources easily, do
it!. And I really cannot see a ESP8266 acting as a App MCU (webserver,
data logger, MQTT client, etc, etc) and doing reliable also other demanding
functions as Processing Zero Crossing Detection.
SOLUTION?
I can see only one, and it is a lesson learned many years ago: keep your real-time processing functions away from your Application MCU/Processor. To do that we just need to move the ZCD processing function from the ESP8266/whatever MCU you want to the MPDM Driver itself:
MPDMv4 - MAINS Phase detection and ZCD processing
What means that? means that in this new configuration our ESP8266 will be free from any type of real-time ZCD processing that is related with the MAINS Dimming process, you just need to send to the MPDMv4 driver a voltage (Vcontrol) in a pre-established range (0-3V for example) that will correspond with different desired Dimming levels.
What we have obtained? A UNIVERSAL MAINS Power Dimmer driver that can be used with any MCU you might want or even no MCU at all. You can dimm it even with a simple Potentiometer at input, no programming, no code, no nothing!!
Legend:
PURPLE - Phase detector output
BLUE - Reversed phase signal (normalised)
Yellow - Triac Driver CMD signal (PWM)
Vcontrol = 0, CMD Duty cycle=6%
Vcontrol = 2V, CMD Duty=50%
Vcontrol = 2.8V, CMD Duty=96%
How can be done that? Easy. Use PWM or a DAC, your choice. For ESP8266 I will recommend you to use a DAC, like the MCP4726 or, why not , the MCP4728, for a full 4 Channel MAINS Dimmers solution. PWM in case of the ESP8266..let's say that is has some limitations that you will discover very soon that you don't like :)
The mains advantages of using DAC?
Notable:
1. Very low to zero overhead on the App MCU
2. High precision (DAC from above are 12 bit!! - 4096 levels of dimming!!)
3. You can use the "set-and-forget" technique using DAC internal nonvolatile memory (EEPROM) and have the same level on the next power-off/on cycle.
IMPLEMENTATION
SCHEMATICS
As the only functional difference between the previous MPDMv3 MAINS Dimmer and the new MPDMv4 is the presence of the ZCD function onboard, basically, without some small bits and pieces around like the MOV protection circuit, you can see it exactly as it is: a MPDMv3 with a ZCD circuit onboard:
MPDMv3 - Phase detection and Triac control
Zero crossing detector circuit
MPDMv4 PCB:
MPDMv4 PCB
As you can see from the above picture, it was carefully designed with proper MAINS isolation from the rest of the driver, you can see even the extra isolation slots done for a even bigger Creepage distance.
And because I know this terms still creates some confusion, short definitions below:
CLEARANCE is the shortest distance in air between two conductive parts. CREEPAGE distance means the shortest distance along the surface of a solid insulating material between two conductive parts.
A creepage distance cannot be less than the associated clearance so that
the shortest creepage distance possible is equal to the required
clearance. However, there is no physical relationship, other than this
dimensional limitation, between the minimum clearance in air and the
minimum acceptable creepage distance.
And the new, freshly baked result:
MPDMv4 - MAINS Power Dimmer/Switch with Phase detection and ZCD Processing
Next time we will move on to the Software side, to see what is going on with our Webserver interface & stuff.
For any new orders / requests please feel free to use as usual: tech at esp8266-projects.com. If
you want for your own experiments to order ESP8266 WIFI Web Power Switch for MAINS - MPSM v.2 DevBoardbare PCBs only,
you can also do it directly at Dirty PCBs, our preferred PCB House:
I was asked to make also a short, basic introduction also for Arduino IDE, so here we go. But before of that I need to make also a short presentation of the MPSMv2 Board with few extra explanations. I will not insist too much on the hardware/schematics/theory of operation part of the story as you have all the details already in the previous articles about, including BOM.
What I want to explain a bit is the concept behind that might help you to understand better how to use the board. To ease the understanding you can see MPSMV2 as 2 separate Boards spliced together, a Breadboard friendly ESP8266 adapter with
integrated 3.3V Power Supply, and a MAINS Triac Switch.
The PCB is designed in such way that no harm will be done by cutting it in half as in the below picture.
MPSMv2 Board - Full and splitted
You can use separatelly the 2 parts, the ESP8266 Adapter with 3V3 regulator and the Triac MAINS Power Switch.
MPSMv2 - ESP8266 adapter with voltage regulator & stuff on the back
The Triac Switch part can be used directly even with a
different ARM, PIC, Arduino, whatever MCU you have arround without any
problems as long as it has a GPIO pin capable to
drive the MOC Optocoupler LED thru CMD port.
Triac Driver part - use proper Fuse and EMI filtering!
Please keep in mind that de design was
done in such a basic way as been designed to be part of a bigger system and used for development and testing.
Please use propper MAINS protection, FUSE and EMI filtering block based
on your specific application requirements!
There were a lot of questions and debates about, including the Hackaday one, why it has not a FUSE, where is the filtering, etc, etc ... they are not there because should not be there in our MPSMv2 design.
Input protection/filtering block for MAINS should not be on the same
board with the driver. They are a lot of explanations behind this but
even only having more flexibility in mind should suffice.
If you
look at the EMI filter below, similar with the choosen one as suitable to be used in our project
you will understand even more why should be not on the MPSMv2 board.
EMI filter vs. MPSMv2 Board
Now let's go back to our main story for today: how to use MPSMv2 with Arduino IDE.
For programming MPSM v2 DevBoard we will use Arduino IDE
1. Software installation
If you didn't have yet installed a proper running ESP8266 Arduino IDE environment the ESP8266 Arduino IDE installation Article might help you. Or the Youtube video from below:
2. Wiring
Connect the MPSMv2 Module to the USB adapter as in the picture below:
MPSMv2 - USB Adapter Wiring
USB MPSMv2
Rx -> Tx
Tx -> Rx
GND -> GND
+5V -> Power input port +5V
Set the PRG jumper (Blue) for programming mode ON !
3. Arduino IDE Programming.
Start Arduino IDE
Select inside IDE your desired ESP8266 Board (default one should be also OK).
Plug-in the USB adapter connected with the MPSMv2 module as above, will see the power on LED's
Open in IDE the basic Blink program
Replace the digital pin 13 as an output PIN with1.
Compile and upload the program to MPSMv3 board.
The result:
Let's test also the Triac driver command pin:
Q & A :
How can you have more than a MAINS power switch channel available? Very simple: just cut the Triac driver part from other MPSMv2 PCB's and connect the Driver side only to the full size MPSMv2 Board.
How many independent MAINS lines can be driven by a single MPSMv2 Board? As many as free IO pins you have available in your project and Triac Drivers you might add to it.
What's the easiest way to have 2 MAINS lines swiched synchronous? Just connect the CMD port from a second available Triac module to the CMD port on the main MPSMv2 Triac Module. The Triac driver from the MPSMv2 will be commanded in sync with the second connected one.
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:
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")
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: