Advertisement
rktect

ZigBee Tx

Jan 9th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <EmonLib.h>
  3. #include <XBee.h>
  4.  
  5. EnergyMonitor emon1;
  6. EnergyMonitor emon2;
  7. LiquidCrystal lcd(11, 10, 9, 8, 7, 6);
  8. XBee xbee = XBee();
  9.  
  10. // we are going to send 10 floats of 4 bytes each
  11. // realPower1, apparentPower1, Vrms1, Irms1, powerFactor1, realPower2, apparentPower2, Vrms2, Irms2, powerFactor2
  12. 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};
  13.  
  14. // union to convert float to byte string
  15. union u_tag {
  16.     uint8_t b[4];
  17.     float fval;
  18. } u;
  19.  
  20. // SH + SL Address of receiving XBee
  21. XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x40BADA55);
  22. ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
  23. ZBTxStatusResponse txStatus = ZBTxStatusResponse();
  24.  
  25. void setup() {
  26.   Serial.begin(9600);
  27.   xbee.setSerial(Serial);
  28.   lcd.begin(16, 2);
  29.   lcd.print("Power Monitor");
  30.  
  31.   pinMode(A0, OUTPUT);
  32.   analogWrite(A0, 125);
  33.   pinMode(A1, INPUT);
  34.   pinMode(A2, INPUT);
  35.   pinMode(A3, INPUT);
  36.  
  37.   emon1.current(A1, 60.6);          // Current: input pin, calibration.
  38.   emon1.voltage(A2, 130.0, 1.7);    // Voltage: input pin, calibration, phase_shift
  39.  
  40.   emon2.current(A3, 60.6);          // Current: input pin, calibration.
  41.   emon2.voltage(A2, 130.0, 1.7);    // Voltage: input pin, calibration, phase_shift
  42.  
  43.   delay(100);
  44. }
  45.  
  46. void loop() {
  47.     digitalWrite(LED_BUILTIN, HIGH);
  48.     emon1.calcVI(20, 2000);                         // Calculate all. No.of wavelengths, time-out
  49.     //emon1.serialprint();
  50.  
  51.     emon2.calcVI(20, 2000);                         // Calculate all. No.of wavelengths, time-out
  52.     //emon2.serialprint();
  53.  
  54.     double realPower = emon1.realPower + emon2.realPower;
  55.     double apparentPower = emon1.apparentPower + emon2.apparentPower;
  56.     double powerFactor = (emon1.powerFactor + emon2.powerFactor) / 2;
  57.     double supplyVoltage = (emon1.Vrms + emon2.Vrms) / 2;
  58.     double irms = emon1.Irms + emon2.Irms;
  59.     digitalWrite(LED_BUILTIN, LOW);
  60.  
  61.     // display on LCD
  62.     String msg1 = String(realPower) + "W " + String(supplyVoltage) + "V";
  63.     lcd.clear();
  64.     lcd.setCursor(0, 0);
  65.     lcd.print(msg1);
  66.  
  67.     //String msg2 = String(irms) + "A " + String(powerFactor) + "PF";
  68.     //lcd.setCursor(0, 1);
  69.     //lcd.print(msg2);
  70.  
  71.     // prepare payload to send
  72.     addToPayload(emon1.realPower, 0);
  73.     addToPayload(emon1.apparentPower, 4);
  74.     addToPayload(emon1.Vrms, 8);
  75.     addToPayload(emon1.Irms, 12);
  76.     addToPayload(emon1.powerFactor, 16);
  77.  
  78.     addToPayload(emon2.realPower, 20);
  79.     addToPayload(emon2.apparentPower, 24);
  80.     addToPayload(emon2.Vrms, 28);
  81.     addToPayload(emon2.Irms, 32);
  82.     addToPayload(emon2.powerFactor, 36);
  83.  
  84.     // transmit with xbee
  85.     xbee.send(zbTx);
  86.  
  87.     String msg2 = String(getFloatFromData(32)) + " " + String(sizeof(payload));
  88.     lcd.setCursor(0, 1);
  89.     lcd.print(msg2);
  90.  
  91.     // following is to check the return packet that should be sent
  92.     // after sending a tx request, we expect a status response
  93.     // wait up to half second for the status response
  94.     if (xbee.readPacket(500)) {
  95.         // got a response!
  96.  
  97.         // should be a znet tx status              
  98.         if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
  99.             xbee.getResponse().getZBTxStatusResponse(txStatus);
  100.  
  101.             // get the delivery status, the fifth byte
  102.             if (txStatus.getDeliveryStatus() == SUCCESS) {
  103.                 // success.  time to celebrate
  104.  
  105.             }
  106.             else {
  107.                 // the remote XBee did not receive our packet. is it powered on?
  108.  
  109.             }
  110.         }
  111.     }
  112.     else if (xbee.getResponse().isError()) {
  113.         //nss.print("Error reading packet.  Error code: ");  
  114.         //nss.println(xbee.getResponse().getErrorCode());
  115.  
  116.     }
  117.     else {
  118.         // local XBee did not provide a timely TX Status Response -- should not happen
  119.  
  120.     }
  121.  
  122. }
  123.  
  124. float checkFloat(float f) {
  125.     if (!isnan(f) && !isinf(f) && isfinite(f)) return f;
  126.     else return 0.0;
  127. }
  128.  
  129. void addToPayload(float val, int pos) {
  130.     u.fval = checkFloat(val);
  131.     for (int i = pos; i < pos + 4; i++){
  132.         payload[i] = u.b[i - pos];
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement