Error initializing socket.tcp()

I am trying to write my first edge driver that uses TCP. I have code that does an RPC call standalone and it works fine. But when I try to use it in an edge driver, I get this error:

2023-12-03T21:48:24.235878002+00:00 ERROR Hello Mylink Hello Mylink Device thread encountered error: [string “st/dispatcher.lua”]:233: Error encountered while processing event for <Device: 20b800ae-93ee-4b27-b19d-d7cc64982cf5 (Hello Mylink Device 1)>:
arg1: table: 0x1df9f60
[string “init.lua”]:40: Assert 1

Here is the code up to that point:

– require st provided libraries
local capabilities = require “st.capabilities”
local Driver = require “st.driver”
local socket = require ‘socket’
–local socket = require “cosock.socket”
local json = require ‘dkjson’
local log = require “log”
local discovery = require “discovery”


– local variables

local last_req_id = 0
– local tcp = socket.tcp()

– local functions

local function send_shutter_command(device, direction)
for id, value in pairs(device.preferences) do
if id == “ipaddress” then
ipaddress = value
elseif id == “port” then
port = value
elseif id == “targetid” then
targetid = value
elseif id == “auth” then
auth = value
end
end
last_req_id = last_req_id + 1
local request = {
id = last_req_id,
params = {targetID=targetid,auth= auth},
method = direction,
}
local requeststr = assert(json.encode(request))
log.info(requeststr)
log.info("IP Address= " … ipaddress … " Port = " … port)
local tcp = socket.tcp()
assert(tcp,“Assert 1”)

Here is the standalone code that works:
local socket = require ‘socket’
local json = require ‘dkjson’
–send_shutter_command(“mylink.move.up”)
–end
–function send_shutter_command(direction)
local direction = “mylink.move.up”
local sock = socket.tcp()
local ipaddress = “192.168.1.174”
local port = 44100
local targetid = “CC1087E0.0”
local auth = “shutters”
local last_req_id = 0
last_req_id = last_req_id + 1
local request = {
id = last_req_id,
params = {targetID=targetid,auth= auth},
method = direction,
}
local requeststr = assert(json.encode(request))
print(requeststr)
assert(sock:connect(ipaddress, port))
local bytessent, err = sock:send(requeststr…“\n”)
assert(bytessent, “failed to send request”)
assert(bytessent == #requeststr + 1, “request only partially sent”)
sock:settimeout(2)
local line, status, response = sock:receive(‘*a’)
–local response = sock:receive(‘*a’)
print(response)
assert(response, “failed to get response:” … tostring(err))
sock:close()

I think that I am missing something basic but I don’t understand what it is.
Help is much appreciated.

let me check :saluting_face:, for the moment I can share with you the tutorial it can help https://github.com/SmartThingsDevelopers/SampleDrivers/blob/main/lightbulb-lan-esp8266/driver/src/server.lua

also if you want to share code, I recommend to share it like this

local lux = require('luxure')
local cosock = require('cosock').socket
local json = require('dkjson')

local hub_server = {}

function hub_server.start(driver)
  local server = lux.Server.new_with(cosock.tcp(), {env='debug'})

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

  -- Endpoint
  server:post('/push-state', function (req, res)
    local body = json.decode(req:get_body())

    local device = driver:get_device_info(body.uuid)
    if body.switch then
      driver:on_off(device, body.switch)
    elseif body.level then
      driver:set_level(device, tonumber(body.level))
    end
    res:send('HTTP/1.1 200 OK')
  end)
  server:listen()
  driver.server = server
end

return hub_server

Thanks. I’ll take a look at what you provided and clean up my future posts.

1 Like

Hi, @Kirbyh
Are you still facing the issue?

No,

I switched to Hubitat. I find it much easier and intuitive. The LUA and JSON stuff has too much of a learning curve. The whole reason why I switched from HomeSeer to SmartThings was to reduce complexity. I find that I can accomplish that with Hubitat and it’s Rule Machine. I have almost everything implemented in just a few days.

Thanks for checking on me,

Kirby