Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have five int values which I want to send wirelessly from one Xbee to another. My problem is: How do I give hexvalues (0x4E) in Serial.write() when I have an integer value (say current, voltage etc)?

Current code:

void loop(){
  int RP= 1391;
  int AP= 1581;
  int PF= 88;
  int V= 25500;
  int I= 62;
  char RPh= char(bitDividerhigh(RP));
  char RPl= char(bitDividerlow(RP));
  char APh= char(bitDividerhigh(AP));
  char APl= char(bitDividerlow(AP));
  char PFh= char(bitDividerhigh(PF));
  char PFl= char(bitDividerlow(PF));
  char Vh= char(bitDividerhigh(V));
  char Vl= char(bitDividerlow(V));
  char Ih= char(bitDividerhigh(I));
  char Il= char(bitDividerlow(I));
  Serial.write(0x7E);
  Serial.write((byte) 0x0);
  Serial.write(0x18);
  Serial.write(0x10);
  Serial.write(0x01);
  Serial.write((byte) 0x0);
  Serial.write((byte) 0x0);
  Serial.write((byte) 0x0);
  Serial.write((byte) 0x0);
  Serial.write((byte) 0x0);
  Serial.write((byte) 0x0);
  Serial.write((byte) 0x0);
  Serial.write((byte) 0x0);
  Serial.write(0xFF);
  Serial.write(0xFE);
  Serial.write((byte) 0x0);
  Serial.write((byte) 0x0);
  Serial.write(RPh);// issue here
  Serial.write(RPl);// issue here
  Serial.write(APh);// issue here
  Serial.write(APl);// issue here
  Serial.write(PFh);// issue here
  Serial.write(PFl);// issue here
  Serial.write(Vh);// issue here
  Serial.write(Vl);// issue here
  Serial.write(Ih);// issue here
  Serial.write(Il);// issue here
  long sum= 0x10 + 0xFF + 0xFF + 0xFF + 0xFE
          + RPh + RPl + APh + APl + PFh + PFl + Vh + Vl + Ih + Il;
  Serial.write( 0xFF - (sum&0xFF));  
  delay(100);
}

int bitDividerhigh(int  input){
  int b= 65280;
  int c= input&b;
  int d= c>>8;
  return d;
}

int bitDividerlow(int input){
  int e= 255;
  int f= input&e;
  return f;
}

I have indicated as comments in the code where I think the data type I gave isn't the same as what its supposed to be. How do I convert integer values into RF data that's perfectly compatible with Serial.write()?

share|improve this question

Issue resolved, I shifted to xbee.h library with a little data handling code for debugging the transmitted data to int and float.

If anybody is interested to learn xbee quickly and properly. Best site i found out there is http://www.desert-home.com/p/the-world-of-xbee.html

Tx sample code for datatype handling:

#include<XBee.h>
#include <SoftwareSerial.h>
XBee xbee = XBee();
unsigned long start = millis();
uint8_t payload[] = { 0, 0,0,0,0,0};
XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x00000000);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
int statusLed = 13;
union {
      float f;
      byte b[4];
   } stuff;
void setup() {
  pinMode(statusLed, OUTPUT);
  Serial.begin(9600);
  //xbee.begin(9600);  // start serial
  xbee.setSerial(Serial);
  flashLed(statusLed, 30, 50);
}
void loop(){
  if (millis() - start > 15000) {
  int test_int = 1232;
  payload[0] = test_int >> 8 & 0xff;      // Converting int to hex could also used highByte() lowByte()
  payload[1] = test_int & 0xff;
  // Sending a float
  stuff.f = 3.14159;
  payload[2] = stuff.b[0];                // Converting using the data type defined in "union"
  payload[3] = stuff.b[1];
  payload[4] = stuff.b[2];
  payload[5] = stuff.b[3];
  xbee.send(zbTx);
  delay(2000);
  // flash TX indicator
   flashLed(statusLed, 5, 100);
  }
  // after sending a tx request, we expect a status response
    // wait up to 5 seconds for the status response
    if (xbee.readPacket(5000)) {
        // got a response!

        // should be a znet tx status               
        if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
           xbee.getResponse().getZBTxStatusResponse(txStatus);

           // get the delivery status, the fifth byte
           if (txStatus.getDeliveryStatus() == SUCCESS) {
                // success.  time to celebrate
                flashLed(statusLed, 5, 50);
           } else {
                // the remote XBee did not receive our packet. is it powered on?
                //flashLed(errorLed, 3, 500);
               flashLed(statusLed, 3, 1000);
           }
        }      
    } else if (xbee.getResponse().isError()) {
      //nss.print("Error reading packet.  Error code: ");  
      //nss.println(xbee.getResponse().getErrorCode());
      // or flash error led
    } else {
      // local XBee did not provide a timely TX Status Response.  Radio is not configured properly or connected
      //flashLed(errorLed, 2, 50);
      flashLed(statusLed, 5, 1000);
    }

    delay(3000);
}

void flashLed(int pin, int times, int wait) {

    for (int i = 0; i < times; i++) {
      digitalWrite(pin, HIGH);
      delay(wait);
      digitalWrite(pin, LOW);

      if (i + 1 < times) {
        delay(wait);
      }
    }
}

**Rx sample code for datatype handling:**

    #include<XBee.h>
#include<SoftwareSerial.h>

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx = ZBRxResponse();
uint8_t option = 0;
uint8_t data = 0;
int statusLed = 13;
#define ssRX 2
#define ssTX 3
SoftwareSerial nss(ssRX, ssTX);
union {
      float f;
      byte b[4];
   } stuff;

void setup() {
  pinMode(statusLed, OUTPUT);
  Serial.begin(9600);
  //xbee.begin(9600);  // start serial
   nss.begin(9600);
  // now that they are started, hook the XBee into
  // Software Serial
  xbee.setSerial(nss);
  // I think this is the only line actually left over
  // from Andrew's original example
  Serial.println("starting up yo!");
  //xbee.setSerial(Serial);
  flashLed(statusLed, 30, 50);
}
// continuously reads packets, looking for RX16 or RX64
void loop() {

    xbee.readPacket();
    //Serial.println("Waiting for readability of software serial pins");
    if (xbee.getResponse().isAvailable()) {
      // got something
      Serial.println("reading the software pins ");
      Serial.println("Frame Type is ");
      flashLed(statusLed, 10, 50);
      Serial.println(xbee.getResponse().getApiId(), HEX);
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a rx packet
        // got a zb rx packet, the kind this code is looking for

        // now that you know it's a receive packet
        // fill in the values
        xbee.getResponse().getZBRxResponse(rx);
        Serial.print("Got an rx packet from: ");
        XBeeAddress64 senderLongAddress = rx.getRemoteAddress64();
        print32Bits(senderLongAddress.getMsb());
        Serial.print(" ");
        print32Bits(senderLongAddress.getLsb());

        // this is how to get the sender's
        // 16 bit address and show it
        uint16_t senderShortAddress = rx.getRemoteAddress16();
        Serial.print(" (");
        print16Bits(senderShortAddress);
        Serial.println(")");

        // The option byte is a bit field
        if (rx.getOption() & ZB_PACKET_ACKNOWLEDGED)
            // the sender got an ACK
          Serial.println("packet acknowledged");
        if (rx.getOption() & ZB_BROADCAST_PACKET)
          // This was a broadcast packet
          Serial.println("broadcast Packet");

        Serial.print("checksum is ");
        Serial.println(rx.getChecksum(), HEX);

        // this is the packet length
        Serial.print("packet length is ");
        Serial.print(rx.getPacketLength(), DEC);

        // this is the payload length, probably
        // what you actually want to use
        Serial.print(", data payload length is ");
        Serial.println(rx.getDataLength(),DEC);

        // this is the actual data you sent
        Serial.println("Received Data: ");
        for (int i = 0; i < rx.getDataLength(); i++) {
          print8Bits(rx.getData()[i]);
          Serial.print(' ');
        }        
        flashLed(statusLed, 10, 50);
        //myLCD.write(0xFE); myLCD.write(0x01);  // Clear Screen
        //myLCD.print("Getting message");
          //delay(15);  
        //myLCD.write(0x0A); // Line Feed
        // Receiving and printing an int
        Serial.println("Debugging data..");
         byte unite1 = rx.getData(0);
         byte unite2 = rx.getData(1);
         //Serial.println(word(unite1));
         //Serial.println(word(unite2));
         Serial.println(word(unite1,unite2));
         // Receiving and printing a float
         stuff.b[0] = rx.getData(2);
         stuff.b[1] = rx.getData(3);
         stuff.b[2] = rx.getData(4);
         stuff.b[3] = rx.getData(5);
         Serial.println(stuff.f);
         flashLed(statusLed, 10, 50);
        //myLCD.write(0xFE); myLCD.write(0x01);  // Clear Screen

      } else {
        // not something we were expecting
        flashLed(statusLed, 3, 1000);    
      }
    } else if (xbee.getResponse().isError()) {
      flashLed(statusLed, 3, 1000);   
     Serial.println(xbee.getResponse().isError());
    } 
}

      void flashLed(int pin, int times, int wait) {

    for (int i = 0; i < times; i++) {
      digitalWrite(pin, HIGH);
      delay(wait);
      digitalWrite(pin, LOW);

      if (i + 1 < times) {
        delay(wait);
      }
    }
}

void print32Bits(uint32_t dw){
  print16Bits(dw >> 16);
  print16Bits(dw & 0xFFFF);
}

void print16Bits(uint16_t w){
  print8Bits(w >> 8);
  print8Bits(w & 0x00FF);
}

void print8Bits(byte c){
  uint8_t nibble = (c >> 4);
  if (nibble <= 9)
    Serial.write(nibble + 0x30);
  else
    Serial.write(nibble + 0x37);

  nibble = (uint8_t) (c & 0x0F);
  if (nibble <= 9)
    Serial.write(nibble + 0x30);
  else
    Serial.write(nibble + 0x37);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.