Пример #1
0
def main():

    DevList = {}
    FormedAll = []  # Ana Dig values after passing through Format()
    FormedAnalog = []  # Ana values after passing through Format()
    FormedDigital = []  # Dig values after passing through Format()

    example_text = '''Example usage:

    # Run discovery process on nework to find XBEE-IO devices
    ./xbeeio.py --port ttyUSB1 --discover
     
    # Readback all IO in default format and output style
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --readall

    # Read all IO and format analogue readings as rounded up voltages
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --readall --format volts

    # Read ADC inputs 0,1 & 3 and format readings as rounded up (TMP36) temperatures 
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --readadc 031 --format temp

    # Read ADC input 2 and format reading as rounded up 4-20mA 
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --readadc 2  --format 420

    # Read Dig Ins 0,1,2 & 3 
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --readdigin 0321

    # Read Dig In 0 
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --readdigin 0

    # Set Dig Out 0,1 & 3 Enabled
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --setdigouts 1101

    # Set Dig out 1 & 3 Enabled
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --setdigouts 0101

    # Set Dig Out 0 = Enabled
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --setdigouts 1000

    # Read all inputs and output in a logline format with analog voltage formatted as rounded up volts
    ./xbeeio.py --port ttyUSB1 --remote XBEE1 --readall --outputlogline --format volts

    '''
    ap = ArgParser(description='XBEE-IO Communication Script',
                   epilog=example_text,
                   formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--port",
                    action='store',
                    dest='port',
                    help="serial port to connect to",
                    required=True)
    ap.add_argument("--discover",
                    action='store_true',
                    dest='show',
                    help="discover the digimesh network devices")
    ap.add_argument("--remote",
                    action='store',
                    dest='remote',
                    help="specify the target XBee NI identifier to talk to")
    ap.add_argument("--readall",
                    action='store_true',
                    dest='readall',
                    help="read back all input/output states")
    ap.add_argument("--format",
                    action='store',
                    dest='format',
                    help="printable format : volts|420|temp")
    ap.add_argument("--readadc",
                    action='store',
                    dest='anapins',
                    help="read back on or more analog inputs")
    ap.add_argument("--readdigin",
                    action='store',
                    dest='digipins',
                    help="read back one or more digital inputs")
    ap.add_argument("--outputstd",
                    action='store_true',
                    dest='std',
                    help="display the output in human readable form (default)")
    ap.add_argument("--outputlogline",
                    action='store_true',
                    dest='log',
                    help="display output in single data log format")
    ap.add_argument("--outputjson",
                    action='store_true',
                    dest='json',
                    help="display output in JSON format")
    ap.add_argument("--setdigouts",
                    action='store',
                    dest='setdigout',
                    help="set digouts as 4bit state <0123>")
    ap.add_argument("--quiet",
                    action='store_true',
                    dest='quiet',
                    help="suppress extra output")

    if len(sys.argv) == 1:
        ap.print_help(sys.stderr)
        sys.exit(1)
    args = ap.parse_args()

    PORT = '/dev/' + args.port

    try:

        if args.show:
            # Instatiate RPi Main Hub
            RPi = DigiMeshDevice(PORT, BAUD_RATE)
            RPi.open()

            DevList = ShowRemote(RPi, args.quiet)

            # Export network devices to file
            with open('network.conf', 'w+') as f:
                f.truncate(0)  # Reset file contents
                for node, address in DevList.items():
                    f.write(node + '|' + address + '\n')

        elif args.remote:

            # Instatiate RPi Main Hub
            RPi = DigiMeshDevice(PORT, BAUD_RATE)
            RPi.open()

            # Scan and save network if it does not exist
            if not os.path.exists('network.conf'):

                DevList = NetworkDiscovery(RPi, args.quiet)
                with open('network.conf', 'w+') as f:
                    for node, address in DevList.items():
                        f.write(node + '|' + address + '\n')

            # Repopulate dictionary for practicality
            with open("network.conf") as f:
                for line in f:
                    (node, address) = line.strip().split('|')
                    DevList.update({node: address})

            # Make sure target NodeID exists in network
            if args.remote in DevList.keys():
                RemoteXBee = RemoteDigiMeshDevice(
                    RPi,
                    XBee64BitAddress.from_hex_string(
                        str(DevList.get(args.remote))))
                #print("\n")
            else:
                print("Target NodeID: " + args.remote +
                      " not found in network.conf, try rescan first.")
                exit()

        if args.setdigout:
            SetDigitalOut(RemoteXBee, args.setdigout, args.quiet)

        # Either --readall
        if args.readall:
            args.anapins = '0,1,2,3'
            args.digipins = '0,1,2,3'
            readall = '0,1,2,3'

            ReadInputs = ReadAnalog(RemoteXBee, readall,
                                    args.quiet) + ReadDigital(
                                        RemoteXBee, readall, args.quiet)
            FormedAll = Format(RemoteXBee, ReadInputs, args.format,
                               args.anapins, args.digipins)

        # Or read specific
        else:
            if args.anapins:

                ReadInputs = ReadAnalog(RemoteXBee, args.anapins, args.quiet)
                FormedAnalog = Format(RemoteXBee, ReadInputs, args.format,
                                      args.anapins, '')  # No digital ''

            if args.digipins:

                ReadInputs = ReadDigital(RemoteXBee, args.digipins, args.quiet)
                FormedDigital = Format(RemoteXBee, ReadInputs, args.format, '',
                                       args.digipins)  # No analog ''

        if args.std:

            if FormedAll:
                Output(RemoteXBee, FormedAll, args.format, args.remote, 'std',
                       args.anapins, args.digipins, args.quiet)
            else:
                Output(RemoteXBee, FormedAnalog + FormedDigital, args.format,
                       args.remote, 'std', args.anapins, args.digipins,
                       args.quiet)

        elif args.json:

            if FormedAll:
                Output(RemoteXBee, FormedAll, args.format, args.remote, 'json',
                       args.anapins, args.digipins, args.quiet)
            else:
                Output(RemoteXBee, FormedAnalog + FormedDigital, args.format,
                       args.remote, 'json', args.anapins, args.digipins,
                       args.quiet)

        elif args.log:

            if FormedAll:
                Output(RemoteXBee, FormedAll, args.format, args.remote, 'log',
                       args.anapins, args.digipins, args.quiet)
            else:
                Output(RemoteXBee, FormedAnalog + FormedDigital, args.format,
                       args.remote, 'log', args.anapins, args.digipins,
                       args.quiet)

        else:

            if not args.show:
                if FormedAll:
                    Output(RemoteXBee, FormedAll, args.format, args.remote,
                           'default', args.anapins, args.digipins, args.quiet)
                else:
                    Output(RemoteXBee, FormedAnalog + FormedDigital,
                           args.format, args.remote, 'default', args.anapins,
                           args.digipins, args.quiet)

    finally:
        if RPi is not None and RPi.is_open():
            RPi.close()
Пример #2
0
def main():
    print(" +----------------------------------------+")
    print(" | XBee Python discovery time chronometer |")
    print(" +----------------------------------------+\n")

    device = DigiMeshDevice(PORT, BAUD_RATE)

    try:
        device.open()

        protocolo = device.get_protocol()

        print(protocolo)

        funcao = device.get_role()

        print("Funcao: %s" % funcao)

        xbee_network = device.get_network()

        xbee_network.set_discovery_timeout(15)  # 15 seconds.

        xbee_network.clear()

        def callback_device_discovered(remote):
            print("Device discovered: %s" % remote)

        def callback_discovery_finished(status):
            if status == NetworkDiscoveryStatus.SUCCESS:
                print("Discovery process finished successfully.")
            else:
                print("There was an error discovering devices: %s" %
                      status.description)

        xbee_network.add_device_discovered_callback(callback_device_discovered)

        xbee_network.add_discovery_process_finished_callback(
            callback_discovery_finished)

        options = xbee_network.get_discovery_options()
        print(utils.hex_to_string(options))

        xbee_network.start_discovery_process()

        print("Discovering remote XBee devices...")

        while xbee_network.is_discovery_running():
            time.sleep(0.1)

        devices = xbee_network.get_devices()
        remote = devices[0]
        print(remote.get_protocol())
        print("Dispositivo %s " % devices[0].get_node_id())

        sourceAddres = device.get_64bit_addr()
        print("Endereco Local: %s" % sourceAddres)
        destAddress = device.get_dest_address()
        print("Endereco Destino: %s" % destAddress)

        modoDeOperacao = device.operating_mode
        print("Mode de Operacao: %s" % modoDeOperacao)

    finally:
        if device is not None and device.is_open():
            device.close()
Пример #3
0
    info.start_timer()

    xbee_network.set_discovery_timeout(25)  # 25 seconds.
    xbee_network.clear()

    xbee_network.add_device_discovered_callback(callback_device_discovered)

    xbee_network.add_discovery_process_finished_callback(
        callback_discovery_finished)

    print("\t=-------- Starting Device discover process --------=\n")

    xbee_network.start_discovery_process()

    while xbee_network.is_discovery_running():
        sleep(0.1)

    print("\nNumber of devices found on the network:", info.n_devices)
    print("Discovery time of each device:", info.discovery_time)

finally:
    if device is not None and device.is_open():
        device.close()
        print("\n\t--------- Device Closed Successfully ---------\n")

print("*discovery time order to be printed in the csv file:",
      order_device_list(info))

send_data_to_csv(order_device_list(info))