Local copy

Dependencies:   C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed

Fork of IBMIoTClientExampleForLPC1768 by Sam Danbury

Committer:
rajathishere
Date:
Tue Jul 01 19:10:47 2014 +0000
Revision:
9:cbabd85f2d5d
Parent:
8:e58e10ca4352
Child:
12:e9ff8869a99d
Added iniital drop of Subscription

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 0:6276e9f72327 1 /*******************************************************************************
samdanbury 0:6276e9f72327 2 * Copyright (c) 2014 IBM Corporation and other Contributors.
samdanbury 0:6276e9f72327 3 *
samdanbury 0:6276e9f72327 4 * All rights reserved. This program and the accompanying materials
samdanbury 0:6276e9f72327 5 * are made available under the terms of the Eclipse Public License v1.0
samdanbury 0:6276e9f72327 6 * which accompanies this distribution, and is available at
samdanbury 0:6276e9f72327 7 * http://www.eclipse.org/legal/epl-v10.html
samdanbury 0:6276e9f72327 8 *
samdanbury 0:6276e9f72327 9 * Contributors: Sam Danbury
samdanbury 0:6276e9f72327 10 * IBM - Initial Contribution
samdanbury 0:6276e9f72327 11 *******************************************************************************/
samdanbury 0:6276e9f72327 12
samdanbury 0:6276e9f72327 13 #include "QuickstartClient.h"
samdanbury 0:6276e9f72327 14
rajathishere 6:a022f983f94b 15 int i =0;
samdanbury 0:6276e9f72327 16 QuickstartClient::QuickstartClient(string mac) {
samdanbury 2:25ddff75a8c7 17 quickstartMode = true;
samdanbury 0:6276e9f72327 18 connected = false;
samdanbury 0:6276e9f72327 19 macAddress = mac;
samdanbury 0:6276e9f72327 20
rajathishere 6:a022f983f94b 21 //Generate topic string for publish
samdanbury 3:ca5b84eb8f3b 22 memcpy(topic, "iot-2/evt/status/fmt/json", 25);
samdanbury 3:ca5b84eb8f3b 23 topic[25] = '\0';
samdanbury 2:25ddff75a8c7 24
rajathishere 6:a022f983f94b 25 //Generate topic string for subscribe
rajathishere 6:a022f983f94b 26 memcpy(subscribeTopic, "iot-2/cmd/blink/fmt/json",24);
rajathishere 6:a022f983f94b 27 subscribeTopic[24] = '\0';
rajathishere 6:a022f983f94b 28
samdanbury 2:25ddff75a8c7 29 loadConfig();
samdanbury 0:6276e9f72327 30
samdanbury 0:6276e9f72327 31 tryMqttConnect();
samdanbury 0:6276e9f72327 32 }
samdanbury 0:6276e9f72327 33
samdanbury 2:25ddff75a8c7 34 void QuickstartClient::loadConfig() {
samdanbury 2:25ddff75a8c7 35
samdanbury 2:25ddff75a8c7 36 ConfigFile cfg;
samdanbury 2:25ddff75a8c7 37
samdanbury 3:ca5b84eb8f3b 38 char value[30];
samdanbury 3:ca5b84eb8f3b 39 char value1[30];
samdanbury 3:ca5b84eb8f3b 40 char value2[30];
samdanbury 3:ca5b84eb8f3b 41 char value3[30];
rajathishere 6:a022f983f94b 42
samdanbury 2:25ddff75a8c7 43 if (cfg.read("/local/device.cfg")) {
samdanbury 2:25ddff75a8c7 44 quickstartMode = false;
samdanbury 2:25ddff75a8c7 45
samdanbury 3:ca5b84eb8f3b 46 if (cfg.getValue("org", value, sizeof(value))) {
samdanbury 2:25ddff75a8c7 47 stringstream ss(value);
samdanbury 2:25ddff75a8c7 48 ss >> org;
samdanbury 2:25ddff75a8c7 49 } else {
samdanbury 2:25ddff75a8c7 50 lcd.printf("No org defined in config\n");
samdanbury 2:25ddff75a8c7 51 }
samdanbury 2:25ddff75a8c7 52
samdanbury 3:ca5b84eb8f3b 53 if (cfg.getValue("type", value1, sizeof(value1))) {
samdanbury 3:ca5b84eb8f3b 54 stringstream ss(value1);
samdanbury 2:25ddff75a8c7 55 ss >> type;
samdanbury 2:25ddff75a8c7 56 } else {
samdanbury 2:25ddff75a8c7 57 lcd.printf("No type defined in config\n");
samdanbury 2:25ddff75a8c7 58 }
samdanbury 2:25ddff75a8c7 59
samdanbury 3:ca5b84eb8f3b 60 if (cfg.getValue("id", value2, sizeof(value2))) {
samdanbury 3:ca5b84eb8f3b 61 stringstream ss(value2);
samdanbury 2:25ddff75a8c7 62 ss >> id;
samdanbury 2:25ddff75a8c7 63 } else {
samdanbury 2:25ddff75a8c7 64 lcd.printf("No id defined in config\n");
samdanbury 2:25ddff75a8c7 65 }
samdanbury 2:25ddff75a8c7 66
samdanbury 3:ca5b84eb8f3b 67 if (cfg.getValue("token", value3, sizeof(value3))) {
samdanbury 3:ca5b84eb8f3b 68 stringstream ss(value3);
samdanbury 2:25ddff75a8c7 69 ss >> token;
samdanbury 2:25ddff75a8c7 70 } else {
samdanbury 2:25ddff75a8c7 71 lcd.printf("No token defined in config\n");
samdanbury 2:25ddff75a8c7 72 }
samdanbury 3:ca5b84eb8f3b 73
samdanbury 2:25ddff75a8c7 74 } else {
samdanbury 2:25ddff75a8c7 75 org = "quickstart";
samdanbury 2:25ddff75a8c7 76 type = "iotsample-mbed-lpc1768";
samdanbury 2:25ddff75a8c7 77 id = macAddress;
samdanbury 2:25ddff75a8c7 78 }
rajathishere 6:a022f983f94b 79 wait(5.0);
samdanbury 2:25ddff75a8c7 80
samdanbury 2:25ddff75a8c7 81 }
samdanbury 2:25ddff75a8c7 82
samdanbury 0:6276e9f72327 83 int QuickstartClient::reconnectDelay(int i) {
samdanbury 0:6276e9f72327 84 if (i < 10) {
samdanbury 0:6276e9f72327 85 return 3; //First 10 attempts try within 3 seconds
samdanbury 0:6276e9f72327 86 } else if (i < 20) {
samdanbury 0:6276e9f72327 87 return 60; //Next 10 attempts retry after every 1 minute
samdanbury 0:6276e9f72327 88 } else {
samdanbury 0:6276e9f72327 89 return 600; //After 20 attempts, retry every 10 minutes
samdanbury 0:6276e9f72327 90 }
samdanbury 0:6276e9f72327 91 }
samdanbury 0:6276e9f72327 92
samdanbury 0:6276e9f72327 93 void QuickstartClient::tryMqttConnect() {
samdanbury 0:6276e9f72327 94 int retryAttempt = 0;
samdanbury 0:6276e9f72327 95
samdanbury 0:6276e9f72327 96 //Reinstantiate TCP socket connection object
samdanbury 0:6276e9f72327 97 mysock = TCPSocketConnection();
samdanbury 0:6276e9f72327 98
samdanbury 0:6276e9f72327 99 while (connected == false) {
samdanbury 0:6276e9f72327 100 lcd.cls();
samdanbury 0:6276e9f72327 101 lcd.locate(0,0);
samdanbury 0:6276e9f72327 102 lcd.printf("Trying to connect...");
samdanbury 0:6276e9f72327 103
samdanbury 0:6276e9f72327 104 //Based on number of connection attempts, determine timeout
samdanbury 0:6276e9f72327 105 int connDelayTimeout = reconnectDelay(++retryAttempt);
samdanbury 0:6276e9f72327 106
samdanbury 0:6276e9f72327 107 //Attempt to reconnect
samdanbury 0:6276e9f72327 108 connect();
samdanbury 0:6276e9f72327 109
samdanbury 0:6276e9f72327 110 //If connection was not established, continue retry
samdanbury 0:6276e9f72327 111 if (connected == false) {
samdanbury 0:6276e9f72327 112 wait(connDelayTimeout);
samdanbury 0:6276e9f72327 113 } else {
samdanbury 0:6276e9f72327 114 break;
samdanbury 0:6276e9f72327 115 }
samdanbury 0:6276e9f72327 116 }
samdanbury 0:6276e9f72327 117 }
samdanbury 0:6276e9f72327 118
samdanbury 0:6276e9f72327 119 void QuickstartClient::connect() {
samdanbury 0:6276e9f72327 120 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
samdanbury 0:6276e9f72327 121 int rc = 0;
samdanbury 0:6276e9f72327 122 int len = 0;
samdanbury 0:6276e9f72327 123 char buf[200];
samdanbury 0:6276e9f72327 124 int buflen = sizeof(buf);
samdanbury 0:6276e9f72327 125
samdanbury 0:6276e9f72327 126 //Connect to TCP socket
samdanbury 0:6276e9f72327 127 mysock.connect(IBM_IOT_BROKER, IBM_IOT_PORT);
samdanbury 0:6276e9f72327 128
samdanbury 2:25ddff75a8c7 129 //Construct client ID
samdanbury 2:25ddff75a8c7 130 string str = string("d:") + org + ":" + type + ":" + id;
rajathishere 8:e58e10ca4352 131 char clientId[str.size()+1];
samdanbury 3:ca5b84eb8f3b 132 memcpy(clientId, str.c_str(), str.size() + 1);
samdanbury 0:6276e9f72327 133
samdanbury 0:6276e9f72327 134 //Set MQTT connect options
samdanbury 0:6276e9f72327 135 data.clientID.cstring = clientId;
samdanbury 0:6276e9f72327 136 data.keepAliveInterval = 20;
samdanbury 0:6276e9f72327 137 data.cleansession = 1;
samdanbury 0:6276e9f72327 138 data.MQTTVersion = 3;
samdanbury 5:1b54a0b7b39d 139 if (!quickstartMode) {
rajathishere 8:e58e10ca4352 140 data.username.cstring = clientId;
rajathishere 8:e58e10ca4352 141 char* authToken= new char[token.size()];
rajathishere 6:a022f983f94b 142 memcpy(authToken, token.c_str(), token.size()+1);
rajathishere 8:e58e10ca4352 143 data.password.cstring = authToken;
samdanbury 5:1b54a0b7b39d 144 }
samdanbury 0:6276e9f72327 145
samdanbury 0:6276e9f72327 146 //Attempt MQTT connect
samdanbury 0:6276e9f72327 147 len = MQTTSerialize_connect(buf, buflen, &data);
samdanbury 0:6276e9f72327 148 rc = 0;
samdanbury 0:6276e9f72327 149 while (rc < len) {
samdanbury 0:6276e9f72327 150 int rc1 = mysock.send(buf, len);
samdanbury 0:6276e9f72327 151 if (rc1 == -1) {
samdanbury 0:6276e9f72327 152 connected = false;
samdanbury 0:6276e9f72327 153 break;
samdanbury 0:6276e9f72327 154 } else {
samdanbury 0:6276e9f72327 155 rc += rc1;
samdanbury 0:6276e9f72327 156 }
samdanbury 0:6276e9f72327 157 }
samdanbury 0:6276e9f72327 158 if (rc == len) {
samdanbury 0:6276e9f72327 159 connected = true;
samdanbury 0:6276e9f72327 160 }
rajathishere 6:a022f983f94b 161 wait(0.2);
samdanbury 0:6276e9f72327 162 }
samdanbury 0:6276e9f72327 163
samdanbury 0:6276e9f72327 164 void QuickstartClient::publish(string thePayload) {
samdanbury 0:6276e9f72327 165 int rc = 0;
samdanbury 0:6276e9f72327 166 int len = 0;
samdanbury 0:6276e9f72327 167 char buf[250];
samdanbury 0:6276e9f72327 168 int buflen = sizeof(buf);
rajathishere 6:a022f983f94b 169
samdanbury 0:6276e9f72327 170 MQTTString topicString = MQTTString_initializer;
samdanbury 0:6276e9f72327 171
samdanbury 0:6276e9f72327 172 topicString.cstring = topic;
rajathishere 8:e58e10ca4352 173
samdanbury 0:6276e9f72327 174 //Convert payload from string to char array
samdanbury 0:6276e9f72327 175 char* payload = new char [thePayload.length()+1];
samdanbury 0:6276e9f72327 176 std::strcpy (payload, thePayload.c_str());
samdanbury 0:6276e9f72327 177 int payloadlen = strlen(payload);
samdanbury 0:6276e9f72327 178 //Attempt MQTT publish
samdanbury 0:6276e9f72327 179 len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen);
samdanbury 0:6276e9f72327 180 rc = 0;
samdanbury 0:6276e9f72327 181 while (rc < len) {
samdanbury 0:6276e9f72327 182 int rc1 = mysock.send(buf, len);
rajathishere 6:a022f983f94b 183 wait(3.0);
samdanbury 0:6276e9f72327 184 if (rc1 == -1) {
samdanbury 0:6276e9f72327 185 //If return code from MQTT publish is -1, attempt reconnect
samdanbury 0:6276e9f72327 186 connected = false;
samdanbury 0:6276e9f72327 187 tryMqttConnect();
samdanbury 0:6276e9f72327 188 break;
samdanbury 0:6276e9f72327 189 } else {
samdanbury 0:6276e9f72327 190 rc += rc1;
samdanbury 0:6276e9f72327 191 }
samdanbury 0:6276e9f72327 192 }
samdanbury 0:6276e9f72327 193 wait(0.2);
samdanbury 0:6276e9f72327 194
samdanbury 0:6276e9f72327 195 if (payload) {
samdanbury 0:6276e9f72327 196 delete payload;
samdanbury 0:6276e9f72327 197 }
rajathishere 6:a022f983f94b 198 }
rajathishere 6:a022f983f94b 199
rajathishere 9:cbabd85f2d5d 200 int QuickstartClient::subscribe() {
rajathishere 6:a022f983f94b 201 int rc = 0;
rajathishere 6:a022f983f94b 202 int len = 0;
rajathishere 6:a022f983f94b 203 char buf[250];
rajathishere 6:a022f983f94b 204 int buflen = sizeof(buf);
rajathishere 6:a022f983f94b 205
rajathishere 6:a022f983f94b 206 MQTTString topicString = MQTTString_initializer;
rajathishere 6:a022f983f94b 207
rajathishere 6:a022f983f94b 208 topicString.cstring = subscribeTopic;
rajathishere 6:a022f983f94b 209
rajathishere 6:a022f983f94b 210 //Attempt MQTT subscribe
rajathishere 9:cbabd85f2d5d 211 len = MQTTSerialize_subscribe(buf, buflen, 0, 1, 1, &topicString, 0);
rajathishere 6:a022f983f94b 212 rc = 0;
rajathishere 6:a022f983f94b 213 while (rc < len) {
rajathishere 6:a022f983f94b 214 int rc1 = mysock.send(buf, len);
rajathishere 6:a022f983f94b 215 if (rc1 == -1) {
rajathishere 6:a022f983f94b 216 break;
rajathishere 6:a022f983f94b 217 }
rajathishere 6:a022f983f94b 218 else {
rajathishere 6:a022f983f94b 219 rc += rc1;
rajathishere 6:a022f983f94b 220 }
rajathishere 6:a022f983f94b 221 }
rajathishere 6:a022f983f94b 222 wait(0.2);
rajathishere 9:cbabd85f2d5d 223 return rc;
rajathishere 6:a022f983f94b 224 }