Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
src/QuickstartClient.cpp
- Committer:
- samdanbury
- Date:
- 2014-06-24
- Revision:
- 3:ca5b84eb8f3b
- Parent:
- 2:25ddff75a8c7
- Child:
- 5:1b54a0b7b39d
File content as of revision 3:ca5b84eb8f3b:
/*******************************************************************************
* Copyright (c) 2014 IBM Corporation and other Contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Sam Danbury
* IBM - Initial Contribution
*******************************************************************************/
#include "QuickstartClient.h"
QuickstartClient::QuickstartClient(string mac) {
quickstartMode = true;
connected = false;
macAddress = mac;
//Generate topic string
memcpy(topic, "iot-2/evt/status/fmt/json", 25);
topic[25] = '\0';
loadConfig();
tryMqttConnect();
}
void QuickstartClient::loadConfig() {
ConfigFile cfg;
char value[30];
char value1[30];
char value2[30];
char value3[30];
if (cfg.read("/local/device.cfg")) {
quickstartMode = false;
if (cfg.getValue("org", value, sizeof(value))) {
stringstream ss(value);
ss >> org;
} else {
lcd.printf("No org defined in config\n");
}
if (cfg.getValue("type", value1, sizeof(value1))) {
stringstream ss(value1);
ss >> type;
} else {
lcd.printf("No type defined in config\n");
}
if (cfg.getValue("id", value2, sizeof(value2))) {
stringstream ss(value2);
ss >> id;
} else {
lcd.printf("No id defined in config\n");
}
if (cfg.getValue("token", value3, sizeof(value3))) {
stringstream ss(value3);
ss >> token;
} else {
lcd.printf("No token defined in config\n");
}
} else {
org = "quickstart";
type = "iotsample-mbed-lpc1768";
id = macAddress;
}
}
int QuickstartClient::reconnectDelay(int i) {
if (i < 10) {
return 3; //First 10 attempts try within 3 seconds
} else if (i < 20) {
return 60; //Next 10 attempts retry after every 1 minute
} else {
return 600; //After 20 attempts, retry every 10 minutes
}
}
void QuickstartClient::tryMqttConnect() {
int retryAttempt = 0;
//Reinstantiate TCP socket connection object
mysock = TCPSocketConnection();
while (connected == false) {
lcd.cls();
lcd.locate(0,0);
lcd.printf("Trying to connect...");
//Based on number of connection attempts, determine timeout
int connDelayTimeout = reconnectDelay(++retryAttempt);
//Attempt to reconnect
connect();
//If connection was not established, continue retry
if (connected == false) {
wait(connDelayTimeout);
} else {
break;
}
}
}
void QuickstartClient::connect() {
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
int rc = 0;
int len = 0;
char buf[200];
int buflen = sizeof(buf);
//Connect to TCP socket
mysock.connect(IBM_IOT_BROKER, IBM_IOT_PORT);
//Construct client ID
string str = string("d:") + org + ":" + type + ":" + id;
char clientId[str.size()];
memcpy(clientId, str.c_str(), str.size() + 1);
//Set MQTT connect options
data.clientID.cstring = clientId;
data.keepAliveInterval = 20;
data.cleansession = 1;
data.MQTTVersion = 3;
//Attempt MQTT connect
len = MQTTSerialize_connect(buf, buflen, &data);
rc = 0;
while (rc < len) {
int rc1 = mysock.send(buf, len);
if (rc1 == -1) {
connected = false;
break;
} else {
rc += rc1;
}
}
if (rc == len) {
connected = true;
}
}
void QuickstartClient::publish(string thePayload) {
int rc = 0;
int len = 0;
char buf[250];
int buflen = sizeof(buf);
MQTTString topicString = MQTTString_initializer;
topicString.cstring = topic;
//Convert payload from string to char array
char* payload = new char [thePayload.length()+1];
std::strcpy (payload, thePayload.c_str());
int payloadlen = strlen(payload);
//Attempt MQTT publish
len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen);
rc = 0;
while (rc < len) {
int rc1 = mysock.send(buf, len);
if (rc1 == -1) {
//If return code from MQTT publish is -1, attempt reconnect
connected = false;
tryMqttConnect();
break;
} else {
rc += rc1;
}
}
wait(0.2);
if (payload) {
delete payload;
}
}
