Radio Router Example

This example contains a simple Insense program to route data up-stream to higher node addresses. It receives (arbitrary) data from the radio and rebroadcasts it on the radio if the packet was received from a node with a lower node address and the data type is a sensor reading. It displays acrivity on the LEDs. This component can be used to route data between two of the components shown in the Radio Sender and Radio Receiver examples when they are out of radio range. It demonstrates how high level routing functionality can be provided in just a few lines of Insense.

// Sensor reading type
type sensorReading is struct(
    byte[] addr ;
    integer cycle ;
    integer photo ;
    integer solar ;
    real temp ;
    real humid
)
 
type IRouter is interface (
    in RadioPacket radioInChan ;
    out any radioOutChan ;
    out bool greenLed ;
    out any stdoutChan
)
 
// Router component
component Router presents IRouter {
 
	thisNodeAddr = getRadioNodeAddress()
 
	constructor() {
	}
 
	behaviour {
 
		// get something from the radio
		receive packet from radioInChan
		send any("\nData received from ") on stdoutChan
		send any(packet.addr[0]) on stdoutChan
		send any(".") on stdoutChan
		send any(packet.addr[1]) on stdoutChan
 
		// project out of any
		project packet.payload as value onto
			sensorReading : { // if we have a sensor reading
			    // do up-stream routing to higher node addresses
			    if packet.addr[0] < thisNodeAddr[0] then {
    				// signal routing with green LED
	    			send true on greenLed
		    		send any(": forwarding data ") on stdoutChan
    				// Re-broadcast the reading on its way
	    			send any(value) on radioOutChan
	    			// turn green LED back off
	    			send false on greenLed
			    }
			}
			default : {
				// signal unknown packet
				send any(": was garbage") on stdoutChan
			}
		}
	}
} 
 
router = new Router()
 
connect router.greenLed to leds.greenState
connect router.radioOutChan to radioOut.broadcast
connect router.radioInChan to radioIn.received
connect router.stdoutChan to standardOut