Hello I'm working on a project with AC dimmer and ESP32s
The aim is to adjust a boiler power, with a AC dimmer. Set point come from a mqtt server.
To ensure the most stable status, I have used 2 x esp32 mcus.
The first one run a simple script who detect the cross zero signal on one pin and set the current cutting signal wit some precise delay.
The delay is set via UART communication, this allow me to switch off the wifi. I've also switched off wifi to get best response time and stability. checked with an oscilloscope.
here is the code:
Code: Select all
from machine import UART, Pin, freq
from time import sleep, sleep_us
import network
p_zvd=Pin(32,Pin.IN)
p_scr=Pin(33,Pin.OUT)
p_tx=17
p_rx=16
delay=0
def pin_cb(p):
global delay
sleep_us(delay)
p_scr.on()
sleep_us(400)
p_scr.off()
#enable max CPU frequency to avoid jittering
#freq(240000000)
#disable wifi to avoid jitter in IRQ
sta_if=network.WLAN(network.STA_IF)
ap_if=network.WLAN(network.AP_IF)
sta_if.active(False)
ap_if.active(False)
#setup irq callback
p_zvd.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=pin_cb)
#setup UART
uart1=UART(1, baudrate=115200, tx=p_tx,rx=p_rx)
timeout=0
while True:
try:
consigne = uart1.readline()
except e:
print(e)
if consigne == None:
print('none')
# setup a timeout
# ensure full power if communication failed
# to avoid cold water situation
timeout+=1
if timeout>=10:
delay=0
else:
try:
delay=int(consigne.decode('utf-8'))
except:
delay=0
print('{}'.format(delay))
#reset timeout counter when communication resume
timeout=0
sleep(5)
The second esp32 run mqtt-as client to fetch setpoint from mqtt broker in asynchronous behaviour. It converts power percentage setpoint to delay and send it periodically to the first one.
I plan to use use it for no time-critical stuff like input temperature sensor, ect...
The code is in WIP but working status.
Be careful with zero volt reference when experimenting with AC dimmer, you may fry at least your mcu or yourself...
I would advice to use a laptop unplugged from grid (or USB isolator)