As the title says... Chat on...

User avatar
By akmenik
#35426 I would like to make Switch which I can control from Domoticz. From Domoticz I can make on and of actions based on http but I have no idea how I can get this information by ESP8266 and set proper values to GPIO. Can you help me?
I write following code it connect to the wifi and initialize TCP server on port 1717(choose random). How I can do now that Domoticz send data to this specific port
Code: Select all-- Starts the module and connect to server.
print("SWITCH")

-- variables
local deviceID = "9"
local server_ip = "192.168.1.150"
local server_port = 8080
local deep_sleep_time = 300 --seconds
PIN = 7

-- wifi connection config
wifi.setmode(wifi.STATION)
wifi.sta.config("AKMEN","af66051515")
local cfg =
{
    ip="192.168.1.100" --..deviceID, --static ip based on id
    netmask="255.255.255.0",
    gateway="192.168.1.1"
}   

-- check every 0.5seconds if we can get an ip, once we get it start the TCP client
tmr.alarm(0, 500, 1, function()
    gotIP = wifi.sta.setip(cfg)
    if ( wifi.sta.status() == 5 ) then
        print("connected to WiFi")
        tmr.stop(0)

        -- restart the module after 30seconds if it's still ON:
        -- it probably means it's stalled
        tmr.alarm(1, 30000, 1, function()
            node.restart()
        end)
       
    end
end)

    server = net.createServer(net.TCP, 30)
    server:listen(1717, function(socket)
        socket:on("receive",function(socket, msg)
            print(msg)
        end)
    end)


I have also second issue I try to update switch status chenge it from off to on using json and following code. When I run this code I see in Domoticz change in last seen but nothing else. What is wrong?
Code: Select alldata="On"
local json = "GET /json.htm?type=command&param=udevice&idx="..deviceID..
                "&nvalue=0&svalue="..data..
                " HTTP/1.1\r\nHost: www.local.lan\r\n"
                .."Connection: keep-alive\r\nAccept: */*\r\n\r\n"
User avatar
By JeremiahLandi
#35556 akmenik,

I haven't seen it done with json before. Have you seen the tutorial that Quindor put together. He does a nice job of outlining it in pure Lua.

http://blog.quindorian.org/2015/01/esp8266-wifi-led-dimmer-part-1-of-x.html
http://blog.quindorian.org/2015/01/esp8266-wifi-led-dimmer-part-2-of-x.html
http://blog.quindorian.org/2015/01/esp8266-wifi-led-dimmer-part-3-of-x.html
http://blog.quindorian.org/2015/01/esp8266-wifi-led-dimmer-part-4-of-x.html

Here is the code he has on his ESP8266:

Code: Select allpwm.setup(3, 1000, 005)
pwm.setup(4, 1000, 005)
pwm.start(3)
pwm.start(4)

LED1_current=005
LED1_target=005
LED2_current=005
LED2_target=005

Fadetime1=5000
Fadetime2=5000

Stepcounter1=0
PosStepcounter1=0
DimTimer1=0

Stepcounter2=0
PosStepcounter2=0
DimTimer2=0

wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSSS")



srv=net.createServer(net.TCP)
srv:listen(43333,function(conn)
    conn:on("receive",function(conn,payload)
   
    print("Input:"..payload)
 
    if string.find(payload,"LED1") then
     LED1_target=tonumber(string.sub(payload, 13) )
     print("Received LED1 Target Value: "..LED1_target)

     Stepcounter1=(LED1_target)-(LED1_current)
     
     if (Stepcounter1) < 0 then
      PosStepcounter1=(Stepcounter1)*-1
      else PosStepcounter1=(Stepcounter1)
     end
     
     if (PosStepcounter1) == 0 then
      PosStepcounter1=(PosStepcounter1)+1
      else PosStepcounter1=(PosStepcounter1)
     end
         
     DimTimer1=(Fadetime1)/(PosStepcounter1)

     if (DimTimer1) == 0 then
      DimTimer1=(DimTimer1)+1
      else DimTimer1=(DimTimer1)
     end

      print (Fadetime1)
      print (Stepcounter1)
      print (PosStepcounter1)
      print (DimTimer1)
      print (LED1_current)
      print (LED1_target)


    tmr.alarm(0, (DimTimer1), 1, function()
     if LED1_current < LED1_target then
      LED1_current = (LED1_current + 1)
      pwm.setduty(3, LED1_current)
    elseif LED1_current > LED1_target then
      LED1_current = (LED1_current - 1)
      pwm.setduty(3, LED1_current)
    elseif LED1_current == LED1_target then tmr.stop(0)
     end end )
    end

    if string.find(payload,"LED2") then
        print("Received LED2 Target Value")
     LED2_target=tonumber(string.sub(payload, 13) )
     
     Stepcounter2=(LED2_target)-(LED2_current)
     
     if (Stepcounter2) < 0 then
      PosStepcounter2=(Stepcounter2)*-1
      else PosStepcounter2=(Stepcounter2)
     end
     
     if (PosStepcounter2) == 0 then
      PosStepcounter2=(PosStepcounter2)+1
      else PosStepcounter2=(PosStepcounter2)
     end
         
     DimTimer2=(Fadetime2)/(PosStepcounter2)

     if (DimTimer2) == 0 then
      DimTimer2=(DimTimer2)+1
      else DimTimer2=(DimTimer2)
     end

      print (Fadetime2)
      print (Stepcounter2)
      print (PosStepcounter2)
      print (DimTimer2)
      print (LED2_current)
      print (LED2_target)


    tmr.alarm(1, (DimTimer2), 1, function()
     if LED2_current < LED2_target then
      LED2_current = (LED2_current + 1)
      pwm.setduty(4, LED2_current)
    elseif LED2_current > LED2_target then
      LED2_current = (LED2_current - 1)
      pwm.setduty(4, LED2_current)
    elseif LED2_current == LED2_target then tmr.stop(1)
     end end )
    end
    end)
    end)

print ("Booted to QuinLED_ESP8266_V0.4")