Problem Statement:
A fire is to be detected using relevant wireless sensor network installed in a remote location to communicate the data to the central server for the monitoring purpose and detection of the fire. Write a program to implement the system using WSN and Different data communication strategies/ algorithms (at least two) to compare the reliability of the data received and efficient timing. Use of Fort Forwarding/Tunneling Protocol is expected.
A fire is to be detected using relevant wireless sensor network installed in a remote location to communicate the data to the central server for the monitoring purpose and detection of the fire. Write a program to implement the system using WSN and Different data communication strategies/ algorithms (at least two) to compare the reliability of the data received and efficient timing. Use of Fort Forwarding/Tunneling Protocol is expected.
PROGRAM
Psqldatabase.cpp
#include "stdafx.h"
#include <string>
#include "libpq-fe.h"
/* Close connection to database */
void CloseConn(PGconn
*conn)
{
PQfinish(conn);
getchar();
exit(1);
}
/* Establish connection to database */
PGconn
*ConnectDB()
{
PGconn *conn = NULL;
conn = PQconnectdb("user=postgres password=server123 dbname=testdb hostaddr=127.0.0.1 port=5432");
if (PQstatus(conn) != CONNECTION_OK)
{
printf("Connection to database failed");
CloseConn(conn);
}
printf("Connection to database - OK\n");
return conn;
}
// Creating Sensor table
void CreateEmployeeTable(PGconn *conn)
{
// Execute with sql statement
PGresult *res = PQexec(conn, "CREATE TABLE sensor (Node char(30), Temperature char(30))");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("Create sensor table failed");
PQclear(res);
CloseConn(conn);
}
printf("Create sensor table - OK\n");
PQclear(res);
}
/* Append SQL statement and insert record into employee table */
void InsertEmployeeRec(PGconn *conn, char * Node, char * Temperature)
{
// Append the SQL statment
std::string sSQL;
sSQL.append("INSERT INTO sensor VALUES ('");
sSQL.append(Node);
sSQL.append("', '");
sSQL.append(Temperature);
sSQL.append("')");
// Execute with sql statement
PGresult *res = PQexec(conn, sSQL.c_str());
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("Insert sensor record failed");
PQclear(res);
CloseConn(conn);
}
printf("Insert sensor record - OK\n");
PQclear(res);
}
void Alarm(char * temp)
{
printf("Fire detected at : %-30s", temp );
}
// Check whether fire is detected
void CheckFire(PGconn *conn)
{
int nFields;
// Start a transaction block
PGresult *res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("BEGIN command failed");
PQclear(res);
CloseConn(conn);
}
PQclear(res);
// Fetch rows from sensor table
res = PQexec(conn, "DECLARE emprec CURSOR FOR select * from sensor");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("DECLARE CURSOR failed");
PQclear(res);
CloseConn(conn);
}
// Clear result
PQclear(res);
res = PQexec(conn, "FETCH ALL in emprec");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
printf("FETCH ALL failed");
PQclear(res);
CloseConn(conn);
}
nFields = PQnfields(res);
char * temp;
for (int i = 0; i < PQntuples(res); i++)
{
for (int j = 0; j < nFields; j++)
{
if(j == 0 )
{ temp = PQgetvalue(res, i, j); }
if(j == 1 )
{
int a = atoi(PQgetvalue(res, i, j));
if( a >= 20) { Alarm(temp); }
// Set the threshold value
// printf("%d", a);
}
printf("\n");
}
}
PQclear(res);
res = PQexec(conn, "CLOSE emprec");
PQclear(res);
res = PQexec(conn, "END");
PQclear(res);
}
/* Fetch sensor record and display it on screen */
void FetchEmployeeRec(PGconn
*conn)
{
// Will hold the number of field in sensor table
int nFields;
// Start a transaction block
PGresult *res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("BEGIN command failed");
PQclear(res);
CloseConn(conn);
}
// Clear result
PQclear(res);
// Fetch rows from sensor table
res = PQexec(conn, "DECLARE emprec CURSOR FOR select * from sensor");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("DECLARE CURSOR failed");
PQclear(res);
CloseConn(conn);
}
// Clear result
PQclear(res);
res = PQexec(conn, "FETCH ALL in emprec");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
printf("FETCH ALL failed");
PQclear(res);
CloseConn(conn);
}
// Get the field name
nFields = PQnfields(res);
// Prepare the header with sensor table field name
printf("\nFetch sensor record:");
printf("\n********************************************************************\n");
for (int i = 0; i < nFields; i++)
printf("%-30s", PQfname(res, i));
printf("\n********************************************************************\n");
// Next, print out the sensor record for each row
for (int i = 0; i < PQntuples(res); i++)
{
for (int j = 0; j < nFields; j++)
printf("%-30s", PQgetvalue(res, i, j));
printf("\n");
}
PQclear(res);
// Close the emprec
res = PQexec(conn, "CLOSE emprec");
PQclear(res);
// End the transaction
res = PQexec(conn, "END");
// Clear result
PQclear(res);
}
/* Erase all record in sensor table */
void RemoveAllEmployeeRec(PGconn
*conn)
{
// Execute with sql statement
PGresult *res = PQexec(conn, "DELETE FROM sensor");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("Delete sensor record failed.");
PQclear(res);
CloseConn(conn);
}
printf("\nDelete sensor record - OK\n");
// Clear result
PQclear(res);
}
/* Drop employee table from the database*/
void DropEmployeeTable(PGconn
*conn)
{
// Execute with sql statement
PGresult *res = PQexec(conn, "DROP TABLE sensor");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("Drop sensor table failed.");
PQclear(res);
CloseConn(conn);
}
printf("Drop sensor table - OK\n");
// Clear result
PQclear(res);
}
int _tmain(int argc, _TCHAR* argv[])
{
PGconn *conn = NULL;
conn = ConnectDB();
FetchEmployeeRec(conn);
CheckFire(conn);
printf("\nPress ENTER to remove all records & table.....\n");
getchar();
RemoveAllEmployeeRec(conn);
DropEmployeeTable(conn);
CloseConn(conn);
return 0;
}
OUTPUT
No comments:
Post a Comment