An AirVantage layer for MQTT

Dependencies:   niMQTT picojson

Dependents:   AV_MQTT_example

AV_MQTT.cpp

Committer:
Nim65s
Date:
2013-08-08
Revision:
0:37b9835622f9
Child:
1:7f1a11a70a2b

File content as of revision 0:37b9835622f9:

#include "AV_MQTT.h"

AV_MQTT::AV_MQTT(char *server, void (*callback)(char *, char*), char *username, char *password,  char *id, int port, bool debug):
    client(server, callback, id, port, username, password, debug), debug(debug) {
    topic = new char[strlen(username) + 15];
    strcpy(topic, username);
    strcat(topic, "/messages/json");
    printf(topic);
    client.sub(topic);
}

void AV_MQTT::pub(char *key, char *value) {
    int key_length = strlen(key);
    int value_length = strlen(value);
    char json[key_length + value_length + 36];
    strcpy(json, "[{\"");
    strcat(json, key);
    strcat(json, "\":[{\"timestamp\":null,\"value\":");
    strcat(json, value);
    strcat(json, "}]}]");
    printf(json);
    client.pub(topic, json);
}