Post your best Lua script examples here

User avatar
By JANGER
#30666 It's pity that tmr.delay runs synchronize way ONLY!!!!!
It takes a whole processor time and lock any other thread
This crasehs the system at all.

I try to dimm AC 230V lights by wifi commands.
Dimmig AC lights we have to use so called zero-cross detection.

Something like:

dim=0 -- lights brightness factor 0...100 (%)

-- output to control led and optotriac GPIO05
outpin=1
gpio.mode(outpin,gpio.OUTPUT)
gpio.write(outpin,gpio.LOW)

-- input from zero-cross-detect transopt GPIO04
inpin=2
gpio.mode(inpin,gpio.INT,gpio.PULLUP)

function zero_cross()
dt=((115-dim)*76)
tmr.delay(dt)
gpio.write(outpin,gpio.HIGH)
tmr.delay(150)
gpio.write(outpin,gpio.LOW)
tmr.wdclr()
return "ON"
end

gpio.trig(inpin,"up",zero_cross)

====================
It works quite well.
(except another problem with tmr.wdclr() you can find and read on another forum thread)

But... it is impossible to run wifi server at the same time like:

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)

====================
Because tmr.delay occupies proccessor. Synchronize way.

When trm.delay executed , and in the same time wifi commands arrive, incidentally or not,
the system hungs up!!! When we are lucky it reboots, when nt, we have do hardware reset.

We trig tmr.delay 50 times per seconds. It's imossible to get wifi message out of the this time without life lucky.


Any idea how to fix?
User avatar
By Barnabybear
#30670 Hi, have you considered using the ESPs PWM pins (12, 13 & 15 off hand - a tripple dimmer even) with a frequency of 100hz.
As I understand once setup this should just sit in the background & mind its own business whilst you do other things (anyone please comment).
It will be a little tricky to setup but not impossible.
Let’s consider 50% on off.
You would need to set the frequency to 100hz & the duty cycle to 50% - work out if when initiated the first pulse is high or low - let’s assume high.
Detect the zero crossing point - delay for 5mS – initiate the PWM – this should start a series of 5mS on / 5mS off pulses - timed for the on to occur half way through the mains cycle & the off to occur at the zero crossing point (best not to go to close, say 95%).
This should sit & run whilst your server does its stuff. It will drift but it’s not a big job to go back & reset the start time every so often. As you have zero crossing detection you can detect any deviations in the mains frequency & compensate for these to reduce drift.
This post deals with PWM
As always easier to type than develop – hope that helps.

EDIT: When I typed this I was thinking that it would be need to adjust the duty cycle to dim the lights. Thinking about this as you would be using triacs (that latch), the duty cycle could be set at about 2% & only the initiation time adjusted to dim. So the longer the initiation delay, the later in the mains cycle the pulse would trigger, the dimmer the light untill it finaly went out.
User avatar
By JANGER
#30707 sorry. it is not working this way.
we cannot start pwm 50 times per seconds

I've set pwm.setup then tried tostart every zero cross detection (50Hz in fact).
none effect.

Barnabybear wrote:Hi, have you considered using the ESPs PWM pins (12, 13 & 15 off hand - a tripple dimmer even) with a frequency of 100hz.
As I understand once setup this should just sit in the background & mind its own business whilst you do other things (anyone please comment).
It will be a little tricky to setup but not impossible.
Let’s consider 50% on off.
You would need to set the frequency to 100hz & the duty cycle to 50% - work out if when initiated the first pulse is high or low - let’s assume high.
Detect the zero crossing point - delay for 5mS – initiate the PWM – this should start a series of 5mS on / 5mS off pulses - timed for the on to occur half way through the mains cycle & the off to occur at the zero crossing point (best not to go to close, say 95%).
This should sit & run whilst your server does its stuff. It will drift but it’s not a big job to go back & reset the start time every so often. As you have zero crossing detection you can detect any deviations in the mains frequency & compensate for these to reduce drift.
This post deals with PWM
As always easier to type than develop – hope that helps.

EDIT: When I typed this I was thinking that it would be need to adjust the duty cycle to dim the lights. Thinking about this as you would be using triacs (that latch), the duty cycle could be set at about 2% & only the initiation time adjusted to dim. So the longer the initiation delay, the later in the mains cycle the pulse would trigger, the dimmer the light untill it finaly went out.
User avatar
By xtal
#30711 You could always try putting you delays in for-next and shorting the tmr.delay to a shorter value
that way the ints could be checked more often...
for x to y do -- int allowed
tmr.delay -- int blocked try 10ms see how things work
next x