Vergil Cola
/
MQTTGatewayK64
Fork of my MQTTGateway
MQTTSManager/MQTTSManager.cpp@0:f1d3878b8dd9, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:45:51 2017 +0000
- Revision:
- 0:f1d3878b8dd9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:f1d3878b8dd9 | 1 | #include "MQTTSManager.h" |
vpcola | 0:f1d3878b8dd9 | 2 | #include "XbeeMonitor.h" |
vpcola | 0:f1d3878b8dd9 | 3 | #include "Utils.h" |
vpcola | 0:f1d3878b8dd9 | 4 | #include "jsmn.h" |
vpcola | 0:f1d3878b8dd9 | 5 | #include <string> |
vpcola | 0:f1d3878b8dd9 | 6 | |
vpcola | 0:f1d3878b8dd9 | 7 | using namespace MQTT; |
vpcola | 0:f1d3878b8dd9 | 8 | |
vpcola | 0:f1d3878b8dd9 | 9 | #define MQTTS_PORT 8883 |
vpcola | 0:f1d3878b8dd9 | 10 | |
vpcola | 0:f1d3878b8dd9 | 11 | static const char * topic_update = "garden_update"; |
vpcola | 0:f1d3878b8dd9 | 12 | static const char * topic_listen = "garden_status"; |
vpcola | 0:f1d3878b8dd9 | 13 | static const char * hostname = "mqtt.mbedhacks.com"; |
vpcola | 0:f1d3878b8dd9 | 14 | static const char * clientID = "mbed-sample"; |
vpcola | 0:f1d3878b8dd9 | 15 | static const char * username = "mbedhacks"; |
vpcola | 0:f1d3878b8dd9 | 16 | static const char * password = "qwer123"; |
vpcola | 0:f1d3878b8dd9 | 17 | |
vpcola | 0:f1d3878b8dd9 | 18 | static MQTTThreadedClient * pmqtt = NULL; |
vpcola | 0:f1d3878b8dd9 | 19 | Thread mqttThd(osPriorityNormal, DEFAULT_STACK_SIZE * 2); |
vpcola | 0:f1d3878b8dd9 | 20 | RadioControlData postdata; |
vpcola | 0:f1d3878b8dd9 | 21 | static char tempbuff[100]; |
vpcola | 0:f1d3878b8dd9 | 22 | |
vpcola | 0:f1d3878b8dd9 | 23 | static int jsoneq(const char * json, jsmntok_t * tok, const char * s) |
vpcola | 0:f1d3878b8dd9 | 24 | { |
vpcola | 0:f1d3878b8dd9 | 25 | if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && |
vpcola | 0:f1d3878b8dd9 | 26 | strncmp(json + tok->start, s, tok->end - tok->start) == 0) { |
vpcola | 0:f1d3878b8dd9 | 27 | return 0; |
vpcola | 0:f1d3878b8dd9 | 28 | } |
vpcola | 0:f1d3878b8dd9 | 29 | return -1; |
vpcola | 0:f1d3878b8dd9 | 30 | } |
vpcola | 0:f1d3878b8dd9 | 31 | |
vpcola | 0:f1d3878b8dd9 | 32 | void messageArrived(MessageData& md) |
vpcola | 0:f1d3878b8dd9 | 33 | { |
vpcola | 0:f1d3878b8dd9 | 34 | int i, r; |
vpcola | 0:f1d3878b8dd9 | 35 | |
vpcola | 0:f1d3878b8dd9 | 36 | jsmn_parser p; |
vpcola | 0:f1d3878b8dd9 | 37 | jsmntok_t t[100]; |
vpcola | 0:f1d3878b8dd9 | 38 | |
vpcola | 0:f1d3878b8dd9 | 39 | Message &message = md.message; |
vpcola | 0:f1d3878b8dd9 | 40 | printf("Arrived Callback 1 : qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
vpcola | 0:f1d3878b8dd9 | 41 | printf("Payload [%.*s]\r\n", message.payloadlen, (char*)message.payload); |
vpcola | 0:f1d3878b8dd9 | 42 | |
vpcola | 0:f1d3878b8dd9 | 43 | // handle payload |
vpcola | 0:f1d3878b8dd9 | 44 | const char * jsonstring = std::string((const char *) message.payload, message.payloadlen).c_str(); |
vpcola | 0:f1d3878b8dd9 | 45 | |
vpcola | 0:f1d3878b8dd9 | 46 | jsmn_init(&p); |
vpcola | 0:f1d3878b8dd9 | 47 | r = jsmn_parse(&p, jsonstring, strlen(jsonstring), t, sizeof(t)/sizeof(t[0])); |
vpcola | 0:f1d3878b8dd9 | 48 | |
vpcola | 0:f1d3878b8dd9 | 49 | uint64_t radio_id = 0; |
vpcola | 0:f1d3878b8dd9 | 50 | int sprinkler_pin = 1; // 0 - turn on sprinkler, 1 - off |
vpcola | 0:f1d3878b8dd9 | 51 | |
vpcola | 0:f1d3878b8dd9 | 52 | /* Top level element is an object */ |
vpcola | 0:f1d3878b8dd9 | 53 | if ((r > 0) && (t[0].type == JSMN_OBJECT)) |
vpcola | 0:f1d3878b8dd9 | 54 | { |
vpcola | 0:f1d3878b8dd9 | 55 | /* Loop over all tokens */ |
vpcola | 0:f1d3878b8dd9 | 56 | for (i = 1; i < r; i++) |
vpcola | 0:f1d3878b8dd9 | 57 | { |
vpcola | 0:f1d3878b8dd9 | 58 | if (jsoneq(jsonstring, &t[i], "radioid") == 0) |
vpcola | 0:f1d3878b8dd9 | 59 | { |
vpcola | 0:f1d3878b8dd9 | 60 | memset(tempbuff, 0, sizeof(tempbuff)); |
vpcola | 0:f1d3878b8dd9 | 61 | strncpy(tempbuff, jsonstring + t[i+1].start, t[i+1].end - t[i+1].start); |
vpcola | 0:f1d3878b8dd9 | 62 | radio_id = strtoull(&tempbuff[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 63 | i++; |
vpcola | 0:f1d3878b8dd9 | 64 | } |
vpcola | 0:f1d3878b8dd9 | 65 | else if (jsoneq(jsonstring, &t[i], "sprinkler") == 0) |
vpcola | 0:f1d3878b8dd9 | 66 | { |
vpcola | 0:f1d3878b8dd9 | 67 | memset(tempbuff, 0, sizeof(tempbuff)); |
vpcola | 0:f1d3878b8dd9 | 68 | strncpy(tempbuff, jsonstring + t[i+1].start, t[i+1].end - t[i+1].start); |
vpcola | 0:f1d3878b8dd9 | 69 | sprinkler_pin = strtoul(&tempbuff[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 70 | i++; |
vpcola | 0:f1d3878b8dd9 | 71 | } |
vpcola | 0:f1d3878b8dd9 | 72 | else |
vpcola | 0:f1d3878b8dd9 | 73 | { |
vpcola | 0:f1d3878b8dd9 | 74 | |
vpcola | 0:f1d3878b8dd9 | 75 | } |
vpcola | 0:f1d3878b8dd9 | 76 | } |
vpcola | 0:f1d3878b8dd9 | 77 | } |
vpcola | 0:f1d3878b8dd9 | 78 | |
vpcola | 0:f1d3878b8dd9 | 79 | // TODO: Send the values to the XBeeMonitor thread |
vpcola | 0:f1d3878b8dd9 | 80 | printf("Radio ID: %llu\r\n", radio_id); |
vpcola | 0:f1d3878b8dd9 | 81 | printf("Sprinkler Pin : %d\r\n", sprinkler_pin); |
vpcola | 0:f1d3878b8dd9 | 82 | postdata.radioID = radio_id; |
vpcola | 0:f1d3878b8dd9 | 83 | postdata.sprinkler_pin = sprinkler_pin; |
vpcola | 0:f1d3878b8dd9 | 84 | postRadioControl(postdata); |
vpcola | 0:f1d3878b8dd9 | 85 | } |
vpcola | 0:f1d3878b8dd9 | 86 | |
vpcola | 0:f1d3878b8dd9 | 87 | int mqttsInit(NetworkInterface * net, const char * pem) |
vpcola | 0:f1d3878b8dd9 | 88 | { |
vpcola | 0:f1d3878b8dd9 | 89 | pmqtt = new MQTTThreadedClient(net, pem); |
vpcola | 0:f1d3878b8dd9 | 90 | if (pmqtt == NULL) |
vpcola | 0:f1d3878b8dd9 | 91 | return -1; |
vpcola | 0:f1d3878b8dd9 | 92 | |
vpcola | 0:f1d3878b8dd9 | 93 | MQTTPacket_connectData logindata = MQTTPacket_connectData_initializer; |
vpcola | 0:f1d3878b8dd9 | 94 | logindata.MQTTVersion = 3; |
vpcola | 0:f1d3878b8dd9 | 95 | logindata.clientID.cstring = (char *) clientID; |
vpcola | 0:f1d3878b8dd9 | 96 | logindata.username.cstring = (char *) username; |
vpcola | 0:f1d3878b8dd9 | 97 | logindata.password.cstring = (char *) password; |
vpcola | 0:f1d3878b8dd9 | 98 | |
vpcola | 0:f1d3878b8dd9 | 99 | pmqtt->setConnectionParameters(hostname, MQTTS_PORT, logindata); |
vpcola | 0:f1d3878b8dd9 | 100 | pmqtt->addTopicHandler(topic_listen, messageArrived); |
vpcola | 0:f1d3878b8dd9 | 101 | |
vpcola | 0:f1d3878b8dd9 | 102 | return 0; |
vpcola | 0:f1d3878b8dd9 | 103 | } |
vpcola | 0:f1d3878b8dd9 | 104 | |
vpcola | 0:f1d3878b8dd9 | 105 | void postMQTTUpdate(SensorData &msg) |
vpcola | 0:f1d3878b8dd9 | 106 | { |
vpcola | 0:f1d3878b8dd9 | 107 | // Serialize data to json string ... |
vpcola | 0:f1d3878b8dd9 | 108 | if (pmqtt) |
vpcola | 0:f1d3878b8dd9 | 109 | { |
vpcola | 0:f1d3878b8dd9 | 110 | PubMessage message; |
vpcola | 0:f1d3878b8dd9 | 111 | message.qos = QOS0; |
vpcola | 0:f1d3878b8dd9 | 112 | message.id = 123; |
vpcola | 0:f1d3878b8dd9 | 113 | |
vpcola | 0:f1d3878b8dd9 | 114 | strcpy(&message.topic[0], topic_update); |
vpcola | 0:f1d3878b8dd9 | 115 | size_t numbytes = snprintf(&message.payload[0], MAX_MQTT_PAYLOAD_SIZE, |
vpcola | 0:f1d3878b8dd9 | 116 | "{\"radio\":%llu,\"status\":{\"sprinkler\":%d,\"humidity\":%d,\"temperature\":%.2f,\"luminance\":%d}}", |
vpcola | 0:f1d3878b8dd9 | 117 | msg.deviceaddr, |
vpcola | 0:f1d3878b8dd9 | 118 | msg.sprinkler, |
vpcola | 0:f1d3878b8dd9 | 119 | msg.humidity, |
vpcola | 0:f1d3878b8dd9 | 120 | msg.temperature, |
vpcola | 0:f1d3878b8dd9 | 121 | msg.luminance); |
vpcola | 0:f1d3878b8dd9 | 122 | printf("[%s]\r\n", &message.payload[0]); |
vpcola | 0:f1d3878b8dd9 | 123 | message.payloadlen = numbytes; |
vpcola | 0:f1d3878b8dd9 | 124 | pmqtt->publish(message); |
vpcola | 0:f1d3878b8dd9 | 125 | } |
vpcola | 0:f1d3878b8dd9 | 126 | } |
vpcola | 0:f1d3878b8dd9 | 127 | |
vpcola | 0:f1d3878b8dd9 | 128 | int runMQTTS() |
vpcola | 0:f1d3878b8dd9 | 129 | { |
vpcola | 0:f1d3878b8dd9 | 130 | if ( pmqtt && (mqttThd.start(mbed::callback(pmqtt, &MQTTThreadedClient::startListener)) != osOK ) ) |
vpcola | 0:f1d3878b8dd9 | 131 | return -1; |
vpcola | 0:f1d3878b8dd9 | 132 | return 0; |
vpcola | 0:f1d3878b8dd9 | 133 | } |