R&D Publishing‎ > ‎

NodeMCU LUA

ESP8266 NODE MCU IR REMOTE CONTROL SONY TV

posted 15 Jul 2016, 02:50 by Lê Duy Nhân

------------------------------------------------------------------------------
-- IR send module
--
-- LICENCE: http://RDLAB.CDMT.VN
-- Le Duy Nhan nhanld@cdmt.vn
--
-- Example:
-- sony(1, 0x0C90)  Volume down
------------------------------------------------------------------------------
local M
do
  -- const
  local SONY_PULSE_US   = 1000000 / 24000
  --local SONY_HDR_MARK   = 2300-800
  local SONY_HDR_MARK   = 2240
  local SONY_HDR_SPACE  = 560
  local SONY_BIT_MARK   = 560
  local SONY_ONE_SPACE  = 1120
  local SONY_ZERO_SPACE = 560
  local SONY_RPT_SPACE  = 2250
  -- cache
  local gpio, bit = gpio, bit
  local mode, write = gpio.mode, gpio.write
  local waitus = tmr.delay
  local isset = bit.isset
  -- 24kHz PWM with 1/3 duty)
  local carrier = function(pin, c)
    c = c / SONY_PULSE_US
    while c > 0 do
      write(pin, 1)
      write(pin, 0)
      c = c + 0
      c = c + 0
      c = c + 0
      c = c + 0
      c = c + 0
      c = c + 0
      c = c * 1
      c = c * 1
      c = c * 1
      c = c - 1
    end
  end
    sony = function(pin, code, tsop)
    local pulse = tsop and pull or carrier
    -- setup transmitter
    mode(pin, 1)
    write(pin, tsop and 1 or 0)
    -- header
    pulse(pin, SONY_HDR_MARK)
    waitus(SONY_HDR_SPACE)
    -- sequence, lsb first
    for i = 11, 0, -1 do
      pulse(pin, (isset(code, i) and SONY_ONE_SPACE or SONY_ZERO_SPACE))
      waitus(SONY_BIT_MARK)
    end
  end
  -- expose
  M = {
    nec = nec,
  }
end
return M

NODEMCU XIVELY GET AND PARSE DATA

posted 12 Jul 2016, 03:50 by Lê Duy Nhân   [ updated 15 Jul 2016, 02:53 ]

function split(text, delim)
    -- returns an array of fields based on text and delimiter (one character only)
    local result = {}
    local magic = "().%+-*?[]^$"

    if delim == nil then
        delim = "%s"
    elseif string.find(delim, magic, 1, true) then
        -- escape magic
        delim = "%"..delim
    end

    local pattern = "[^"..delim.."]+"
    for w in string.gmatch(text, pattern) do
        table.insert(result, w)
    end
    return result
end

tmr.alarm(1,25000,tmr.ALARM_AUTO, function()
sk=net.createConnection(net.TCP,0) 
sk:on("receive", function(sck, payload)
local str=string.sub(payload,string.find(payload,'CH01'),-1)
local tab=split(str,'\n')

print("Time:"..string.sub(tab[1],6,33))
print("Va="..string.sub(tab[1],34,-1), "Vb="..string.sub(tab[2],34,-1),
"Vc="..string.sub(tab[3],34,-1))

print("Ia="..string.sub(tab[4],34,-1), "Ib="..string.sub(tab[5],34,-1),
"Ic="..string.sub(tab[6],34,-1))

print("P="..string.sub(tab[7],34,-1), "Q="..string.sub(tab[8],34,-1),
"PF="..string.sub(tab[9],34,-1), "Freq="..string.sub(tab[10],34,-1))


--print(payload) 
--tab=cjson.decode(string.sub(payload,string.find(payload,"{"),-1))
--print("Va="..tab.current_value.."V")
sk:close()
end)
sk:on("disconnection", function(sck,c) print("Disconnect") end)
sk:connect(80,"api.xively.com")
sk:on("connection", function(sck, c) print("Connect Ok")
sck:send("GET /v2/feeds/104704.csv HTTP/1.1\r\nHOST: api.xively.com\r\nX-Apikey: YOUR_API_XIVELY_KEY\r\n\r\n")
--sck:send("GET /v2/feeds/104704/datastreams/CH01.json HTTP/1.1\r\nHOST: api.xively.com\r\nX-Apikey: YOUR_API_XIVELY_KEY\r\n\r\n")
end)
end)

1-2 of 2