Shop OBEX P1 Docs P2 Docs Learn Events
How to use XBee with Activity Board? — Parallax Forums

How to use XBee with Activity Board?

JarvanJarvan Posts: 31
edited 2014-01-02 17:16 in Learn with BlocklyProp
I have a pair of XBEE ,a Propeller Board of Educaiton and a Activity Board , How to make the two board communicate with XBEE , and where can I find some example spin code for ActivityBoard.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-01-02 09:33
    Have you seen this page? It looks like it could help.
  • edited 2014-01-02 12:45
    This is a draft of material coming soon to learn.parallax.com.

    Full duplex serial communication allows both outgoing and incoming messages to be sent/received at the same time. Examples of devices where full duplex serial communication can be useful include XBee radios, other microcontrollers, and even the SimpleIDE terminal. Although the Propeller uses half duplex serial communication with SimmpleIDE Terminal by default, that can be disabled and replaced with full duplex serial communication. This example demonstrates how to use full duplex serial communication with XBee radios, and also with the SimpleIDE Terminal.

    Parts

    This activity uses two XBee radios and an XBee to USB adapter.
    (2) Xbee 802.15.4 radios. Options include: XB24-API-001 and XB24-AWI-001
    (1) XBee USB Adapter Board
    (misc) Jumper wires

    Other options: You can substitute the XStick 802.15.4 in place of an XBee adapter and one of the XBee modules. You can also just get the XBee Wireless Pack, which has two 802.15.4 XBee radios and the USB adapter.

    Prep Work

    Before you start, let’s make sure that the Propeller Chip is not sending an output signal to the XBee module’s DO output. If one is trying to send a high signal and the other is trying to send a low signal at opposite ends of a wire, it could damage either the Propeller I/O pin or the XBee.
    • Use SimpleIDE’s Load EEPROM & Run button to load this program into the Propeller Chip’s EEPROM:
    #include "simpletools.h"
    
    int main()
    {
      input(9);
    }
    


    Circuit

    Now, we can safely build the circuit, without having to worry about some old program in the Propeller chip’s EEPROM sending signals to the XBee module’s DO pin.

    attachment.php?attachmentid=105884&d=1388696002
    • Set the Acitivty Board’s PWR switch to 0.
    • Use jumper wires to connect XBee DO to Propeller P9 and XBee DI to Propeller P8.
    • Make sure the USB cable is UNPLUGGED from the XBee/USB adapter.
    • Socket the XBee as shown in the adapter’s documentation.
    • Connect the XBee adapter to your computer with a USB A/Mini-B cable.
    • Socket the second XBee in the Propeller Activity Board as shown in the picture below.
    • Connect your Activity Board to the PC with another USB A/Mini-B cable.

    attachment.php?attachmentid=105885&d=1388697093

    i-box: If you haven't already installed the latest USB driver, SimpleIDE, or Learn folder, go to Propeller C - Set up SimpleIDE and Propeller C - Start Simple.

    Test Code

    The test code is a loopback connection, meaning it sends back what it receives. After setting up SimpleIDE terminal to talk with the XBee adapter, you’ll be able to type characters in to send them to the XBee connected to the adapter. That XBee will broadcast those characters on its radio frequency. The XBee on the propeller will receive those characters from the airwaves, and send them to the Propeller as serial messages. The Propeller program sends “You typed: + the character” back to the Activity Board’s XBee, which broadcasts that. The XBee connected to the USB adapter receives that broadcast, and sends it to the PC, which gets displayed back on the SimpleIDE terminal.
    • Download XBee Loopback.
    • Unzip XBee Loopback into a folder.
    • Use SimpleIDE’s Open Project button to open XBee Loopback.side. (Open it from the unzipped folder. Do not try to open the one that’s inside the zip file!)
    • The COM port dropdown is at the top-right of the button bar. Click it to see your port list, and set it to your Activity Board’s port. (If you don’t know the COM port number, check the list once, then unplug your Activity Board and check again. Whichever port disappeared must be your Activity Board. Plug back in and select the port that reappeared.)
    • Next, set your COM port dropdown to your XBee/USB adapter’s port.
    • Click the Program menu and select Open Terminal.
    • Change the Baud rate dropdown from 115200 to 9600.
    • Press/release the RST button on the Activity Board.
    • Start typing characters and verify that they are sent back prepended with “You typed: h“, “You typed :e”, etc.

    attachment.php?attachmentid=105886&d=1388697319

    If you want to modify and re-run your program, follow these steps:
    • Close SimpleIDE Terminal.
    • Set your SimpleIDE COM dropdown back to the Activity Board.
    • Use the Load EEPROM & Run button to load the modified program into EEPROM.
    • Set the COM back to the XBee adapter port.
    • Click Program -> Open Terminal.
    • IMPORTANT: If you have SimpleIDE 0.9.44 or older, you will have to manually set the baud rate from 9600 to a different value, and then back to 9600.
    • Press and release the Activity Board’s RST button, and you should see the Click this terminal, and type on keyboard… prompt.

    How it Works

    The library for full duplex serial communication is called fdserial, and #include "fdserial.h" makes its functions available to the application. Then, fdserial *xbee sets up a full duplex serial device identifier. In the main function, xbee = fdserial_open(9, 8, 0, 9600) configures the serial connection to receive messages on the P9 I/O pin, send with P8, set the mode to 0, and the baud rate to 9600 bits per second. The fdserial_open function also returns a memory address to the xbee pointer variable. Now that xbee has that pointer, it can be used as a device identifier with any fdserial function with fdserial *term parameters as well as any simpletext function that has a text_t *device parameters. Keep in mind that there could be more than one full duplex serial device connected to the Propeller, so we use the device identifier to tell functions like dprint and fdserial_rxChar which serial device they are communicating with. That’s why we see xbee in function calls like dprint(xbee, "Click this terminal, \n"), and c = fdserial_rxChar(xbee).
    /*
      XBee Loopback.c
    */
    
    #include "simpletools.h"
    #include "fdserial.h"
    
    fdserial *xbee;
    
    int main()
    {
      xbee = fdserial_open(9, 8, 0, 9600);
    
      writeChar(xbee, CLS);
      dprint(xbee, "Click this terminal, \n");
      dprint(xbee, "and type on keybaord...\n\n");
    
      char c;
     
      while(1)
      {
        c = fdserial_rxChar(xbee);
        if(c != -1)
        {
          dprint(xbee, "You typed: %c\n", c);
        }
      }  
    }
    
    As with half duplex serial, full duplex serial identifiers can be used with dprint, which gives you formatting options that are much like print with the SimpleIDE Terminal. For example, dprint(xbee, "Click this terminal, \n") sends the message followed by the \n newline character. You can also use %d for sending the text representation of integer values, and even %f for floating point representations.

    The fdserial library runs code in another cog that accumulates bytes it receives into a buffer array. If there’s nothing in it, the fdserial_rxReady function returns -1. If there are one or more characters in the buffer, fdserial_rxReady returns the oldest character. After repeated calls to fetch all the characters, the function returns -1 again to indicate that there’s nothing in the buffer.

    Inside the while(1) loop, c = fdserial_rxChar(xbee) either gets -1, or the oldest character in the buffer. When it gets -1, the code inside the if(c != -1) block gets skipped –because c is equal to -1. When c contains a character, it’s not equal to -1, and in that case, the dprint(xbee, "You typed: %c\n", c) code does get executed.

    Did You Know?

    The XBee manual is available from digi.com.
    • Go to www.digi.com.
    • Click Support -> Documentation
    • Scroll down the list and select Xbee / XBee-PRO 802.15.4 Modules.
    • Click Product Manual: XBee / XBee-PRO 802.15.4 RF Modules.

    The serial *xbee declaration is global since it is made before any functions. This is useful because it allows any function in the application to pass xbee device nickname in calls to simpletext functions like dprint and putChar.

    Any simpletext function with a text_t *device parameter can be used with full duplex serial peripherals like the XBee. For a complete list of simpletext functions, see …Documents\SimpleIDE\Learn\Simple Libraries\Text Devices\libsimpletext\Documentation simpletext Library.html.

    The fdserial_open function is part of the fdserial library. For a full list of (half duplex) serial functions, see Documents\SimpleIDE\Learn\Simple Libraries\Text Devices\libfdserial\Documentation fdserial Library.html.

    The fdserial library’s receive buffer is considered FIFO. That’s an abbreviation of first-in-first-out, meaning the oldest byte that was received is the first one to be returned by fdserial_rxChar(xbee).

    The fdserial_open function’s mode parameters expects a value with binary 1s and 0s. The mode 0 in xbee = fdserial_open(9, 8, 0, 9600) is the most common one, but other variations include:
    • 0b0000 // Same as 0, true serial communication, nothing special
    • 0b0001 // Treat incoming signal as inverted (1 is 0, and 0 is 1)
    • 0b0010 // Invert signal for outgoing messages
    • 0b0100 // Ignores echoes created by a circuit that copies the signal the propeller transmits to its receive line

    You can even set the multiple mode bits. For example, you could pass a mode of 0b0011 to invert both the transmit and receive signals.

    Try this

    You might expect to see a function like xbcmd in the code below in an XBee library. This function simplifies putting the XBee into command mode for configuration. In its default mode, the XBee radio broadcasts the serial data it receives for other xbees. When it is switched to command mode, it treats the serial data it receives as configuration data. You can use this mode to set its network address, baud rate, and many other features.

    In the XBee Command Mode example (below), the main function sends +++ to the XBee module to put it into command mode. So long as the XBee doesn’t receive anything else from the Propeller for a couple seconds, it switches to command mode and replies with OK. If the Propeller gets the OK message, it then sends ATBD to find out what baud rate the XBee is set to. According to the XBee manual from Digi, since it’s at 9600, the XBee will reply with 3. Last, but not least, it sends XBCN to exit command mode, and the XBee replies with OK. After exiting command mode, the XBee returns to its normal mode of radio broadcasting the serial messages it receives.

    attachment.php?attachmentid=105887&d=1388697584
    • Set the COM port dropdown to your Activity Board.
    • Click New Project, save it as XBee Command Mode Example.c.
    • Paste the code below into SimpleIDE.
    • Click Run with Terminal.
    • Verify that the conversation with your XBee matches the one in the terminal capture above.
    • There is no such thing as an ATBc command. What do you think will happen if you change ATBD to ATBc in xbcmd("ATBD\r", response, 10, 20).
    • Does the reply make sense to you? Try adding this code right below the xbcmd you just modified: if(!strcmp(“ERROR”, response)) print(“The XBee didn’t understand!”);
    /*
      XBee Command Mode Example.c
    */
    
    #include "simpletools.h"
    #include "fdserial.h"
    
    fdserial *xbee;
    char response[10];
    
    int xbcmd(char *cmd, char *reply, int bytesMax, int msMax);
    
    int main()
    {
      xbee = fdserial_open(9, 8, 0, 9600);
      pause(1000);
      print("cmd = +++\n");
      int bytes = xbcmd("+++", response, 10, 2000);
      if(bytes == 0)
        print("Timeout error.\n");
      else
      {
        print("reply = %s", response);
        print("cmd = ATBD\n");
        xbcmd("ATBD\r", response, 10, 20);
        print("reply = %s", response); 
        print("cmd = ATCN\n");
        xbcmd("ATCN\r", response, 10, 20);
        print("reply = %s", response); 
      }
    }
    
    int xbcmd(char *cmd, char *reply, int bytesMax, int msMax)
    {
      int c = -1, n = 0;
      writeStr(xbee, cmd);
      memset(reply, 0, bytesMax);
    
      int tmax = (CLKFREQ/1000) * msMax;
      int tmark = CNT;
    
      while(1) 
      {
        c = fdserial_rxCheck(xbee); 
        if(c != -1)
          reply[n++] = c;
        if(CNT - tmark > tmax)
          return 0;
        if(c == '\r')
          return n;
      }
    }
    


    Your Turn

    To drive home the point that you can have multiple full duplex serial connections, let’s update the Try This example so that it uses fdserial to communicate with SimpleIDE terminal, and another instance of fdserial running in another cog to communicate with the XBee. Here is a start on modifications to XBee Command Mode Example.c:

    attachment.php?attachmentid=105888&d=1388697917

    We created another fdserial identifier named term, which is short for terminal. The simpleterm_close function call shuts down the default half duplex communication, which will cause print calls to stop working. But that’s fine because we can use full duplex serial.

    Note the last command in this example was modified from print(“cmd = +++”) to dprint(term, “cmd = +++”). If you do that with all the print statements, it’ll work the same as before, but will be running a full duplex serial connection (instead of half duplex serial) under the hood.
    397 x 279 - 225K
    479 x 372 - 303K
    490 x 289 - 54K
    553 x 273 - 21K
    558 x 351 - 69K
  • edited 2014-01-02 14:55
    Renaming from "How to use XEBB with Activity Board?" to "How to use XBee with Activity Board?"
  • jazzedjazzed Posts: 11,803
    edited 2014-01-02 15:06
    Nice tutorial Andy.
  • edited 2014-01-02 15:45
    Hey, thanks jazzed!
  • TymkrsTymkrs Posts: 539
    edited 2014-01-02 17:08
    http://forums.parallax.com/archive/index.php/t-141739.html

    A
    lso has a LOT of propeller + xbee examples - the placement of the xbee is different, but the code should still be the same since it's on a propeller. Good luck!

    Addie
  • JarvanJarvan Posts: 31
    edited 2014-01-02 17:16
    Thanks, Andy.
  • Tymkrs wrote: »
    http://forums.parallax.com/archive/index.php/t-141739.html

    A
    lso has a LOT of propeller + xbee examples - the placement of the xbee is different, but the code should still be the same since it's on a propeller. Good luck!

    Addie

    This link doesn't work. Trying to transmit floating point numbers over the XBee using SimpleIDE.
  • Tymkrs wrote: »
    http://forums.parallax.com/archive/index.php/t-141739.html

    A
    lso has a LOT of propeller + xbee examples - the placement of the xbee is different, but the code should still be the same since it's on a propeller. Good luck!

    Addie

    This link doesn't work. Trying to transmit floating point numbers over the XBee using SimpleIDE.


    Try looking at these links: https://www.reddit.com/r/Tymkrs/comments/3bxl7g/xbee_propeller/
    https://www.reddit.com/r/Tymkrs/comments/3bxpqc/xbee_propeller_sensors/

    Though I think most of my examples are more in Spin rather than C...
Sign In or Register to comment.