Shop OBEX P1 Docs P2 Docs Learn Events
Xbee P2 FlexBASIC — Parallax Forums

Xbee P2 FlexBASIC

I have just started to look into using Xbee in my P2 FlexBASIC robot project. Anybody have something working already?

I found some Xbee modules, that I had purchased back some time ago. Two are S1 modules and two are S2 modules, I think I will try to use the S2 modules, because they have mesh capability.

I also downloaded the Xctu User Guide; I viewed the Jon youtube session on Xbee, but I think I will be more comfortable with a paper manual in front of me.

So, my intent is to be able to communicate with the robot via the Xbee, instead of a wire.

Ray

Comments

  • JonnyMacJonnyMac Posts: 9,159
    edited 2021-12-18 18:00

    Parallax has an XBee book. The code is in PBASIC (?) and Spin, but the information is useful.

    Jon Titus (a member of the Parallax community) wrote a book on using XBee some time ago. The XCTU screens don't match, but the information is still very useful. It has lots of examples in C which you have comfort with, so you should be able to modify them easily.
    -- https://www.amazon.com/Hands-XBEE-Lab-Manual-Communications/dp/0123914043/

  • PublisonPublison Posts: 12,366
    edited 2021-12-18 21:00
  • This is my first try at using xbee with FlexBASIC. The program compiles, but it looks like the two xbee's are not understanding each other.

    I tested out the xbee's using an Activity Board, and the xbee USB breakout, that setup, the xbee's, was working as expected.

    I also, on the FlexBASIC side, tried out both SmartSerial and SimpleSerial, neither one was doing the job. Not sure if that is where the problem is. Since FlexBASIC does not have access to a P2 FullDuplexSerial, I could not test out a third program. Not sure what to try next to get this xbee setup too work.

    Ray

    ' testxbee.bas
    
    ' Compiler Directives
    OPTION BASE 0
    OPTION EXPLICIT
    
    ' Constants
    CONST HEAPSIZE = 8192
    'CONST _clkfreq = 180_00_000
    
        ' CLASSes
        'dim xbee as class using "spin/SmartSerial.spin"
        dim xbee as class using "spin/SimpleSerial.spin2"
        xbee.startx(12, 13, 0, 9600)
        open SendRecvDevice(@xbee.tx,@xbee.rx) as #3
    
    ' Member Variables
        'dim inBuff, inBuffx as string
        dim val1# as single
        dim inBuffx as string
    
    ' Main loop
    do
    
        print #3, "> ";
        input #3, inBuffx
        if inBuffx == "a" then
            'print #3, using "##.##"; val1# 
            pinhi 10
        else if inBuffx == "b" then
            pinlo 10
        else
            print #3, "??"
        end if
    loop
    
  • Here is what I have going so far, it is sort of working.

    The problem that I am having is, for some reason, the 'if inBuffx = "a"' is not working as expected. When I press the 'a' key on the remote keyboard, the program is getting 97, and not the ascci 'a'. When I change to 'if inBuffx5 = 97', then it recognizes that the 'a' key was pressed, and it does what it is supposed to do.

    I switched over to jm_FullDuplexSerial.spin2 for this exersise, to get more control, but it seems that I cannot get FlexBASIC to cooperate correctly. I need help.

    Ray

    ' testxbee.bas
    
    ' Compiler Directives
    OPTION BASE 0
    OPTION EXPLICIT
    
    ' Constants
    CONST HEAPSIZE = 8192
    'CONST _clkfreq = 180_00_000
    
        ' CLASSes
        'dim xbee as class using "spin/SmartSerial.spin"
        'dim xbee as class using "spin/SimpleSerial.spin2"
        'xbee.startx(12, 13, 0, 9600)
        'open SendRecvDevice(@xbee.tx,@xbee.rx) as #3
        dim xbee as class using "jm_fds.spin2"  ' jm_FullDuplexSerial.spin2
    
        xbee.start(13, 12, 0, 9600)
    
    ' Member Variables
        'dim inBuff, inBuffx as string
        'dim val1# as single
        dim inBuffx as string
        'dim inBuffy as string
    
    ' Main loop
    do
    
        'print #3, "> ";
        xbee.tx(10)
        xbee.tx(13)
        xbee.str("> ")  ' This shows up on remote terminal.
        'input #3, inBuffx
        chr$(inBuffx) = xbee.rx()  ' Get the keypress on remote terminal.
        'inBuffy = strint$(inBuffx%)  ' Convert integer to a string.  
        if inBuffx =  "a" then 'chr$("a") then  ' a
            'print #3, using "##.##"; val1# 
            print "You pressed "; inBuffx   ' Show what was pressed on remote keyboard.
            pinhi 11
        else if inBuffx = "b" then 'chr$("b") then  ' b
            print "You pressed "; inBuffx  ' Show what was pressed on remote keyboard.
            pinlo 11
        else
            'print #3, "??"
            xbee.tx(10)
            xbee.tx(13)
            print "You pressed "; inBuffx 'inBuffx%  ' Show what was pressed on remote keyboard.
            xbee.str("??")
            'xbee.tx(13)
        end if
    loop
    
  • 97 is the ASCII code for the letter 'a'. If you want to convert the result of xbee.rx into a character string, you need to do:

       inBuffx = chr$(xbee.rx())
    

    which is not the same as what you wrote.

  • My setup, at the moment is an xbee S1, in a SIP module with the P2 Edge setup. The other xbee S1, in the USB module, is acting as the base, and can be connected to whichever PC that I want.

    I have the xbee's talking to each other, so that has been a good sign. All that I did was connect each xbee to xctu, set everything to default, write it, and everything seems to be working as expected, in transparent mode. I guess this was the easy part.

    I have been using the GTK serial port terminal program on the xbee base side, but it looks like I might have to write a special terminal program, for that side of things.

    Now the hard part, I want to replace the S1 with the S2, and try to get the mesh capability thing to work. More reading and making mistakes.

    Ray

  • JonnyMacJonnyMac Posts: 9,159
    edited 2021-12-22 00:20

    Now the hard part, I want to replace the S1 with the S2, and try to get the mesh capability thing to work.

    I believe you have to switch to an API mode for that to work -- it's no longer a simple matter of serial passthrough.

  • RsadeikaRsadeika Posts: 3,837
    edited 2021-12-21 22:35

    Below is a working FlexBASIC program for testing the xbee that is connected to the P2 Edge setup.

    I am using jm_FullDuplexSerial.spin2 for this program, I kind of wish it had a strrx() function so I could use a string on input instead of just a character. I did try to get SmartSerial.spin or SimpleSerial.spin2 for the using a string on input, but I could not get it to work. Hopefully something will show up to fix the problem.

    Next, I will change out the S1 xbee with an S2 xbee and start looking into having more than two xbee's online, and see what I can do with that.

    I did do a search on ebay for xbee related stuff, not that much is available. I do have some TP-link Kasa WiFi plug mini modules implemented, it could be nice if I could replace them with an xbee equivalent.

    Ray

    ' testxbee.bas
    
    ' Compiler Directives
    OPTION BASE 0
    OPTION EXPLICIT
    
    ' Constants
    CONST HEAPSIZE = 8192
    'CONST _clkfreq = 180_00_000
    
        ' CLASSes
        'dim xbee as class using "spin/SmartSerial.spin"
        'dim xbee as class using "spin/SimpleSerial.spin2"
        'xbee.startx(13, 12, 0, 9600)
        'open SendRecvDevice(@xbee.tx,@xbee.rx) as #3
        dim xbee as class using "jm_fds.spin2"  ' jm_FullDuplexSerial.spin2
    
        xbee.start(13, 12, 0, 9600)
    
    ' Member Variables
        'dim inBuff, inBuffx as string
        'dim val1# as single
        dim inBuffx$ as string
        'dim inBuffy as string
    
    ' Main loop
    do
    
        'print #3, "> ";
        xbee.tx(10)
        xbee.tx(13)
        xbee.str("> ")  ' This shows up on remote terminal.
        'input$ #3, inBuffx$
        'inBuffx$ = input$(10,3)
        inBuffx$ = chr$(xbee.rx())  ' Get the keypress on remote terminal.
        'inBuffy = strint$(inBuffx%)  ' Convert integer to a string.  
        if inBuffx$ = "o" then ' o
    
            print "You pressed "; inBuffx$  ' Show what was pressed on remote keyboard.
            pinhi 11 ' LED on p11
        else if inBuffx$ = "f" then ' f
            print "You pressed "; inBuffx$  ' Show what was pressed on remote keyboard.
            pinlo 11 ' LED on p11
        else
            'print #3, "??"
            xbee.tx(10)
            xbee.tx(13)
            print "You pressed "; inBuffx$ 'inBuffx%  ' Show what was pressed on remote keyboard.
            xbee.str("??")
    
        end if
    loop
    
  • I am making some headway, but not there just yet.

    I am trying to setup a star topology, using an S2, S1, and S1. The S2, i have setup as a coordinator-AT, and the S1 I have setup as end device-AT. The part that I am having trouble with is the addressing of the units.

    The S2 coordinator-AT, CH-14, PAN ID-234, SC-1ffe, DH-0, DL-2, MY ADDRESS-1.
    The S1 end device-AT, CH-14, PAN ID-234, SC-1ffe, DH-0, DL-1, MY ADDRESS-2.
    The S1 end device-AT, CH-14, PAN ID-234, SC-1ffe, DH-0, DL-0, MY ADDRESS-3.

    Their is communication between the coordinator and the end device, but I cannot talk to the individual end device, both respond at the same time. I cannot figure this addressing scheme.

    Ray

  • This is the new code for the xbee modules, I added a third end device to the mix.

    The S2 coordinator-AT, CH-17, PAN ID-234, SC-1ffe, DH-0, DL-ffff, MY ADDRESS-0.
    The S1 end device-AT, CH-17, PAN ID-234, SC-1ffe, DH-ffff, DL-0, MY ADDRESS-1.
    The S1 end device-AT, CH-17, PAN ID-234, SC-1ffe, DH-ffff, DL-0, MY ADDRESS-2.
    The S1 end device-AT, CH-17, PAN ID-234, SC-1ffe, DH-ffff, DL-0, MY ADDRESS-3.

    I tried using dh and Dl for addressing, put I never got it work, so I decided, for the time being, to use a different method. So far it seems to be working, as expected. The FlexBASIC example shows how the individual module ID would work, in this case, in a very simplistic manner.

    The coordinator component can get very complicated, not sure if Python would be the language to use. So far every thing seems to working as expected, in this very simplistic form.

    Ray

    FlexBASIC example

    ' testxbee.bas
    
    ' Compiler Directives
    OPTION BASE 0
    OPTION EXPLICIT
    
    ' Constants
    CONST HEAPSIZE = 8192
    'CONST _clkfreq = 180_00_000
    
        ' CLASSes
        'dim xbee as class using "spin/SmartSerial.spin"
        'dim xbee as class using "spin/SimpleSerial.spin2"
        'xbee.startx(13, 12, 0, 9600)
        'open SendRecvDevice(@xbee.tx,@xbee.rx) as #3
        dim xbee as class using "jm_fds.spin2"  ' jm_FullDuplexSerial.spin2
    
        xbee.start(13, 12, 0, 9600)
    
    ' Member Variables
        'dim inBuff, inBuffx as string
        'dim val1# as single
        dim inBuffx$ as string
        'dim inBuffy as string
    
    ' Main loop
    do
        inBuffx$ = chr$(xbee.rx())  ' Get the keypress on remote terminal.
        ' Check if xbee coordinator is sending commands.
      if inBuffx$ = "3" then   ' xbee module ID
        inBuffx$ = chr$(xbee.rx())  ' Get the keypress on remote terminal.
            if inBuffx$ = "o" then ' o
                pinhi 11 ' LED on p11
            else if inBuffx$ = "f" then ' f
    
                pinlo 11 ' LED on p11
            else
                xbee.str("??")
            end if
        end if
    loop
    

    Python example

    #import xbee
    from serial import Serial
    import time
    
    PORT = '/dev/ttyUSB3'
    BAUD = 9600
    
    ser = Serial(PORT, BAUD)
    #xbee = Serial(PORT, BAUD)
    
    def xbee1():  # xbee module no. 1
        #xbee.tx(dest_addr='\x00\x01', data='n')
        ser.write('1'.encode("utf-8"))
        ser.write('n'.encode("utf-8"))
        time.sleep(2)
        ser.write('1'.encode("utf-8"))
        ser.write('f'.encode("utf-8"))
    
    # Wait for and get the response
    #print(xbee.wait_read_frame())
    #print(ser.wait_read_frame())
    #time.sleep(3)
    def xbee2():  # xbee module no. 2
        ser.write('2'.encode("utf-8"))
        ser.write('n'.encode("utf-8"))
        time.sleep(2)
        ser.write('2'.encode("utf-8"))
        ser.write('f'.encode("utf-8"))
    
    def xbee3():  # xbee module no. 3
        ser.write('3'.encode("utf-8"))
        ser.write('o'.encode("utf-8"))
        time.sleep(2)
        ser.write('3'.encode("utf-8"))
        ser.write('f'.encode("utf-8"))
    
    inBuff=1
    
    while 1:
        inBuff = input("> ")
        if inBuff == 'quit':
            ser.close()
            exit()
        elif inBuff == 'mod1':
            xbee1()
        elif inBuff == 'mod2':
            xbee2()
        elif inBuff == 'mod3':
            xbee3()
        else:
            print("??")
    
    
Sign In or Register to comment.