Simple Insense program requesting sensor readings and sending results on broadcast radio and using the LEDs to indicate increases in temperature (red), humidity (green) and light (blue). The SenorReader 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 component channels for each of the following:
- LEDs
- visible light (photo) sensor
- total solar radiation light (solar) sensor
- temperature sensor
- humidity sensor
- button sensor
- radioIn and radioOut
- as well as a standard output channel
// Sensor reading type
type sensorReading is struct(
byte[] addr ;
integer cycle ;
integer photo ;
integer solar ;
real temp ;
real humid
)
// Interface for Sender component
type ISender 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 ;
out any radioChan ; out bool blueLed
)
// Sender component
component Sender presents ISender {
cycle = 0
nodeAddr = getRadioNodeAddress()
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(nodeAddr, cycle, photoValue, solarValue, tempValue, humidityValue)
// send reading struct to printer, leds, and over the radio
send reading on printerChan
send reading on ledsChan
send any(reading) on radioChan
// increment cycle counter
cycle := cycle + 1
}
}
// Interface for Printer component sending readings
// and explanatory text to standard output
type IPrinter is interface (
in sensorReading input ;
out any stdOutChan
)
// Standard output and radio printer component
component Printer presents IPrinter {
constructor() {
}
behaviour {
// get reading struct
receive reading from input
// send text+readings in struct on stdout
send any("\nCycle ") on stdOutChan
send any(reading.cycle) on stdOutChan
send any(":\tphoto = ") on stdOutChan
send any(reading.photo) on stdOutChan
send any("\tsolar = ") on stdOutChan
send any(reading.solar) on stdOutChan
send any("\ttemp = ") on stdOutChan
send any(reading.temp) on stdOutChan
send any("\thumid = ") on stdOutChan
send any(reading.humid) on stdOutChan
}
}
// Interface for LED output component
type ledOutput is interface (
in sensorReading input ;
out ledState setLeds ;
out bool redLed ;
out bool greenLed ;
out bool blueLed
)
// LED output component
component LedOutput presents ledOutput {
avgTemp = 0.0
avgSolar = 0
avgHumid = 0.0
constructor() {
}
behaviour {
// turn off 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()
pr = new Printer()
sr = new Sender()
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 pr.input
connect sr.ledsChan to lo.input
connect sr.radioChan to radioOut.broadcast
connect sr.blueLed to leds.blueState
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 pr.stdOutChan to standardOut