private ReadData(String port, int baud, String url) throws Exception { // XBee xbee = new XBee(); try { // replace with the com port of your receiving XBee (typically your end device) // xbee.open("/dev/tty.usbserial-A6005uRz", 9600); xbee.open(port, baud); setAPMode(2); // set options of network if (xbee.sendAtCommand(new AtCommand("JN", 0x01)).isOk()) log.info("Set join Notification"); String node_identifier = "0013A200408BC826\0"; if (xbee.sendAtCommand(new AtCommand("CH", 0x0F)).isOk()) log.info("Set CH"); int[] pan = {0x69, 0x69}; if (xbee.sendAtCommand(new AtCommand("ID", pan)).isOk()) log.info("Set PAN_ID"); if (xbee.sendAtCommand(new AtCommand("NI", ByteUtils.stringToIntArray(node_identifier))) .isOk()) log.info("Set Node Identifier: " + node_identifier); while (true) { try { // we wait here until a packet is received. XBeeResponse response = xbee.getResponse(); log.info("received response " + response.toString()); if (response.getApiId() == ApiId.ZNET_RX_RESPONSE) { // we received a packet from ZNetSenderTest.java ZNetRxResponse rx = (ZNetRxResponse) response; log.info( "Received RX packet, option is " + rx.getOption() + ", sender 64 address is " + ByteUtils.toBase16(rx.getRemoteAddress64().getAddress()) + ", remote 16-bit address is " + ByteUtils.toBase16(rx.getRemoteAddress16().getAddress()) + ", data is " + ByteUtils.toBase16(rx.getData())); String mac = ByteUtils.toBase16(rx.getRemoteAddress64().getAddress(), "").replaceAll("0x", ""); log.info(mac); // convertimos el int[] en string con los datos que necesitamos // dia,hora,minuto,segundo,bateria,temp_aire,hume_aire,temp_suelo,hume_suelo,secuencia String resultado = convertirDatos(rx.getData()); // optionally we may want to get the signal strength (RSSI) of the last hop. // keep in mind if you have routers in your network, this will be the signal of the last // hop. AtCommand at = new AtCommand("DB"); xbee.sendAsynchronous(at); XBeeResponse atResponse = xbee.getResponse(); int rssi = 0; if (atResponse.getApiId() == ApiId.AT_RESPONSE) { rssi = -((AtCommandResponse) atResponse).getValue()[0]; // remember rssi is a negative db value log.info("RSSI of last response is " + rssi); } else { // we didn't get an AT response log.info("expected RSSI, but received " + atResponse.toString()); } // Enviamos los datos recibidos al wsn PostData(url, mac, resultado + "#" + rssi); } else { log.debug("received unexpected packet " + response.toString()); } } catch (Exception e) { log.error(e); } } } finally { if (xbee.isConnected()) { xbee.close(); } } }
private ZNetSenderExample() throws XBeeException { XBee xbee = new XBee(); try { // replace with your com port and baud rate. this is the com port of my coordinator // xbee.open("COM5", 9600); // my coordinator com/baud xbee.open("COM17", 9600); // my end device // xbee.open("/dev/tty.usbserial-A6005uPi", 9600); // replace with end device's 64-bit address (SH + SL) // XBeeAddress64 addr64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x0a, 0x3e, 0x02); // my other remote XBeeAddress64 addr64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x33, 0x1D, 0xC3); // create an array of arbitrary data to send int[] payload = new int[] {0, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // first request we just send 64-bit address. we get 16-bit network address with status // response ZNetTxRequest request = new ZNetTxRequest(addr64, payload); System.out.println("zb request is " + request.getXBeePacket().getPacket()); System.out.println("sending tx " + request); while (true) { log.info( "request packet bytes (base 16) " + ByteUtils.toBase16(request.getXBeePacket().getPacket())); long start = System.currentTimeMillis(); // log.info("sending tx packet: " + request.toString()); try { ZNetTxStatusResponse response = (ZNetTxStatusResponse) xbee.sendSynchronous(request, 10000); // update frame id for next request request.setFrameId(xbee.getNextFrameId()); log.info("received response " + response); // log.debug("status response bytes:" + ByteUtils.toBase16(response.getPacketBytes())); if (response.getDeliveryStatus() == ZNetTxStatusResponse.DeliveryStatus.SUCCESS) { // the packet was successfully delivered if (response.getRemoteAddress16().equals(XBeeAddress16.ZNET_BROADCAST)) { // specify 16-bit address for faster routing?.. really only need to do this when it // changes request.setDestAddr16(response.getRemoteAddress16()); } } else { // packet failed. log error // it's easy to create this error by unplugging/powering off your remote xbee. when // doing so I get: packet failed due to error: ADDRESS_NOT_FOUND log.error("packet failed due to error: " + response.getDeliveryStatus()); } // I get the following message: Response in 75, Delivery status is SUCCESS, 16-bit address // is 0x08 0xe5, retry count is 0, discovery status is SUCCESS log.info( "Response in " + (System.currentTimeMillis() - start) + ", Delivery status is " + response.getDeliveryStatus() + ", 16-bit address is " + ByteUtils.toBase16(response.getRemoteAddress16().getAddress()) + ", retry count is " + response.getRetryCount() + ", discovery status is " + response.getDeliveryStatus()); } catch (XBeeTimeoutException e) { log.warn("request timed out"); } try { // wait a bit then send another packet Thread.sleep(1000); } catch (InterruptedException e) { } } } finally { xbee.close(); } }