Shop OBEX P1 Docs P2 Docs Learn Events
Xbee not working in API mode — Parallax Forums

Xbee not working in API mode

NicopowersNicopowers Posts: 12
edited 2016-08-12 00:56 in Propeller 1
Hello everyone,

I am putting together a wireless XBee communication system where i read a temperature using a TMP36 Analog sensor connected to a MCP3208 (Analog to digital converter) connected to my Propeller Mini (this will be an XBee router), and send that data to a XBee coordinator which is connected to another Propeller Mini. Both Propeller Mini's are connected to a separate computer, so they can be individually accessed through XCTU and Spin programming tool. I have not been able to establish the XBee communication and I must use API mode so I can take advantage of its features.

My first question is, do I have to previously setup the XBee devices in any particular mode (like AT mode or API mode) in XCTU?

My second question is, When i execute the command :
XB.AT_Config(string("ATAP 1"))

Should I expect that XCTU shows that XBee in API mode? Or it will just do it for that session and return to AT mode?

Below please find both the Coordinators and Router code:

XBee Coordinator Code :
CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

  ' Set pins and Baud rate for XBee comms
  XB_Rx         = 17  
  XB_Tx         = 16
  XB_Baud       = 9600
   
  ' Set pins and baud rate for PC comms  
  PC_Rx         = 31  
  PC_Tx         = 30
  PC_Baud       = 9600

  ' Set address of base unit
  MY_Addr       = 0
  
    
OBJ

  XB    : "XBee_Object_2"
  PC    : "FullDuplexSerial" 
  
PUB Start 
  ' Configure XBee & PC Comms
  XB.start(XB_Rx, XB_Tx, 0,9600)     
  PC.start(PC_Rx, PC_Tx, 0, 9600)

  XB.AT_Init                    ' fast config
  XB.AT_ConfigVal(string("ATMY"), MY_Addr)
  XB.AT_Config(string("ATAP 1"))' Switch to API mode   

  PC.str(string("Coordinator in API mode ready at address:"))
  PC.dec(MY_Addr)
  PC.Tx(13)

  Repeat
    ProcessFrame

Pub ProcessFrame | Temp 
  '' Accepts incoming frame for data and diplay
  
  XB.API_Rx                     ' wait for API data
  if XB.RxIdent == $81          ' If data identifier is a msg string
                                ' Display source address
     PC.Str(string(13,"Data Received from address: "))
     PC.DEC(XB.srcAddr)
                                ' Display received string
     PC.Str(string(13,"Message String   : "))
     PC.str(XB.RxData)
                                ' Pull out 1st dec value
                                ' and display
     PC.str(string(13,"Remote Temperature  : "))
     Temp := XB.ParseDEC(XB.RxData,1)
     PC.DEC(Temp)
    
     PC.str(string(13,"RF Signal strength: "))
     PC.DEC(-XB.rxRSSI)          ' Display RSSI level   
     
     ControlPin(XB.srcAddr,4,1)  ' Call method to turn on LED 4
                                 ' on sending end device
     XB.Delay(50)                ' Delay
     ControlPin(XB.srcAddr,4,0)  ' Call method to turn off LED 4
                                 ' on sending unit

     PC.str(string(13,"------------------------------------------"))   


Pub ControlPin(destAddr, pin, state)
'' This method accepts address, pin and state for control
'' builds a packet to be sent in API mode and transmits
'' Data is sent as BYTE values instead of decimal strings.
    XB.API_NewPacket             ' clean out packet of old data
    XB.API_AddStr(string("i"))   ' Add an i to packet
    XB.API_AddByte(pin)          ' Add a byte of pin number
    XB.API_AddByte(state)        ' Add a byte of pin state
                                 ' Send the packet to the 
    XB.API_txPacket(destAddr,XB.API_Packet,3)




My_Setup.jpg




XBee Router Code :
VAR
  long     tempch2
  long     stack[50]
CON
    _clkmode = xtal1 + pll16x                           
    _xinfreq = 5_000_000
    LEDs_START      = 16                      
    LEDs_END        = 18
    ' I/O and Baud rate for XBee comms 
    XB_Rx     = 1  
    XB_Tx     = 2
    XB_Baud   = 9600

    ' XBee addresses
    DL_Addr = 0    ' Send data to this address (base)
    MY_Addr = $5F77    ' This units address

    dpin = 15       'both din and dout of the mcp3208 are connected to this pin on the prop demo board.
    cpin = 14       'the clock pin of the mcp3208 is connected to this pin on the prop demo board.
    spin = 0        'the chip select pin of the mcp 3208 is connected to this pin on the prop demo board.
    mcp3208ratio = 0.0008056640625

OBJ
adc     : "MCP3208"
pst     : "Parallax Serial Terminal Plus"
fstr    : "FloatString"
f32     : "FloatMath"
sd      : "PropBOE MicroSD2"
XB      : "XBee_Object_2"
PUB Main
pst.start(9600)   'Start the Parallax Serial Terminal object at 115200 baud
adc.start(dpin, cpin, spin, 255)  'Start the MCP3208 object and enable all 8 channels as single-ended inputs.
XB.Start(XB_RX, XB_TX ,0, XB_Baud)

XB.AT_Init
XB.AT_ConfigVal(string("ATMY"), MY_Addr)   
XB.AT_ConfigVal(string("ATDL"), DL_Addr)
XB.AT_Config(string("ATAP 1"))' Switch to API mode            

sd.Mount(0)
sd.FileNew(String("Sensordata.txt"))
sd.FileOpen(String("Sensordata.txt"), "A")
sd.WriteStr(String("CH2 Values", 13, 10))
sd.UnMount
          
cognew (CH2, @stack)
sdwriter ' writes to SD Card

repeat                     ' Accept incoming in loop
  GetData

Pub GetData | IO, dataIn, value 
  dataIn := XB.rx          ' wait for incoming byte
  If dataIn == "i"         ' i = I/O control
     IO := XB.rx           ' Accept IO number as byte value
     value := XB.rx        ' Accept state (1/0) as byte
     dira[IO]~~            ' Set direction of pin
     outa[IO] := value     ' Set state of pin
       

PUB CH2   
dira[18]~~
outa[18]~
XB.Delay(1000)

repeat  
  
  pst.Str(String(pst#cs, pst#NL, pst#HM, "Temperature value= "))
  tempch2 := f32.FFloat(adc.in(2))
  tempch2 := f32.FMul((f32.FSub((f32.FMul(tempch2,  mcp3208ratio)), 0.50)), 100.0)
  pst.Str(fstr.FloatToString(tempch2))
  if (tempch2 => 20.0) AND (tempch2 < 30.0)     
      !outa[18]
      waitcnt(clkfreq + cnt)
  elseif (tempch2 => 18.0) AND (tempch2 < 20.0)
      !outa[18]
      waitcnt(clkfreq/2 + cnt)
  elseif (tempch2 => 16.0) AND (tempch2 < 18.0)
      !outa[18]
      waitcnt(clkfreq/4 + cnt)
  elseif (tempch2 => 14.0) AND (tempch2 < 16.0)
      !outa[18]
      waitcnt(clkfreq/8 + cnt)
  else  
      !outa[18]
      waitcnt(clkfreq/16 + cnt)
  tempch2 := fstr.FloatToString(tempch2)
  XB.str(tempch2)         ' send tempch2
  XB.Delay(5000)          ' wait 5 seconds and send again
  
PUB sdwriter
  repeat
    sd.Mount(0)
    sd.FileOpen(String("Sensordata.txt"), "A")
  
    sd.WriteDec(tempch2)
    sd.WriteByte(13)                             ' Carriage return
    sd.WriteByte(10)                             ' New line
    waitcnt(clkfreq * 5 + cnt)
    
    sd.FileClose  
   sd.Unmount  

Once again, I only want to use API mode but I have seen examples on the internet where they initialize the XBee communication with AT mode methods and then switch to API mode before sending or receiving data. Please help me get the XBee communication working or reference me a guide involving similar XBee implementation with API mode. I am attaching two separate archives for the XBee router spin code and for the XBee coordinator code.

Comments

  • ElectrodudeElectrodude Posts: 1,657
    edited 2016-08-12 03:47
    I always do all of my configuration through XCTU. This includes setting the baud rate and enabling API mode if desired. I never mix AT and API modes. You can change settings through API mode (see "XB.API_Config(string("XX"), val)") if you need to, but I've never needed to.

    Also, if you would like to use one of the four port serial drivers instead of several single port ones to save some cogs, you may be interested in my modified version of the XBee object that uses any variant of four port FDS instead of standard single port FDS..
Sign In or Register to comment.