Background
Modeling and Simulation are central to the IoT Applications. Modeling and Switching is a broad discipline with a wide range of technologies and methodologies. Most of the technologies focus on a specific aspect with a different purpose (e.g., discrete event simulation in industrial systems vs. continuous simulation in electrical and computer engineering). There is a need for an all-encompassing approach, especially in the IoT arena. Atomiton TQL provides right set of hooks to create an all-encompassing approach to simulation.
Greenhouse Problem Statement
For the purpose of this tutorial to explain implementation approaches to simulation we look at the Greenhouse monitoring system with following scenario.
For the purpose of this tutorial let's model the Greenhouse monitoring system with following configuration.
- Greenhouse: size: 40 feet by 110 feet
- crop types, tomatoes and peppers
- 3 lanes, each lane has 10 zones
- Total 30 zones (each zone is 10 feet by 10 feet. Lay out is 3 by 10): 20 zones (2 by 10) are tomatoes, 10 zones are peppers
Each zone subdivided into 9 grids. Each grid is 3.33 feet by 3.33 feet. Total 270 grids
Zone Content:
- 1 temperature sensor (total 30)
- 1 humidity sensor (total 30)
- 1 camera (total 30)
- 1 irrigation motor (total 30)
- 1 heater (total 30)
Zone Grid Content:
- 1 ambient light sensor (total 270)
- 1 light (total 270)
- 1 irrigation nozzle (total 270)
- 1 soil moisture sensor (total 270)
External Conditions:
- 1 ambient light sensor
- 1 temperature sensor
- 1 humidity sensor
Simulation Design Patterns
At the time of writing this tutorial Atomiton TQLEngine does not provide specific language constructs to implement simulation. But the existing Atomic domain languages capabilities can be exploited to achieve simple to complex simulation. The simulation design patterns can be classified as follows:
Name | Description | Use Cases | Implementation Flow |
---|---|---|---|
@ThingFacet Level | Seamless integration within the attribute's associate action |
|
|
@ThingFacet Level | Separate Action to start the simulation |
|
|
@Protocol Handler Level | Push the simulation logic down into protocol handler level |
| Within Protocol Handler. |
@Behavior Tree | Simulation logic is driven primarily by Behavior Trees. |
| Within BDL Facet Instance |
Create DataModel to store non-thing related data
TQLEngine provides DataModel to store "pure" data, DataModels do not combine facets. They are simply models to store application data that is essential to create a meaningful real-world IoT application.
Custom Types
We create two custom types to store GeoLocation and BoundingBox.
GeoLocation - To Store GeoLocation
GeoLocation Type<Def Name="GeoLocation"> <Number Name="latitude"/> <Number Name="longitude"/> </Def>
BoundingBox - To Store Geo Boundary of Zone, etc
BoundingBox Type<Def Name="BoundingBox"> <GeoLocation Name="Vertex1"/> <GeoLocation Name="Vertex2"/> <GeoLocation Name="Vertex3"/> <GeoLocation Name="Vertex4"/> </Def>
EnvInfo - Capture all the important environment related information like temperature, humidity, light, pressuue
BoundingBox Type<Def Name="EnvInfo"> <Number Name="Temperature"/> <Number Name="Humidity"/> <Number Name="Light"/> <Number Name="Pressure"/> <Number Name="SoilMoisture"/> <String Name="LastUpdatedByProvider"/> </Def>
List of DataModels
Greenhouse : Overall DataModel holder for Greenhouse. GHLanes is defined of type reference. There are two types of references - simple reference and smart reference. Smartness can be defined by providing a "Find" Query at the time of model definition or at the time of instantiating the model. GHLanes is defined as Reference to include all the Lanes from Lane model.
Greenhouse DataModel<DataModel Name="Greenhouse"> <Sid Name="GreenhouseID"/> <BoundingBox Name="GreenhouseLocation"/> <Integer Name="LaneCount"/> <Double Name="LaneWidth"/> <Double Name="ZoneLength"/> <Integer Name="ZoneInLane"/> <Integer Name="ZoneCount"/> <Double Name="GHLength"/> <Double Name="GHWidth"/> <EnvInfo Name="ExternalEnv"/> <EnvInfo Name="InternalEnv"/> <String Name="VentOnOffState"/> <String Name="FansOnOffState"/> <Boolean Name="SunnyDay"/> <Reference Name="GHLanes"> <Find> <Lane> <LaneID ne=""/> </Lane> </Find> </Reference> </DataModel>
Grid - DataModel for storing Grid information.
Grid DataModel<DataModel Name="Grid"> <Sid Name="GridID"/> <String Name="ZoneID"/> <String Name="GridName"/> <BoundingBox Name="GridLocation"/> <Integer Name="GridNSPosition"/> <Integer Name="GridWEPosition"/> <Double Name="GridLength"/> <Double Name="GridWidth"/> <Double Name="SoilMoisture"/> <Double Name="AmbientLight"/> <Reference Name="ZoneGrid" Type="Zone" Cardinality="1"/> </DataModel>
Zone - DataModel for storing Zone information. Does a Smart Reference to Grid at instantiation time by simply defining ZoneGrids as Reference type. The Find query can be add at the instantiation time.
Zone DataModel<DataModel Name="Zone"> <Sid Name="ZoneID"/> <String Name="LaneID"/> <String Name="ZoneName"/> <BoundingBox Name="ZoneLocation"/> <Double Name="ZoneWidth"/> <Double Name="ZoneLength"/> <Integer Name="GridOnLength"/> <Integer Name="GridOnWidth"/> <Integer Name="GridCount"/> <Double Name="GridLength"/> <Double Name="GridWidth"/> <String Name="EndZone"/> <Double Name="ZoneTemperature"/> <Double Name="ZoneHumidity"/> <Double Name="ZoneAvgSM"/> <Double Name="ZoneAvgLight"/> <String Name="CropType"/> <Date Name="SeedingDate" format="$SimpleDateFormat(dd-MM-yyyy)"/> <Date Name="LastFertilisationDate" format="$SimpleDateFormat(dd-MM-yyyy)"/> <String Name="HarvestingSeason"/> <Integer Name="NoOfPlants"/> <Double Name="VPD"/> <Reference Name="LaneZone" Type="Lane" Cardinality="1"/> <Reference Name="ZoneGrids"> <Find> <Grid> <GridID ne=""/> </Grid> </Find> </Reference> </DataModel>
Lane - DataModel to hold Lane information. Defines Smart Reference to Zones.
Lane DataModel<DataModel Name="Lane"> <Sid Name="LaneID"/> <String Name="LaneName"/> <String Name="GreenhouseID"/> <Double Name="LaneLength"/> <Double Name="LaneWidth"/> <Integer Name="ZoneInLane"/> <String Name="BorderLane"/> <BoundingBox Name="LaneLocation"/> <Reference Name="GreenhouseLane" Type="Greenhouse" Cardinality="1"/> <Reference Name="LaneZones"> <Find> <Zone> <ZoneID ne=""/> </Zone> </Find> </Reference> </DataModel>
Data + Things
We have to assign 1 temperature sensor per zone. We do this by:
- Use the content of MultipleSensors tutorial project and bring in all the model content into this tutorial.
- Use Simple Reference in TempSensorModel to a particular Zone using ZoneID
Other sensors and actuators can be referenced in a similar fashion.
<ThingModel Name="TempSensorModel" Combines="TempSensorFacet"> <Sid Name="TempSensorId"/> <Reference Name="TempSensorInZone" Type="Zone" Cardinality="1"/> </ThingModel>
Queries
Let's start writing some queries to create and read the data.
Create Grids
<Query> <Find> <Zone> <ZoneName eq="Zone1"/> </Zone> </Find> <Create> <Grid> <GridName> Grid1 </GridName> <GridNSPosition> 1 </GridNSPosition> <GridWEPosition> 1 </GridWEPosition> <GridLength> 3.33 </GridLength> <GridWidth> 3.33 </GridWidth> <SoilMoisture> 0 </SoilMoisture> <AmbientLight> 0 </AmbientLight> <ZoneGrid>[:$Response.Message.Value.Find.Result.Zone.ZoneID:]</ZoneGrid> </Grid> </Create> </Query>
Create Zone
<Query> <DeleteAll> <Zone> <ZoneID ne=""/> </Zone> </DeleteAll> <Create> <Zone> <ZoneWidth> 10 </ZoneWidth> <ZoneLength> 10 </ZoneLength> <GridOnLength> 3 </GridOnLength> <GridOnWidth> 3 </GridOnWidth> <GridCount> 9 </GridCount> <GridLength> 3.33 </GridLength> <GridWidth> 3.33 </GridWidth> <EndZone> No </EndZone> <ZoneTemperature> 0 </ZoneTemperature> <ZoneHumidity> 0 </ZoneHumidity> <CropType>Tomato</CropType> </Zone> </Create> </Query>
Create Lane
<Query> <Create> <Lane> <LaneWidth> 110 </LaneWidth> <ZoneInLane> 10 </ZoneInLane> <BorderLane> Yes </BorderLane> <LaneZones> </LaneZones> </Lane> </Create> </Query>
Create Greenhouse
<Query> <Create> <Greenhouse> <LaneCount> 3 </LaneCount> <LaneWidth> 110 </LaneWidth> <ZoneLength> 10 </ZoneLength> <ZoneInLane> 10 </ZoneInLane> <ZoneCount> 90 </ZoneCount> <ExtEnvInfo> <Temperature> 0 </Temperature> <Humidity> 0 </Humidity> <Light> 0 </Light> </ExtEnvInfo> <GHLanes></GHLanes> </Greenhouse> </Create> </Query>
Create Temperature Sensor
TempSensorInZone value is the ZoneID value of the Zone. This is dynamically assigned by querying the Zone Model.
<Query> <DeleteAll> <TempSensorModel> <TempSensorId ne=""/> </TempSensorModel> </DeleteAll> <Find> <Zone> <ZoneName eq="Zone1"/> </Zone> </Find> <Create> <TempSensorModel> <TempValueInC> 0 </TempValueInC> <TempValueInF> 0 </TempValueInF> <TempSensorInZone> [:$Response.Message.Value.Find.Result.Zone.ZoneID:] </TempSensorInZone> </TempSensorModel> </Create> </Query>
Find Temperature above x degree C
<Query> <Find nested="*" format="Version"> <TempSensorModel> <TempSensorId ne="" /> </TempSensorModel> </Find> </Query>