Sensor Reader Example

A simple Insense program requesting sensor readings and printing out results on standard output and using the LEDs to indicate increases in temperature (red), humidity (green) and light (blue). The SensorReader component gets readings from the sensors, puts them into a struct and sends the struct to the PrintOutput and LedOutput components for printing and LED display respectively. Apart from the three components defined here, the Insense Runtime also defines components for each of the following:

  • LEDs
  • visible light (photo) sensor
  • total solar radiation light (solar) sensor
  • temperature sensor
  • humidity sensor
  • button sensor
  • as well as a standard output channel of type in any

The button sensor is not used in the program.

 // Sensor reading type
type sensorReading is struct( 
    integer photo ;
    integer solar ;
    real temp ;
    real humid
)
 
 
// Interface for Sensor Reader
type ISensorReader is interface (
    out bool photoRequestChan ; in integer photoValueChan ; 
    out bool solarRequestChan ; in integer solarValueChan ; 
    out bool tempRequestChan ; in real tempValueChan ; 
    out bool humidityRequestChan ; in real humidityValueChan ;
    out sensorReading printerChan ; out sensorReading ledsChan
)
 
// Sensor reader component
component SensorReader presents ISensorReader {
 
	constructor() {	
	}
 
	behaviour {
		// get photo sensor reading
		send true on photoRequestChan
		receive photoValue from photoValueChan 
 
		// get solar sensor reading
		send true on solarRequestChan
		receive solarValue from solarValueChan 
 
		// get temp reading
		send true on tempRequestChan
		receive tempValue from tempValueChan
 
		// get humidity reading 
		send true on humidityRequestChan
		receive humidityValue from humidityValueChan
 
		// put readings into struct
		reading = new sensorReading(photoValue, solarValue, tempValue, humidityValue)
 
		send reading on ledsChan
		send reading on printerChan
	}
} 
 
 
// Interface for standard output printer
type IPrintOutput is interface ( 
    in sensorReading input ; 
    out any output 
)
 
// Standard output printer component
component PrintOutput presents IPrintOutput {
 
	cycle = 0
 
	constructor() {	
	}
 
	behaviour {
		// get reading struct
		receive reading from input
 
		// send text+readings in struct on stdout
		send any("Cycle ") on output // here
		send any(cycle) on output
 
		send any(":\tphoto = ") on output
		send any(reading.photo) on output
		send any("\tsolar = ") on output
		send any(reading.solar) on output
		send any("\ttemp = ") on output
		send any(reading.temp) on output
		send any("\thumid = ") on output
		send any(reading.humid) on output
		send any("\n") on output
 
		// increment cycle counter
		cycle := cycle + 1
	}
} 
 
 
 
// Interface for LED output component
type ILedOutput is interface 
    in sensorReading input ; out ledState setLeds ;
    out bool redLed ; out bool greenLed ; out bool blueLed
)
 
// LED output component
component LedOutput presents ILedOutput {
 
	avgTemp = 0.0
	avgSolar = 0
	avgHumid = 0.0
 
	constructor() {	
	}
 
	behaviour {
		// turn of all LEDs using led setState channel
		send RedOffGreenOffBlueOff on setLeds
 
		// get sensor readings
		receive reading from input 
 
		// represent readings via LEDs, R/G/B for high Temp/Humidity/Light 
		send ((reading.solar - avgSolar) > 20) on blueLed
		send ((reading.humid - avgHumid ) > 2.0) on greenLed
		send ((reading.temp - avgTemp) > 0.2) on redLed
 
		// adjust averages
		if reading.temp > avgTemp then { avgTemp := avgTemp + 0.1 }
		else { avgTemp := avgTemp - 0.1 }
		if reading.humid > avgHumid then { avgHumid := avgHumid + 0.5 }
		else { avgHumid := avgHumid - 0.5 }		
		if reading.solar > avgSolar then { avgSolar := avgSolar + 1 }
		else { avgSolar := avgSolar - 1 }				
	}
} 
 
lo = new LedOutput()
po = new PrintOutput()
sr = new SensorReader()
 
connect sr.photoRequestChan to lightHumidTempSensor.photoRequest
connect sr.photoValueChan to lightHumidTempSensor.photoOutput
connect sr.solarRequestChan to lightHumidTempSensor.solarRequest
connect sr.solarValueChan to lightHumidTempSensor.solarOutput
connect sr.tempRequestChan to lightHumidTempSensor.tempRequest
connect sr.tempValueChan to lightHumidTempSensor.tempOutput
connect sr.humidityRequestChan to lightHumidTempSensor.humidRequest
connect sr.humidityValueChan to lightHumidTempSensor.humidOutput
connect sr.printerChan to po.input
connect sr.ledsChan to lo.input
 
connect lo.setLeds to leds.setState
connect lo.redLed to leds.redState
connect lo.greenLed to leds.greenState
connect lo.blueLed to leds.blueState
 
connect po.output to standardOut