Code sample to be used with Bluemix for education purpose

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Revision:
14:1f961d19f3cf
Parent:
13:85801e3b83d3
Child:
15:09458079f4bb
diff -r 85801e3b83d3 -r 1f961d19f3cf main.cpp
--- a/main.cpp	Wed Jul 22 13:51:22 2015 +0000
+++ b/main.cpp	Fri Jul 24 15:40:33 2015 +0000
@@ -28,8 +28,6 @@
 #include "C12832.h"
 #include "Arial12x12.h"
 #include "rtos.h"
-#include <string>
-#include <iostream>
 
 // Update this to the next number *before* a commit
 #define __APP_SW_REVISION__ "11"
@@ -57,6 +55,7 @@
 #include "K64F.h"
 #endif
 
+
 bool quickstartMode = true;
 char org[11] = ORG;  
 char type[30] = TYPE;
@@ -64,12 +63,42 @@
 char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
 
 bool connected = false;
+bool mqttConnecting = false;
+bool netConnected = false;
+bool netConnecting = false;
+bool ethernetInitialising = true;
+int retryAttempt = 0;
+int menuItem = 0;
+
 char* joystickPos = "CENTRE";
 int blink_interval = 0;
 
 char* ip_addr = "";
 char* gateway_addr = "";
-bool netConnected = false;
+char* host_addr = "";
+int connectTimeout = 1000;
+
+// If we wanted to manually set the MAC address,
+// this is how to do it. In this example, we take
+// the original Mbed Set MAC address and combine it
+// with a prefix of our choosing.
+ /*
+extern "C" void $Super$$mbed_mac_address(char *s);
+extern "C" void $Sub$$mbed_mac_address(char *s) 
+{
+    char otherMac[6] = "";
+    $Super$$mbed_mac_address(otherMac);
+    
+    char mac[6];
+    mac[0] = 0x00;
+    mac[1] = 0x08;
+    mac[2] = 0xdc;
+    mac[3] = otherMac[3];
+    mac[4] = otherMac[4];
+    mac[5] = otherMac[5];
+    memcpy(s, mac, 6);
+}
+*/
 
 
 void off()
@@ -149,39 +178,57 @@
         case 3:
             lcd.printf("MQTT Status:");
             lcd.locate(0,16);
-            lcd.printf(connected ? "Connected" : "Disconnected");
+            if(mqttConnecting){
+                lcd.printf("Connecting... %d/5", retryAttempt);
+            } else {
+                lcd.printf(connected ? "Connected" : "Disconnected");
+            }
             break;
         case 4:
+            lcd.printf("Ethernet State:");
+            lcd.locate(0,16);
+            lcd.printf(ethernetInitialising ? "Initialising..." : "Initialised");
+            break;
+        case 5:
             lcd.printf("Socket State:");
             lcd.locate(0,16);
-            lcd.printf(netConnected ? "Connected" : "Disconnected");
+            if(netConnecting){
+                lcd.printf("Connecting... %d/5", retryAttempt);
+            } else {
+                lcd.printf(netConnected ? "Connected" : "Disconnected");
+            }
             break;
-        case 5:
+        case 6:
             lcd.printf("IP Address:");
             lcd.locate(0,16);
             lcd.printf("%s", ip_addr);
             break;
-        case 6:
+        case 7:
             lcd.printf("Gateway:");
             lcd.locate(0,16);
             lcd.printf("%s",gateway_addr);
             break;
-        case 7:
+        case 8:
             lcd.printf("App version:");
             lcd.locate(0,16);
             lcd.printf("%s",__APP_SW_REVISION__);
             break;
+        case 9:
+            lcd.printf("Current Timeout:");
+            lcd.locate(0,16);
+            lcd.printf("%d ms",connectTimeout);
+            break;
     }
 }
 
 
 void setMenu()
 {
-    static int menuItem = 0;
+    
     if (Down)
     {
         joystickPos = "DOWN";
-        if (menuItem >= 0 && menuItem < 7)
+        if (menuItem >= 0 && menuItem < 9)
             printMenu(++menuItem);
     } 
     else if (Left)
@@ -191,7 +238,7 @@
     else if (Up)
     {
         joystickPos = "UP";
-        if (menuItem <= 7 && menuItem > 0)
+        if (menuItem <= 9 && menuItem > 0)
             printMenu(--menuItem);
     }
     else if (Right)
@@ -202,10 +249,16 @@
 
 void menu_loop(void const *args)
 {
+    int count = 0;
     while(true){
         setMenu();
-         Thread::wait(100);
+        if(++count % 10 == 0)
+        {
+            printMenu(menuItem);
+        }
+        Thread::wait(100);
     }
+    
 }
 
 
@@ -228,25 +281,37 @@
     
     char hostname[strlen(org) + strlen(iot_ibm) + 1];
     sprintf(hostname, "%s%s", org, iot_ibm);
-    DEBUG("hostname is %s\n", hostname);
     EthernetInterface& eth = ipstack->getEth();
     ip_addr = eth.getIPAddress();
     gateway_addr = eth.getGateway();
-    int rc = ipstack->connect(hostname, IBM_IOT_PORT);
+    
+    // Construct clientId - d:org:type:id
+    char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
+    sprintf(clientId, "d:%s:%s:%s", org, type, id);
+    
+    /* Network debug statements */
+    LOG("=====================================\n");
+    LOG("Connecting Ethernet.\n");
+    LOG("IP ADDRESS: %s\n", eth.getIPAddress());
+    LOG("MAC ADDRESS: %s\n", eth.getMACAddress());
+    LOG("Gateway: %s\n", eth.getGateway());
+    LOG("Network Mask: %s\n", eth.getNetworkMask());
+    LOG("Server Hostname: %s\n", hostname);
+    LOG("Client ID: %s\n", clientId);
+    LOG("=====================================\n");
+    
+    netConnecting = true;
+    int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout);
     if (rc != 0){
         WARN("IP Stack returned: %d\n", rc);    
         return rc;
     }
     netConnected = true;
+    netConnecting = false;
     
- 
-     
-    // Construct clientId - d:org:type:id
-    char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
-    sprintf(clientId, "d:%s:%s:%s", org, type, id);
-    DEBUG("clientid is %s\n", clientId);
-    
+
     // MQTT Connect
+    mqttConnecting = true;
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
     data.MQTTVersion = 3;
     data.clientID.cstring = clientId;
@@ -266,6 +331,7 @@
         displayMessage("Scroll with joystick");
 
     }
+    mqttConnecting = false;
     return rc;
 }
 
@@ -279,7 +345,7 @@
 
 void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
 {
-    int retryAttempt = 0;
+    
     connected = false;
     
     // make sure a cable is connected before starting to connect
@@ -386,19 +452,18 @@
     Thread yellow_thread(flashing_yellow);
     Thread menu_thread(menu_loop);  
     
-    printf("connecting...");
+    LOG("***** IBM IoT Client Ethernet Example *****\n");
     MQTTEthernet ipstack;
-    
+    ethernetInitialising = false;
     MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
+    LOG("Ethernet Initialised.\n");
     
     
     if (quickstartMode)
     {
         getMac(ipstack.getEth(), id, sizeof(id));
     }
-    
     attemptConnect(&client, &ipstack);
-    
     if (!quickstartMode) 
     {
         int rc = 0;
@@ -421,8 +486,6 @@
             led2 = LED2_OFF;
         else if (count % blink_interval == 0)
             led2 = !led2;
-        //if (count % 20 == 0)
-        //    setMenu();
         client.yield(10);  // allow the MQTT client to receive messages
     }
 }