10.09.2016 Views

Hacking_and_Penetration_Testing_with_Low_Power_Devices

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Remote control via python<br />

169<br />

serial_port ¼ serial.Serial(’/dev/ttyO2’, 57600)<br />

xbee ¼ Xbee(serial_port)<br />

xbee.tx(dest_addr¼’\x00\x00’, data¼”This is my data”)<br />

The XBee module supports receiving data synchronously or asynchronously. The<br />

following code snippet demonstrates synchronous mode:<br />

import serial<br />

from xbee import XBee<br />

serial_port ¼ serial.Serial(’/dev/ttyUSB0’, 57600)<br />

xbee ¼ XBee(serial_port)<br />

while True:<br />

try:<br />

print xbee.wait_read_frame()<br />

except KeyboardInterrupt:<br />

break<br />

serial_port.close()<br />

In order to use asynchronous mode, a callback function must be defined <strong>and</strong> registered.<br />

The upside of using asynchronous mode is that your script can be off doing<br />

other things when no data are being received by the XBee modem. The following<br />

snippet demonstrates the use of asynchronous mode:<br />

import serial<br />

import time<br />

from xbee import Xbee<br />

def dispatch_packets(data):<br />

saddr ¼ data[’source_addr’]<br />

saddr_long ¼ data[’source_addr_long’]<br />

rec_data ¼ data[’rf_data’]<br />

print “Received %i bytes from MAC %s <strong>with</strong> address %s” %<br />

(len(rec_data), repr(saddr_long), repr(saddr))<br />

serial_port ¼ serial.Serial(’/dev/ttyUSB0’, 57600)<br />

xbee ¼ Xbee(serial_port, callback¼dispatch_packets)<br />

while True:<br />

try:<br />

print “Doing something else”<br />

sleep(1)<br />

except KeyboardInterupt:<br />

break<br />

xbee.halt()<br />

serial_port.close()<br />

In order to control our drones using XBee, we must establish a protocol for this<br />

communication. The simple protocol we will use is to start packets <strong>with</strong> a comm<strong>and</strong><br />

or packet identifier followed by a colon. Comm<strong>and</strong>s sent to drones will start <strong>with</strong><br />

“c:”. Responses back to the comm<strong>and</strong> console will be of the form “lr:” followed by a number of packets beginning <strong>with</strong> “r:”. Announcements<br />

can be sent from the drones by send a packet that starts <strong>with</strong> “a:”.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!