From f36bd9d69238045dadcecf406db205529c5bf875 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 8 Jun 2016 16:05:13 +0200 Subject: [PATCH] integrated mqtt client --- Makefile | 2 +- myTestModelServer.c | 139 +++++++++++++++++++++++++++++--------------- 2 files changed, 94 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 40fd2c7..255cbbd 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ TestModel.h: TestModel.c myTestModelServer.o: myTestModelServer.c TestModel.h myTestModelServer: myTestModelServer.o TestModel.o - $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $^ $(LIBS) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $^ $(LIBS) -lmosquitto .c.o: $(CC) $(CFLAGS) -c $< diff --git a/myTestModelServer.c b/myTestModelServer.c index ee862d0..0891cff 100644 --- a/myTestModelServer.c +++ b/myTestModelServer.c @@ -14,11 +14,16 @@ #include #include +#include #include "TestModel.h" -UA_Logger logger = Logger_Stdout; -UA_Boolean running = true; +/* + * Configuration + */ +#define MQTT_HOST "localhost" +#define MQTT_PORT 1883 +#define MQTT_TEMPERATURE_TOPIC "temperature" /* @@ -27,78 +32,120 @@ UA_Boolean running = true; uint32_t upTime = 0; double measuredTemperature = 0; + + + +UA_Logger logger = Logger_Stdout; +UA_Boolean running = true; + + + static void stopHandler(int sign) { - UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c"); - running = false; + UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c"); + running = false; } static UA_StatusCode readInteger(void *handle, const UA_NodeId nodeid, UA_Boolean sourceTimeStamp, const UA_NumericRange *range, UA_DataValue *dataValue) { - dataValue->hasValue = true; - UA_Variant_setScalarCopy(&dataValue->value, handle, &UA_TYPES[UA_TYPES_INT32]); - return UA_STATUSCODE_GOOD; + dataValue->hasValue = true; + UA_Variant_setScalarCopy(&dataValue->value, handle, &UA_TYPES[UA_TYPES_INT32]); + return UA_STATUSCODE_GOOD; } static UA_StatusCode readDouble(void *handle, const UA_NodeId nodeid, UA_Boolean sourceTimeStamp, - const UA_NumericRange *range, UA_DataValue *dataValue) { - dataValue->hasValue = true; - UA_Variant_setScalarCopy(&dataValue->value, handle, &UA_TYPES[UA_TYPES_DOUBLE]); - return UA_STATUSCODE_GOOD; + const UA_NumericRange *range, UA_DataValue *dataValue) { + dataValue->hasValue = true; + UA_Variant_setScalarCopy(&dataValue->value, handle, &UA_TYPES[UA_TYPES_DOUBLE]); + return UA_STATUSCODE_GOOD; } +void connect_callback(struct mosquitto *mosq, void *obj, int result) { +} + +void message_callback(struct mosquitto *mosq, void *obj, + const struct mosquitto_message *message) { + UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "received mqtt message with topic %s", + message->topic); + if (0 == strcmp(message->topic, MQTT_TEMPERATURE_TOPIC)) { + UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "this is about the temperature"); + measuredTemperature = atof((char*)message->payload); + } +} + +uint64_t millis() { + struct timeval te; + gettimeofday(&te, NULL); + uint64_t ms = te.tv_sec * 1000 + te.tv_usec / 1000; + return ms; +} + int main(int argc, char** argv) { - signal(SIGINT, stopHandler); /* catches ctrl-c */ + signal(SIGINT, stopHandler); /* catches ctrl-c */ - /* initialize the server */ - UA_ServerConfig config = UA_ServerConfig_standard; - UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664); - config.networkLayers = &nl; - config.networkLayersSize = 1; - UA_Server *server = UA_Server_new(config); + /* initialize the server */ + UA_ServerConfig config = UA_ServerConfig_standard; + UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664); + config.networkLayers = &nl; + config.networkLayersSize = 1; + UA_Server *server = UA_Server_new(config); - /* create nodes from nodeset */ - TestModel(server); + /* create nodes from nodeset */ + TestModel(server); - UA_DataSource uptimeDataSource = (UA_DataSource) { - .handle = &upTime, .read = readInteger, .write = 0}; - UA_Server_setVariableNode_dataSource(server, UA_NODEID_NUMERIC(2, 6006), - uptimeDataSource); + UA_DataSource uptimeDataSource = (UA_DataSource) { + .handle = &upTime, .read = readInteger, .write = 0}; + UA_Server_setVariableNode_dataSource(server, UA_NODEID_NUMERIC(2, 6006), + uptimeDataSource); - UA_DataSource measuredTemperatureDataSource = (UA_DataSource) { - .handle = &measuredTemperature, .read = readDouble, .write = 0}; - UA_Server_setVariableNode_dataSource(server, UA_NODEID_NUMERIC(2, 6004), - measuredTemperatureDataSource); + UA_DataSource measuredTemperatureDataSource = (UA_DataSource) { + .handle = &measuredTemperature, .read = readDouble, .write = 0}; + UA_Server_setVariableNode_dataSource(server, UA_NODEID_NUMERIC(2, 6004), + measuredTemperatureDataSource); - /* - UA_StatusCode retval = UA_Server_run(server, &running); //UA_blocks until running=false - */ + UA_StatusCode retval = UA_Server_run_startup(server); + - UA_StatusCode retval = UA_Server_run_startup(server); - if (retval == UA_STATUSCODE_GOOD) { + mosquitto_lib_init(); + + char mqttClientId[24]; + struct mosquitto *mosq; + mosq = mosquitto_new(mqttClientId, true, 0); + mosquitto_connect_callback_set(mosq, connect_callback); + mosquitto_message_callback_set(mosq, message_callback); + mosquitto_connect(mosq, MQTT_HOST, MQTT_PORT, 60); + mosquitto_subscribe(mosq, NULL, MQTT_TEMPERATURE_TOPIC, 0); + + + if (retval == UA_STATUSCODE_GOOD) { - while (running == true) { - upTime = time(0); - measuredTemperature += 0.25; + while (running == true) { + upTime = time(0); + // measuredTemperature += 0.25; - uint16_t canWait = 0; + uint16_t canWait = 0; - canWait = UA_Server_run_iterate(server, 0); - // UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "canWait: %i", canWait); + canWait = UA_Server_run_iterate(server, 0); + // UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "canWait: %i", canWait); - struct timeval timeout = { .tv_sec = 0, .tv_usec = canWait * 1000 }; - select(0, 0, 0, 0, &timeout); - } + uint64_t beforeMosquittoLoop = millis(); + mosquitto_loop(mosq, 0, 1); + uint64_t durationMosquittoLoop = millis() - beforeMosquittoLoop; + canWait = (canWait > durationMosquittoLoop) ? (canWait - durationMosquittoLoop) : 0; + + struct timeval timeout = { .tv_sec = 0, .tv_usec = canWait * 1000 }; + select(0, 0, 0, 0, &timeout); } + } - /* ctrl-c received -> clean up */ - UA_Server_delete(server); - nl.deleteMembers(&nl); - // return (int)retval; + /* ctrl-c received -> clean up */ + UA_Server_delete(server); + nl.deleteMembers(&nl); + // return (int)retval; }