Ferdinand Keil's

electronic notes

Mar 30, 2013

Connect V-USB devices to the Internet with the TL-WR703N - Part 1

Note: This article is a follow-up to Use the TP-LINK TL-WR703N as a WiFi client with OpenWrt.

The reason why I bought the TL-WR703N orignally was to connect my cigar humidifier circuit to the internet. In the last article I established a WiFi connection to my local network. Now I needed a way to talk to my circuit over USB. It includes a USB port, which I used so far to update the firmware. So all that was left was some firmware based on V-USB and a script/program on the router side talking to it.

I soon found out, that the easiest way to get talking to USB devices was using Lua with libusb. However, the original OpenWrt package for the TL-WR703N doesn't include a libusb binding for Lua. I'm not going to compile OpenWrt, so... to the Googles! As it happenend to be, Madox had already done all the hard work for me. He posted several firmware images for the TL-WR703N, of which I chose the Standard version. Updating OpenWrt given a proper firmware image is really easy, so I won't go into detail here.

Now I needed a known-good firmware for my circuit to get started developing Lua scripts. Objective Development, the people behind V-USB, developed several example firmwares demonstrating their library. I chose the PowerSwitch, as it comes with a command-line utility that works under Windows.

I then adapted the firmware for my needs. In the end it would only toggle one PIN which is connected to an LED. To make sure it's working, I then connected the device to my PC and ran the command-line utility. It worked flawlessly first try. Now, I went on to duplicate this functionality in Lua. The source for the command-line utility is included with the PowerSwitch example, so I started from that. The resulting Lua script is shown below. The script does not implement all functions of the PowerSwitch. It only works on port 0 and can not return the status of the device.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- usage: lua powerswitch.lua on|off [duration]
-- duration (in seconds) is optional

PSCMD_STATUS =  1
PSCMD_ON =      2
PSCMD_OFF =     3

usb = require('libusb1')

local handle = usb.open_device_with_vid_pid(0x16C0, 0x05DC);

if (handle~=nil) then
    print('Device initialised with success !!')

    local port = 0
    local requesttype = usb.LIBUSB_REQUEST_TYPE_VENDOR + usb.LIBUSB_RECIPIENT_DEVICE + usb.LIBUSB_ENDPOINT_IN
    local request = PSCMD_STATUS
    if (arg[1] == 'on') then
        request = PSCMD_ON
    end
    if (arg[1] == 'off') then
        request = PSCMD_OFF
    end

    local duration = 0
    if (arg[2] ~= nil) then
        duration = arg[2] * 5
    end

    usb.control_transfer(handle, requesttype, request, duration, port, 8, 5000)
else
    print('Device initialisation failed !!')
end

There is not much documentation available on how to use libusb with Lua. I first looked at the Interfacing with Lua tutorial for the Pinguino, but soon found out it was written for a different libusb binding. The binding for Lua included in Madox's package is lualibusb1, for which a function reference is available. Some important things to know about lualibusb1 are:

  • open_device_with_vid_pid([ctx, ] vendorid, productid) is implemented, which makes finding the right device really easy.
  • When requiring the library, type usb = require('libusb1') and then work with the object usb.
  • Constants from libusb are available, but some have different names. All are prefixed with LIBUSB_, but I still had to look up the names in the source of lualibusb1.

One important thing I almost forgot to mention: it is not possible to hook up a V-USB device directly to a TL-WR703N. That is because the TL-WR703N only supports USB2.0 or high-speed devices. So the V-USB device has to be connected to a USB hub, which is in turn connected to the router. Any cheap USB2.0 hub will do, I used this one: LOGILINK UA0136.