6.2 Lab: Collecting Data in a Computer
In the previous activities we have configured remote XBees from our python program. We have also send data to remote devices using python and the API mode. Now it's time to receive data using python and the API mode.
We should configure the remote computer to periodically gather data from one or more of its pins and send it to the coordinator. The coordinator is configured in mode API 2. In our python program, we create a function that handles the reception of each data packet. We will simply print the received information on the screen.
Code sink_in_server
import serial
import time
from xbee import ZigBee
print 'Asynchronously printing data from remote XBee'
serial_port = serial.Serial('/dev/ttyUSB0', 9600)
def print_data(data):
"""
This method is called whenever data is received.
Its only argument is the data within the frame.
"""
print data['samples']
"""
Make sure the XBee is configured in API mode 2
when using escaped=True
"""
zigbee = ZigBee(serial_port, escaped=True, callback = print_data)
while True:
try:
time.sleep(0.001)
except KeyboardInterrupt:
break
zigbee.halt();
serial_port.close()
Activity:
Decide which data you want to collect and what you want to do with it. Build your protototype and share your expertise with the world. One possibility is to visualize or interpret the data in an original way. Another possible challenge is to activate an actuator in response of the data that have been received. We are getting into unknown territories here, so don't expect it to be easy!