Skip to content

Commit

Permalink
countertrack implements MQTT Mosquitto client and persists data in
Browse files Browse the repository at this point in the history
sqlite3 database. Includes makefile and configuration script for using
GPIOs
  • Loading branch information
hectorcamblor committed Nov 18, 2013
1 parent 1070f0a commit 227ffe3
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 5 deletions.
10 changes: 9 additions & 1 deletion countertrack.sensor/Gait/Gait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
#include "Gait.h"
#include <cstdlib>
#include <stdio.h>

Gait::Gait() {
direction = 0;
Expand All @@ -19,10 +20,17 @@ Gait::Gait(int currentDirection, vector<int>currentSequence) {
gaitDate = time(0);
}

Gait::Gait(int currentDirection, vector<int>currentSequence, time_t currentTime) {
direction = currentDirection;
sequence = currentSequence;
gaitDate = currentTime;
}

bool Gait::sameGait(Gait lastGait) {
bool sameGait = false;

if (abs(gaitDate - lastGait.getGaitDate()) < 500) {
if (abs(gaitDate - lastGait.getGaitDate()) == 0) {
printf("Same time gait\n");
if (direction == 1 && lastGait.getDirection() == 1 &&
sequence[sequence.size()-1] > lastGait.getSequence()[lastGait.getSequence().size() -1]
) {
Expand Down
1 change: 1 addition & 0 deletions countertrack.sensor/Gait/Gait.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Gait {
public:
Gait();
Gait(int currentDirection, vector<int> currentSequence);
Gait(int currentDirection, vector<int> currentSequence, time_t currentTime);
bool sameGait(Gait lastGait);

int getDirection() const {
Expand Down
52 changes: 52 additions & 0 deletions countertrack.sensor/MQTT_Publisher/mqtt_publisher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <cstdio>
#include <cstring>
#include <ctime>
#include <string>

#include "mqtt_publisher.h"
#include <mosquittopp.h>

using namespace std;

mqtt_publisher::mqtt_publisher(const char *id, const char *host, int port, const char* publishTopic) : mosquittopp(id)
{
int keepalive = 60;
published_flag = 0;
topic = publishTopic;

/* Connect immediately. This could also be done by calling
* mqtt_tempconv->connect(). */
connect(host, port, keepalive);
};

void mqtt_publisher::on_connect(int rc)
{
printf("Connected with code %d.\n", rc);

}

void mqtt_publisher::on_publish(int rc)
{
printf("Sent\n");
published_flag = 1;
}

void mqtt_publisher::on_message(const struct mosquitto_message *message)
{

}

void mqtt_publisher::on_disconnect(int rc)
{
printf("Disconnected\n");
}

int mqtt_publisher::get_published_flag()
{
return published_flag;
}

void mqtt_publisher::set_published_flag()
{
published_flag = 0;
}
25 changes: 25 additions & 0 deletions countertrack.sensor/MQTT_Publisher/mqtt_publisher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef TEMPERATURE_CONVERSION_H
#define TEMPERATURE_CONVERSION_H

#include <mosquittopp.h>

class mqtt_publisher : public mosqpp::mosquittopp
{
public:
mqtt_publisher(const char *id, const char *host, int port, const char* publishTopic);
~mqtt_publisher();

void on_connect(int rc);
void on_publish(int rc);
void on_message(const struct mosquitto_message *message);
void on_disconnect(int rc);
int get_published_flag();
void set_published_flag();

private:
int published_flag;
const char* topic;
const char* data;
};

#endif
138 changes: 134 additions & 4 deletions countertrack.sensor/Mat/Mat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <sqlite3.h>
#include <string.h>
#include <mosquittopp.h>

using namespace std;

char* Mat::deviceName = "";

Mat::Mat () {
zoneReg = ZoneRegister();
lastGait = Gait();
getDeviceName();
publisher = new mqtt_publisher("counterPublisher", "localhost", 1883, "countertrack.jms.topic.sensor");
}

int Mat::checkActiveZones () {
Expand Down Expand Up @@ -225,24 +232,29 @@ bool Mat::checkGait() {

Gait first;
Gait second;
time_t currentTime = time(0);
// ____________________________________________________________________________________________________//

// Detección del sentido de la pisada

if (gaitSequence[gaitSequence.size() - 1] > gaitSequence[0]) {
cout << "Individuo entrando" << endl;
existsGait = true;
first = Gait(1, gaitSequence);
first = Gait(1, gaitSequence, currentTime);
if(!first.sameGait(lastGait)) {
cout << "Send" << endl;
insert(1);
mqttPublish(1);
lastGait = first;
}
} else {
cout << "Individuo saliendo" << endl;
existsGait = true;
first = Gait(0, gaitSequence);
first = Gait(0, gaitSequence, currentTime);
if(!first.sameGait(lastGait)) {
cout << "Send" << endl;
insert(0);
mqttPublish(0);
lastGait = first;
}
}
Expand All @@ -266,15 +278,15 @@ bool Mat::checkGait() {
if (differentZones >= 3) {
if (secondaryGait[counter2 - 1] > secondaryGait[0]) {
cout << "Segunda pisada entrante" << endl;
second = Gait(1, secondaryGait);
second = Gait(1, secondaryGait, currentTime);
if(!second.sameGait(lastGait)) {
cout << "Send" << endl;
lastGait = second;
}
existsGait = true;
} else {
cout << "Segunda pisada saliente" << endl;
second = Gait(0, secondaryGait);
second = Gait(0, secondaryGait, currentTime);
if (!second.sameGait(lastGait)) {
cout << "Send" << endl;
lastGait = second;
Expand All @@ -284,6 +296,124 @@ bool Mat::checkGait() {
}
}
zoneReg.reset();
cout << "_________________________________________________" << endl;
return existsGait;
}

void Mat::getDeviceName()
{
sqlite3 *db;
sqlite3_stmt *stmt;
char *zErrMsg = 0;
int rc;
char *sql;

/* Open database */
rc = sqlite3_open("/opt/database/countertrack.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stdout, "Opened database successfully\n");
}

/* Prepare SQL statement */
sql = "SELECT * FROM CONFIGURATION WHERE KEY = 'DEVICE_NAME'";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Statement prepared correctly\n");
}

const char* devicePointer;
while (sqlite3_step(stmt) == SQLITE_ROW) {
devicePointer = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
}
sqlite3_finalize(stmt);

sqlite3_close(db);

strcpy(device, devicePointer);
printf("Device name stored = %s\n", device);
}


int Mat::deviceName_callback(void *NotUsed, int argc, char **argv, char **azColName)
{
Mat::deviceName = argv[2];
return 0;
}

void Mat::insert(int direction)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char sql[100];

/* Open database */
rc = sqlite3_open("/opt/database/countertrack.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stdout, "Opened database successfully\n");
}

/* Create SQL statement */
time_t date = time(0);
sprintf(sql,
"INSERT INTO COUNTER(COUNT_DATE,DEVICE_NAME,WAY) VALUES ('%li000','%s','%i')",
date, device, direction);
printf("%s\n",sql);
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, insert_callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Insertion completed successfully\n");
}
sqlite3_close(db);
}

int Mat::insert_callback(void *NotUsed, int argc, char **argv, char **azColName)
{
return 0;
}

void Mat::mqttPublish(int direction)
{
int rc = 0;
time_t date = time(0);

char data[20];
sprintf(data, "%d-%li000", direction, date);

mosqpp::lib_init();

// Check connection
rc = publisher->loop();
if(rc) {
publisher->reconnect();
printf("Lost connection detected, reconnect\n");
}

rc = publisher->publish(NULL, "countertrack.jms.topic.sensor", strlen(data), data);

while (publisher->get_published_flag() == 0) {
rc = publisher->loop();

if(rc){
publisher->reconnect();
printf("Lost connection after publishing, reconnect\n");
break;
}
}
// Set published flag to 0
publisher->set_published_flag();

mosqpp::lib_cleanup();
}
10 changes: 10 additions & 0 deletions countertrack.sensor/Mat/Mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@

#include "../ZoneRegister/ZoneRegister.h"
#include "../Gait/Gait.h"
#include "../MQTT_Publisher/mqtt_publisher.h"

class Mat {
public:
Mat();
int checkActiveZones();
bool checkGait();
void getDeviceName();
void insert(int direction);
void mqttPublish(int direction);

private:
ZoneRegister zoneReg;
Gait lastGait;
class mqtt_publisher* publisher;
char device[10];
static char* deviceName;
static int deviceName_callback(void *NotUsed, int argc, char **argv, char **azColName);
static int insert_callback(void *NotUsed, int argc, char **argv, char **azColName);
void setDeviceName(char* device);
};

#endif /* MAT_H_ */
14 changes: 14 additions & 0 deletions countertrack.sensor/Release/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
all:
g++ -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.d" -o "src/main.o" "../src/main.cpp"

g++ -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Gait/gait.d" -MT"Gait/gait.d" -o "Gait/gait.o" "../Gait/Gait.cpp"

g++ -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"ZoneRegister/zoneRegister.d" -MT"ZoneRegister/zoneRegister.d" -o "ZoneRegister/zoneRegister.o" "../ZoneRegister/ZoneRegister.cpp"

g++ -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Mat/mat.d" -MT"Mat/mat.d" -o "Mat/mat.o" "../Mat/Mat.cpp"

g++ -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"MQTT_Publisher/mqtt_publisher.d" -MT"MQTT_Publisher/mqtt_publisher.d" -o "MQTT_Publisher/mqtt_publisher.o" "../MQTT_Publisher/mqtt_publisher.cpp"

g++ -L/usr/local/lib -o "countertrack-sensor" ./src/main.o ./Mat/mat.o ./Gait/gait.o ./ZoneRegister/zoneRegister.o ./MQTT_Publisher/mqtt_publisher.o -lmosquittopp -lsqlite3


27 changes: 27 additions & 0 deletions countertrack.sensor/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash -e

if [ ! -d /sys/class/gpio/gpio30 ]; then echo 30 > /sys/class/gpio/export; fi #P9_11
if [ ! -d /sys/class/gpio/gpio60 ]; then echo 60 > /sys/class/gpio/export; fi #P9_12
if [ ! -d /sys/class/gpio/gpio31 ]; then echo 31 > /sys/class/gpio/export; fi #P9_13
if [ ! -d /sys/class/gpio/gpio50 ]; then echo 50 > /sys/class/gpio/export; fi #P9_14
if [ ! -d /sys/class/gpio/gpio48 ]; then echo 48 > /sys/class/gpio/export; fi #P9_15
if [ ! -d /sys/class/gpio/gpio51 ]; then echo 51 > /sys/class/gpio/export; fi #P9_16
if [ ! -d /sys/class/gpio/gpio3 ]; then echo 3 > /sys/class/gpio/export; fi #P9_21
if [ ! -d /sys/class/gpio/gpio2 ]; then echo 2 > /sys/class/gpio/export; fi #P9_22
if [ ! -d /sys/class/gpio/gpio49 ]; then echo 49 > /sys/class/gpio/export; fi #P9_23
if [ ! -d /sys/class/gpio/gpio15 ]; then echo 15 > /sys/class/gpio/export; fi #P9_24

echo in > /sys/class/gpio/gpio30/direction
echo in > /sys/class/gpio/gpio60/direction
echo in > /sys/class/gpio/gpio31/direction
echo in > /sys/class/gpio/gpio50/direction
echo in > /sys/class/gpio/gpio48/direction
echo in > /sys/class/gpio/gpio51/direction
echo in > /sys/class/gpio/gpio3/direction
echo in > /sys/class/gpio/gpio2/direction
echo in > /sys/class/gpio/gpio49/direction
echo in > /sys/class/gpio/gpio15/direction




0 comments on commit 227ffe3

Please sign in to comment.