Skip to content

Commit

Permalink
#62
Browse files Browse the repository at this point in the history
  • Loading branch information
Keoma Brun committed May 20, 2018
1 parent 59a6564 commit b812ade
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
*.log
.idea/
Empty file added code/software/__init__.py
Empty file.
Empty file added code/software/app/__init__.py
Empty file.
51 changes: 30 additions & 21 deletions code/software/app/mercatorRunExperiment.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#!/usr/bin/python

# =========================== adjust path =====================================

import os
import sys
if __name__ == '__main__':
here = sys.path[0]
sys.path.insert(0, os.path.join(here, '..', 'lib'))

# =========================== imports =========================================

import argparse
Expand All @@ -19,12 +11,12 @@
import socket

# Mercator
import MoteHandler
import MercatorDefines as d
from ..lib import MoteHandler
from ..lib import MercatorDefines as d

# IoT-lab
import iotlabcli as iotlab
from iotlabcli import experiment
from iotlabcli import experiment, node

# =========================== logging =========================================

Expand All @@ -41,7 +33,6 @@

# =========================== body ============================================


class MercatorRunExperiment(object):

FREQUENCIES = [n+11 for n in range(16)] # frequencies to measure on, in IEEE notation
Expand All @@ -62,20 +53,33 @@ def __init__(self, args, serialports, site="local"):
self.nbpackets = args.nbpackets
self.txpksize = args.txpksize
self.txpower = args.txpower
self.experiment_id = args.expid

# use the file created by auth-cli command
usr, pwd = iotlab.get_user_credentials()

# authenticate through the REST interface
self.api = iotlab.rest.Api(usr, pwd)

# connect to motes
for s in serialports:
logfile.debug("connected to %s", s)
self.motes[s] = MoteHandler.MoteHandler(s, self._cb)
self.motes[s] = MoteHandler.MoteHandler(s, self._cb, reset_cb=self._reset_cb)
if not self.motes[s].isActive:
logconsole.info("DELETED %s", s)
del self.motes[s]
raise Exception("Mote {0} is not responding.".format(s))

# get current datetime
now = datetime.datetime.now().strftime("%Y.%m.%d-%H.%M.%S")
self.file = gzip.open('{0}{1}-{2}_raw.csv.gz'.format(DATASET_PATH,
self.site,
now),
'wb')

# open file
self.file = gzip.open(
'{0}{1}-{2}_raw.csv.gz'.format(
DATASET_PATH,
self.site,
now
),
'wb'
)

# write settings
settings = {
Expand All @@ -85,7 +89,7 @@ def __init__(self, args, serialports, site="local"):
"tx_count": self.nbpackets,
"transaction_count": self.nbtrans,
"node_count": len(self.motes),
"site": self.site,
"location": self.site,
"channel_count": len(self.FREQUENCIES),
"start_date": now,
"txpower": self.txpower
Expand All @@ -94,7 +98,7 @@ def __init__(self, args, serialports, site="local"):
self.file.write('\n')

# write csv header
self.file.write('datetime,src,dst,frequency,rssi,crc,expected,' +
self.file.write('datetime,src,dst,channel,rssi,crc,expected,' +
'transaction_id,pkctr\n')

try:
Expand Down Expand Up @@ -236,6 +240,11 @@ def _cb(self, serialport, notif):
logfile.debug("Node %s restarted",
d.format_mac(self.motes[serialport].get_mac()))

def _reset_cb(self, mote):
logfile.debug('restarting mote {0}'.format(mote.serialport))
node.node_command(self.api, 'reset', self.experiment_id, [mote.serialport])
logfile.debug('mote {0} restarted'.format(mote.serialport))

@staticmethod
def _quit_callback():
print "quitting!"
Expand Down
28 changes: 18 additions & 10 deletions code/software/lib/MoteHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@
import Hdlc
import MercatorDefines as d

BAUDRATE = 500000
TIMEOUT_RESPONSE = 3
MAX_TIMEOUTS = 3

class MoteHandler(threading.Thread):

_BAUDRATE = 500000
TIMEOUT_RESPONSE = 3
STAT_UARTNUMRXCRCOK = 'uartNumRxCrcOk'
STAT_UARTNUMRXCRCWRONG = 'uartNumRxCrcWrong'
STAT_UARTNUMTX = 'uartNumTx'

STAT_UARTNUMRXCRCOK = 'uartNumRxCrcOk'
STAT_UARTNUMRXCRCWRONG = 'uartNumRxCrcWrong'
STAT_UARTNUMTX = 'uartNumTx'
class MoteHandler(threading.Thread):

def __init__(self, serialport, cb=None):
def __init__(self, serialport, cb=None, reset_cb=None):

self.serialport = serialport
self.cb = cb
self.reset_cb = reset_cb
self.serialLock = threading.Lock()
self.dataLock = threading.RLock()
self.mac = None
Expand All @@ -36,13 +37,15 @@ def __init__(self, serialport, cb=None):
self.isActive = True
self.response = None
self._iotlab = False
self.timeouts = 0
self._reset_stats()

try:
if self.iotlab:
self.serial = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serial.connect((serialport, 20000))
else:
self.serial = serial.Serial(self.serialport, self._BAUDRATE)
self.serial = serial.Serial(self.serialport, BAUDRATE)
except Exception as err:
msg = 'could not connect to {0}, reason: {1}'.format(serialport, err)
print msg
Expand Down Expand Up @@ -133,12 +136,17 @@ def send_REQ_ST(self):
)
)

self.waitResponseEvent.wait(self.TIMEOUT_RESPONSE)
self.waitResponseEvent.wait(TIMEOUT_RESPONSE)

if not self.waitResponseEvent.isSet():
print "-----------timeout--------------" + self.serialport
self.isActive = False
self.timeouts += 1
if self.timeouts > MAX_TIMEOUTS:
self.reset_cb(self)
return
else:
self.timeouts = 0

with self.dataLock:
self.waitResponse = False
Expand Down
Empty file added code/software/lib/__init__.py
Empty file.

0 comments on commit b812ade

Please sign in to comment.