-->
Page 1 of 1

garage door opener without external processor

PostPosted: Fri May 22, 2015 6:46 am
by Fitz R
hi all,
I´m new to ESP6288, but I got a simple WIFI Server based Garagedoor opener working.
It just puts some GPIOs HIGH or LOW as needed.

In order to add more functionality, I have tested the interrupt mode of the GPIOs, to detect i.e. when the door is fully closed or opened and stop the motor. That works also well.

But I´m having problems combining this two tasks.

I tried also timer alarms to check if the end-switches are activated, but until now I havent succeeded.

Can you give me a hint how to perform this tastks "parallely"? ( I know, it is not really parallel, but it should be possible to stop the motor 0.5 seconds after reaching the end position...)

this is my code:
the wifi connection is established in the init.lua, then it passes to "garage.lua":


Code: Select all
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
taster1 = 2
taster2 = 1
gpio.mode(taster1, gpio.INPUT)
gpio.mode(taster2, gpio.INPUT)


TIME_ALARM = 25
button_state = 1
button_time = 0

function buttonHandler()
button_state_new = gpio.read(taster1)
if button_state ==1 and button_state_new ==0 then
button_time =tmr.time()
print("Taster1 pressed")
elseif button_state==0 and button_state_new ==1 then
button_time_new=tmr.time()
--if tmr.time()>(button_time+2) then
--tmr.stop(1)
print("taster1 depdessed")
--end
end
button_state = button_State_new
end



--function onChange ()
--    print('The pin value has changed to '..gpio.read(taster1))
--end


--function debounce (func)
--    local last = 0
--    local delay = 200000

--    return function (...)
--        local now = tmr.now()
--        if now - last < delay then return end

--        last = now
--        return func(...)
--    end
--end



--local pinValue = gpio.read(taster1)

--if pinValue == gpio.LOW then
--    print 'taster1 is low'
--else
--    print 'taster1 is high'
--end



srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
      buf = buf.."<html><head><title>Waldner´s Torsteuerung</title><body style='text-align: center'>";
      buf = buf.."<h1>Waldner´s Torsteuerung</h1>";
      buf = buf.."<h3>Garagentor</h3>";
        buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button style='height: 100px; width: 100px; border-radius:50%;'>öffnen</button></a>&nbsp;<a href=\"?pin=OFF1\"><button style='height: 100px; width: 100px; border-radius:50%;'>schliessen</button></a></p>";
        buf = buf.."<h3>Aussentor</h3>";
      buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button style='height: 100px; width: 100px; border-radius:50%;'>öffnen</button></a>&nbsp;<a href=\"?pin=OFF2\"><button style='height: 100px; width: 100px; border-radius:50%;'>schliessen</button></a></p>";
      buf = buf.."<h3>Aussentor</h3>";
      buf = buf.."<p>GPIO4 <a href=\"?pin=ON3\"><button style='height: 100px; width: 100px; border-radius:50%;'>öffnen</button></a>&nbsp;<a href=\"?pin=OFF3\"><button style='height: 100px; width: 100px; border-radius:50%;'>schliessen</button></a></p>";
        buf = buf.."<h3>Aussentor</h3>";
      buf = buf.."<p>GPIO5 <a href=\"?pin=ON4\"><button style='height: 100px; width: 100px; border-radius:50%;'>öffnen</button></a>&nbsp;<a href=\"?pin=OFF4\"><button style='height: 100px; width: 100px; border-radius:50%;'>schliessen</button></a></p>";
      buf = buf.."<h3>Aussentor</h3>";
      
          buf = buf.."</body>";
      local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.LOW);
      elseif(_GET.pin == "ON3")then
              gpio.write(taster1, gpio.HIGH);
        elseif(_GET.pin == "OFF3")then
              gpio.write(taster1, gpio.LOW);
      elseif(_GET.pin == "ON4")then
              gpio.write(taster2, gpio.HIGH);
        elseif(_GET.pin == "OFF4")then
              gpio.write(taster2, gpio.LOW);
   
        end
        client:send(buf);
        client:send(adc.read(0));
        client:close();


    tmr.alarm(1, TIME_ALARM, 1, buttonHandler)

     
         
        collectgarbage();
    end)

   

end)


Fixed: garage door opener without external processor

PostPosted: Mon Jun 15, 2015 2:07 am
by Fitz R
I got it working, it checks now every 200ms if the end switch is closed and stops the motor.
Just had to add a timer alarm at the right place.

Now I will be working on an ESP8266 controled incubator, I´ll post the results here very soon...