Debug | Creating Drivers for LAN Devices with SmartThings Edge

I am following the tutorial " Tutorial | Creating Drivers for LAN Devices with SmartThings Edge" and can’t seem to get the device registered into the SmartThings app.

I am able to configure and set up the ESP8266 with the app. I am also able to upload the driver to my SmartThings hub. Everything seems to be working until the driver tried to get the device information from the xml file on the ESP device. The ESP seems to “lock-up” after sending the location of the xml file, and therefore it seems that the Edge driver times out and tries to process a NULL message at line 38.

The source is a clone of the SampleDrivers from github

Any help in deeper troubleshooting would be much appreciated.

See logs:

Here is the terminal log from the ESP8266

NodeMCU 3.0.0.0 built on nodemcu-build.com provided by frightanic.com
        branch: release
        commit: f25dc56d3c6213b8ac7ce46d1293466137746eae
        release:
        release DTS: 202112300746
        SSL: false
        build type: float
        LFS: 0x0 bytes total capacity
        modules: file,gpio,http,net,node,pwm2,sjson,tmr,uart,wifi
 build 2022-06-15 21:04 powered by Lua 5.1.4 on SDK 3.0.1-dev(fce080e)
> LightBulb ESP8266 App Ready...
> WiFi Access Point enabled...

> service: station
status:  connected
ssid:    XXXXXXXXXX
bssid:   e4:c3:2a:12:7e:92

service:  station
status:   IP Address ready
action:   start UPnP Socket
netmask:  255.255.255.0
gateway:  255.255.255.0
>>> DEVICE AVAILABLE OVER LAN AT: 192.168.1.105

INCOMING TRAFFIC:
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900
MAN: "ssdp:discover"
MX: 4
ST: urn:SmartThingsCommunity:device:LightBulbESP8266:1

SSDP RESPONSE:
HTTP/1.1 200 OK
Cache-Control: max-age=100
EXT:
SERVER: NodeMCU/Lua5.1.4 UPnP/1.1 LightBulbESP8266/0.1
ST: upnp:rootdevice
USN: uuid:6003e2-SN-ESP8266-696
Location: http://192.168.1.105:80/LightBulbESP8266.xml


Here is the associated logcat:

connecting... connected
2022-06-15T21:29:36.461603962+00:00 TRACE LAN LightBulb  Setup driver LAN-LightBulb with lifecycle handlers:
DeviceLifecycleDispatcher: LAN-LightBulb
  default_handlers:
    init:
    added:
    removed:
    driverSwitched:
  child_dispatchers:

2022-06-15T21:29:36.465603837+00:00 TRACE LAN LightBulb  Setup driver LAN-LightBulb with Capability handlers:
CapabilityCommandDispatcher: LAN-LightBulb
  default_handlers:
    refresh:
      refresh
    switchLevel:
      setLevel
    colorControl:
      setColor
    switch:
      on
      off
  child_dispatchers:

2022-06-15T21:29:36.473678587+00:00 TRACE LAN LightBulb  Server.new_with
2022-06-15T21:29:36.476659587+00:00 TRACE LAN LightBulb  Server:post
2022-06-15T21:29:36.479638129+00:00 TRACE LAN LightBulb  Server:listen
2022-06-15T21:29:36.498580670+00:00 DEBUG LAN LightBulb  driver device thread event handled
2022-06-15T21:29:36.501626170+00:00 TRACE LAN LightBulb  Received event with handler _resync
2022-06-15T21:29:36.504757920+00:00 TRACE LAN LightBulb  Received event with handler environment_info
2022-06-15T21:29:36.508690920+00:00 TRACE LAN LightBulb  Received event with handler environment_info
2022-06-15T21:29:36.511569129+00:00 DEBUG LAN LightBulb  Z-Wave hub node ID environment changed.
2022-06-15T21:29:36.515362212+00:00 TRACE LAN LightBulb  Received event with handler discovery
2022-06-15T21:29:36.531520170+00:00 INFO LAN LightBulb  ===== SCANNING NETWORK...
2022-06-15T21:29:37.551655254+00:00 INFO LAN LightBulb  ===== DEVICE FOUND IN NETWORK...
2022-06-15T21:29:37.554529796+00:00 INFO LAN LightBulb  ===== DEVICE DESCRIPTION AT: http://192.168.1.105:80/LightBulbESP8266.xml
2022-06-15T21:29:37.557379837+00:00 INFO LAN LightBulb  ===== FETCHING DEVICE METADATA...
2022-06-15T21:29:37.570759671+00:00 ERROR LAN LightBulb  discovery thread encountered error: [string "discovery.lua"]:38: attempt to index a nil value (field 'root')

That example hasn’t been updated for some of the changes required in recent firmware. See the top post in the link below for changes to how socket.http should be pulled in, plus the dkjson library has been changed to st.json.

1 Like

Thanks for the help! I got the sample working, but it still seems finicky. Getting a lot of network errors, and colorControl not working; but that is troubleshooting for another day.

1 Like

Keep up the good work! :+1: Lan drivers are the ones community need right now. Zigbee and Z-Wave devices are covered quite well already.

Hi, @AVGAVR!
Thank you for sharing your experience, I’ll open a report to update that sample.
Let us know if you need any more help. In the case of the colorControl capability, check if you’re receiving an error from the actions in the capability handler:

Thanks, @philh30 for the help as well.

When will this example be updated?
SampleDrivers/lightbulb-lan-esp8266 at main · SmartThingsDevelopers/SampleDrivers · GitHub

It’s been a while after this hub firmware update, but there’s NO reference code for LAN connected devices after this update.

Sorry about that, the team is already working on that. I’ll keep you updated.

1 Like

@philh30, @AVGAVR
The sample was updated according to the requirements of the Hub’s firmware v43.

I made a test and I was able to install the device correctly and send commands.
Please, let me know if you have any questions.

1 Like

Hello @nayelyz
Does this sample driver work with Hub’s firmware v46? It doesn’t look like the server post callback is calling.

Hi, @nereusuj.

Could you elaborate on this part, please? Do you mean this is not executed? https://github.com/SmartThingsDevelopers/SampleDrivers/blob/main/lightbulb-lan-esp8266/driver/src/server.lua#L16

If you see an error in the driver, please share it with us.

@nayelyz Maybe referring to this? I think this should change with the new firmware?

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

My problem, shown below, is the http post in the app\src\server.lua is not executed but times out instead. This is the only piece of the tutorial example code that does not work and it was working back in December. Any idea what changed and how to fix it?

This section of code is supposed to update the device state at ST when a change is made on the ESP8266 web control page.

==============
Debug print output

PUSH STATE
URL: http://192.168.1.145:56603/push-state
DATA: {“switch”:“off”,“uuid”:“f2e61bf1-6dcb-4797-800d-2b34f80e5535”}
HTTP client: Connection timeout
-1 nil

====================
Code in app\src\server.lua


– Push Remote State
local function push_state(data)
if not DEV.HUB.addr or not DEV.HUB.port then
print(‘NO HUB REGISTERED’)
return nil
end

– Prepare URL
local url = string.format(
‘http://%s:%s/push-state’, DEV.HUB.addr, DEV.HUB.port)
– JSONstringify table
data.uuid = DEV.HUB.ext_uuid
local data = sjson.encode(data)

print('PUSH STATE\r\nURL: '…url…
'\r\nDATA: '…data)
return http.post(
url,‘Content-Type: application/json\r\n’,data,
function(code, data) print(code, data) end)
end

Yes, right. I have the same error with @Rlgarner54 .

Sorry for the delay.
Recently, there have been issues with the function register_channel_handler. So, instead of using this one, you need to change it to:

cosock.spawn(function()
  local client = serversock:accept()
  watch_socket(client)
end)

CC @nereusuj

That helped point me in the right direction. To add it to the tutorial sample I made several
changes. The biggest issue was what is watch_socket? I replaced it as shown and now the http post does not time out and ST updates the light status correctly.

Thanks (I hope the html below renders correctly)
Capture

local lux = require(‘luxure’)
–rlg local cosock = require(‘cosock’).socket
local cosock = require(‘cosock’)
local json = require(‘dkjson’)

local hub_server = {}

function hub_server.start(driver)
--rlg local server = lux.Server.new_with(cosock.tcp(), {env=‘debug’})
local server = lux.Server.new_with(cosock.socket.tcp(), {env=‘debug’})

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

cosock.spawn(function()
--rlgRTE local serversock = server.sock --rlg added
–rlgRTE local client = serversock:accept()
–rlgRTE watch_socket(client)

while true do –rlgALT
server:tick() --rlgALT
end –rlgALT
end)

1 Like

Thank you @Rlgarner54 ! You made my day! :grinning:

Thank you very much @Rlgarner54 :smiley: