XBee-MATLAB Interfacing

Hello again!

Even with the info & tutorials on NeuroRighter’s Google Site, I’m not getting a clear picture of how I would connect it to my XBee… and from the info on using NeuroRighter in a closed-loop, it looks like I might need to learn C++ / C#…

and use MATLAB anyway, so! I decided to shift gears and focus on XBee <–> MATLAB interfacing first.

There’s some documentation on this, but it’s mostly limited to these two articles:

  1. Wireless control and monitoring of an LED using XBee, which has a function defining the XBee class, and
  2. Continuous monitoring of wireless network of temperature sensors using MATLAB and XBee, which builds off of the latter.

Both of those are made to work with XBee Series 2 modules (see comments below #1), and I believe I’m working with Series 1 modules…

Not to worry, though! I found some other resources: on StackOverflow, an arduino forum, a DIGI forum, and a question MATLAB Answers.

In the comments below official MATLAB post #1, someone mentioned that the difference with Series 1 modules was that you didn’t actually need to ‘envelope’ them in a class, etc.—you could just use ‘serial’. Based on that, the StackOverflow thread looked the most promising.  I tried out the first bit of code on that thread, and here’s a video of it working (hooray!): 181221KA_xbeeMATLAB_1

What I Understand/Have Learned

So this is the code I’m using:

 s = serial('COM13', 'BaudRate', 9600, 'Terminator', 'CR', 'StopBit', 1, 'Parity', 'None');
fopen(s);
while(1)
 while(s.BytesAvailable==0)
 end
fprintf(s, '1');
 fscanf(s)
 s.BytesAvailable
end
  • So ‘s’ and following commands setup that MATLAB is going to be reading from the COM13 XBee (I’m sending commands from one I have hooked up to COM12).
  • ‘fopen’ opens serial communication
  • the first while loop just keeps checking if the XBee I’m controlling via XTCU has sent anything
  • ‘fprint(s, ‘1’)’ prints out what the COM13 XBee has received from the COM12 one onto the MATLAB screen. Apparently this needs to take a command, so that’s what the ‘1’ is; that’s what it sends as a response to the COM12 XBee, and that’s why there are all these weird 1s in the video above.
    • I programmed this to say ‘hello there’, but then it responded with like after every character I sent from the COM12 one, so… I think it might have to do with the baud rate. (It sends a ‘hello’ after the first character after [enter] or after 19 characters if I don’t use [enter]).
  • Not actually sure what ‘fscan’ does just yet, because I took it out and couldn’t see any difference…

Edit: 5 min later…

  •  fprint is actually just meant to print something to the Serial device.
  • If you take it out altogether, fscan does the job and doesn’t send weird ‘1’s. Same rules apply as specified under ‘fprint’ above.

Leave a Reply

Your email address will not be published. Required fields are marked *