Working with a Network - GideonLeGrange/panstamp-java GitHub Wiki

The Network object represents a panStamp wireless network. To obtain the Network representation for a network, we need to open the serial port and talk to the modem sketch running on the locally connected panStamp. This is usually a panStick USB device with a panStamp plugged in.

Open a network

Here we open the panStick connected to the serial port with a speed of 38400 bps:

Network nw = Network.createSerial("/dev/ttyUSB0", 38400);
// Do things with the network 
// close when done 
nw.close();

In the above example the serial port used is /dev/ttyUSB0 but on Windows it will be something like COM3.

Read and set network information

The network settings associated with the panStamp network can be read and changed by using get and set methods on the Network object.

This example shows how to read network settings, once again assuming our network instance is called nw:

System.out.println("Network settings:");
System.out.printf("Frequency channel: %d\n", nw.getChannel());
System.out.printf("Network ID: %04x\n", nw.getNetworkId());
System.out.printf("Device address: %d\n", nw.getDeviceAddress());

These settings can also be changed. Here is an example of changing the network ID for the panStamp network:

nw.setNetworkId(0x1234);

Other settings are manipulated in a similar fashion, and reading the API JavaDoc should be sufficient to understand those.

Find devices in the network

For a panStamp network to be useful, there must be some panStamp wireless nodes connected to the network. The representations for these nodes can be used by working with PanStamp object instances.

We can find out what panStamp devices are currently known to the network. Assuming we have a connected panStamp network nw, we can get the list of devices and print the address for each:

List<PanStamp> devices = nw.getDevices();
for (PanStamp device : devices) {
   System.out.printf("panStamp with address %d is on the network\n", device.getAddress(), device.getName());
}

If we want to know if a single device is on the network, we can query the gateway with the address of the device:

boolean hasIt = nw.hasDevice(5);
System.out.printf("Do we have device #5? %s\n", (hasIt ? "yes" : "no"));

If we know that a device exists on the network, we can retrieve the PanStamp instance for it by it's address:

PanStamp dev = nw.getDevice(5);

Handling network events

It is possible to attach a listener interface to a Network to receive notification of network events. The network will call it's event handlers for in the following cases:

  • A new panStamp device is detected in the network
  • A panStamp device was removed from the network

The following example shows how to add and implement the NetworkListener interface to receive network updates:

nw.addListener(new NetworkListener()) {

	@Override
	public void deviceDetected(Network gw, PanStamp dev) {
	    System.out.printf("Device with address %d added to network", dev.getAddress());
	}

	@Override
	public void deviceRemoved(Network gw, PanStamp dev) {
	    System.out.printf("Device with address %d removed to network", dev.getAddress());
	}

});

The deviceDetected() method is called when a new device is detected (usually when a previously unknown panStamp device sends a message to the network). The deviceRemoved() method is called when a device is removed by the application.

Obviously real application logic will be more interesting and complex.

Convenience class

Because it is quite possible that developers may only be interested in implementing one of the available interface methods on NetworkListener, we provide a simple convenience class implementation called AbstractNetworkListener that can be extended. The user can then override the method they're interested in implementing. Simply subclass AbstractNetworkListener and override for example deviceDetected():

   nw.addListener(new AbstractNetworkListener() {

        @Override
        public void deviceDetected(Network gw, PanStamp dev) {
            System.out.printf("Device with address %d added to network", dev.getAddress());
        }
        
    });
⚠️ **GitHub.com Fallback** ⚠️