Fire- Detection Program
1.
Install PostgreSQL
database.
2.
After
installation start Command Prompt and go to PostgreSQL bin folder and start
postgresql server i.e. :
C:\Programfiles\Postgresql\9.3\bin >psql
template1 postgresql
3.
It will ask for
password authentication which provided during the installation steps.
Password for userpostgres: ________(DB pwd)
Output:
template1-#
4.
Creating
database by:
create
database testdb(your DB name)
Output: CREATE DATABASE
template-1# _
5.
Configuring
libraries and header files path in Visual Studio:
Ø Right click on
project folder
Ø In C/C++ > General > Additional Include
Directories >C:\Program Files\PostgreSQL\9.3\include
Ø In Linker >Additional Include Directories >C:\Program
Files\PostgreSQL\9.3\lib
Ø In Linker > Enable Incremental Linking > No
6.
Program :
7.
#include"stdafx.h"
8.
#include<string>
9.
#include"libpq-fe.h"
10.
11.
12.
/* Close connection to database */
13.
voidCloseConn(PGconn *conn)
14.
{
15.
PQfinish(conn);
16.
getchar();
17.
exit(1);
18.
}
19.
20.
/* Establish connection to database */
21.
PGconn *ConnectDB()
22.
{
23.
PGconn *conn
= NULL;
24.
25.
conn =
PQconnectdb("user=postgres password=server123
dbname=testdbhostaddr=127.0.0.1 port=5432");
26.
27.
if (PQstatus(conn) != CONNECTION_OK)
28.
{
29.
printf("Connection to
database failed");
30.
CloseConn(conn);
31.
}
32.
33.
printf("Connection to database - OK\n");
34.
35.
return conn;
36.
}
37.
38.
// Creating Sensor table
39.
voidCreateEmployeeTable(PGconn *conn)
40.
{
41.
// Execute with sql statement
42.
PGresult *res = PQexec(conn, "CREATE
TABLE sensor (Node char(30), Temperature char(30))");
43.
44.
if (PQresultStatus(res) != PGRES_COMMAND_OK)
45.
{
46.
printf("Create sensor
table failed");
47.
PQclear(res);
48.
CloseConn(conn);
49.
}
50.
51.
printf("Create sensor table - OK\n");
52.
53.
PQclear(res);
54.
}
55.
56.
/* Append SQL statement and insert record
into employee table */
57.
voidInsertEmployeeRec(PGconn *conn, char * Node, char *
Temperature)
58.
{
59.
// Append the SQL statment
60.
std::string
sSQL;
61.
sSQL.append("INSERT INTO sensor VALUES ('");
62.
sSQL.append(Node);
63.
sSQL.append("', '");
64.
sSQL.append(Temperature);
65.
sSQL.append("')");
66.
67.
// Execute with sql statement
68.
PGresult *res
= PQexec(conn, sSQL.c_str());
69.
70.
if (PQresultStatus(res) != PGRES_COMMAND_OK)
71.
{
72.
printf("Insert sensor
record failed");
73.
PQclear(res);
74.
CloseConn(conn);
75.
}
76.
77.
printf("Insert sensor record - OK\n");
78.
79.
PQclear(res);
80.
}
81.
82.
83.
void Alarm(char * temp)
84.
{
85.
printf("Fire detected at : %-30s", temp );
86.
}
87.
88.
// Check whether fire is detected
89.
voidCheckFire(PGconn *conn)
90.
{
91.
intnFields;
92.
93.
// Start a transaction block
94.
PGresult
*res = PQexec(conn, "BEGIN");
95.
96.
if (PQresultStatus(res) != PGRES_COMMAND_OK)
97.
{
98.
printf("BEGIN command
failed");
99.
PQclear(res);
100.
CloseConn(conn);
101.
}
102.
PQclear(res);
103.
// Fetch rows from sensor table
104.
res =
PQexec(conn, "DECLARE emprec CURSOR FOR select
* from sensor");
105.
if (PQresultStatus(res) != PGRES_COMMAND_OK)
106.
{
107.
printf("DECLARE CURSOR
failed");
108.
PQclear(res);
109.
CloseConn(conn);
110.
}
111.
// Clear result
112.
PQclear(res);
113.
res =
PQexec(conn, "FETCH ALL in emprec");
114.
115.
if (PQresultStatus(res) != PGRES_TUPLES_OK)
116.
{
117.
printf("FETCH ALL
failed");
118.
PQclear(res);
119.
CloseConn(conn);
120.
}
121.
122.
nFields = PQnfields(res);
123.
char * temp;
124.
for (int i = 0; i
<PQntuples(res); i++)
125.
{
126.
for (int j = 0; j
<nFields; j++)
127.
{
128.
if(j == 0 )
129.
{ temp = PQgetvalue(res, i, j); }
130.
if(j == 1 )
131.
{
132.
int a = atoi(PQgetvalue(res, i, j));
133.
if( a >= 20) { Alarm(temp); } // Set the
threshold value
134.
// printf("%d", a);
135.
}
136.
printf("\n");
137.
}
138.
}
139.
PQclear(res);
140.
res =
PQexec(conn, "CLOSE emprec");
141.
PQclear(res);
142.
res =
PQexec(conn, "END");
143.
PQclear(res);
144.
}
145.
146.
147.
/* Fetch sensor record and display it on
screen */
148.
voidFetchEmployeeRec(PGconn *conn)
149.
{
150.
// Will hold the number of field in sensor table
151.
intnFields;
152.
153.
// Start a transaction block
154.
PGresult
*res = PQexec(conn, "BEGIN");
155.
156.
if (PQresultStatus(res) != PGRES_COMMAND_OK)
157.
{
158.
printf("BEGIN command
failed");
159.
PQclear(res);
160.
CloseConn(conn);
161.
}
162.
163.
// Clear result
164.
PQclear(res);
165.
166.
// Fetch rows from sensor table
167.
res =
PQexec(conn, "DECLARE emprec CURSOR FOR select
* from sensor");
168.
if (PQresultStatus(res) != PGRES_COMMAND_OK)
169.
{
170.
printf("DECLARE CURSOR
failed");
171.
PQclear(res);
172.
CloseConn(conn);
173.
}
174.
175.
// Clear result
176.
PQclear(res);
177.
178.
res =
PQexec(conn, "FETCH ALL in emprec");
179.
180.
if (PQresultStatus(res) != PGRES_TUPLES_OK)
181.
{
182.
printf("FETCH ALL
failed");
183.
PQclear(res);
184.
CloseConn(conn);
185.
}
186.
187.
// Get the field name
188.
nFields = PQnfields(res);
189.
190.
// Prepare the header with sensor table field name
191.
printf("\nFetch sensor record:");
192.
printf("\n********************************************************************\n");
193.
for (int i = 0; i
<nFields; i++)
194.
printf("%-30s",
PQfname(res, i));
195.
printf("\n********************************************************************\n");
196.
197.
// Next, print out the sensor record for each
row
198.
for (int i = 0; i
<PQntuples(res); i++)
199.
{
200.
for (int j = 0; j
<nFields; j++)
201.
printf("%-30s",
PQgetvalue(res, i, j));
202.
printf("\n");
203.
}
204.
205.
PQclear(res);
206.
207.
// Close the emprec
208.
res =
PQexec(conn, "CLOSE emprec");
209.
PQclear(res);
210.
211.
// End the transaction
212.
res =
PQexec(conn, "END");
213.
214.
// Clear result
215.
PQclear(res);
216.
}
217.
218.
/* Erase all record in sensor table */
219.
voidRemoveAllEmployeeRec(PGconn *conn)
220.
{
221.
// Execute with sql statement
222.
PGresult *res = PQexec(conn, "DELETE
FROM sensor");
223.
224.
if (PQresultStatus(res) != PGRES_COMMAND_OK)
225.
{
226.
printf("Delete sensor record
failed.");
227.
PQclear(res);
228.
CloseConn(conn);
229.
}
230.
231.
printf("\nDelete sensor record - OK\n");
232.
233.
// Clear result
234.
PQclear(res);
235.
}
236.
237.
/* Drop employee table from the database*/
238.
voidDropEmployeeTable(PGconn *conn)
239.
{
240.
// Execute with sql statement
241.
PGresult *res = PQexec(conn, "DROP
TABLE sensor");
242.
243.
if (PQresultStatus(res) != PGRES_COMMAND_OK)
244.
{
245.
printf("Drop sensor
table failed.");
246.
PQclear(res);
247.
CloseConn(conn);
248.
}
249.
250.
printf("Drop sensor table - OK\n");
251.
252.
// Clear result
253.
PQclear(res);
254.
}
255.
256.
257.
258.
int _tmain(intargc,
_TCHAR* argv[])
259.
{
260.
PGconn *conn = NULL;
261.
262.
conn =
ConnectDB();
263.
//
CreateEmployeeTable(conn);
264.
// InsertEmployeeRec(conn, "Node-3", 26);
265.
// InsertEmployeeRec(conn, "Node-4", 43);
266.
FetchEmployeeRec(conn);
267.
CheckFire(conn);
268.
printf("\nPress ENTER to remove all records &
table.....\n");
269.
getchar();
270.
271.
272.
RemoveAllEmployeeRec(conn);
273.
DropEmployeeTable(conn);
274.
275.
CloseConn(conn);
276.
277.
return 0;
278.
}
279.
280.