rktect

ZigBee Rx

Jan 9th, 2015
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <XBee.h>
  2. #include <SoftwareSerial.h>
  3.  
  4. XBee xbee = XBee();
  5. ZBRxResponse rx = ZBRxResponse();
  6.  
  7. // Xbee addresses
  8. XBeeAddress64 addr64_pwr1 = XBeeAddress64(0x0013a200, 0x40b00caa);  // interior power panel sensors
  9.  
  10. // PWR1 data container
  11. uint8_t payload[40] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  12.  
  13. // union to convert float to byte string
  14. union u_tag {
  15.     uint8_t b[4];
  16.     float fval;
  17. } u;
  18.  
  19. // Connect Arduino pin 2 to Tx and 3 to Rx of the XBee
  20. // I know this sounds backwards, but remember that output
  21. // from the Arduino is input to the Xbee
  22. #define ssRX 2
  23. #define ssTX 3
  24. SoftwareSerial nss(ssRX, ssTX);
  25.  
  26. void setup() {
  27.   Serial.begin(9600);
  28.   // software serial port
  29.   nss.begin(9600);
  30.   // now that they are started, hook the XBee into Software Serial
  31.   xbee.setSerial(nss);
  32.  
  33.   Serial.println("starting up yo!");
  34. }
  35.  
  36. void loop() {
  37.     // doing the read without a timer makes it non-blocking, so
  38.     // you can do other stuff in loop() as well.
  39.     xbee.readPacket();
  40.     // so the read above will set the available up to
  41.     // work when you check it.
  42.     if (xbee.getResponse().isAvailable()) {
  43.         // got something
  44.  
  45.         if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
  46.             xbee.getResponse().getZBRxResponse(rx);
  47.  
  48.             uint32_t addrMsb = rx.getRemoteAddress64().getMsb();
  49.             uint32_t addrLsb = rx.getRemoteAddress64().getLsb();
  50.  
  51.             if (addrMsb == addr64_pwr1.getMsb() && addrLsb == addr64_pwr1.getLsb()) {
  52.                 // ****************** sender is pwr1 ******************
  53.                 Serial.print("{\"a\":\"");
  54.                 Serial.print(addrMsb, HEX);
  55.                 Serial.print(addrLsb, HEX);
  56.  
  57.                 Serial.print("\",\"len\":\"");
  58.                 Serial.print(rx.getDataLength(), DEC);
  59.  
  60.                 Serial.print("\",\"offset\":\"");
  61.                 Serial.print(rx.getDataOffset(), DEC);
  62.  
  63.                 Serial.print("\",\"packetlen\":\"");
  64.                 Serial.print(rx.getPacketLength(), DEC);
  65.  
  66.                 if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) Serial.print("\",\"ACK\"");
  67.                 else Serial.print("\",\"not_ack\"");
  68.  
  69.                 for (int i = 18; i < 18 + 40; i++)
  70.                     payload[i - 18] = rx.getData()[i];
  71.                
  72.                 float realPower1 = getFloatFromData(0);
  73.                 Serial.print("\",\"rp1\":\"");
  74.                 Serial.print(realPower1);
  75.  
  76.                 Serial.print(",data:\"");
  77.                 for (int i = 0; i < rx.getDataLength(); i++){
  78.                     Serial.write(' ');
  79.                     if (iscntrl(rx.getData()[i]))
  80.                         Serial.write(' ');
  81.                     else
  82.                         Serial.write(rx.getData()[i]);
  83.                     Serial.write(' ');
  84.                 }
  85.  
  86.                 Serial.println("\"}");
  87.             }
  88.             else {
  89.                 // another RX sender shouldn't exist yet
  90.                 Serial.println("RX no match Addr64");
  91.             }
  92.  
  93.         }
  94.        
  95.     }
  96.     else if (xbee.getResponse().isError()) {
  97.       // some kind of error happened, I put the stars in so
  98.       // it could easily be found
  99.       Serial.print("e=error code:");
  100.       Serial.println(xbee.getResponse().getErrorCode(),DEC);
  101.     }
  102.     else {
  103.  
  104.     }
  105. }
  106.  
  107. void addToPayload(float val, int pos) {
  108.     u.fval = val;
  109.     for (int i = pos; i < pos + 4; i++){
  110.         payload[i] = u.b[i];
  111.     }
  112. }
  113.  
  114. float getFloatFromData(int pos) {
  115.     u.fval = 0.0;
  116.     for (int i = 0; i < 4; i++){
  117.         u.b[i] = payload[i + pos];
  118.     }
  119.     return u.fval;
  120. }
RAW Paste Data