Shop OBEX P1 Docs P2 Docs Learn Events
passing xbee data to lcd — Parallax Forums

passing xbee data to lcd

solder_fumessolder_fumes Posts: 18
edited 2015-02-23 18:54 in Accessories
I don't know that my problem is as much as with the xbee as with my coding, so I'm posting it here for more exposure.

My setup is two Prop activity boards, and three xbee modules. I have an xbee on each activity board, with the third connected to my PC as a traffic monitor.

On board/xbee combo is set up to transmit a floating point number. This seems to work fine, as I can see the data with the traffic monitor xbee so I know it's being transmitted.

My problem is with the receive side. I can't get the received data to print either on the LCD or the terminal. If I substitute a fixed value for the variable called "data", then all works...I see the data on the LCD and on the terminal. However when I try to capture the data from the xbee, I don't get any data to the LCD or terminal.

It doesn't really need to be float, it can be decimal too. But I can't get either to work.

Any help greatly appreciated!

Thanks,

John

Here is the code on the transmitter side...
/*
  transmit voltage over xbee
*/
#include "adcDCpropab.h"                            // Include adcDCpropAB
#include "simpletools.h"                            // Include simpletools
#include "fdserial.h"

fdserial *xbee;
 
int main()                                          // Main function
{
  xbee = fdserial_open (7, 6, 0, 9600);
  
  
  adc_init(21, 20, 19, 18);                         // CS=21, SCL=20, DO=19, DI=18
  float volts;                                      // Voltage variable
  
 
  while(1)                                          // Loop repeats indefinitely
  {
    volts = adc_volts(0) * 2;                       // Check A/D 0 and double value
               
    putChar(HOME);                                   // Cursor -> top-left "home"
    {
    print("Volts = %f\n", volts, CLREOL);            // Display voltage on terminal (if connected)
    }     
    {
      dprint (xbee, "%f\n",  volts, CLREOL);        // send votage variable to the xbee
    }
             
  pause(1000);                                      // wait 1 second                                
 }     
}


And here is the receive code...
/*
receive voltage over xbee and display on lcd and terminal
*/

#include "simpletools.h"                       // Include required libraries
#include "fdserial.h"
#include "serial.h"

fdserial *xbee;
serial *lcd;
serial *term;

int main()                                      // Main function
{

const int ON = 22;
const int CLR = 12; 

xbee = fdserial_open(7, 6, 0, 9600);           // initialize devices
lcd = serial_open(13, 13, 0, 9600);
term = serial_open(31, 30, 0, 115200);

float data;                                    // declare data variable as type float

writeChar(lcd, ON);                            // prepare LCD to receive data
writeChar(lcd, CLR);
pause(1000);


while(1)                                       // loop....this is line 36
{
data = fdserial_rxChar(xbee);                  // receive xbee data
if(data != -1);
}                                              // this is line 40
  
{
writeFloatPrecision(lcd, data, 5, 3);          // send received data to devices as 5 characters total, precision of 3
writeFloatPrecision(term, data, 5, 3); 
}

Comments

  • ElectrodudeElectrodude Posts: 1,660
    edited 2015-01-16 16:10
    I'm not sure you understand brackets properly. This might just be the forum being stupid, but code is a lot more readable and mainainable (= fixable) if you have consistent indentation.

    Transmitter:
      while(1)                                          // Loop repeats indefinitely
      {
        volts = adc_volts(0) * 2;                       // Check A/D 0 and double value
                   
        putChar(HOME);                                   // Cursor -> top-left "home"
        { // why is this here?
        print("Volts = %f\n", volts, CLREOL);            // Display voltage on terminal (if connected)
        }     
        { // why is this here?
          dprint (xbee, "%f\n",  volts, CLREOL);        // send votage variable to the xbee
        }
                 
      pause(1000);                                      // wait 1 second                                
     }     
    

    Receiver:
    while(1)                                       // loop....this is line 36
    {
    data = fdserial_rxChar(xbee);                  // receive xbee data
    if(data != -1); // useless if statement
    }                                              // this is line 40
      
    { // never gets called - outside of loop
    writeFloatPrecision(lcd, data, 5, 3);          // send received data to devices as 5 characters total, precision of 3
    writeFloatPrecision(term, data, 5, 3); 
    }
    
    

    For the receiver, did you mean:
    while(1)                                       // loop....this is line 36
    {
         data = fdserial_rxChar(xbee);                  // receive xbee data
        if(data != -1)  // no semicolon
        {
            writeFloatPrecision(lcd, data, 5, 3);          // send received data to devices as 5 characters total, precision of 3
            writeFloatPrecision(term, data, 5, 3); 
        }
    }
    

    Note that
    if (whatever);
    
    is useless; since whitespace doesn't matter in C, it's the same as
    if (whatever)
        ;
    
    which means "if whatever then don't do anything, and then go on" (not else).
  • SCASSCAS Posts: 3
    edited 2015-02-23 18:54
    When you send a float over a uart, you must break it up from a single 32 bit float into 4 - 8 bit chars or uint8's you cant just shift these 4 bytes together to make a float. Because it is 4 chars. Not one float. You must assemble these 4 bytes into a single 32 bit uint then cast it into a float. Then it will display.
  • i also have a mini project that i am trying to do that involves displaying the received data from the xbee onto a 16*2 lcd..both the lcd and xbee are connected to an arduino uno board. below are the connections i made..i want to broadcast the state of the traffic light on a remote lcd display
    5746 x 2446 - 311K

  • Sending serial data as in:
    dprint (xbee, "%f\n",  volts, CLREOL);
    
    Sends the float variable, 'volts' as a text string (as an example "4.735"). You need a way to convert the text string back into a float. In the following example (not tested as I'm not running an XBee right now), dscan is used to turn the sent string back into a float type.
    /*
    receive voltage over xbee and display on lcd and terminal
    */
    #include "simpletext.h"
    #include "simpletools.h"                 // Include required libraries
    #include "serial.h"
    #include "fdserial.h"
    
    fdserial *xbee;
    serial *lcd;
    serial *term;
    
    int main()
    {
    const int ON = 22;
    const int CLR = 12;
    float data;                               // declare data variable as type float
      
      xbee = fdserial_open(7, 6, 0, 9600);    // initialize devices
      lcd = serial_open(13, 13, 0, 9600);
      term = serial_open(31, 30, 0, 115200);                      
    
      writeChar(lcd, ON);                     // prepare LCD to receive data
      writeChar(lcd, CLR);
      pause(1000);
    
      int count;
        
      while(1)
      {
        count = fdserial_rxCount(xbee);            // check to see if there are characters in the serial buffer
    
        if(count > 0)                              // only convert if there is some data to convert
        {  
          count =  dscan(xbee, "%f\n", data);      // reads in a text string, converting it to the specified format
          writeFloatPrecision(lcd, data, 5, 3);    // send received data to devices as 5 characters total, precision of 3
          writeFloatPrecision(term, data, 5, 3); 
          fdserial_rxFlush(xbee);                  // clear out the serial buffer
        }
      }     
    }
    

    This code does not correctly assess the data in the incoming serial buffer, just checks to see if there's anything at all in the buffer. For correctness, the code should do a better job of qualifying the input before attempting to write it out...

    There are several other ways to "skin this cat", but dscan seems simple. The C language's standard libraries offer conversion functions that could also be used.


    dgately
Sign In or Register to comment.