I want to give my array the necessary format to send a Data Frame in API mode through my Xbee

Hello guys, I have a problem with my program I want to give my array the necessary format to send a Data Frame in API mode through my Xbee. So basically I take the Time&Date from a RTC DS1307 and make an HEX array.

Like this:
{0x7E, 0x00, 0x35, 0x10, 0x01, 0x00}

but I have this:

30363A32333A34352030352F30332F32303231

I need this :

{0x30, 0x36, 0x3A, … }

My code :

#include <SafeString.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SoftwareSerial.h>

char buffer [25]; // 25 and a bit
char packet2 [40];  // using SafeString will ensure this does not overflow.


void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();
  Serial.println("DS1307RTC convertir valores a HEX");
  Serial.println("-------------------");
}
void loop() {
  
  sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%04d", 6, 23, 45, 5, 3, 2021);
  Serial.println(buffer); // con este print compruebo que la fecha y hora

  cSFA(sfPacket,packet2); // or long hand, createSafeStringFromCharArray(sfPacket, packet2); // picks up packet2 size automatically
  for (size_t i = 0; i < strlen(buffer); i++) { // only process the chars we have
    if (buffer[i] < 16) {
      sfPacket += '0'; // add leading 0
    }
    sfPacket.print(buffer[i], HEX); // you can print to SafeStrings
    Serial.println(buffer[i], HEX); // con este print compruebo que he convertido los caracteres a Hex
  }
  Serial.println(packet2); // now updated with hex values
  
  //xbee.write (buffer , sizeof(buffer));  // Manda el mensaje al nodo PC or sizeof(buffer)+1 if you want to send the terminating '\0'
  //}
  delay(10000);
}

There is no such thing as a HEX array. It is an array of numbers. You can write those numbers in any notation you want
{0x7E, 0x00, 0x35, 0x10, 0x01, 0x00} is the same as {127, 0, 53, 16, 1, 0}

print() turns numbers into ascii characters, write() does not. If you truly want to send bytes to Xbee, you should not be dealing with strings (or SafeStrings - ugh)

1 Like

do you mean binary or ASCII?

do you notice the binary value of 0x30 is β€˜0’, 0x31 is β€˜1’, 0x32 is β€˜2’ …

{0x30, 0x36, 0x3A, … } is β€˜0’, β€˜6’, β€˜:’

The Hex value of an ASCII character.

I found on Youtube a way to send an API frame with Xbee, which has helped me. When I run the program, the plot is sent through the Xbee and I can receive it on my other computer through another Xbee, I can see XX.XX 00:11:46 05/01/2021 in the software.

At the moment my intention is to convert 00:11:46 01/05/2021 to hexadecimal, put it in an array and this concatenate it to the array composed of the delimiter bit, the length of the frame and the type of frame. I already know that my plot is like this: 05/01/2021, so its length will not vary.

The first bytes of the frame:
// 7E 00 28 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00
They will not vary.

But the time and date will change as time goes by.

#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3); //(Rx, TX)

byte packet[] = {0x7E, 0x00, 0x28, 0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x41, 0x4E, 0xF1, 0x24, 0xFF, 0xFE, 0x00, 0x00, 0x58, 0x58, 0x2E, 0x58, 0x58, 0x20, 0x30, 0x30, 0x3A, 0x31, 0x31, 0x3A, 0x34, 0x36, 0x20, 0x30, 0x31, 0x2F, 0x30, 0x35, 0x2F, 0x32, 0x30, 0x32, 0x31, 0x0D, 0x34}

//Message sent from Arduino to PC through Xbee.
/7E 00 28 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00 58 58 2E 58 58 20 30 30 3A 31 31 3A 34 36 20 30 31 2F 30 35 2F 32 30 32 31 0D 34

//This is the message I want to send
//XX.XX 00:11:46 01/05/2021

void setup (){
  Serial.begin(9600);
  xbee.begin(9600);

  delay (1000);

  Serial.println("La primer prueba de envio de informacion del sensor hacia la computadora :");
  }
  
void loop () {
  xbee.write (packet , sizeof(packet));

  delay(3000);
  
  }  

I try to send the data, like this {127, 0, 53, 16, 1, 0} but my program dont run the array. I think it is because those values are not Hex.

I found on Youtube a way to send an API frame with Xbee, which has helped me. When I run the program, the plot is sent through the Xbee and I can receive it on my other computer through another Xbee, I can see XX.XX 00:11:46 05/01/2021 in the software.

At the moment my intention is to convert 00:11:46 01/05/2021 to hexadecimal, put it in an array and this concatenate it to the array composed of the delimiter bit, the length of the frame and the type of frame. I already know that my plot is like this: 05/01/2021, so its length will not vary.

The first bytes of the frame:
// 7E 00 28 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00
They will not vary.

But the time and date will change as time goes by.

#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3); //(Rx, TX)

byte packet[] = {0x7E, 0x00, 0x28, 0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x41, 0x4E, 0xF1, 0x24, 0xFF, 0xFE, 0x00, 0x00, 0x58, 0x58, 0x2E, 0x58, 0x58, 0x20, 0x30, 0x30, 0x3A, 0x31, 0x31, 0x3A, 0x34, 0x36, 0x20, 0x30, 0x31, 0x2F, 0x30, 0x35, 0x2F, 0x32, 0x30, 0x32, 0x31, 0x0D, 0x34}

//Message sent from Arduino to PC through Xbee.
/7E 00 28 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00 58 58 2E 58 58 20 30 30 3A 31 31 3A 34 36 20 30 31 2F 30 35 2F 32 30 32 31 0D 34

//This is the message I want to send
//XX.XX 00:11:46 01/05/2021

void setup (){
  Serial.begin(9600);
  xbee.begin(9600);

  delay (1000);

  Serial.println("La primer prueba de envio de informacion del sensor hacia la computadora :");
  }
  
void loop () {
  xbee.write (packet , sizeof(packet));

  delay(3000);
  
  }  

this is not clear

you could send data as binary

byte packet [] = {127, 0, 53, 16, 1, 0};

    xbee.write (packet , sizeof(packet));

or you could format the binary data as an ASCII string using HEX notation

void
ascii (void)  {
    char buffer [100] = "";
    char s [10];
    for (unsigned i = 0; i < sizeof(packet); i++)  {
        sprintf (s, " 0x%02x", packet [i]);
        strcat (buffer, s);
    }

    printf ("%s\n", buffer);
}

which produces
"0x7f 0x00 0x35 0x10 0x01 0x00"

sending the data as binary transmits 6 bytes while the ascii string transmits 30 bytes

1 Like

When I use this

byte packet [] = {127, 0, 53, 16, 1, 0};

    xbee.write (packet , sizeof(packet));

I received an error and this is really frustated.

is the Xbee expecting a series of bytes or a series of characters ?

Please post an example that works with hard coded values that works

Hi Bob thanks

This is the code that Works perfectly.

#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3); //(Rx, TX)

byte packet[] = {0x7E, 0x00, 0x28, 0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x41, 0x4E, 0xF1, 0x24, 0xFF, 0xFE, 0x00, 0x00, 0x58, 0x58, 0x2E, 0x58, 0x58, 0x20, 0x30, 0x30, 0x3A, 0x31, 0x31, 0x3A, 0x34, 0x36, 0x20, 0x30, 0x31, 0x2F, 0x30, 0x35, 0x2F, 0x32, 0x30, 0x32, 0x31, 0x0D, 0x34}

//Message sent from Arduino to PC through Xbee.
/7E 00 28 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00 58 58 2E 58 58 20 30 30 3A 31 31 3A 34 36 20 30 31 2F 30 35 2F 32 30 32 31 0D 34

//This is the message I want to send
//XX.XX 00:11:46 01/05/2021

void setup (){
  Serial.begin(9600);
  xbee.begin(9600);

  delay (1000);

  Serial.println("La primer prueba de envio de informacion del sensor hacia la computadora :");
  }
  
void loop () {
  xbee.write (packet , sizeof(packet));

  delay(3000);
  
  }  

OK, so the Xbee is expecting bytes not ASCII characters

What error do you get and where does it come from when you use

byte packet [] = {127, 0, 53, 16, 1, 0};

    xbee.write (packet , sizeof(packet));

the above is a mix of binary and ascii information.
there are several nulls in the first 17 bytes, but the remainder translates to – 4X.XX 00:11:46 01/05/2021

presumably the receiver is expecting a packet of information the includes a header. it’s not clear that the packet format is

Hi Gcjr Thanks for your opinion
ok this is the Format of my Frame.

image

I have the XCTU software, there i can make frames in API mode.
So a made de Frame like this:

So I take the generate frame :
7E 00 27 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00 58 58 2E 58 58 20 30 30 3A 31 31 3A 34 36 20 30 31 2F 30 35 2F 32 30 32 31 41

And copy in my program to transmit but the program dont run when use : … 7E 00 27 10 … only when I add 0x7E, 0x00, 0x…

And ok yes, I accept that I dont pretty smart programing.

I see what happens, and I sorry becuase the program run when I use {127, 0, 53, 16, 1, 0};

So i Need to convert : 7E 00 27 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00 58 58 2E 58 58 20 30 30 3A 31 31 3A 34 36 20 30 31 2F 30 35 2F 32 30 32 31 41
I decimal values, and put in packet array to send .

there are several things in the packet that must be correct for the packet to even be accepted by the receiver – the first 4 bytes and the last byte.

the first byte must be 0x7e and the the next two bytes must be the length of the packet (presumably this include the header bytes and checksum, but not sure). i don’t know how many Frame Types there are or what they are. And the final checksum byte needs to match the preceding data.

the packet will not be accepted if these bytes are not correct – wrong number of bytes in the packet or incorrect checksum

even if the packet length remains fixed, the checksum must be recalculated for each new packet

I understand that of course, what happens is that since I know that my data has this structure (XX.XX 00:11:46 05/01/2021) 25bytes and they will not change, because they will always be those. XX.XX is a voltage value that I am going to take the ADC, but hey that is another matter. What I mean is that the data sizes will not vary, that’s why I see the frame header as a constant that will not vary, the delimiter bit, the length of the frame, the type of frame it is for transmit and the address of the node to which I am going to send the data frame.

On the other hand, if I am aware that the checksum does vary, because each time the frame is sent, the seconds are often, the minutes also therefore the checksum will vary.

I would love to know how I can do that operation to get the checksum.


image

as the note you posted describes

unsigned char
checksum (
    byte *pkt,
    int   size )
{
    unsigned char sum = 0;
    for (int i = 3; i < size-1; i++)
        sum += pkt [i];
    return 0xff - sum;
}
1 Like

Ok ok Thanks for tour response, I understand

Hi Gcjr Thanks you for take your time, aprreciate

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.