Added preliminary support for sending telemetry data to local mqtt server
This commit is contained in:
parent
04656badb3
commit
8c8febe27b
|
|
@ -17,6 +17,7 @@ set(CMAKE_BUILD_TYPE Debug)
|
||||||
project(gilles)
|
project(gilles)
|
||||||
|
|
||||||
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--no-as-needed -ldl")
|
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--no-as-needed -ldl")
|
||||||
|
find_package(eclipse-paho-mqtt-c REQUIRED)
|
||||||
|
|
||||||
add_subdirectory(src/gilles/gameloop)
|
add_subdirectory(src/gilles/gameloop)
|
||||||
add_subdirectory(src/gilles/simulatorapi)
|
add_subdirectory(src/gilles/simulatorapi)
|
||||||
|
|
@ -24,7 +25,7 @@ add_subdirectory(src/gilles/helper)
|
||||||
add_subdirectory(src/gilles/slog)
|
add_subdirectory(src/gilles/slog)
|
||||||
|
|
||||||
add_executable(gilles src/gilles/gilles.c)
|
add_executable(gilles src/gilles/gilles.c)
|
||||||
target_link_libraries(gilles m ${LIBUSB_LIBRARY} ncurses argtable2 config gameloop helper slog simulatorapi)
|
target_link_libraries(gilles m ncurses argtable2 config gameloop helper slog simulatorapi eclipse-paho-mqtt-c::paho-mqtt3c)
|
||||||
|
|
||||||
|
|
||||||
# used for enabling additional compiler options if supported
|
# used for enabling additional compiler options if supported
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <MQTTClient.h>
|
||||||
|
|
||||||
#include "gameloop.h"
|
#include "gameloop.h"
|
||||||
#include "../helper/parameters.h"
|
#include "../helper/parameters.h"
|
||||||
|
|
@ -12,6 +14,13 @@
|
||||||
|
|
||||||
#define DEFAULT_UPDATE_RATE 100
|
#define DEFAULT_UPDATE_RATE 100
|
||||||
|
|
||||||
|
#define ADDRESS "tcp://localhost:1883"
|
||||||
|
#define CLIENTID "gilles"
|
||||||
|
#define TOPIC "telemetry"
|
||||||
|
//#define PAYLOAD "Hello, MQTT!"
|
||||||
|
#define QOS 0
|
||||||
|
#define TIMEOUT 10000L
|
||||||
|
|
||||||
WINDOW* win1;
|
WINDOW* win1;
|
||||||
WINDOW* win2;
|
WINDOW* win2;
|
||||||
WINDOW* win3;
|
WINDOW* win3;
|
||||||
|
|
@ -82,7 +91,7 @@ int curses_init()
|
||||||
box(win4, 0, 0);
|
box(win4, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int looper(Simulator simulator)
|
int looper(Simulator simulator, Parameters* p)
|
||||||
{
|
{
|
||||||
|
|
||||||
SimData* simdata = malloc(sizeof(SimData));
|
SimData* simdata = malloc(sizeof(SimData));
|
||||||
|
|
@ -100,11 +109,56 @@ int looper(Simulator simulator)
|
||||||
|
|
||||||
timeout(DEFAULT_UPDATE_RATE);
|
timeout(DEFAULT_UPDATE_RATE);
|
||||||
|
|
||||||
|
bool mqtt = p->mqtt;
|
||||||
|
bool mqtt_connected = false;
|
||||||
|
|
||||||
|
MQTTClient client;
|
||||||
|
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
|
||||||
|
MQTTClient_message pubmsg = MQTTClient_message_initializer;
|
||||||
|
MQTTClient_deliveryToken token;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
// Create a new MQTT client
|
||||||
|
MQTTClient_create(&client, ADDRESS, CLIENTID,
|
||||||
|
MQTTCLIENT_PERSISTENCE_NONE, NULL);
|
||||||
|
|
||||||
|
// Connect to the MQTT server
|
||||||
|
if (mqtt == true)
|
||||||
|
{
|
||||||
|
conn_opts.keepAliveInterval = 20;
|
||||||
|
conn_opts.cleansession = 1;
|
||||||
|
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
|
||||||
|
MQTTClient_disconnect(client, 10000);
|
||||||
|
sloge("Failed to connect, return code %d", rc);
|
||||||
|
//exit(-1);
|
||||||
|
}
|
||||||
|
mqtt_connected = true;
|
||||||
|
}
|
||||||
|
|
||||||
int go = true;
|
int go = true;
|
||||||
while (go == true)
|
while (go == true)
|
||||||
{
|
{
|
||||||
simdatamap(simdata, simmap, simulator);
|
simdatamap(simdata, simmap, simulator);
|
||||||
|
|
||||||
|
if (mqtt_connected == true)
|
||||||
|
{
|
||||||
|
char payloads[6][20];
|
||||||
|
sprintf(payloads[0], "gas, lap=%i, %04f", simdata->lap, simdata->gas);
|
||||||
|
sprintf(payloads[1], "brake, lap=%i, %04f", simdata->lap, simdata->brake);
|
||||||
|
sprintf(payloads[2], "steer, lap=%i, %04f", simdata->lap, simdata->brake);
|
||||||
|
sprintf(payloads[3], "gear, lap=%i, %04i", simdata->lap, simdata->gear);
|
||||||
|
sprintf(payloads[4], "speed, lap=%i, %04i", simdata->lap, simdata->velocity);
|
||||||
|
|
||||||
|
for (int k =0; k < 6; k++)
|
||||||
|
{
|
||||||
|
pubmsg.payload = payloads[k];
|
||||||
|
pubmsg.payloadlen = strlen(payloads[k]);
|
||||||
|
pubmsg.qos = QOS;
|
||||||
|
pubmsg.retained = 0;
|
||||||
|
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wclear(win1);
|
wclear(win1);
|
||||||
wclear(win2);
|
wclear(win2);
|
||||||
wclear(win3);
|
wclear(win3);
|
||||||
|
|
@ -505,6 +559,12 @@ int looper(Simulator simulator)
|
||||||
delwin(win1);
|
delwin(win1);
|
||||||
endwin();
|
endwin();
|
||||||
|
|
||||||
|
if (mqtt_connected == true)
|
||||||
|
{
|
||||||
|
MQTTClient_disconnect(client, 10000);
|
||||||
|
}
|
||||||
|
MQTTClient_destroy(&client);
|
||||||
|
|
||||||
free(simdata);
|
free(simdata);
|
||||||
free(simmap);
|
free(simmap);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "../helper/parameters.h"
|
#include "../helper/parameters.h"
|
||||||
#include "../helper/confighelper.h"
|
#include "../helper/confighelper.h"
|
||||||
|
|
||||||
int looper (Simulator simulator);
|
int looper (Simulator simulator, Parameters* p);
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ int main(int argc, char** argv)
|
||||||
config_setting_lookup_float(config_device, "thresh2", &m->thresh2);
|
config_setting_lookup_float(config_device, "thresh2", &m->thresh2);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
looper(1);
|
looper(1, p);
|
||||||
|
|
||||||
free(config_file_str);
|
free(config_file_str);
|
||||||
free(cache_dir_str);
|
free(cache_dir_str);
|
||||||
|
|
|
||||||
|
|
@ -15,21 +15,21 @@ ConfigError getParameters(int argc, char** argv, Parameters* p)
|
||||||
|
|
||||||
// set return structure defaults
|
// set return structure defaults
|
||||||
p->program_action = 0;
|
p->program_action = 0;
|
||||||
p->max_revs = 0;
|
p->mqtt = false;
|
||||||
p->verbosity_count = 0;
|
p->verbosity_count = 0;
|
||||||
|
|
||||||
// setup argument handling structures
|
// setup argument handling structures
|
||||||
const char* progname = "csimtelem";
|
const char* progname = "gilles";
|
||||||
|
|
||||||
struct arg_lit* arg_verbosity1 = arg_litn("v","verbose", 0, 2, "increase logging verbosity");
|
struct arg_lit* arg_verbosity = arg_litn("v","verbose", 0, 2, "increase logging verbosity");
|
||||||
struct arg_lit* arg_verbosity2 = arg_litn("v","verbose", 0, 2, "increase logging verbosity");
|
|
||||||
|
|
||||||
struct arg_rex* cmd1 = arg_rex1(NULL, NULL, "play", NULL, REG_ICASE, NULL);
|
struct arg_rex* cmd1 = arg_rex1(NULL, NULL, "play", NULL, REG_ICASE, NULL);
|
||||||
struct arg_str* arg_sim = arg_strn("s", "sim", "<gamename>", 0, 1, NULL);
|
struct arg_str* arg_sim = arg_strn("s", "sim", "<gamename>", 0, 1, NULL);
|
||||||
|
struct arg_lit* arg_mqtt = arg_lit0("S", NULL, "send data to local mqtt server");
|
||||||
struct arg_lit* help = arg_litn(NULL,"help", 0, 1, "print this help and exit");
|
struct arg_lit* help = arg_litn(NULL,"help", 0, 1, "print this help and exit");
|
||||||
struct arg_lit* vers = arg_litn(NULL,"version", 0, 1, "print version information and exit");
|
struct arg_lit* vers = arg_litn(NULL,"version", 0, 1, "print version information and exit");
|
||||||
struct arg_end* end = arg_end(20);
|
struct arg_end* end = arg_end(20);
|
||||||
void* argtable[] = {cmd1,arg_sim,arg_verbosity1,help,vers,end};
|
void* argtable[] = {cmd1,arg_sim,arg_verbosity,arg_mqtt,help,vers,end};
|
||||||
int nerrors;
|
int nerrors;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -46,7 +46,11 @@ ConfigError getParameters(int argc, char** argv, Parameters* p)
|
||||||
{
|
{
|
||||||
p->program_action = A_PLAY;
|
p->program_action = A_PLAY;
|
||||||
p->sim_string = arg_sim->sval[0];
|
p->sim_string = arg_sim->sval[0];
|
||||||
p->verbosity_count = arg_verbosity1->count;
|
p->verbosity_count = arg_verbosity->count;
|
||||||
|
if (arg_mqtt->count > 0)
|
||||||
|
{
|
||||||
|
p->mqtt = true;
|
||||||
|
}
|
||||||
exitcode = E_SUCCESS_AND_DO;
|
exitcode = E_SUCCESS_AND_DO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
#ifndef _PARAMETERS_H
|
#ifndef _PARAMETERS_H
|
||||||
#define _PARAMETERS_H
|
#define _PARAMETERS_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int program_action;
|
int program_action;
|
||||||
const char* sim_string;
|
const char* sim_string;
|
||||||
const char* save_file;
|
bool mqtt;
|
||||||
int max_revs;
|
|
||||||
int granularity;
|
|
||||||
int verbosity_count;
|
int verbosity_count;
|
||||||
}
|
}
|
||||||
Parameters;
|
Parameters;
|
||||||
|
|
@ -15,8 +15,6 @@ Parameters;
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
A_PLAY = 0,
|
A_PLAY = 0,
|
||||||
A_CONFIG_TACH = 1,
|
|
||||||
A_CONFIG_SHAKER = 2
|
|
||||||
}
|
}
|
||||||
ProgramAction;
|
ProgramAction;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue