0% found this document useful (0 votes)
122 views

SQL Client Server

This document provides information and instructions for connecting a SIMATIC IOT2000 device to a SQL database server. It describes how to install and configure MariaDB as the SQL server on the IOT2000. It also describes how to connect to the SQL server from Node-RED, running on the IOT2000, to serve as an SQL client. The document contains information on preconditions, installation steps, security measures, and creating a sample database for testing the setup.

Uploaded by

Rodrigo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

SQL Client Server

This document provides information and instructions for connecting a SIMATIC IOT2000 device to a SQL database server. It describes how to install and configure MariaDB as the SQL server on the IOT2000. It also describes how to connect to the SQL server from Node-RED, running on the IOT2000, to serve as an SQL client. The document contains information on preconditions, installation steps, security measures, and creating a sample database for testing the setup.

Uploaded by

Rodrigo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

SIMATIC IOT2000

Database connection
Siemens
SIMATIC IOT2020, SIMATIC IOT2040 Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/Item ID Support
Frei verwendbar
Warranty and liability

Warranty and liability

Note The Application Examples are not binding and do not claim to be complete
regarding the circuits shown, equipping and any eventuality. The Application
Examples do not represent customer-specific solutions. They are only intended
to provide support for typical applications. You are responsible for ensuring that
the described products are used correctly. These Application Examples do not
relieve you of the responsibility to use safe practices in application, installation,
operation and maintenance. When using these Application Examples, you
recognize that we cannot be made liable for any damage/claims beyond the
liability clause described. We reserve the right to make changes to these
Application Examples at any time without prior notice.
If there are any deviations between the recommendations provided in these
Application Examples and other Siemens publications – e.g. Catalogs – the
contents of the other documents have priority.

We do not accept any liability for the information contained in this document.
Any claims against us – based on whatever legal reason – resulting from the use of
the examples, information, programs, engineering and performance data etc.,
described in this Application Example shall be excluded. Such an exclusion shall
not apply in the case of mandatory liability, e.g. under the German Product Liability
Act (“Produkthaftungsgesetz”), in case of intent, gross negligence, or injury of life,
body or health, guarantee for the quality of a product, fraudulent concealment of a
 Siemens AG 2018 All rights reserved

deficiency or breach of a condition which goes to the root of the contract


(“wesentliche Vertragspflichten”). The damages for a breach of a substantial
contractual obligation are, however, limited to the foreseeable damage, typical for
the type of contract, except in the event of intent or gross negligence or injury to
life, body or health. The above provisions do not imply a change of the burden of
proof to your detriment.
Any form of duplication or distribution of these Application Examples or excerpts
hereof is prohibited without the expressed consent of the Siemens AG.

Security Siemens provides products and solutions with industrial security functions that
informa- support the secure operation of plants, systems, machines and networks.
tion In order to protect plants, systems, machines and networks against cyber
threats, it is necessary to implement – and continuously maintain – a holistic,
state-of-the-art industrial security concept. Siemens’ products and solutions only
form one element of such a concept.
Customer is responsible to prevent unauthorized access to its plants, systems,
machines and networks. Systems, machines and components should only be
connected to the enterprise network or the internet if and to the extent necessary
and with appropriate security measures (e.g. use of firewalls and network
segmentation) in place.
Additionally, Siemens’ guidance on appropriate security measures should be
taken into account. For more information about industrial security, please visit
http://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them
more secure. Siemens strongly recommends to apply product updates as soon
as available and to always use the latest product versions. Use of product
versions that are no longer supported, and failure to apply latest updates may
increase customer’s exposure to cyber threats.
To stay informed about product updates, subscribe to the Siemens Industrial
Security RSS Feed under http://www.siemens.com/industrialsecurity.

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 2
Table of contents

Table of contents
Warranty and liability ................................................................................................... 2
1 Task ..................................................................................................................... 4
1.1 Overview............................................................................................... 4
2 Demo 1 – Install SQL server on IOT2000......................................................... 6
2.1 Preconditions for installing MariaDB .................................................... 6
2.2 Installing MariaDB ................................................................................ 7
2.2.1 Create a new user for secure installation ............................................. 7
2.2.2 Optional: mount USB-Drive .................................................................. 7
2.2.3 Install binaries ...................................................................................... 9
2.2.4 Testing installed MariaDB server ....................................................... 11
2.2.5 Securing MariaDB installation ............................................................ 11
2.3 Adding MariaDB to autostart .............................................................. 12
2.4 Creating an example database .......................................................... 12
3 Demo 2 – SQL client with Node-RED ............................................................. 14
3.1 Preconditions for an SQL client with Node-RED ................................ 14
3.2 Start Node-RED ................................................................................. 14
3.2.1 Auto start Node-RED on boot ............................................................ 14
3.2.2 Manual start Node-RED ..................................................................... 16
3.3 Install required Node-RED nodes ...................................................... 17
3.4 Configure Node-RED dashboard ....................................................... 18
 Siemens AG 2018 All rights reserved

3.5 Configure Node-RED flow .................................................................. 19


4 Demo 3 – SQL client with Eclipse and C++ ................................................... 24
4.1 Preconditions for a SQL client with Eclipse........................................ 24
4.2 Create a new Eclipse IOT2000 project .............................................. 24
4.3 Edit project settings to use a shared library ....................................... 25
4.4 Install libmysqlclient on the IOT2000 ................................................. 26
4.4.1 Install mysqlclient library only ............................................................. 26
4.4.2 Install mysqlclient library from a MySQL server installation ............... 27
4.5 Creating the user program ................................................................. 27
5 Checklist ........................................................................................................... 30
5.1 Demo 1 – install MariaDB server ....................................................... 30
5.2 Demo 2 – SQL client with Node-RED ................................................ 30
5.3 Demo 3 – SQL client with Eclipse and C++ ....................................... 30
6 Related links ..................................................................................................... 31
7 History............................................................................................................... 31

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 3
1 Task

1 Task
1.1 Overview
Introduction
This application example is divided into three parts.
One example shows how to install an SQL server at the IOT200 device.
Two examples show how to connect the IOT2000 with an SQL server.
Every example was created with the IOT2000 example image V2.2 (see Related
links)

Figure 1-1 Demo overview


Demo 1
IOT2000
 Siemens AG 2018 All rights reserved

Demo 2
IOT2000 Server

SQL Database

Demo 3
IOT2000 Server

SQL Database
C++ SQL Client

Goals
After working through this document you will know how to
 Install an SQL server on the IOT2000
 Create an SQL client with Eclipse as C++ application
 Create an SQL client with Node-RED

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Frei verwendbar
Version 1.0, 05/2018 4
1 Task

NOTICE Precondition!
To use this sample it is required that the initial operation of the documents
“Setting up the SIMATIC IOT2000” and “Getting Started” has been executed
once.

NOTICE Precondition!
Basic Linux knowledge is needed to understand the examples
 Siemens AG 2018 All rights reserved

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 5
2 Demo 1 – Install SQL server on IOT2000

2 Demo 1 – Install SQL server on IOT2000


This chapter describes how to install MariaDB as SQL server on the IOT2000.
MariaDB is a fork of MySQL and open source. MariaDB supports any statement
that is known by MySQL. Because of this fact it is possible to replace a MySQL
installation with MariaDB (or vice versa) at any time.
The example uses MariaDB because it is open source.

Figure 2-1 MariaDB as server

2.1 Preconditions for installing MariaDB


 Siemens AG 2018 All rights reserved

To start you have to download MariaDB 5.5 for a Linux x86-system because the
IOT2000 has a 32-bit processor.

Table 2-1 Download MariaDB installer package


No. Action
1. Download MariaDB 5.5 Series from https://downloads.mariadb.org/

2.

The version 5.5.59 was the current version at the time of creation of this
document.
NOTE
It may differ at the time you will download it.

Frei verwendbar

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 6
2 Demo 1 – Install SQL server on IOT2000

2.2 Installing MariaDB


2.2.1 Create a new user for secure installation

To create some secure environment for the MariaDB server it is required to create
a new user. That user will be used to run MariaDB. Because of the file system right
management MariaDB has only access to files that are owned by the installer user
(e.g. "mysql") or files that allow access for the user.
Table 2-2 Create new user
No. Action
1. Connect to the IOT2000 via Putty
2. Create new user group mysql
groupadd mysql
3. Add new user mysql to the previously created group mysql
useradd –g mysql mysql
 Siemens AG 2018 All rights reserved

2.2.2 Optional: mount USB-Drive

In Linux USB-drive are not automatically mounted to a folder. So it is required to


configure the system to automatically mount the drive on every reboot. The
filesystem of the USB-drive is also important because a filesystem like ext4 is a bit
different from mounting a fat32 filesystem.
An external mass memory is recommended because normally an SD-card does not
have that much memory to store data. Performance could be a reason as well to
introduce an external mass storage.

Table 2-3 Mount external USB device


No. Action
1. Create folder for mounting your USB device
mkdir /media/usb-drive

2. Insert you USB device into the IOT2000


3. Find your device by entering this command:
blkid

The device is “/dev/sda1”


4. Type the following command to open the file “/etc/fstab” to add your USB device
to be automatically mounted on startup:
nano /etc/fstab

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 7
2 Demo 1 – Install SQL server on IOT2000

No. Action

Add the line like in the image below and replace /dev/sda1 and vfat with your
device id and type
 Siemens AG 2018 All rights reserved

Make sure that you have written everything in 1 line

Press “Ctrl + X” to exit and answer the following question with Yes (y)
5. Mount USB device manually with this command:
mount -a

The device is now accessible via “/media/usb-drive”

NOTE If you don’t want to use an USB-drive as storage you have to change all values
where /media/usb-drive is written with your own path in the following steps.

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 8
2 Demo 1 – Install SQL server on IOT2000

2.2.3 Install binaries

We are using the USB-drive as storage for MariaDB. If you want you can change it
to any directory. To do that you have to change the datadir parameter in the
following table to match your directory. Also make sure that the mysql user has
access to that folder.
To give the mysql user permission to a specific directory you can use the following
command by substituting DIR with your used directory:

chown -R mysql DIR

This command has to be executed only if you are using a different directory than
the USB-device.

Table 2-4 Install MariaDB binaries


No. Action
1. Copy downloaded file to “/home/root/” on the IOT2000 (WinSCP)
 Siemens AG 2018 All rights reserved

2. Create directory /usr/local by entering


mkdir /usr/local
3. Navigate to “/usr/local/”. There we are installing MariaDB
cd /usr/local
4. Extract downloaded MariaDB package (replace xx with your downloaded version)
tar -zxvpf /home/root/mariadb-5.5.xx-linux-i686.tar.gz
5. Create soft link to easily access MariaDB folder (replace xx with your
downloaded version)
ln -s mariadb-5.5.xx-linux-i686 mysql

6. Edit the file /etc/my.cnf by entering


nano /etc/my.cnf

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 9
2 Demo 1 – Install SQL server on IOT2000

No. Action

[mysqld]
datadir=/media/usb-drive/sqldb
7. Switch to the mysql directory with
cd mysql
Change current user to the previously created mysql user
 Siemens AG 2018 All rights reserved

8.
su mysql

9. Start installation script for MariaDB


./scripts/mysql_install_db --datadir=/media/usb-drive/sqldb

NOTE You can set any path to datadir but you have to use the same in the my.cnf file
and when starting the installation script
Also note that the mysql user must have read and write permission to that folder
The following command can be used to change the owner of a directory
chown -R mysql DIR (You have to change DIR to your own directory)

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 10
2 Demo 1 – Install SQL server on IOT2000

2.2.4 Testing installed MariaDB server

Table 2-5 Test the installed MariaDB server


No. Action
1. To start the server manually you have to run
./bin/mysqld_safe --user=mysql &

2. Test the connection with the following command:


./bin/mysql

It may take some time until the server is started


3. If you can successfully connect you can exit the program by typing exit
 Siemens AG 2018 All rights reserved

2.2.5 Securing MariaDB installation

To secure our installation we can run a script that helps removing some tables that
are not needed and we are forced to set a password for the root user of MariaDB.
We also prevent external access as root user to increase security.
Table 2-6 Secure installed MariaDB server
No. Action
1. Logoff from mysql user with the command
exit

2. Change directory again to /usr/local/mysql with


cd /usr/local/mysql

3. To secure your newly installed database you have to run


./bin/mysql_secure_installation
4. Complete the following wizard by setting a root password and accepting
everything with yes (y)

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 11
2 Demo 1 – Install SQL server on IOT2000

2.3 Adding MariaDB to autostart


To add MariaDB to autostart you have to add it to the init.d system that manages
all autostart programs and scripts. By adding MariaDB to start on boot you do not
have to start it manually after each reboot.

Table 2-7 Add MariaDB server to auto start


No. Action
1. Copy startup script to the auto start folder
cp support-files/mysql.server /etc/init.d/mysql

2. Add copied file to the init.d system


update-rc.d mysql defaults

2.4 Creating an example database


 Siemens AG 2018 All rights reserved

Create an example database which will be used in the client examples.


Table 2-8 Create an example database
No. Action
1. Start SQL console and login with the root password that you set during the
securing procedure
./bin/mysql -u root -p

2. Create a database with the name “iot2000DB”


CREATE DATABASE iot2000DB;

3. Select previously selected database to continue operations on this one


USE iot2000DB;

4. Create an example table


CREATE TABLE iot2000Table (insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
value FLOAT);

5. Insert a row to the table


INSERT INTO iot2000Table (value) VALUES (42);

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 12
2 Demo 1 – Install SQL server on IOT2000

No. Action

6. To receive the value you can use select to retrieve rows from the database
SELECT * FROM iot2000Table;

7. Create a user that can access the server from outside localhost
You may replace password with your own one
CREATE USER 'iot2000user' IDENTIFIED BY 'password';

8. Give the new user access to the iot2000DB


GRANT ALL ON iot2000DB.* TO 'iot2000user';

9. Close SQL console


exit
 Siemens AG 2018 All rights reserved

NOTE Make sure that you end each command with an “;”

Table 2-9 SQL statements for creating example table


SQL commands
CREATE DATABASE iot2000DB;
USE iot2000DB;
CREATE TABLE iot2000Table (insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
value FLOAT);
INSERT INTO iot2000Table (value) VALUES (42);
SELECT * FROM iot2000Table;
CREATE USER 'iot2000user' IDENTIFIED BY 'password';
GRANT ALL ON iot2000DB.* TO 'iot2000user';
exit

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 13
3 Demo 2 – SQL client with Node-RED

3 Demo 2 – SQL client with Node-RED


This chapter describes how the IOT2000 can connect to an SQL Database which is
located on a server in the network. To work through all steps you need a
connection to the IOT2000 with Putty and an Internet browser.

Figure 3-1 Node-RED as SQL client

IOT2000 Server

SQL Database

3.1 Preconditions for an SQL client with Node-RED


You need an internet connection in order to download the required MySQL node
for Node-RED that is used by this example. Additionally you need a running
 Siemens AG 2018 All rights reserved

MySQL compatible server where you can establish a connection to.


In this example we use a database with the same structure as we created
previously in
Creating an example database.
It is possible to use a non-MySQL database but then you have to install a node that
works with that specific SQL like MSSQL.

3.2 Start Node-RED


3.2.1 Auto start Node-RED on boot

Table 3-1 Auto start Node-RED on boot


No. Action
1. Type in iot2000setup to start the IOT2000 configuration program

2. Select Software

Frei verwendbar

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 14
3 Demo 2 – SQL client with Node-RED

No. Action

3. Next select Manage Autostart Options


 Siemens AG 2018 All rights reserved

4. Check the checkbox Auto start node-red with blank

5. Exit the setup


6. Restart the IOT2000 to start Node-RED with reboot or follow instructions on how
to manually start Node-RED

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 15
3 Demo 2 – SQL client with Node-RED

No. Action

NOTE It may take up to 5 minutes until complete Node-RED startup after IOT2000
power-on.

3.2.2 Manual start Node-RED

The following table shows how to start node-red manually.

Table 3-2 Manual start Node-RED


No. Action
1. Type in node /usr/lib/node_modules/node-red/red & to start node-red
 Siemens AG 2018 All rights reserved

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 16
3 Demo 2 – SQL client with Node-RED

3.3 Install required Node-RED nodes

NOTE For the next steps an online connection to the internet is needed for the
IOT2000. The needed nodes are directly downloaded from the web.

Table 3-3 Install required Node-RED nodes


No. Action
1. Add the IP-Address of the IOT2000 and the port 1880 to a browser (e.g
http://192.168.200.1:1880)

2. Open “Manage palette” which is located on the top right of the page
 Siemens AG 2018 All rights reserved

3. Search for this “mysql” and install the node “node-red-node-mysql”

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 17
3 Demo 2 – SQL client with Node-RED

3.4 Configure Node-RED dashboard

Table 3-4 Configure Node-RED dashboard


No. Action
1. Select dashboard on the right side and click on +tab to create a new tab for the
ui

2. Click on edit to rename Tab 1 to a more precise name and click on Update
 Siemens AG 2018 All rights reserved

3. Add two groups that are used for writing and reading values from and to the SQL
database

4. Rename one group to SQL Input with the default width parameter and the other
group to SQL Output with width set to 10

5. The UI can be accessed by entering for example http://192.168.200.1:1880/ui/ to


a web browser

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 18
3 Demo 2 – SQL client with Node-RED

3.5 Configure Node-RED flow

Table 3-5 Configure Node-RED flow


No. Action
1. Drag&Drop the following
nodes from the left side
to the center of the page

1x slider
1x button
3x function
2x mysql
1x text
 Siemens AG 2018 All rights reserved

2. Double click on Flow 1 to edit the name of the flow to structure it if you have multiple
flows. Save changes with a click on Done

3. Double click each of the inserted nodes to open the edit page. The values you have
to enter are shown in the following steps. You save your changes by pressing Done
4. Slider configuration

5. Button configuration

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 19
3 Demo 2 – SQL client with Node-RED

No. Action

6. Function 1 (INSERT):
msg.topic = "INSERT INTO iot2000Table (value) VALUES (" + msg.payload + ");";
return msg;
 Siemens AG 2018 All rights reserved

7. Function 2 (SELECT):
msg.topic = "SELECT * FROM iot2000Table ORDER BY insert_time DESC LIMIT 25;";
return msg;

8. Function 3 (Format)
var output = "";for (var i = 0; i < msg.payload.length; i++) {
var item = msg.payload[i];
output += `${i+1} - ${item.insert_time} --> [${item.value}]<br>`;
}

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 20
3 Demo 2 – SQL client with Node-RED

No. Action
msg.payload = output;
return msg;

9. The following steps have to be done for both mysql nodes


 Siemens AG 2018 All rights reserved

10. Enter the settings of your database

11. Text configuration

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 21
3 Demo 2 – SQL client with Node-RED

No. Action

12. Connect all node together like in the following graphic


 Siemens AG 2018 All rights reserved

13. Click on Deploy on the top right corner of the page

14. The result should look like the image below.


By moving the slider of Input value a new row with the current timestamp will be
added to the SQL database.
By pressing the Read values button the 25 newest entries will be shown in
descending order.

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 22
3 Demo 2 – SQL client with Node-RED

No. Action

15. After pressing Read values the screen may look similar to the screen below if have
moved the slide some times.
 Siemens AG 2018 All rights reserved

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 23
4 Demo 3 – SQL client with Eclipse and C++

4 Demo 3 – SQL client with Eclipse and C++


4.1 Preconditions for a SQL client with Eclipse
In this example we use a database with the same structure as we created
previously in
Creating an example database.

Figure 4-1 SQL client with Eclipse and C++

IOT2000 Server

SQL Database
C++ SQL Client

4.2 Create a new Eclipse IOT2000 project


 Siemens AG 2018 All rights reserved

Table 4-1 Create a new Eclipse IOT2000 project


No. Action
1. Create a new SIMATIC IOT2000 C++ project

2. Extract the downloaded MariaDB folder with 7zip into the newly created Eclipse
project. You can ignore any errors that occur during the unzip process. Only
include and lib folder are required.

Frei verwendbar

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 24
4 Demo 3 – SQL client with Eclipse and C++

No. Action

3. Open the lib folder that has been created.


Delete libmysqlclient.so
Rename libmysqlclient.so.18.0.0 to libmysqlclient.so.
This has to be done because Windows does not support Symbolic links to files.
 Siemens AG 2018 All rights reserved

NOTE If you modify the files outside of Eclipse it may be possible to refresh you project
by pressing F5

4.3 Edit project settings to use a shared library


To use a shared library the linker options have to be edited

Table 4-2 Edit project settings to use a shared library


No. Action
1. Select the newly created project and press Alt+Enter to open the properties
page
2. The library has to be added to the “IOT2000 C++ Linker” search path. Therefore
these parameters have to be added: (replace xx with your downloaded version)
-L"${ProjDirPath}\mariadb-5.5.xx-linux-i686\lib"
(Adds the “mariadb-5.5.xx-linux-i686\lib” folder to the search path)
-lmysqlclient (The name of the library without prefix [lib] and extension [.so].

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 25
4 Demo 3 – SQL client with Eclipse and C++

No. Action

Complete command: (replace xx with your downloaded version)


i586-poky-linux-g++ -L"${ProjDirPath}\mariadb-5.5.xx-linux-i686\lib" -lmysqlclient
 Siemens AG 2018 All rights reserved

4.4 Install libmysqlclient on the IOT2000


To install the required shared mysqlclient library you have two different options.
The first one copies the library directly to your default library folder and the second
one modifies the library search path to include the shared library from an SQL
server installation.

4.4.1 Install mysqlclient library only

Table 4-3 Install library only

1. Copy all libmysqlclient files located at the lib folder from the downloaded
MariaDB archive to /usr/lib on the IOT2000

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 26
4 Demo 3 – SQL client with Eclipse and C++

4.4.2 Install mysqlclient library from a MySQL server installation

Table 4-4 Install library from a previously installed MySQL server installation
No. Action
1. Open library configuration file by typing
nano /etc/ld.so.conf

2. Add the following line to include the lib folder of the server installation. Here we
use the path that we used in Demo 1 – Install SQL server

3. Run the command ldconfig to update the library search path


 Siemens AG 2018 All rights reserved

4.5 Creating the user program


The user program utilizes functionality of the Shared Object.
To demonstrate the SQL connection via C++ the example database created at
Creating an example database will be used.

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 27
4 Demo 3 – SQL client with Eclipse and C++

Table 4-5 Creating the user program


No. Action
1. Open the file “example.cpp” and enter the following code:
#include <stdio.h>
/* replace xx with your downloaded version */
#include "mariadb-5.5.xx-linux-i686/include/mysql/mysql.h"

int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

/* Database settings */
/* replace IP address with the IP of your IOT */

const char *server = "192.168.1.2";


const char *user = "iot2000user";
const char *password = "password";
const char *database = "iot2000DB";

conn = mysql_init(NULL);
 Siemens AG 2018 All rights reserved

/* Connect to database */
if (!mysql_real_connect(conn, server, user,
password, database, 0, NULL,
0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
return 1;
}

/* SQL query */
if (mysql_query(conn,
"SELECT * FROM iot2000Table ORDER BY
insert_time DESC LIMIT 25")) {
fprintf(stderr, "%s\n", mysql_error(conn));
return 1;
}

res = mysql_use_result(conn);

/* Output values */
printf("Values in database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s --> [%3s]\n", row[0], row[1]);

/* Close connection */
mysql_free_result(res);
mysql_close(conn);

return 0;
}

2. The program can now be started and will use the previously installed
“libmysqlclient.so”

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 28
4 Demo 3 – SQL client with Eclipse and C++

No. Action

3. The output of the program may look like the image below
 Siemens AG 2018 All rights reserved

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 29
5 Checklist

5 Checklist
This chapter contains a Checklist which summarizes all important steps in this
application example.

5.1 Demo 1 – install MariaDB server

Table 5-1 Demo 1 checklist


No. Action
1. Download MariaDB
2. Create a user to run MariaDB
3. Install MariaDB binaries
4. Secure installed MariaDB installation
5. Add MariaDB to auto start
6. Create an example database

5.2 Demo 2 – SQL client with Node-RED


 Siemens AG 2018 All rights reserved

Table 5-2 Demo 2 checklist


No. Action
1. Start Node-RED
2. Install required nodes
3. Configure Node-RED dashboard
4. Configure Node-RED flow

5.3 Demo 3 – SQL client with Eclipse and C++

Table 5-3 Demo 3 checklist


No. Action
1. Create a new Eclipse IOT2000 project
2. Edit project linker setting
3. Install libmysqlclient on the IOT2000
4. Create the user program

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 30
6 Related links

6 Related links
Table 4-1 Related links
Topic
\1\ SIMATIC IOT2000 forum
www.siemens.com/iot2000-forum
\2\ SIMATIC IOT2000 Setting Up
https://support.industry.siemens.com/tf/ww/en/posts/155642/
\3\ SIMATIC IOT2000 Getting Started
https://support.industry.siemens.com/tf/ww/en/posts/155643/

7 History
Table 5-1 History
Version Date Modifications
V1.0 05/2018 First version
 Siemens AG 2018 All rights reserved

Frei verwendbar SIMATIC IOT2000 SQL Server / Client


Version 1.0, 05/2018 31

You might also like