local switch esp

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
BigDog
New user
Posts: 8
Joined: 04 Sep 2015, 10:04

local switch esp

#1 Post by BigDog » 17 Sep 2015, 15:29

i want to make a esp with a button and a relais
but want to make it so that

you can push the button and the relais go's on or off
and you can get the relais on or off with domoticz

so that if the wifi go's down you can still power te relais manual
and afcourse its easy :)
you can insert it behind a switch outlet

Greetz BigDog

rtenklooster
Normal user
Posts: 320
Joined: 15 Apr 2015, 14:17

Re: local switch esp

#2 Post by rtenklooster » 17 Sep 2015, 17:28

BigDog wrote:i want to make a esp with a button and a relais
but want to make it so that

you can push the button and the relais go's on or off
and you can get the relais on or off with domoticz

so that if the wifi go's down you can still power te relais manual
and afcourse its easy :)
you can insert it behind a switch outlet

Greetz BigDog
With a button you mean a physical switch / button?
Currently that's not supported, but if you modify the switch code, you can add a digitalWrite function to actuate the relays.
Richard - Groningen (NL) - Image

maluko
Normal user
Posts: 112
Joined: 02 Sep 2015, 00:49

Re: local switch esp

#3 Post by maluko » 18 Sep 2015, 00:35

I have this code on init.lua(NodeMCU) but if you can put on easy better,
my must important feature are the off delay so when i click to open the gate it change ON but after " tmr.delay(500000)" delay it return OFF, the gate has a push button so need to be like this.

Code: Select all

wifi.setmode(wifi.STATION)
 wifi.sta.config("ssid","pass")
 wifi.sta.connect()
 wifi.sta.setip({ip="10.55.10.xx",netmask="255.255.255.0",gateway="10.55.10.xx"})
 print("ESP8266 mode is: " .. wifi.getmode())
led1 = 4
led2 = 6
led3 = 1
led4 = 2
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
gpio.mode(led3, gpio.OUTPUT)
gpio.mode(led4, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(5555,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.."<h1>Relé WIFI </h1>";
        buf = buf.."<p>PORTÃO <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 1 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 2 <a href=\"?pin=ON3\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF3\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 3 <a href=\"?pin=ON4\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF4\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "ON1")then
              gpio.write(led1, gpio.LOW);
               if(_GET.pin == "ON1")then
                  tmr.delay(500000);
                   gpio.write(led1, gpio.HIGH);
               end
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.LOW);
        elseif(_GET.pin == "OFF3")then
              gpio.write(led3, gpio.HIGH);
        elseif(_GET.pin == "ON3")then
              gpio.write(led3, gpio.LOW);
        elseif(_GET.pin == "OFF4")then
              gpio.write(led4, gpio.HIGH);
        elseif(_GET.pin == "ON4")then
              gpio.write(led4, gpio.LOW);     
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

BigDog
New user
Posts: 8
Joined: 04 Sep 2015, 10:04

Re: local switch esp

#4 Post by BigDog » 18 Sep 2015, 11:21

that would also be a very nice addon
didnt know that it wasnt supported yet becose i must wait a while before the esp's araive from china :)
i think they are now on a airplane somewhere :)

Greetz BigDog

maluko
Normal user
Posts: 112
Joined: 02 Sep 2015, 00:49

Re: local switch esp

#5 Post by maluko » 27 Sep 2015, 20:37

maluko wrote:I have this code on init.lua(NodeMCU) but if you can put on easy better,
my must important feature are the off delay so when i click to open the gate it change ON but after " tmr.delay(500000)" delay it return OFF, the gate has a push button so need to be like this.

Code: Select all

wifi.setmode(wifi.STATION)
 wifi.sta.config("ssid","pass")
 wifi.sta.connect()
 wifi.sta.setip({ip="10.55.10.xx",netmask="255.255.255.0",gateway="10.55.10.xx"})
 print("ESP8266 mode is: " .. wifi.getmode())
led1 = 4
led2 = 6
led3 = 1
led4 = 2
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
gpio.mode(led3, gpio.OUTPUT)
gpio.mode(led4, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(5555,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.."<h1>Relé WIFI </h1>";
        buf = buf.."<p>PORTÃO <a href=\"?pin=ON1\"><button>ON</button></a> <a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 1 <a href=\"?pin=ON2\"><button>ON</button></a> <a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 2 <a href=\"?pin=ON3\"><button>ON</button></a> <a href=\"?pin=OFF3\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 3 <a href=\"?pin=ON4\"><button>ON</button></a> <a href=\"?pin=OFF4\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "ON1")then
              gpio.write(led1, gpio.LOW);
               if(_GET.pin == "ON1")then
                  tmr.delay(500000);
                   gpio.write(led1, gpio.HIGH);
               end
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.LOW);
        elseif(_GET.pin == "OFF3")then
              gpio.write(led3, gpio.HIGH);
        elseif(_GET.pin == "ON3")then
              gpio.write(led3, gpio.LOW);
        elseif(_GET.pin == "OFF4")then
              gpio.write(led4, gpio.HIGH);
        elseif(_GET.pin == "ON4")then
              gpio.write(led4, gpio.LOW);     
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

How about add a delay on switch output to not have other software do this, because dont have trust on comunication by net.
For open gate this is a preciose feature, need only a dry contact.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests