We have moved at community.getvera.com

Author Topic: openLuup: serial connection  (Read 2015 times)

Offline vosmont

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
openLuup: serial connection
« on: September 04, 2016, 03:33:49 pm »
Here is a little tutorial to be able to use a device connected via USB on openLuup.

1/ Retrieve the usb device

With the command "dmesg", you will find the mounted point.
Code: [Select]
[    7.151351] usbcore: registered new interface driver usbserial
[    7.151525] usbcore: registered new interface driver usbserial_generic
[    7.151663] usbserial: USB Serial support registered for generic
[    7.230878] usbcore: registered new interface driver pl2303
[    7.231047] usbserial: USB Serial support registered for pl2303
[    7.231167] pl2303 1-1.3:1.0: pl2303 converter detected
[    7.323296] usb 1-1.3: pl2303 converter now attached to ttyUSB0

My USB device is reachable on /dev/ttyUSB0

2/ Install Serial to Network Proxy

Install ser2net
Code: [Select]
sudo apt-get update
sudo apt-get install ser2net

3/ Configuration

Edit the file "ser2net.conf"
Code: [Select]
sudo nano /etc/ser2net.conf

Add this line (it depends on your device; see config on your Vera in Apps->Develop apps->Serial Port configuration)
Code: [Select]
3481:raw:0:/dev/ttyUSB0:9600 8DATABITS NONE 1STOPBIT

Restart the deamon
Code: [Select]
sudo /etc/init.d/ser2net restart

4/ Open the connection

In openLuup :
Code: [Select]
luup.io.open( lul_device, "127.0.0.1", "3481" )

then your plugin will react as on a Vera (incoming tag in implementation file)

5/ TODO

The proxy ser2net is also used on the Vera. The config made in the UI is used to launch the deamon ser2net directly.

e.g. : (get from "ps -x" on a Vera)
Code: [Select]
/bin/sh -c ser2net -n  -C "3483:raw:0:/dev/ttyUSB0:57600 1STOPBIT NONE" -C "3482:raw:0:/dev/ttyUSB1:38400 1STOPBIT NONE" -C "3481:raw:0:/dev/ttyUSB2:9600 1STOPBIT NONE"

It will be great to find a way to propose this to openLuup's users
« Last Edit: September 04, 2016, 04:14:24 pm by vosmont »

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +290/-70
  • "Less is more"
Re: openLuup: serial connection
« Reply #1 on: September 04, 2016, 04:13:05 pm »
Thanks so much for the tutorial!  Hopefully should extend the applicability of openLuup!

There must be an expert out there who can advise on step 5.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1574
  • Karma: +110/-31
Re: openLuup: serial connection
« Reply #2 on: September 06, 2016, 06:03:38 am »
Hi,

For step five you would need to duplicate the Serial Port function where you attach a serial device to a plugin.

For the Smart Meter plugin that processed using incoming I also look for the IP attribute set (with help from nlrb). If so I do an explicit luup.io.open call. this is for Vera if someone uses an external serial to IP converter rather then plugging directly in to the Vera. This logic comes in handy for openLuup as I enter the local IP and the port configured in ser2net in the plugin attribute.

This is the bit of code that does this. Quick to add to a plugin you want to have supported on openLuup. (do need to filter out leading/trailing spaces as those are entered quickly in AltUI with firefox it seems)
Code: [Select]
local ip = luup.attr_get("ip",lul_device)
if (ip ~= nil and ip ~= "") then
local ipaddr, port = string.match(ip, "(.-):(.*)")
if (port == nil) then
ipaddr = ip
port = 80
end
log("IP = " .. ipaddr .. ", port = " .. port)
luup.io.open(lul_device, ipaddr, tonumber(port))
end

PS: I saw you did find to thread on some issues with this on Raspberry PI with Jessie/Debian due to a bug in timeout handling in LuaSock on that platform.

Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.

Offline vosmont

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
Re: openLuup: serial connection
« Reply #3 on: September 09, 2016, 02:29:05 am »
Great ! Thanks a lot reneboer  :)

Here is the code I use in a plugin, to be Vera and openLuup compliant :

Code: [Select]
-- Check IO connection
local function _checkIoConnection()
if not luup.io.is_connected( g_parentDeviceId ) then
-- Try to connect by ip (openLuup)
local ip = luup.attr_get( "ip", g_parentDeviceId )
if ( ( ip ~= nil ) and ( ip ~= "" ) ) then
local ipaddr, port = string.match( ip, "(.-):(.*)" )
if ( port == nil ) then
ipaddr = ip
port = 80
end
log( "Open connection on ip " .. ipaddr .. " and port " .. port, "init" )
luup.io.open( g_parentDeviceId, ipaddr, tonumber( port ) )
end
end
if not luup.io.is_connected( g_parentDeviceId ) then
error( "Serial port not connected. First choose the serial port and restart the lua engine.", "init" )
UI.showSysMessage( "Choose the Serial Port", SYS_MESSAGE_TYPES.ERROR )
return false
else
local ioDevice = tonumber(( Variable.get( g_parentDeviceId, VARIABLE.IO_DEVICE ) ))
if ioDevice then
-- Check serial settings
local baud = Variable.get( ioDevice, VARIABLE.BAUD ) or "9600"
if ( baud ~= "9600" ) then
error( "Incorrect setup of the serial port. Select 9600 bauds." )
UI.showSysMessage( "Select 9600 bauds for the Serial Port", SYS_MESSAGE_TYPES.ERROR )
return false
end
log( "Baud is 9600", "init" )
end
end
log( "Serial port is connected", "init" )
return true
end

In openLuup, I just have to set ip attr to "127.0.0.1:3481"

Offline Forzaalfa

  • Full Member
  • ***
  • Posts: 227
  • Karma: +5/-0
Re: openLuup: serial connection
« Reply #4 on: May 26, 2018, 04:53:20 am »
Step 4: Where exactly in openLuup? (bear with me, im new to this!)
I've installes the rfx plugin and ser2net, so where do I put this link?
Vera Plus - RFXtrx433e

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1574
  • Karma: +110/-31
Re: openLuup: serial connection
« Reply #5 on: May 29, 2018, 11:11:23 am »
Step 4: Where exactly in openLuup? (bear with me, im new to this!)
I've installes the rfx plugin and ser2net, so where do I put this link?
That is for the plug in author normally to add to the plugin start up code.

Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.