Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <XBee.h>
- #include <SoftwareSerial.h>
- XBee xbee = XBee();
- ZBRxResponse rx = ZBRxResponse();
- // Xbee addresses
- XBeeAddress64 addr64_pwr1 = XBeeAddress64(0x0013a200, 0x40b00caa); // interior power panel sensors
- // PWR1 data container
- 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 };
- // union to convert float to byte string
- union u_tag {
- uint8_t b[4];
- float fval;
- } u;
- // Connect Arduino pin 2 to Tx and 3 to Rx of the XBee
- // I know this sounds backwards, but remember that output
- // from the Arduino is input to the Xbee
- #define ssRX 2
- #define ssTX 3
- SoftwareSerial nss(ssRX, ssTX);
- void setup() {
- Serial.begin(9600);
- // software serial port
- nss.begin(9600);
- // now that they are started, hook the XBee into Software Serial
- xbee.setSerial(nss);
- Serial.println("starting up yo!");
- }
- void loop() {
- // doing the read without a timer makes it non-blocking, so
- // you can do other stuff in loop() as well.
- xbee.readPacket();
- // so the read above will set the available up to
- // work when you check it.
- if (xbee.getResponse().isAvailable()) {
- // got something
- if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
- xbee.getResponse().getZBRxResponse(rx);
- uint32_t addrMsb = rx.getRemoteAddress64().getMsb();
- uint32_t addrLsb = rx.getRemoteAddress64().getLsb();
- if (addrMsb == addr64_pwr1.getMsb() && addrLsb == addr64_pwr1.getLsb()) {
- // ****************** sender is pwr1 ******************
- Serial.print("{\"a\":\"");
- Serial.print(addrMsb, HEX);
- Serial.print(addrLsb, HEX);
- Serial.print("\",\"len\":\"");
- Serial.print(rx.getDataLength(), DEC);
- Serial.print("\",\"offset\":\"");
- Serial.print(rx.getDataOffset(), DEC);
- Serial.print("\",\"packetlen\":\"");
- Serial.print(rx.getPacketLength(), DEC);
- if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) Serial.print("\",\"ACK\"");
- else Serial.print("\",\"not_ack\"");
- for (int i = 18; i < 18 + 40; i++)
- payload[i - 18] = rx.getData()[i];
- float realPower1 = getFloatFromData(0);
- Serial.print("\",\"rp1\":\"");
- Serial.print(realPower1);
- Serial.print(",data:\"");
- for (int i = 0; i < rx.getDataLength(); i++){
- Serial.write(' ');
- if (iscntrl(rx.getData()[i]))
- Serial.write(' ');
- else
- Serial.write(rx.getData()[i]);
- Serial.write(' ');
- }
- Serial.println("\"}");
- }
- else {
- // another RX sender shouldn't exist yet
- Serial.println("RX no match Addr64");
- }
- }
- }
- else if (xbee.getResponse().isError()) {
- // some kind of error happened, I put the stars in so
- // it could easily be found
- Serial.print("e=error code:");
- Serial.println(xbee.getResponse().getErrorCode(),DEC);
- }
- else {
- }
- }
- void addToPayload(float val, int pos) {
- u.fval = val;
- for (int i = pos; i < pos + 4; i++){
- payload[i] = u.b[i];
- }
- }
- float getFloatFromData(int pos) {
- u.fval = 0.0;
- for (int i = 0; i < 4; i++){
- u.b[i] = payload[i + pos];
- }
- return u.fval;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement