Ejemplo n.º 1
0
class XBeeRadio(Node):
    def __init__(self):
        super().__init__('xbee_radio')

        self.declare_parameter('xbee_port', '/dev/xbee')
        self.declare_parameter('xbee_baud', 9600)
        self.declare_parameter('is_gsu', False)
        port = self.get_parameter('xbee_port').value
        baud = self.get_parameter('xbee_baud').value
        is_gsu = self.get_parameter('is_gsu').value

        # setup XBee radio and add callback to publish
        self.device = DigiMeshDevice(port, baud)
        self.device.open()
        self.device.add_data_received_callback(self.rx_callback)
        self.xnet = self.device.get_network()
        # if is_gsu:
        #     self.set_aggregator()

        self._publisher = self.create_publisher(Packet, 'received', 10)
        self._subscription = self.create_subscription(Packet, 'transmit',
                                                      self.tx_callback, 10)
        self._subscription

    def set_aggregator(self):
        ''' Call this if the device is connected to a groundstation '''
        self.device.build_aggregate_routes()

    def tx_callback(self, msg):
        ''' ROS: callback for data wanting to be transmitted by the XBee radio '''
        self.get_logger().info(f"Transmitting: {msg.data}")
        dev_addr = XBee64BitAddress.from_hex_string(msg.dev_addr)
        self.device.send_data_async_64(
            dev_addr,
            bytearray(struct.pack(str(len(msg.data)) + 'c', *msg.data)))

    def rx_callback(self, msg):
        ''' XBEE: callback for data received by the XBee radio '''
        data = list(struct.unpack(str(len(msg.data)) + 'c', msg.data))
        dev_addr = str(msg.remote_device.get_64bit_addr())
        self.get_logger().info(
            f"Received: {data} from {dev_addr} at time: {msg.timestamp}")
        packet = Packet()
        packet.data = data
        packet.dev_addr = dev_addr
        packet.timestamp = msg.timestamp
        packet.is_broadcast = msg.is_broadcast
        self._publisher.publish(packet)
Ejemplo n.º 2
0
        data = packet.data[4:]
        print("expecting ", packet_counters[dev_addr]," packets")
        packet_buffers[dev_addr] = b''
    else:
        packet_counters[dev_addr] -= 1
        data = packet.data

    packet_buffers[dev_addr] += data

    if packet_counters[dev_addr] is 0:
        with xbee.read_lock: # Acquire lock to read command data from GCS
            telemetry_data = ToGCS.deserialize(packet_buffers[dev_addr])
            print(packet.remote_device.get_node_id(), ": ", telemetry_data)


device.add_data_received_callback(packet_received)

try:
    while True:
        cmd = input("Enter command (+,-,s,e,m): ")
        if cmd is '+':
            state += 1
            print("New state", state)
        if cmd is '-':
            state -= 1
            print("New state", state)
        if cmd is 's':
            stop = not stop
        if cmd is 'e':
            ToERU(stop, state, hiker_pos, geo_bounds, LatLng(5,5), LatLng(5.5,5.5), False, None, True).serialize().transmit(device, devices['eru'])
        if cmd is 'm':
    request.setHeader('Access-Control-Allow-Headers', 'x-prototype-version,x-requested-with')
    request.setHeader('Access-Control-Max-Age', 2520)
    
    content = json.loads(request.content.read().decode("utf8"))
    print(content)
    command = content['command']
    DID = content['deviceID']

    if DID != "00000000":
        toSend={"CMD":"HMOD","MODE":command, "TS": str(int(time.time()))}
        remote_device = RemoteXBeeDevice(device, XBee64BitAddress.from_hex_string(DID))

        # Send data using the remote object.
        device.send_data_async(remote_device, json.dumps(toSend))
    else:
        toSend={"CMD":"HMOD","MODE":command, "TS": str(int(time.time()))}
        print("broadcasting command " + json.dumps(toSend))
        device.send_data_broadcast(json.dumps(toSend))




    return json.dumps({"result":"success"})


device = DigiMeshDevice(PORT, BAUD_RATE)
device.open()
device.add_data_received_callback(my_data_received_callback)

app.run("0.0.0.0", 5001)
Ejemplo n.º 4
0
import time
from digi.xbee.devices import DigiMeshDevice
import xbee

comm_port = "/dev/ttyUSB0"
baud_rate = "9600"

device = DigiMeshDevice(port=comm_port, baud_rate=baud_rate)
device.open()

print("This device's name: ", device.get_node_id())


def sample_callback(message):
    print('Received: ', message.data, '\nFrom:', message.remote_device.get_node_id())
    print(xbee.ToGCS.deserialize(message.data))
    
device.add_data_received_callback(sample_callback)

try:
    while True:
        print('Waiting....')
        time.sleep(1)
except KeyboardInterrupt:
    print('stopping')
finally:
    device.del_data_received_callback(sample_callback)