Initial contribution.

This commit is contained in:
Roger Light
2014-05-07 23:27:00 +01:00
commit 0364bd1be7
372 changed files with 39627 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
CFLAGS=-Wall -ggdb
LDFLAGS=../../lib/libmosquitto.so.1 -lmysqlclient
.PHONY: all clean
all : mosquitto_mysql_log
mosquitto_mysql_log : mysql_log.o
${CC} $^ -o $@ ${LDFLAGS}
mysql_log.o : mysql_log.c
${CC} -c $^ -o $@ ${CFLAGS} -I../../lib
clean :
-rm -f *.o mosquitto_mysql_log

View File

@@ -0,0 +1,118 @@
#include <signal.h>
#include <stdio.h>
#include <string.h>
#ifndef WIN32
# include <unistd.h>
#else
# include <process.h>
# define snprintf sprintf_s
#endif
#include <mosquitto.h>
#include <mysql/mysql.h>
#define db_host "localhost"
#define db_username "mqtt_log"
#define db_password "password"
#define db_database "mqtt_log"
#define db_port 3306
#define db_query "INSERT INTO mqtt_log (topic, payload) VALUES (?,?)"
#define mqtt_host "localhost"
#define mqtt_port 1883
static int run = 1;
static MYSQL_STMT *stmt = NULL;
void handle_signal(int s)
{
run = 0;
}
void connect_callback(struct mosquitto *mosq, void *obj, int result)
{
}
void message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
MYSQL_BIND bind[2];
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = message->topic;
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].buffer = message->payload;
mysql_stmt_bind_param(stmt, bind);
mysql_stmt_execute(stmt);
}
int main(int argc, char *argv[])
{
MYSQL *connection;
my_bool reconnect = true;
char clientid[24];
struct mosquitto *mosq;
int rc = 0;
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
mysql_library_init(0, NULL, NULL);
mosquitto_lib_init();
connection = mysql_init(NULL);
if(connection){
mysql_options(connection, MYSQL_OPT_RECONNECT, &reconnect);
connection = mysql_real_connect(connection, db_host, db_username, db_password, db_database, db_port, NULL, 0);
if(connection){
stmt = mysql_stmt_init(connection);
mysql_stmt_prepare(stmt, db_query, strlen(db_query));
memset(clientid, 0, 24);
snprintf(clientid, 23, "mysql_log_%d", getpid());
mosq = mosquitto_new(clientid, true, connection);
if(mosq){
mosquitto_connect_callback_set(mosq, connect_callback);
mosquitto_message_callback_set(mosq, message_callback);
rc = mosquitto_connect(mosq, mqtt_host, mqtt_port, 60);
mosquitto_subscribe(mosq, NULL, "#", 0);
while(run){
rc = mosquitto_loop(mosq, -1, 1);
if(run && rc){
sleep(20);
mosquitto_reconnect(mosq);
}
}
mosquitto_destroy(mosq);
}
mysql_stmt_close(stmt);
mysql_close(connection);
}else{
fprintf(stderr, "Error: Unable to connect to database.\n");
printf("%s\n", mysql_error(connection));
rc = 1;
}
}else{
fprintf(stderr, "Error: Unable to start mysql.\n");
rc = 1;
}
mysql_library_end();
mosquitto_lib_cleanup();
return rc;
}

View File

@@ -0,0 +1,18 @@
CFLAGS=-Wall -ggdb -I../../lib -I../../lib/cpp
LDFLAGS=-L../../lib ../../lib/cpp/libmosquittopp.so.1 ../../lib/libmosquitto.so.1
.PHONY: all clean
all : mqtt_temperature_conversion
mqtt_temperature_conversion : main.o temperature_conversion.o
${CXX} $^ -o $@ ${LDFLAGS}
main.o : main.cpp
${CXX} -c $^ -o $@ ${CFLAGS}
temperature_conversion.o : temperature_conversion.cpp
${CXX} -c $^ -o $@ ${CFLAGS}
clean :
-rm -f *.o mqtt_temperature_conversion

View File

@@ -0,0 +1,23 @@
#include "temperature_conversion.h"
int main(int argc, char *argv[])
{
class mqtt_tempconv *tempconv;
int rc;
mosqpp::lib_init();
tempconv = new mqtt_tempconv("tempconv", "localhost", 1883);
while(1){
rc = tempconv->loop();
if(rc){
tempconv->reconnect();
}
}
mosqpp::lib_cleanup();
return 0;
}

View File

@@ -0,0 +1,6 @@
This is a simple example of the C++ library mosquittopp.
It is a client that subscribes to the topic temperature/celsius which should
have temperature data in text form being published to it. It reads this data as
a Celsius temperature, converts to Farenheit and republishes on
temperature/farenheit.

View File

@@ -0,0 +1,45 @@
#include <cstdio>
#include <cstring>
#include "temperature_conversion.h"
#include <mosquittopp.h>
mqtt_tempconv::mqtt_tempconv(const char *id, const char *host, int port) : mosquittopp(id)
{
int keepalive = 60;
/* Connect immediately. This could also be done by calling
* mqtt_tempconv->connect(). */
connect(host, port, keepalive);
};
void mqtt_tempconv::on_connect(int rc)
{
printf("Connected with code %d.\n", rc);
if(rc == 0){
/* Only attempt to subscribe on a successful connect. */
subscribe(NULL, "temperature/celsius");
}
}
void mqtt_tempconv::on_message(const struct mosquitto_message *message)
{
double temp_celsius, temp_farenheit;
char buf[51];
if(!strcmp(message->topic, "temperature/celsius")){
memset(buf, 0, 51*sizeof(char));
/* Copy N-1 bytes to ensure always 0 terminated. */
memcpy(buf, message->payload, 50*sizeof(char));
temp_celsius = atof(buf);
temp_farenheit = temp_celsius*9.0/5.0 + 32.0;
snprintf(buf, 50, "%f", temp_farenheit);
publish(NULL, "temperature/farenheit", strlen(buf), buf);
}
}
void mqtt_tempconv::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
printf("Subscription succeeded.\n");
}

View File

@@ -0,0 +1,17 @@
#ifndef TEMPERATURE_CONVERSION_H
#define TEMPERATURE_CONVERSION_H
#include <mosquittopp.h>
class mqtt_tempconv : public mosqpp::mosquittopp
{
public:
mqtt_tempconv(const char *id, const char *host, int port);
~mqtt_tempconv();
void on_connect(int rc);
void on_message(const struct mosquitto_message *message);
void on_subscribe(int mid, int qos_count, const int *granted_qos);
};
#endif