[ST Edge] Issue with HTTP request in Edge drivers

I have a luxure HTTP server listening on the hub (similar to the lightbulb-lan-esp8266 example), and I have no issue accessing http://IP:port on my browser.

However, when I http.request to the hub-IP:port, it [string “socket”]:1406: timeout. Is this a bug?

I have no issue if I http.request other devices, but just not the hub IP:port.

I want to know whether the server is running or not.

Hi, @hongtat.

Can you give more details about your implementation, please?

As I understand you have a server similar to this one, and you make an HTTP request to that server within the same driver?

Could you share the snippet code of the implementation that causes this error, please?

Hi @nayelyz .

Yes. Within the same driver.
I’m able to access the http://hub:port/ via a browser, but HTTP request to it within the same driver will get a timeout.

-- https://github.com/SmartThingsDevelopers/SampleDrivers/blob/main/lightbulb-lan-esp8266/driver/src/server.lua
-- Added this
    server:get('/', function(req, res)
        res:send('Hello world!')
    end)

---

function lifecycle_handler.init(driver, device)    
  device.thread:call_on_schedule(
    300,
    function ()
        return commands.ping(
                driver.server.ip,
                driver.server.port,
                device)
    end,
    'Ping schedule')
end

---

function command_handler.ping(address, port, device)
  local addr = tostring(address .. ':' .. port)
  local success, data = command_handler.send_lan_command(addr, 'GET', '/')
end

function command_handler.send_lan_command(address, method, path, body)
    local dest_url = 'http://'..address .. path
    local query = neturl.buildQuery(body or {})
    local res_body = {}

    local _, code = http.request({
        method=method,
        url=dest_url..'?'..query,
        sink=ltn12.sink.table(res_body),
    })

    -- Handle response
    if code == 200 then
        print('commands:send_lan_command] Response: "' .. code .. '", url: "' .. dest_url .. '", query: "' .. utils.stringify_table(query) .. '"')
        return true, res_body
    else
        print('[commands:send_lan_command] Response error: "' .. code .. '"')
    end
    return false, nil
end

Sorry for the late response.

I was able to replicate the error, you mention. I’m making other tests to see what can be causing the issue.
I’ll also check if there are any restrictions on doing internal requests in the driver with the engineering team, but due to the holidays, their answer might take some time.

3 Likes

Ok, so, the issue is that when we do this:

driver:register_channel_handler(server.sock, function ()
  server:tick()
end)

The server is running in the same thread as lifecycle_handler.init, so, we need to spawn the server on its own thread as follows:

local cosock = require('cosock')

local server = lux.Server.new_with(cosock.socket.tcp(), { env = 'debug' })

server:listen()

cosock.spawn(function()
  while true do
    server:tick(log.error)
  end
end, "server run loop")

server:get('/', function(req, res)
  res:send('hello world')
end)
--sample of making the request:
local body, code, headers, msg = assert(http.request(string.format("http://...", server:get_ip(), server.port)))

Please, let me know your results

Hi @nayelyz

I’m afraid I’m unable to get it to work. I’m still getting [string "socket"]:1406: timeout.
I’ll send you a DM.