Code sample to be used with Bluemix for education purpose
Dependencies: C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
Diff: main.cpp
- Revision:
- 8:80d49dd91542
- Parent:
- 7:63a7aa4deaf8
- Child:
- 9:58eb378727d9
diff -r 63a7aa4deaf8 -r 80d49dd91542 main.cpp --- a/main.cpp Wed Aug 20 13:46:35 2014 +0000 +++ b/main.cpp Wed Oct 01 13:27:35 2014 +0000 @@ -13,6 +13,7 @@ * Contributors: * Sam Danbury - initial implementation * Ian Craggs - refactoring to remove STL and other changes + * Sam Grove - added check for Ethernet cable. *******************************************************************************/ #include "LM75B.h" @@ -24,20 +25,10 @@ #include "rtos.h" // Configuration values needed to connect to IBM IoT Cloud -#define QUICKSTARTMODE 1 -#if (QUICKSTARTMODE) -#define ORG "quickstart" -#define ID "" -#define AUTH_METHOD "" -#define AUTH_TOKEN "" -#define TYPE DEFAULT_TYPE_NAME -#else -#define ORG "Replace with your org" -#define ID "Replace with your id" -#define TYPE "Replace with your type" -#define AUTH_METHOD "Replace with your auth-method" -#define AUTH_TOKEN "Replace with your auth-token" -#endif +#define ORG "quickstart" // For a registered connection, replace with your org +#define ID "" // For a registered connection, replace with your id +#define AUTH_TOKEN "" // For a registered connection, replace with your auth-token +#define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type #define MQTT_PORT 1883 #define MQTT_TLS_PORT 8883 @@ -53,7 +44,7 @@ #include "K64F.h" #endif -bool quickstartMode = (QUICKSTARTMODE) ? true : false; +bool quickstartMode = true; char org[11] = ORG; char type[30] = TYPE; char id[30] = ID; // mac without colons @@ -210,7 +201,7 @@ data.password.cstring = auth_token; } - if ((rc = client->connect(&data)) == 0) + if ((rc = client->connect(data)) == 0) { connected = true; green(); @@ -233,6 +224,12 @@ { int retryAttempt = 0; connected = false; + + // make sure a cable is connected before starting to connect + while (!linkStatus()) { + wait(1.0f); + WARN("Ethernet link not present. Check cable connection\n"); + } while (connect(client, ipstack) != 0) { @@ -243,7 +240,15 @@ #endif int timeout = getConnTimeout(++retryAttempt); WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); - wait(timeout); + + // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed + // or maybe just add the proper members to do this disconnect and call attemptConnect(...) + + // this works - reset the system when the retry count gets to a threshold + if (retryAttempt == 5) + NVIC_SystemReset(); + else + wait(timeout); } } @@ -264,7 +269,7 @@ message.payloadlen = strlen(buf); LOG("Publishing %s\n", buf); - return client->publish(pubTopic, &message); + return client->publish(pubTopic, message); } @@ -339,6 +344,8 @@ int main() { + quickstartMode = (strcmp(org, "quickstart") == 0); + lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen led2 = LED2_OFF; // K64F: turn off the main board LED