1. Serial Temperature Sensor
Key Concepts
I. Hardware Setup
In order to run the tutorial, you need to set up the hardware. If you are using Raspberry Pi and Arduino, the instructions are here. If you are using Libelium, the instructions are here.
II. Abstract the device using ThingFacet
Write a ThingFacet
To abstract the interactions with the temperature/humidity sensors setup in the previous section, we create a TempFacetSerial ThingFacet.
We add two types of attributes in this ThingFacet (1) parameters required to make a protocol specific invocation (2) attribute(s) to store the information received from the sensor. For the purpose of this tutorial we have protocol specific parameters as (Unit, Peripheral, InterfacePort, Interface, etc). The full message received on the serial interface will be stored in the PerifMsg attribute and the Celsius Temperature Value will be stored in the TempC Parameter.
<ThingFacet Name="TempFacetSerial"> <String Name="PerifMsg"/> <String Name="TempC"/> <String Name="Unit" default="Celsius"/> <String Name="Peripheral"/> <String Name="InterfacePort"/> <String Name="Interface"/> <String Name="UniqueId" default="56789"/> <String Name="Baudrate"/> <String Name="Format" default="ascii"/> <String Name="Operation"/> <String Name="Payload"/> ... </ThingFacet>
Write an Action with Workflow
The next step in ThingFacet is writing a Action which contains a workflow responsible for making external protocol specific call and store the output in the appropriate ThingFacet attribute. An important decision while writing Action is correct protocol selection. See the /wiki/spaces/TQLDocs/pages/1179871 in the Working with Things section. For this serial sensor we need the perif:// protocol handler, which is part of the TQLEngine.
1. Write an Action name as SerialReadAction
<ThingFacet Name="TempFacetSerial"> ... <Action Name="SerialReadAction"> ... </Action> </ThingFacet>
2. Create a Workflow within the Action, with one single continuous running Task and waiting for the Event with its ActionArgument.
<Action Name="SerialReadAction"> <Workflow Limit="1" Live="1" Timeout="-1"> <Task name="Main" while="true"> <Event name="Argument" as="ActionArgument"/> ... </Task> </Workflow> </Action>
Here we used three modifiers for this workflow. Limit = "1" means there can be at most one instance of this workflow waiting. Live = "1" means there can be at most one instance of this workflow running. Timeout ="-1" means this workflow will never be timed out. We used a modifier while = "true" with the Task to make the workflow running in a continuous loop, because it needs to run repeatedly, not just once. For more details, refer to workflow modifiers and the lifecycle of a workflow.
The task will be activated by the event handler Event called ActionArgument. "ActionArgument" is the event generated whenever the attribute(s) associated with this Action is modified (See Associate Action with a ThingFacet Attribute). ActionArgument carries all the current values of the ThingFacet attributes, which can be used in the task if needed.
3. Invoke PERIF call: Method of PERIF is GET. We use Invoke modifier Get for this purpose. The parameters required PERIF are passed as part of <Message> and <Value>. More details of the perif:// handler are in the Working with Things Section.
<Invoke name="InvokeSerialRead" waitFor="Argument" scope="process" get="perif://"> <Message> <Value> <InterfacePort>[%:Event.Argument.interfacePort.Value:%]</InterfacePort> <Baudrate>[%:Event.Argument.Baudrate.Value:%]</Baudrate> <Interface>[%:Event.Argument.Interface.Value:%]</Interface> <UniqueId>[%:Event.Argument.UniqueId.Value:%]</UniqueId> <Operation>[%:Event.Argument.Operation.Value:%]</Operation> <format>[%:Event.Argument.Format.Value:%]</format> <Payload>[%:Event.Argument.Payload.Value:%]</Payload> <Peripheral>[%:Event.Argument.Peripheral.Value:%]</Peripheral> </Value> </Message> </Invoke>
Here Invoke gets the attribute values that the Event ActionArgument (or Argument) carries. It uses them as the parameter values for the PERIF by substitution. For example,
<Baudrate>[%:Event.Argument.baudrate.Value:%]</Baudrate>
means replacing the value of Message.Value.Baudrate with the value of Event.Argument.baudrate.Value.
Value substitution (rather than assignment) is very common in TQL, given that it is a declarative language. The notation [%: is part of the /wiki/spaces/TEACH/pages/21170773. More details on /wiki/spaces/TEACH/pages/21170773 can be found in the Developer's Guide.
4. Process the message received from the sensor
<Output Name="Result" As="ActionResult"> <Value> <PerifMsg> [%:Invoke.InvokeSerialRead.Message.Value.received:%] </PerifMsg> <TempC> [%:Invoke.InvokeSerialRead.Message.Value/substring("[%:Invoke.InvokeSerialRead.Message.Value.received:%]",7,5):%] </TempC> </Value> </Output>
Here we used an XPath statement to parse the TempC value out of the message. The sketch that is installed on the Arduino when it comes with the IoT Kit writes a message on the serial interface that looks as follows: TEMPC:25.94#TEMPF:78.69#HUMPCT:0.0#AMB:618. So in the processing of the output two things are done here. The "Known" of the <PerifMsg> attribute of the facet is set to the entire message string. And with the XPath substring function is used to extract the Celsius Temperature value (in the example 25.94). Instead of the substring function that uses a fixed start and length one could of course use other XPath functions to make this code more robust.
Alternatively, you can use Javascript. An example of using JavaScript for message processing within an Action can be found in another tutorial here.
Associate Action with a ThingFacet Attribute
We will now have to associate a ThingFacet Attribute (PerifMsg) with the Action using the KnownBy modifier. When PerifMsg is "KnownBy" SerialReadAction, any PerifMsg value changes will activate the SerialReadAction. We do the same for the TempC attribute to set its "Known" in the output of the action.
<ThingFacet Name="TempFacetSerial"> ... <String Name="PerifMsg" update="auto" KnownBy="SerialReadAction"> <String Name="TempC" update="auto" KnownBy="SerialReadAction"/> </ThingFacet>
The attribute modifier update= "auto" makes sure that once the action associated with this attribute is triggered, its workflow continues to run and wait for subsequent sensor events (not just the first event). This modifier is only used with actionable attributes. For more details, refer to Automatic Action trigger.
III. Combine ThingFacet with a ThingModel to persist data
Finally, in order to use ThingFacet we have to combine it with a ThingModel. We define ThingModel to contain only a unique system identifier.
<ThingModel Name="TempSensor" combines="TempFacetSerial"> <Sid Name="sensorId"/> </ThingModel>
Only when the TempSensor ThingModel "combines" TempFacetSerial, can TempFacetSerial be instantiated within the TempSensor and accessible by TQL queries. TempSensor will inherit all the attributes from TempFacetSerial, in addition to its own attributes. The ThingFacet TempFacetSerial hence serves as a reusable component.
More details on the use of "combines" can be found here. Information on Sid can be found here.
IV. Deploy and test via queries and subscriptions
Export, import and deploy
Once the models are built in a TQLStudio project, you can export the project. The URL of the Zip file containing the content of your project will be sent over to your email account. Once you have downloaded the engine – launch the TQLEngine User Interface in the browser and select Import Project. You can copy the URL from Export Project Email and hit import button. Go to ThingSpaces and Deploy the project.
Instantiation
Here we use a query to instantiate the sensor, creating 1 instance of the TempSensor ThingModel. In the query, we first use "DeleteAll" to delete all the current instances of the TempSensor, if there are any. The Save will save the values into the corresponding model attributes (inherited from the ThingFacet). These values provide the runtime parameters for the perif:// handler, as well as providing an update of the PerifMsg and TempC actionable attributes ($Null ( )), which will trigger the associated action SerialReadAction. The instantiation query below has been used on a setup where the engine is running locally on a Mac Pro and the Arduino board was directly connected to the Mac.
<Query> <DeleteAll format="version,current"> <TempSensor> <sensorId ne=""/> </TempSensor> </DeleteAll> <Save format="version,current"> <!-- This will read --> <TempSensor> <Peripheral>serial</Peripheral> <Baudrate>9600</Baudrate> <InterfacePort>/dev/cu.usbmodem1411</InterfacePort> <Interface>serial</Interface> <Format>ascii</Format> <Operation>receive</Operation> <UniqueId>76522</UniqueId> <Payload>$Null()</Payload> <PerifMsg>$Null()</PerifMsg> <TempC>$Null()</TempC> </TempSensor> </Save> </Query>
Query and subscription
Now we can try a simple Find Query to get sensor values
<Query> <Find format="version,known"> <TempSensor> <sensorId ne="" /> </TempSensor> </Find> </Query>
You will see the result from the RESULTS window:
<Query Storage='TqlSubscription'> <Save> <TqlSubscription Label='TempSensor' sid='20'> <Topic>*Atomiton.Sensor.TempSensor*</Topic> </TqlSubscription> </Save> </Query>
Or you can subscribe to the specific attribute of the model.
<Topic>Atomiton.Sensor.TempSensor.TempC*</Topic>
The Label is the string that will be attached to every message you receive from this subscription. Here we give an sid to this instance of the subscription.
V. Source Code
Import into TQLStudio
ProjectName | Import Link |
---|---|
Temp Sensor Serial | TempSensor |
The full content of the code is here.