/////////////////// MessageListener////////////////// @Override public void dataMessage(ZNetRxResponse packet) { if (packet.getData()[0] == 0xFB) { circleIndicatorOn(); } }
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(); } } }