# Error initializing socket.tcp()

**URL:** <https://community.smartthings.com/t/error-initializing-socket-tcp/274247>\
**Category:** Writing Edge Drivers\
**Created:** [December 3, 2023, 10:20pm UTC](https://community.smartthings.com/t/error-initializing-socket-tcp/274247 "2023-12-03T22:20:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirbyh](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@Kirbyh](https://community.smartthings.com/u/Kirbyh)\
**Post date:** [December 3, 2023, 10:20pm UTC](https://community.smartthings.com/t/error-initializing-socket-tcp/274247/1 "2023-12-03T22:20:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AlejandroPadilla](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/alejandropadilla/32/175701_2.png) [@AlejandroPadilla](https://community.smartthings.com/u/AlejandroPadilla)\
**Post date:** [December 4, 2023, 5:48pm UTC](https://community.smartthings.com/t/error-initializing-socket-tcp/274247/2 "2023-12-04T17:48:29Z")

</div>

let me check 🫡, 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](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

```lua
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

```

---

<div class="post-metadata">

**Author:** ![Kirbyh](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@Kirbyh](https://community.smartthings.com/u/Kirbyh)\
**Post date:** [December 4, 2023, 10:23pm UTC](https://community.smartthings.com/t/error-initializing-socket-tcp/274247/3 "2023-12-04T22:23:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nayelyz](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@nayelyz](https://community.smartthings.com/u/nayelyz)\
**Post date:** [January 5, 2024, 11:53pm UTC](https://community.smartthings.com/t/error-initializing-socket-tcp/274247/4 "2024-01-05T23:53:04Z")

</div>

> [@Kirbyh](#):
>
> Thanks. I’ll take a look at what you provided and clean up my future posts.

Hi, @Kirbyh  
Are you still facing the issue?

---

<div class="post-metadata">

**Author:** ![Kirbyh](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@Kirbyh](https://community.smartthings.com/u/Kirbyh)\
**Post date:** [January 6, 2024, 12:59pm UTC](https://community.smartthings.com/t/error-initializing-socket-tcp/274247/5 "2024-01-06T12:59:22Z")

</div>

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
