From 227ffe371d1e1539e81efa7b83ae940178d04cc5 Mon Sep 17 00:00:00 2001 From: hector Date: Mon, 18 Nov 2013 12:11:13 +0100 Subject: [PATCH] countertrack implements MQTT Mosquitto client and persists data in sqlite3 database. Includes makefile and configuration script for using GPIOs --- countertrack.sensor/Gait/Gait.cpp | 10 +- countertrack.sensor/Gait/Gait.h | 1 + .../MQTT_Publisher/mqtt_publisher.cpp | 52 +++++++ .../MQTT_Publisher/mqtt_publisher.h | 25 ++++ countertrack.sensor/Mat/Mat.cpp | 138 +++++++++++++++++- countertrack.sensor/Mat/Mat.h | 10 ++ countertrack.sensor/Release/Makefile | 14 ++ countertrack.sensor/config.sh | 27 ++++ 8 files changed, 272 insertions(+), 5 deletions(-) create mode 100644 countertrack.sensor/MQTT_Publisher/mqtt_publisher.cpp create mode 100644 countertrack.sensor/MQTT_Publisher/mqtt_publisher.h create mode 100644 countertrack.sensor/Release/Makefile create mode 100755 countertrack.sensor/config.sh diff --git a/countertrack.sensor/Gait/Gait.cpp b/countertrack.sensor/Gait/Gait.cpp index 5aeab3a..9f4cc26 100644 --- a/countertrack.sensor/Gait/Gait.cpp +++ b/countertrack.sensor/Gait/Gait.cpp @@ -6,6 +6,7 @@ */ #include "Gait.h" #include +#include Gait::Gait() { direction = 0; @@ -19,10 +20,17 @@ Gait::Gait(int currentDirection, vectorcurrentSequence) { gaitDate = time(0); } +Gait::Gait(int currentDirection, vectorcurrentSequence, 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] ) { diff --git a/countertrack.sensor/Gait/Gait.h b/countertrack.sensor/Gait/Gait.h index e3f79d3..3be5a83 100644 --- a/countertrack.sensor/Gait/Gait.h +++ b/countertrack.sensor/Gait/Gait.h @@ -16,6 +16,7 @@ class Gait { public: Gait(); Gait(int currentDirection, vector currentSequence); + Gait(int currentDirection, vector currentSequence, time_t currentTime); bool sameGait(Gait lastGait); int getDirection() const { diff --git a/countertrack.sensor/MQTT_Publisher/mqtt_publisher.cpp b/countertrack.sensor/MQTT_Publisher/mqtt_publisher.cpp new file mode 100644 index 0000000..b84ecc5 --- /dev/null +++ b/countertrack.sensor/MQTT_Publisher/mqtt_publisher.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +#include "mqtt_publisher.h" +#include + +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; +} diff --git a/countertrack.sensor/MQTT_Publisher/mqtt_publisher.h b/countertrack.sensor/MQTT_Publisher/mqtt_publisher.h new file mode 100644 index 0000000..ff3509e --- /dev/null +++ b/countertrack.sensor/MQTT_Publisher/mqtt_publisher.h @@ -0,0 +1,25 @@ +#ifndef TEMPERATURE_CONVERSION_H +#define TEMPERATURE_CONVERSION_H + +#include + +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 diff --git a/countertrack.sensor/Mat/Mat.cpp b/countertrack.sensor/Mat/Mat.cpp index f4900a9..051a7bc 100644 --- a/countertrack.sensor/Mat/Mat.cpp +++ b/countertrack.sensor/Mat/Mat.cpp @@ -8,12 +8,19 @@ #include #include #include +#include +#include +#include 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 () { @@ -225,6 +232,7 @@ bool Mat::checkGait() { Gait first; Gait second; + time_t currentTime = time(0); // ____________________________________________________________________________________________________// // Detección del sentido de la pisada @@ -232,17 +240,21 @@ bool Mat::checkGait() { 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; } } @@ -266,7 +278,7 @@ 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; @@ -274,7 +286,7 @@ bool Mat::checkGait() { 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; @@ -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(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(); +} diff --git a/countertrack.sensor/Mat/Mat.h b/countertrack.sensor/Mat/Mat.h index 65deac7..ace477e 100644 --- a/countertrack.sensor/Mat/Mat.h +++ b/countertrack.sensor/Mat/Mat.h @@ -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_ */ diff --git a/countertrack.sensor/Release/Makefile b/countertrack.sensor/Release/Makefile new file mode 100644 index 0000000..ffdf734 --- /dev/null +++ b/countertrack.sensor/Release/Makefile @@ -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 + + diff --git a/countertrack.sensor/config.sh b/countertrack.sensor/config.sh new file mode 100755 index 0000000..0d4fa98 --- /dev/null +++ b/countertrack.sensor/config.sh @@ -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 + + + +