Addressing issue with xbee
lrtnorfolk
Posts: 19
Good morning All,
Hope you are having a good weekend. I have a question regarding addressing the xbee. I have 2 xbee pro's (802.15.4) connected to 2 propellers. They work great. I download several examples of code and have not experienced any problems. However, I decided to write some spin code on my own (base on example code and documentation). I want the remote unit to wake up for 10 seconds and simply "sniff", or stay in the receive mode until it receives a signal ("A"). Then reply with a signal ("B"). After 10 seconds, it goes to sleep for 10 seconds, then repeat the process.
The base unit simply transmits a signal ("A") once a second. When it receives a response ("B"), it lights an LED. The code works fine if I declare the remotes address in the base code (XB.AT_Config(string("ATDL 2")) ). But if I try and use the remote's address as a variable, it does not associate. Is there a way to get the base unit to force an address variable before it receives a signal? Below code is what I am currently working with. The first is the base code. The second is the remote code.
Any assistance would be greatly appreciated.
[PHP]CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
XB_Rt = 8 ' XBee out
XB_Tt = 10 ' XBee in
XB_Baud = 9600 ' XBee Baud Rate
LED = 0 ' led light
CR = 13
OBJ
XB : "XBee_Object"
VAR
Long DL_Addr, stack[100]
Pub Start | cog
XB.start(XB_Rt, XB_Tt, 0, XB_Baud) ' comms for XBee
XB.AT_Init ' fast Command Mode
XB.AT_Config(string("ATMY 0")) ' base
XB.AT_Config(string("ATDL 2")) ' remote
cognew(indata,@stack)
repeat
xb.tx("A")
xb.cr
xb.delay(1000)
pub indata | input
dira[LED]~~
repeat
input := XB.rx ' incoming byte
If input == "B" ' If command
input := XB.RxDecTime(500) ' remote address
xb.delay(1000)
XB.AT_ConfigVal(string("ATDL"),input) ' remote varible
!outa[LED] ' led on off[/PHP]
[PHP]CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
XB_Rt = 20 ' out
XB_Tt = 21 ' in
XB_Baud = 9600 ' baud rate
SLP = 19
my_addr = 2 ' remote address
CR = 13
OBJ
XB : "XBee_Object"
VAR
Long stack[100]
Pub Start | DataIn, cog
XB.start(XB_Rt, XB_Tt, 0, XB_Baud) ' comms for XBee
XB.AT_Init ' fast Command mode
XB.AT_ConfigVal(string("ATMY "),MY_Addr) ' remote
XB.AT_Config(string("ATDL 0")) ' base
XB.AT_Config(string("ATSM 1")) ' sleep mode
dira[SLP]~~
repeat
outa[SLP]~
XB.Delay(20)
cog:=cognew(xb_xb, @stack)
xb.delay(10000)
cogstop(cog)
XB.Delay(50)
outa[SLP]~~
XB.Delay(10000)
pub xb_xb | datain
repeat
DataIn := XB.rx ' wait
If DataIn == "A"
xb.tx("B")
xb.dec(my_addr)
xb.cr
[/PHP]
Hope you are having a good weekend. I have a question regarding addressing the xbee. I have 2 xbee pro's (802.15.4) connected to 2 propellers. They work great. I download several examples of code and have not experienced any problems. However, I decided to write some spin code on my own (base on example code and documentation). I want the remote unit to wake up for 10 seconds and simply "sniff", or stay in the receive mode until it receives a signal ("A"). Then reply with a signal ("B"). After 10 seconds, it goes to sleep for 10 seconds, then repeat the process.
The base unit simply transmits a signal ("A") once a second. When it receives a response ("B"), it lights an LED. The code works fine if I declare the remotes address in the base code (XB.AT_Config(string("ATDL 2")) ). But if I try and use the remote's address as a variable, it does not associate. Is there a way to get the base unit to force an address variable before it receives a signal? Below code is what I am currently working with. The first is the base code. The second is the remote code.
Any assistance would be greatly appreciated.
[PHP]CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
XB_Rt = 8 ' XBee out
XB_Tt = 10 ' XBee in
XB_Baud = 9600 ' XBee Baud Rate
LED = 0 ' led light
CR = 13
OBJ
XB : "XBee_Object"
VAR
Long DL_Addr, stack[100]
Pub Start | cog
XB.start(XB_Rt, XB_Tt, 0, XB_Baud) ' comms for XBee
XB.AT_Init ' fast Command Mode
XB.AT_Config(string("ATMY 0")) ' base
XB.AT_Config(string("ATDL 2")) ' remote
cognew(indata,@stack)
repeat
xb.tx("A")
xb.cr
xb.delay(1000)
pub indata | input
dira[LED]~~
repeat
input := XB.rx ' incoming byte
If input == "B" ' If command
input := XB.RxDecTime(500) ' remote address
xb.delay(1000)
XB.AT_ConfigVal(string("ATDL"),input) ' remote varible
!outa[LED] ' led on off[/PHP]
[PHP]CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
XB_Rt = 20 ' out
XB_Tt = 21 ' in
XB_Baud = 9600 ' baud rate
SLP = 19
my_addr = 2 ' remote address
CR = 13
OBJ
XB : "XBee_Object"
VAR
Long stack[100]
Pub Start | DataIn, cog
XB.start(XB_Rt, XB_Tt, 0, XB_Baud) ' comms for XBee
XB.AT_Init ' fast Command mode
XB.AT_ConfigVal(string("ATMY "),MY_Addr) ' remote
XB.AT_Config(string("ATDL 0")) ' base
XB.AT_Config(string("ATSM 1")) ' sleep mode
dira[SLP]~~
repeat
outa[SLP]~
XB.Delay(20)
cog:=cognew(xb_xb, @stack)
xb.delay(10000)
cogstop(cog)
XB.Delay(50)
outa[SLP]~~
XB.Delay(10000)
pub xb_xb | datain
repeat
DataIn := XB.rx ' wait
If DataIn == "A"
xb.tx("B")
xb.dec(my_addr)
xb.cr
[/PHP]
Comments
.AT_ConfigVal(string, val) accepts a value, so that doesn't seem to be the issue.... looking this over..
You say "But if I try and use the remote's address as a variable, it does not associate. Is there a way to get the base unit to force an address variable before it receives a signal? "
I'm not sure what you mean by associate. Is the remote not getting an "A" or is the base not getting the replied "B"? It does work perfectly if you use the "ATMY 2" code? Can you light a remote indicator to see if it gets the "A"?
Also, I am unclear on what you mean in the second sentence. Have the base to force an address before it receives a signal? What do you mean?
I pretty positive the AT_ConfigVal() code does it's job as it's used in examples.
It looks like you start a 2nd cog to receive/send data... Seems like you need to be careful with that, Declaring objects in one cog and using them from another cog can sometimes cause problems. I've been away from heavy Propeller programming for a couple years now, so some things are cloudy. There's no real good reason to use another cog to do that.
-Martin
Thank you Sir for replying!!! Your code is what I based my code on. Sorry if I was not clear with my post. I have a tendency to write before I think. Anyway, I would like to remove the following line from the base code...XB.AT_Config(string("ATDL 2")) ' remote.
The program works perfectly with this line include. But I would like to remove it and simply read the address of the remote unit using the following code...
input := XB.RxDecTime(500) ' remote address
XB.AT_ConfigVal(string("ATDL"),input) ' remote variable
When I remove the line XB.AT_Config(string("ATDL 2")) ' remote...
The remote units TX and RX lights on the Adapter board do not illuminate and the 2 xbees don't "associate". Not sure if "associate" is the proper word, but it seems that the remote unit does not RX, thus, will not TX. If I keep XB.AT_Config(string("ATDL 2")) ' remote
the lights blink off and on as expected (on the remote unit), and the LED (connected to the base unit) blinks off and on as expected.
I can have the remote unit initiate the conversation (vice the base), and it works fine. I would prefer not to do this as the remote unit will run on batteries, thus I am trying to conserve power anywhere I can. Thank you for your time Sir. Have a good one.
Renee
For troubleshooting, it might help if you start up a debugging serial port on the base end, so that you can look at what is actually being received.
I, too, see a potential conflicts with running this in two cogs. As far as I know, Martin's object does not use locks, and your program is asking the XB object to transmit data in one cog while the other cog is potentially trying to put the XB into command mode to change the configuration--well--that could mess things up. For starters, just run the receiver in one cog, alternating between sending and receiving in one loop, and add another serial port for debugging.
Thanks for replying. I need the base unit to read the MY address of the remote units. In the future, I will have multiple remote units, thus I will need to distinguish between them. I will re-write my code so everything is done in one cog. Thanks for the advice. It is greatly appreciated.
Renee
Martin