Log Humidity and Temperature Values to Database and Calculate Dew Point Using External C Code Integration Workflow
This example shows how to use Simulink® Support Package for Raspberry Pi® Hardware to integrate a user-defined C function with a Simulink model and achieve the following:
Calculate dew point
Store relative humidity and ambient temperature data values to a SQLite database
This example uses the C function in the C Caller block to calculate the dew point from relative humidity and ambient temperature values, and the C function in the MATLAB Function block to store these data values in the SQLite database.
Prerequisites
1. For more information on how to use Simulink Support Package for Raspberry Pi Hardware to run a Simulink model on Raspberry Pi hardware, see Get Started with Simulink Support Package for Raspberry Pi Hardware.
2. This example uses the SQLite3 library to create an SQLite database that stores the relative humidity and the ambient temperature values. Execute these commands in the MATLAB® Command Window to install the SQLite3 library on your Raspberry Pi.
a. Establish a connection to the Raspberry Pi hardware.
r = raspberrypi('Raspberry Pi IP address','Username','Password');
b. Open the Raspberry Pi terminal.
r.openShell;
3. Execute these commands in the Raspberry Pi terminal to download and install the SQLite3 library on your Raspberry Pi hardware.
sudo apt-get update;
sudo apt-get install libsqlite3-dev;
Required Hardware
Raspberry Pi hardware board
SenseHAT
Files and Folder Structure
The following files are included with this example to help you integrate the C functions with the preconfigured Simulink model.
File Name | Description ---------------------------------------------------------------------------------------------------------------------------------- | ExternalCode_include /calculateDewPoint.h | Header file that contains the C function declarations and macro definitions for calculating the dew point. | This file includes definitions of the calculateDewPoint function, which estimates the dew point from the relative humidity and ambient temperature values. -------------------------------------------------------------------------------------------------------------------------------------- ExternalCode_include /ExternalCode_sqlite.h | Header file that contains the C function declarations and macro definitions for storing the relative humidity and ambient temperature values in the SQLite database. | This file contains the custom C function insertToDB that logs these values in the database. ----------------------------------------------------------------------------------------------------------------------------------------- ExternalCode_src /calculateDewPoint.c | Source code file that includes the calculateDewPoint.h header file and the formula to calculate dew point. ------------------------------------------------------------------------------------------------------------------------------------------- ExternalCode_src /ExternalCode_sqlite.c | Source code file that includes the ExternalCode_sqlite.h header file. | It also includes the header files and variables that are required when the Simulink model runs in the Accelerator and Rapid Accelerator modes. | The function defined in this file creates a table in the database that stores the relative humidity and ambient temperature values. --------------------------------------------------------------------------------------------------------------------------------------------
Configure Simulink Model and Calibrate Parameters
This example uses a preconfigured Simulink model from the Simulink Support Package for Raspberry Pi Hardware.
Open the raspberrypi_externalcode
Simulink model.
You can run this example either in the simulation mode by using Constant blocks that synthesize relative humidity and ambient temperature values, or you can deploy the Simulink model on your Raspberry Pi hardware and interface the SenseHAT to measure these values. This example uses the HTS221 Humidity Sensor block to capture the relative humidity and ambient temperature data.
Add Simulation Sources Area
In the Add Simulation Sources area, you can use either the Constant blocks or use the Slider to set the humidity and temperature values. The default humidity value is 50
, while the default temperature value is 34
. This data is multiplexed and provided as one of the inputs to the Manual Switch in the Design your algorithm here area.
Add Raspberry Pi Sensors and Inputs Area
In the Add Raspberry Pi Sensors and Inputs area, the HTS221 Humidity Sensor block parameters are configured to its default values. The data from the Humidity and Temp output ports of the block is multiplexed and provided as the other input to the Manual Switch.
Design your algorithm here Area
In the Design your algorithm here area, configure the C Caller block and the MATLAB Function block. The default position of the Manual Switch is set such that it receives data from the Add Simulation Sources area.
A. Configuring C Caller Block
In this example, the Integrate C Code Using C Caller Blocks block uses a predefined C function, calculateDewPoint.c
, to calculate the dew point from relative humidity and ambient temperature data values obtained from either the Add Simulation Sources area or the Add Raspberry Pi Sensors and Inputs area.
Double-click the Integrate C Code Using C Caller Blocks block to view the Block Parameters dialog box. Follow this procedure to add or change the source files and their dependencies.
1. In the C Caller block, click Open Block Dialog and then click the gear icon to open the Simulation Target tab in the Configuration Parameters dialog box.
2. When compiling the generated code, in the Additional build information section, select the Include directories tab and enter the name of the directory you want to add to the include path. In this example, the name is set to ExternalCode_include
.
3. Similarly, in the Source files tab, enter the names of the source files to be compiled and linked to the generated code. In this example, the name is set to ExternalCode_src\calculateDewPoint.c
.
Tip: Include the folders and files within the same working directory as the example to avoid providing their absolute paths.
4. In the Insert custom C code in generated section, in the Header file tab, enter the names of the header files to be included for the corresponding source code. In this example, the name is set to #include "calculateDewPoint.h"
.
5. Select Import custom code. Then, select the Enable custom code analysis option.
6. Click Apply.
7. In the Configuration Parameters dialog box, in the left pane, go to Code Generation > Custom Code.
8. Select the Use the same custom code settings as Simulation Target option. In this example, the custom code for the simulation and the code generation is the same. The header file name and the source code file names are auto populated in the dialog box. If your source code is different for the simulation and the code generation, your can specify the header files and the source code files in this dialog box.
9. Click Apply.
10. On the C Caller Block Parameters dialog box, click the refresh icon to import your source code and its dependencies. The Diagnostic Viewer displays the following message.
Parsing custom code in model 'raspberrypi_externalcode' successful.
11. Select the C function listed under the Function Name parameter. For this example, select calculateDewPoint
.
12. To map the C function arguments from your C code to the Simulink ports, click Port specifications. This section specifies the arguments used in the C function and how they connect to your C Caller block in the Simulink model.
13. Change the Scope for the b
and c
arguments from Input to Parameter.
14. Click Apply. Now when you double-click the C Caller block, observe the b
and c
input parameters. The default values are set to 17.62
and 243.5
, respectively. These values are based on the dew point calculation formula.
15. Connect the multiplexed humidity and temperature output to the Humidity and Temp input ports of the C Caller block named calculateDewPoint
. Then, connect the out port of the C Caller block to the Add Raspberry Pi Actuators and Outputs area.
B. Configuring MATLAB Function Block
The function saveToDB
is used in the MATLAB Function block to store the relative humidity and ambient temperature values in the SQLite database. This function accepts two input parameters, Humidity and Temp, and stores them in the SQLite databased named sqlitedb_Humidity_Temp.db
. For the code generation behavior, the MATLAB Function block depends on an external C function insertToDB
that is called in the ExternalCode_sqlite.c
file. Whereas in the simulation behavior, various functions from the Database Toolbox are used to store the relative humidity and ambient temperature values into the SQLite database.
In the Rate Transition block, set the Output port sample time parameter to 10
to store these sample values to the database every 10
seconds.
Follow this procedure to add or change the source files and their dependencies.
1. In the Modeling tab, select Model Settings and open the Configuration Settings dialog box.
2. From the left pane, select the Simulation Target tab.
3. In the Additional build information section, select the Source files tab and enter the names of the source files to be compiled and linked with the generated code. In this example, the name is set to ExternalCode_src\ExternalCode_sqlite.c
.
4. In the Insert custom C code in generated section, in the Header file tab, enter the names of the header files to be included for the corresponding source code. In this example, the name is set to #include "ExternalCode_sqlite.h"
.
5. Click Apply.
Follow this procedure to add the SQLite library linker in the Configuration Parameters dialog box for the Hardware board parameter set to Raspberry Pi
.
1. In the Configuration Parameters dialog box, from the left pane select Code Generation.
2. In the Build process section, set the Build configuration parameter to Specify
.
3. In the Tools column, in the Linker row, add the option: -lsqlite3
4. Click Apply > OK.
Add Display and Visualization Area
The Dashboard Gauge block displays the connected output of the C Caller block on a circular scale during simulation. For more information on how to connect the out port of the C Caller block to the Dashboard Gauge block, see Tune and Visualize Your Model with Dashboard Blocks.
Add Raspberry Pi Actuators and Outputs Area
After deploying the Simulink model on your Raspberry Pi hardware, you can view the LED output on your Raspberry Pi hardware when the dew point value crosses or is equal to 25
.
Configure these parameters in the Compare To Constant block.
1. Set the Operator parameter to >=
.
2. Set the Constant value parameter to 25
.
Run Simulink Model
Follow this procedure to run the Simulink model in the simulation mode.
1. Position the Manual Switch in the Design your algorithm here area to the output received from the Add Simulation Sources area.
2. On the Simulation tab of the Simulink model, click Run.
3. Move the Humidity and the Temp Slider. You can view the set values on the Dashboard Display block in the Design your algorithm here area.
4. Observe the calculated dew point value on the circular Gauge.
5. In the current working directory of the example, observe that the database file sqlite_Humidity_Temp.db
is created.
6. To view the relative humidity and ambient temperature values stored in the database, execute these commands in the MATLAB Command Window.
dbfile = fullfile(pwd, 'sqlitedb_Humidity_Temp.db');
conn = sqlite(dbfile);
sqlquery = 'SELECT * FROM MYDATA';
results = fetch(conn,sqlquery);
Deploy Simulink Model on Raspberry Pi Hardware
Ensure to complete these steps before you deploy the Simulink model on your Raspberry Pi hardware.
SQLite3 database installation is complete on Raspberry Pi.
Close the existing SQLite database connection if already created in the simulation mode by executing this command in the MATLAB Command Window:
close (conn);
Follow this procedure to deploy the Simulink model on your Raspberry Pi hardware board.
1. Position the Manual Switch in the Design your algorithm here area towards the output received from the Add Raspberry Pi Sensors and Inputs area.
2. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start.
3. Observe the LED on your Raspberry Pi hardware board. This LED glows only for a dew point value above or equal to 25. This dew point value depends on the real-time relative humidity and ambient temperature values recorded by the humidity sensor on your SenseHAT.
4. To view the database file present in the current directory of the Raspberry Pi, execute this command on the Raspberry Pi terminal: ls -l *.db
Observe that the sqlite_Humidity_Temp.db
database file has been created in the HOME
directory of the Raspberry Pi.
5. To view the database files from the Raspberry Pi terminal to MATLAB, execute these commands in the MATLAB Command Window.
r.getfile('sqlite_Humidity_Temp.db');
conn = sqlite(dbfile);
sqlquery = 'SELECT * FROM MYDATA';
results = fetch(conn,sqlquery);