In the learn how to create a Raspberry Pi sensor article, we demonstrated how to develop a Pi that is able to sense light, motion and temperature. In the same article, we also discussed how to use C# code to interact with hardware components and capture values sensed. This article will pick up from where the previous article left. The article will focus on demonstrating how to persist captured values in a database, exporting the data, creating an actuator and a controller.

Functionality to support data persistence is available in the Data Clayster library. Data persistence happens via an object database that evaluates the objects that you have designed and creates a database schema that is able to hold the defined objects. The entry point to data persistence is referencing an object as shown below.

After referencing an object database, information that will be used to connect to the database needs to be provided. One approach used in passing the connection information is adding the connection parameters in a .config file from where the application can read it. This tutorial will demonstrate persisting data to a SQLite database. The parameters that will enable connection to the database are shown below

To perform data manipulation activities such as storing, updating, searching and deleting a database, a proxy object is used as shown below. By using a database object we ensure data is not lost when our Raspberry Pi is powered off.

For our data to be useful we need to go beyond persisting it in a database and be able to export it for consumption by other applications. To fulfill this requirement, the IoT library provides an XML based sensor format. The format orders data by timestamp and for each timestamp it is possible to have string, Boolean, date or enumeration fields. Each field has optional and required metadata associated with it. Required metadata include name and a value of the correct field type. Optional metadata that can be associated with a field include the quality of service and a readout type.

The process of exporting data begins with method call of Start () and ends with a method call of End (). The methods called to initiate and terminate the export process are available in the data export class. The data process export is simplified by calling an intermediate function to retrieve the data from a record object and temporarily store them in another record as an array. The C# code used to retrieve the sensed data is shown below

Before any data export can happen, there are several checks done on the data. The readout type, the time interval and fields are checked to ensure they conform to what the client requested. Fields that meet the criteria set by the client are exported. The C# code used to check and export fields is shown below.

In an IoT project, a sensor is used to capture the state of an environment while an actuator utilizes the sensed state to interact with the environment. Because this article is a continuation of the ‘learn how to create a sensor project’, the hardware mentioned here is an addition of previously used hardware. Hardware needed for the actuator includes an alarm and digital outputs, which are connected through GPIO pins. The DigitalOutput class provides a mechanism to interface with digital outputs. To interface with the alarm the SoftwarePwm class will be used. The code used to interact with the outputs is shown below.

A controller is the intelligent link between the sensor and the actuator. The controller processes the data captured by the sensor and communicates its output through the actuator. If the project we have developed is deployed in a home security environment, then an alarm would sound if there is a combination of darkness and movement. Before the controller can do any processing, it needs to acquire the sensed data. The variables shown below will be used to hold the sensed data:

Earlier in the article, we demonstrated how to export data in an XML based format. At this stage we need to process the data to detect differences in current and previous data. This processing is implemented using the code shown below.

In this article, we discussed persisting sensed data into a database, exporting sensed data in an XML based format and using the sensed data to provide intelligence to our IoT project.

Learn How to Create an Actuator and a Controller was last modified: by