140 likes | 457 Vues
Wireless Embedded Systems (0120442x) Sensor Network Programming and MoteLib Simulator. Chaiporn Jaikaeo chaiporn.j@ku.ac.th Department of Computer Engineering Kasetsart University. Outline. Network programming with MoteLib MoteSim – MoteLib's Simulator. Virtual Mote.
E N D
Wireless Embedded Systems(0120442x)Sensor Network Programmingand MoteLib Simulator Chaiporn Jaikaeo chaiporn.j@ku.ac.th Department of Computer EngineeringKasetsart University
Outline • Network programming with MoteLib • MoteSim – MoteLib's Simulator
Virtual Mote Mote and Network Emulator
Mote and Network Simulation • Motes are modeled with standard C program • Exact same code as used for the actual hardware • Network is modeled with Python program from motesim import Simulator, Mote MOTEAPP = 'build/sim/count-radio.elf' sim = Simulator() sim.addNode(Mote(MOTEAPP), (100,100)) sim.addNode(Mote(MOTEAPP), (200,100)) sim.run()
Creating Executable for Simulator • Executable for simulator can be built by running • Executable will be stored under build/simdirectory make PLATFORM=sim
Modeling Network • Network is modeled with Python • Make sure the following directories are part of PYTHONPATH • Try the following program sim-blink.py • Can be found in examples/ directory $MOTELIB_DIR/platforms/sim/python $MOTELIB_DIR/lib/python from motesim import Simulator, Mote sim = Simulator() sim.addNode(Mote('build/sim/blink.elf'), (100,100)) sim.addNode(Mote('build/sim/blink.elf'), (200,100)) sim.run()
Create a mote running 'blink' app Create a mote running 'count' app Add m2 to the simulationat position (200,100) Virtual Mote Creation • Virtual mote object can be instantiated from class Mote • Each virtual mote instance must be added to the simulation using addNode method from motesim import Simulator, Mote sim = Simulator() m1 = Mote('build/sim/blink.elf') m2 = Mote('build/sim/count.elf') sim.addNode(m1, (100,100)) sim.addNode(m2, (200,100)) sim.run()
Enabling Python Console • IPython is recommended for better interaction from motesim import Simulator, Mote sim = Simulator() sim.addNode(Mote('build/sim/blink.elf'), (100,100)) sim.addNode(Mote('build/sim/blink.elf'), (200,100)) sim.run(script=sim.console) $ sudo apt-get install ipython
Accessing Node Objects • Node objects are stored in the node list inside sim object >>> n = sim.nodes[2] >>> dir(n)
Turning Node On and Off • Node can be turned off using shutdown()method • boot() method turns node back on >>> sim.nodes[3].shutdown() >>> sim.nodes[3].boot()
Emulating Button • Button pressing can be emulated by two methods: • pressButton() • releaseButton() >>> n = sim.nodes[3] >>> n.pressButton() >>> n.releaseButton()
Emulating UART • Node's UART interface can be emulated via TCP socket • Not activated by default • Use activateSocket() inside node's uart object to activate >>> n = sim.nodes[3] >>> n.uart.activateSocket() ('0.0.0.0', 32345) >>> $ telnet localhost 57597 Trying 0.0.0.0... Connected to 0. Escape character is '^]'. ^] telnet> mode c Listening port switch to character mode
Exercise: Voting Machine • Reimplement the wireless voting application • Allow user to cast a vote using the USER button • Voting choices are: Red (1), Yellow (2), Green (3), or No Vote (0) • When the USER button is pressed, • Set LED status accordingly • Report current vote to the base station (#0) with message type 50 • Integrate base station functionality into your app
Exercise (cont'd) • Create a virtual network with 5 nodes • Activate UART at base station (node #0) • Telnet to the open port • Emulate button pressing at other nodes