Left here for archival purposes.

User avatar
By Armand Hansen
#35456 Im busy working on a dimming light which my ESP-03 will control. But what I have read up on, I have a problem. Please see my code below then I will explain:

Code: Select alldevice_id = "553CDA2DEAC90"
query_id = ""
dim = 120

wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSWORD")
wifi.sta.connect()
wifi.sta.setip({ip="10.0.0.122",netmask="255.255.255.0",gateway="10.0.0.2"})

outpin = 7 --GPIO13
gpio.mode(outpin,gpio.OUTPUT)
gpio.write(outpin,gpio.LOW)
inpin = 6 --GPIO12
gpio.mode(inpin,gpio.INT,gpio.PULLUP)

function zero_cross()
    dt = 75*dim
    tmr.delay(dt)
    gpio.write(outpin,gpio.HIGH)
    tmr.delay(1)
    gpio.write(outpin,gpio.LOW)
    tmr.wdclr()
end

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

function sendData()
    if(wifi.sta.status() == 5)then
        conn=net.createConnection(net.TCP, 0)
        conn:connect(PORT,'IP')
        if(firstStart == 0)then
            conn:send(device_id)
            conn:send("|0|")
        else
            if(query_id == nil)then
                conn:send(device_id)
                conn:send("|0|")
                conn:send(dim)
            else
                conn:send(device_id)
                conn:send("|")
                conn:send(query_id)
                conn:send("|")
                conn:send(dim)
                query_id = nil
            end
        end
        conn:on("receive", function(conn, payload)
            payload = string.gsub(payload, " ", "")
            dim = string.sub(payload, 0, string.find(payload, "|")-1)
            payload = string.gsub(payload, dim.."|", "")
            query_id = payload
            conn:close()
        end)
    else
        wifi.sta.connect()
    end
end
tmr.alarm(6, 1000, 1, sendData )


The problem that I am facing is that when at the bottom of the script I start tmr.alarm()... But the under the function zero_cross() its uses tmr.delay and that seems to make tmr.alarm() not function anymore. All that happens is the ESP just keeps on restarting. If I run the above code seperate (just the dimming function or just the sendData function then everything works perfect). Does anyone have any suggestions?
User avatar
By xtal
#35577 you might try implementing like this, that I saw on one of TerryE's posts...
Code: Select alldo_next = c_station                                                                      --  do c_station on next tmr event
tmr.alarm( 0, 250, 1, function() do_next() end)                --.25ms chek next do

It will help tame the beast, you can gain some control , just load do_next with the next function you want to do , then the next tmr event it is done ...
[hopefully]
I'm using this on my spa/pool code and it works quite well ....

What device are you using for the dimming, I would assume your're using 50-60 cyc
Using triac etc. I would try
- zerocross If not 100% light off -- if sw on -- tmr.alarm [ I think U can use tmr 0-5] -- exit
- tmr event if sw on then light on

set the tmr.alarm to 0-8+ms -- 60 hz
correlate your dim% with the tme.alarm


if you need finer control then us tmr.delay and wdt-clr if needed but stay under 10ms and hope for the best
User avatar
By Armand Hansen
#35616 Hi xtal

Thank you for the response. I might sound stupid here :oops: but could you help me implement the examples that you gave me into my code as I am note sure where to place it.

I have build my own dimming device from examples that people have posted. This is one of the links:
http://www.instructables.com/id/Arduino ... e-circuit/
And i then connected it to the ESP and used the code from:
http://www.esp8266-projects.com/2015/05 ... -with.html

Thank you so much for your help.