openLuup: serial connection

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.

[    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

sudo apt-get update
sudo apt-get install ser2net

3/ Configuration

Edit the file “ser2net.conf”

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)

3481:raw:0:/dev/ttyUSB0:9600 8DATABITS NONE 1STOPBIT

Restart the deamon

sudo /etc/init.d/ser2net restart

4/ Open the connection

In openLuup :

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)

/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

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.

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)

		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

Great ! Thanks a lot reneboer :slight_smile:

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

-- 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”

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?

[quote=“Forzaalfa, post:5, topic:193597”]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?[/quote]
That is for the plug in author normally to add to the plugin start up code.

Cheers Rene