@Override
public void initialize() {
// TODO: Initialize the thing. If done set status to ONLINE to indicate proper working.
// Long running initialization should be done asynchronously in background.
updateStatus(ThingStatus.ONLINE);
String PORT = "COM1";
int BAUD_RATE = 9600;
XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE);
}
public class MyDataReceiveListener implements IDataReceiveListener {
/*
* (non-Javadoc)
* @see com.digi.xbee.api.listeners.IDataReceiveListener#dataReceived(com.digi.xbee.api.models.XBeeMessage)
*/
@Override
public void dataReceived(XBeeMessage xbeeMessage) {
System.out.format("From %s >> %s | %s%n", xbeeMessage.getDevice().get64BitAddress(),
HexUtils.prettyHexString(HexUtils.byteArrayToHexString(xbeeMessage.getData())),
new String(xbeeMessage.getData()));
}
}
Now, where i can put the code to receive the command from XBee device? In the initialize function?
Every things created should have almost two param: The address of XBee from where receive command and the Pin number (the value to assign to the thing). Address and pin number/value are in the data packet received. When data is received, I have to assign the value to the correct item.
Can you help me in the development?
Thanks!
Roberto
public void initialize() {
// TODO: Initialize the thing. If done set status to ONLINE to indicate proper working.
// Long running initialization should be done asynchronously in background.
updateStatus(ThingStatus.ONLINE);
String PORT = "/dev/ttyUSB0";
int BAUD_RATE = 9600;
XBeeDevice localDevice = new XBeeDevice(PORT, BAUD_RATE);
Timer readDIOTimer = new Timer();
String REMOTE_NODE_IDENTIFIER = "XBEE_B";
try {
localDevice.open();
// Obtain the remote XBee device from the XBee network.
XBeeNetwork xbeeNetwork = localDevice.getNetwork();
RemoteXBeeDevice remoteDevice = xbeeNetwork.discoverDevice(REMOTE_NODE_IDENTIFIER);
if (remoteDevice == null) {
logger.error(
"Couldn't find the remote XBee device with '" + REMOTE_NODE_IDENTIFIER + "' Node Identifier.");
localDevice.close();
}
remoteDevice.setIOConfiguration(IOLINE_IN, IOMode.DIGITAL_IN);
localDevice.setIOConfiguration(IOLINE_OUT, IOMode.DIGITAL_OUT_LOW);
readDIOTimer.schedule(new UpdateOutputTask(localDevice, remoteDevice), 0, 250);
} catch (XBeeException e) {
e.printStackTrace();
localDevice.close();
}
// Note: When initialization can NOT be done set the status with more details for further
// analysis. See also class ThingStatusDetail for all available status details.
// Add a description to give user information to understand why thing does not work
// as expected. E.g.
// updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
// "Can not access device as username and/or password are invalid");
}
private static class UpdateOutputTask extends TimerTask {
private XBeeDevice localDevice;
private RemoteXBeeDevice remoteDevice;
public UpdateOutputTask(XBeeDevice local, RemoteXBeeDevice remote) {
this.localDevice = local;
this.remoteDevice = remote;
}
@Override
public void run() {
try {
// Read the digital value from the remote input line.
IOValue value = remoteDevice.getDIOValue(IOLINE_IN);
logger.error(IOLINE_IN + ": " + value);
// Set the previous value to the local output line.
localDevice.setDIOValue(IOLINE_OUT, value);
} catch (XBeeException e) {
e.printStackTrace();
}
}
}
In the inizialise() function, I can get the value from a specific pin of a specific device, identified by name "XBEE_B"
Now:
How can I define a thing (string and switch type), that can accept two param (Name and pin number)?
How can I update his value when i receive a data from device?