Skip to content

Instantly share code, notes, and snippets.

@twatteyne
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twatteyne/e6624caf6fee6c855aab to your computer and use it in GitHub Desktop.
Save twatteyne/e6624caf6fee6c855aab to your computer and use it in GitHub Desktop.
IoT-LAB: forward serial traffic from motes to your local computer

The IoT-LAB platform lets you log into the server of the site your motes are located at (e.g. rennes.iot-lab.info). It all works fine.

In some cases, you want to be able to run programs and interact with the motes' serial port on your local computer. One example is running the OpenVisualizer from the OpenWSN project.

This Gist contains a couple of ideas about how to do that using SSH forwarding and a pair of Python scripts.

How it works

  • Create an experiment. You can use the code in bsp_uart.hex as an example, the mote simply prints Hello, World! on its serial port every 2s. The source code is at https://github.com/openwsn-berkeley/openwsn-fw.
  • use PuTTY to connect to the site's server (e.g. rennes.iot-lab.info). Enable SSH port forwarding by doing the following configuration. In Connection, SSH, Tunnels, add a new forwarded port by entering:
    • Source port: 1234
    • Destination: localhost:1234
    • Click the Add button
    • Under Session, click the Save button
  • Using that configuration, log into the server
  • On the server, start the openforwarderserver.py script by typing the following (replace the names of the motes by the ones in your experiment!):
python openforwarderserver.py wsn430-9,wsn430-34,wsn430-35
  • on your local computer (while the PuTTY session is running!), start openforwarderclient.py
  • you now see the serial output of each mote on your local computer.

What's missing

  • send data from your computer to the motes
  • frame the data sent over TCP (regex? JSON? HDLC? protocol buffers?)
  • control the motes opened by the server from your client. This would allow the server to run for ever, for example in a screen session.

If you want to help, e-mail me at watteyne@eecs.berkeley.edu.

import sys
import threading
import socket
import Queue
import time
class moteThread(threading.Thread):
def __init__(self,mote,sendFunc):
self.mote = mote
self.sendFunc = sendFunc
threading.Thread.__init__(self)
self.name = 'moteThread@{0}'.format(self.mote)
self.daemon = True
self.start()
def run(self):
self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.s.connect((self.mote,20000))
while True:
fromMote = self.s.recv(1024)
self.sendFunc(self.mote,fromMote)
class serverThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.name = 'serverThread'
self.daemon = True
self.q = Queue.Queue()
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.dataLock = threading.RLock()
self.connected = False
self.start()
def send(self,mote,bytesToSend):
with self.dataLock:
if not self.connected:
return
self.q.put((mote,bytesToSend))
def close(self):
self.s.close()
def run(self):
self.s.bind(('', 1234))
self.s.listen(1)
while True:
(conn,addr) = self.s.accept()
with self.dataLock:
self.connected = True
print 'Connection from {0}.'.format(addr)
try:
while True:
(mote,bytesToSend) = self.q.get()
conn.send('{0}:{1}'.format(mote,bytesToSend))
except:
with self.dataLock:
self.connected = False
print 'Connection lost.'
conn.close()
if __name__=='__main__':
# start server
serverT = serverThread()
# start connections to
motes = sys.argv[1].split(',')
for mote in motes:
moteThread(mote,serverT.send)
# wait for user to stop
raw_input('Press enter to close.\n')
serverT.close()
time.sleep(1)
sys.exit(0)
import sys
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',1234))
while True:
data = s.recv(1024)
print data,
:1040000048656C6C6F2C20576F726C64210D0A0030
:10401000314000393C4010113D402100B012824631
:104020003C4000113D4000403E401000B0125C4753
:10403000B0125047B01258473182D2D31900B14361
:104040000600053C1F4106003F53814F0600819347
:104050000600F823F2C080002100B1430600053CB1
:104060001F4106003F53814F060081930600F8234D
:10407000F2D080002100B1430600053C1F4106003C
:104080003F53814F060081930600F8231F41020031
:104090003FF0F0FF2FD3814F0200B1C010000200AB
:1040A000B1D0200002001F4102003FD0C000814F6C
:1040B00002001F4102003FF0FFF83FD00002814F95
:1040C0000200B1C000080200B1C000100200B1D06F
:1040D000002002001F4102003FF0FF3F814F02001D
:1040E0001E4102003D402F117C401100B0128A4356
:1040F0001F4104003FD01F00814F0400B1D02000B9
:1041000004001F4104003FF03FFE3FD0C000814F3C
:1041100004001F4104003FF0FFF9814F04001F41DC
:1041200004003FF0FFE7814F0400B1C0002004000D
:104130001F4104003FF0FF3F3FD00080814F04004B
:104140001E4104003D402F117C401500B0128A43EF
:104150002F413FF0FCFF2FD3814F00002F413FF054
:10416000F3FF2FD2814F00002F413FF0CFFF3FD010
:104170001000814F00002F413FF03FFF3FD0400033
:10418000814F0000B1C000010000B1D0000200006A
:10419000B1C000040000B1D000080000B1C00010A0
:1041A0000000B1D0002000002F413FF0FF3F814FC1
:1041B00000002E413D402F117C401700B0128A4371
:1041C00031523041824C1C11C2431E11C24D1F118D
:1041D000C24E2011824F2211D24102002411D2413D
:1041E00004002511D24106002611D2432711D29393
:1041F00025110E20E2C21D000B3C1F422211DF429E
:104200007E00000092531C11D2531E11F2531F1155
:10421000C2931F1125241F421C11E24F7F00F2B0F0
:1042200010000300FC2BF2C0100003005E422011BE
:104230004E8305245E830C245E83DF27E33FC29315
:104240001E11E0231F422211DF427E000000DA3FF0
:104250001F422211DF427E00000092532211D23F02
:10426000D29326110220E2D21D00C2432711304111
:10427000B240805A2001C2435600C2435700F24068
:104280008E005800E2C302004E43023C03435E53DB
:104290007E93FC23E2B30200F52FD2D31A00F2D0B2
:1042A00080002200F2C020002200F2C02000240082
:1042B000F2C020002300F2D020002500B0123A44C2
:1042C000B0129446B0126C44B0123443B01210468F
:1042D000B012D645B012A24632D230413E40030061
:1042E0000D433C402C11B0127046B01270423D405C
:1042F000AE463C409645B012DC46B012E6463C4025
:10430000D246B01244473C43B012CA44C2932E1165
:10431000FD27C2432E11C2432D11C2432C115F420F
:104320002C114F4F5C4F0011B0122047C2932D113A
:10433000ED23FC3F3E400C000D433C401C11B012ED
:104340007046D2437800E2D33300E2D33200E2D2A7
:104350003300E2D23200F2D23300F2D23200E2D2A3
:104360001D00E2D21E00F2D016007800F240A30039
:104370007900C2437D00E2437C00C2437B00F2D05F
:1043800030000500D2C3780030410A1221820A4D64
:10439000C14C00000F4E8F103FF0FF00C14F0100D5
:1043A0000C4E3E400001B012F845C14E02005312BF
:1043B000531253120F4A4E437D4003000C413C50B0
:1043C0000600B012C44131500A003A4130410D128A
:1043D0000C120F120E12B0123247F2B0200023005E
:1043E0001128F2C020002300B0129E445C930320E9
:1043F000B1C010000800B0121A473E413F413C4195
:104400003D410013FF3F1F422E012F8306242F83BF
:1044100012243F80060008240E3C829316110B24C0
:10442000921216115C4330418293141104249212AB
:1044300014115C4330414C433041B0123847B01244
:104440004A47B0120E47B0121447B0121A47B012C2
:104450003E47E2D32A00E2D33600E2D22A00F2D26B
:104460002A00D2D32A00D2D336003041F2403000A5
:104470001B00F2D0C0000400F2D010007000F2D097
:1044800020007100F2427400C2437500F2405B00EC
:104490007300D2C37000B012F6463040EE46B01240
:1044A0002C47F2B020002400062882931A11082419
:1044B00092121A11053C82931811022492121811BB
:1044C000F2E0200024005C4330411E4212110F4CE8
:1044D0001F521211824F12111D4290010D8E0C9D20
:1044E000042CB2D0110082013041824F9201B2D02F
:1044F0001000820130410D120C120F120E12B01278
:104500003247B012C6465C930320B1C010000800C9
:10451000B0121A473E413F413C413D4100130D124C
:104520000C120F120E12B0123247B01206445C93F6
:104530000320B1C010000800B0121A473E413F41AD
:104540003C413D4100130D120C120F120E12B0121D
:104550003247B012FE465C930320B1C01000080041
:10456000B0121A473E413F413C413D4100130D12FC
:104570000C120F120E12B0123247B012BA465C93F0
:104580000320B1C010000800B0121A473E413F415D
:104590003C413D410013D2532C11F29010002C11DC
:1045A000072C5F422C114F4F5C4F001130402047C9
:1045B000D2432D1130410D120C120F120E12B012F7
:1045C0003247B1C010000800B0121A473E413F41C7
:1045D0003C413D4100132E430D433C402F11B0128E
:1045E0007046C2433011B0123840D24330113C43C0
:1045F00030402C467CF37EF30F4E0D4C0E431C4393
:104600000D5D0E6E0E9F01280E8F0C6CF92B304144
:104610002E420D433C401011B0127046824382017D
:1046200082439201B2402001800130413C53824CD0
:1046300072018243640182437401B240060060014A
:10464000B2D01001600130410A1221830A4C814A24
:1046500000000D410D535C43B0126047F83F0F4C12
:10466000043CFF4D00001F533E530E93FA2330418C
:104670000F4C043CCF4D00001F533E530E93FA23C2
:1046800030410F4C0F5D033CCC4300001C530C9F8A
:10469000FB233041F2D070003100F2D070003200C4
:1046A00030413E420D433C40141130407046B01240
:1046B0000647B012264730402047B012F646921205
:1046C00028114C433041B012EE4692122A114C434D
:1046D0003041D2432E113C433040CA44824C281111
:1046E000824D2A113041F2D0C00000003041F2C0AA
:1046F000400002003041F2C08000020030419212BE
:1047000010115C433041F2E0100031003041E2C250
:1047100029003041F2C229003041D2C32900304182
:10472000C24C770030415C42760030411C4270013F
:104730003041D2D329003041E2C329003041D2C3F5
:1047400035003041824C10113041E2C33500304118
:104750003040DC42304048463040544730405E46AE
:024760003041E6
:04FFE800CE431E45A1
:04FFF0006E45F64420
:02FFF600B6450E
:02FFFA0046457A
:02FFFE001040B1
:00401001AF
:00000001FF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment