Sending Data via paired XBee PRO S1 prints wrong data

Hello there,

We got 2 robots from university with paired Xbee Pro S1 modules (can only communicate with the paired counterpart). They are attached to a Nibo2 robot and I am trying to solve a maze with mine.
So one of my Nibos drives through a maze and writes down walls and paths (1 for wall, 0 for path).
I am saving them in an twodimensional array and want to send them to my other robot.

I am able to send simple data (like a 1 or a 0) but somehow everything gets ridiculous messed up when trying to loop through the array to send the data. I am getting numbers like -26669 which simply is not possible at all.
I hope someone is able to tell me what I am doing wrong.

My code to receive Data:

// libraries for standard functions of the Nibo2
#include <nibo/niboconfig.h>
#include <nibo/iodefs.h>
#include <nibo/bot.h>

// libary for the use of the display
#include <nibo/display.h>

// libary for the use of the graphical functions
#include <nibo/gfx.h>

// libary for standard input and output operations of the Nibo2
#include <stdio.h>

// includes interrupts
#include <stdint.h>

// includes header file for the use of the UART1-Port
//#include "uart0.h"

// include delay operations
#include <nibo/delay.h>


// includes SPI communication library
#include <nibo/spi.h>

// includes microcontroller hardware interrupts
#include <avr/interrupt.h>

// includes xBee Communication
// be aware to include it locally with ""
#include "xBee.h"


/**
 * Main function of the helloXBee.c file.
 * Receives and sends chars using the UART1-Port of the Nibo.
 */
int main(){


    // initialize the robot
    bot_init();

    // initialize spi-port
    spi_init();

    // initialize the display and the graphical operations
    display_init();
    gfx_init();

    // buffer for the chars, is needed to print the chars on the display
    char text[20] = "";

    // declare and initialize a variable for storing received characters
    uint8_t x=52;

    // prints text on the Display
    sprintf(text, "Hallo XBEE");
    gfx_move(0, 0);
    gfx_print_text(text);


    // initialize xBee Module
    xBee_init();
    int maze[10][10];
    int row = 0, col = 0;

    // Operation loop
    while (row != 9 && col !=9) {

        // if the receive buffer is not empty ->
        if (xBee_receivedData())
        {
            // -> get next received data byte of the buffer
            x = xBee_readByte();

            if(x == 1){
             if(row == 9){
             row = 0;
             col++;
             maze[row][col] = 1;
             x = 52;
             } else {
             maze[row][col] = 1;
             x = 52;
             row++;
             }
            }if(x == 0) {
             row++;
             maze[row][col] = 0;
             x = 52;
            }
        }


        // Delay for the operation loop
        _delay_ms(5);

    }
    gfx_move(0, 40);
    if(maze[9][9] == 1){
     gfx_print_text("Stelle 9 9 ist 1");
    } else {
     x = maze [9][9];
     sprintf(text, "9 9 ist: %c", (char)x);
     gfx_move(0, 15);
     gfx_print_text(text);
    }

    return 0;
}

My Code to send Data:

// libraries for standard functions of the Nibo2
#include <nibo/niboconfig.h>
#include <nibo/iodefs.h>
#include <nibo/bot.h>

// libary for the use of the display
#include <nibo/display.h>

// libary for the use of the graphical functions
#include <nibo/gfx.h>

// libary for standard input and output operations of the Nibo2
#include <stdio.h>

// includes interrupts
#include <stdint.h>

// includes header file for the use of the UART1-Port
//#include "uart0.h"

// include delay operations
#include <nibo/delay.h>

// includes SPI communication library
#include <nibo/spi.h>

// includes microcontroller hardware interrupts
#include <avr/interrupt.h>

// includes xBee Communication
// be aware to include it locally with ""
#include "xBee.h"

/**
 * Main function of the helloXBee.c file.
 * Receives and sends chars using the UART1-Port of the Nibo.
 */
int main() {

 // initialize the robot
 bot_init();

 // initialize spi-port
 spi_init();

 // initialize the display and the graphical operations
 display_init();
 gfx_init();

 // buffer for the chars, is needed to print the chars on the display
 char text[20] = "";

 // declare and initialize a variable for storing received characters
 uint8_t x;

 int maze[10][10] = {{1,0,0,1,1,1,1,1,1,1},
 {1,0,0,1,0,0,1,0,0,1},
 {1,0,0,1,0,0,1,0,0,1},
 {1,0,0,1,0,0,1,0,0,1},
 {1,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,1},
 {1,1,1,1,1,1,1,0,0,1},
 {1,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,1},
 {1,1,1,1,1,1,1,0,0,1}
 };

 // initialize xBee Module
 xBee_init();
 int row = 0, col = 0;
 // Operation loop
 while (1) {
 if (xBee_readyToSend()) {
 if(col == 9 && row == 8){
 gfx_print_text("Habe gesendet!");
 }

 x = maze[row][col];
 xBee_sendByte(x);
 row++;
 if(row == 9){
 row = 0;
 col++;
 }
 }

 // Delay for the operation loop
 _delay_ms(500);

 }

 gfx_move(0, 30);
 gfx_print_text("Fertig");

 return 0;
}

xBee.c:

#include <avr/io.h>
#include "xBee.h"
//#include <nibo/uart0.h>
//#include <nibo/uart0.h>


/**
 * Function which initialize the UART0 Port of the Nibo and sets the baudrate of the port
 * Default baude rate of the modules is 9600
 */
void initUART0(){
    uart0_set_baudrate(9600);
    uart0_enable();
}

/**
 * Initializes and enables the xBee Module
 */
void xBee_init() {

    //LED ausschalten
    DDRE &= ~(1 << 0);

    initUART0();

}

/**
 * Transmits a single byte
 */
void xBee_sendByte(uint8_t data) {
    // -> puts the char into the buffer to send the char
    uart0_putchar(data);
}

/**
 * Checks if xBee is ready to transmit data
 */
uint8_t xBee_readyToSend() {
    return !uart0_txfull();
}

/**
 * Checks if xBee received something
 */
uint8_t xBee_receivedData() {
    return !uart0_rxempty();
}

/**
 * Reads the next byte of received data
 */
uint8_t xBee_readByte() {
    // -> get char out of the buffer
    return uart0_getchar();
}

xBee.h:

#ifndef NIBO_xBee_H_
#define NIBO_xBee_H_

#ifndef NIBO_USE_UART0
#define NIBO_USE_UART0
#endif

// The XBee module communicates over UART
#include <nibo/uart0.h>

#include <stdint.h>


/**
 * Initializes and enables the xBee Module
 */
void xBee_init();

/**
 * Transmits a single byte
 */
void xBee_sendByte(uint8_t data);

/**
 * Checks if xBee is ready to transmit data
 */
uint8_t xBee_readyToSend();

/**
 * Checks if xBee received something
 */
uint8_t xBee_receivedData();

/**
 * Reads the next byte of received data
 */
uint8_t xBee_readByte();


#endif // NIBO_xBee_H

You need to look into creating or using a protocol, because your Xbee library works as a simple serial pipeline. You have no way of syncronising your data right now and you are using a wireless product which are very likely to loose data.

GPS modules for instance use NMEA 0183 standard to send serial data to microcontrollers.

The messages look like this

$GPAAM,A,A,0.10,N,WPTNME*32

As you can see there is a Start character "$" and all values are separated by ",". At the beginning are a few letters describing the message protocol that will be used. And you can have a checksum at the end.

There is a library called TinyGPS which is a parsing library for the these NMEA GPS messages. You could create a similar library for your protocol.

Note: Please use code tags around your code to make the post more readable. You can go back and modify your post. :slight_smile:

Read before you post a programming question

Klaus_K:
You need to look into creating or using a protocol, because your Xbee library works as a simple serial pipeline. You have no way of syncronising your data right now and you are using a wireless product which are very likely to loose data.

Diddo

To fix this, you can use a proven serial transfer library to automatically packetize and parse serial data (even over wireless like XBees). The library can also be downloaded and installed using the Arduino IDE's Libraries Manager (search "SerialTransfer").

Example code:

TX Arduino:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

RX Arduino:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

Thanks for the information. Sadly I did not know how to put code in code brackets. I did not find the symbol since it seems like I am half blind :D.

The Class you mentioned can't be used by me because it sadly is for c++ (has a cpp file in) and i hace to write in C. I hope I will find a C class for that. Maybe even to send an twodimensional array. That would be best.

When i found something I will add it for people having the same problem.

#Edit:
Ok I did not find anything helpful. The only Thing I found is that XBee is not able to send like all at once by itself.
If someone knows something that could help me I would be thankful.