An AirVantage layer for MQTT

Dependencies:   niMQTT picojson

Dependents:   AV_MQTT_example

Committer:
Nim65s
Date:
Thu Aug 08 16:08:18 2013 +0000
Revision:
0:37b9835622f9
Child:
1:7f1a11a70a2b
First functionnal release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nim65s 0:37b9835622f9 1 #include "AV_MQTT.h"
Nim65s 0:37b9835622f9 2
Nim65s 0:37b9835622f9 3 AV_MQTT::AV_MQTT(char *server, void (*callback)(char *, char*), char *username, char *password, char *id, int port, bool debug):
Nim65s 0:37b9835622f9 4 client(server, callback, id, port, username, password, debug), debug(debug) {
Nim65s 0:37b9835622f9 5 topic = new char[strlen(username) + 15];
Nim65s 0:37b9835622f9 6 strcpy(topic, username);
Nim65s 0:37b9835622f9 7 strcat(topic, "/messages/json");
Nim65s 0:37b9835622f9 8 printf(topic);
Nim65s 0:37b9835622f9 9 client.sub(topic);
Nim65s 0:37b9835622f9 10 }
Nim65s 0:37b9835622f9 11
Nim65s 0:37b9835622f9 12 void AV_MQTT::pub(char *key, char *value) {
Nim65s 0:37b9835622f9 13 int key_length = strlen(key);
Nim65s 0:37b9835622f9 14 int value_length = strlen(value);
Nim65s 0:37b9835622f9 15 char json[key_length + value_length + 36];
Nim65s 0:37b9835622f9 16 strcpy(json, "[{\"");
Nim65s 0:37b9835622f9 17 strcat(json, key);
Nim65s 0:37b9835622f9 18 strcat(json, "\":[{\"timestamp\":null,\"value\":");
Nim65s 0:37b9835622f9 19 strcat(json, value);
Nim65s 0:37b9835622f9 20 strcat(json, "}]}]");
Nim65s 0:37b9835622f9 21 printf(json);
Nim65s 0:37b9835622f9 22 client.pub(topic, json);
Nim65s 0:37b9835622f9 23 }