Connect through Wifi to IBM MQTT cloud https://quickstart.internetofthings.ibmcloud.com

Dependencies:   MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 X_NUCLEO_IKS01A1 mbed NDefLib X_NUCLEO_NFC01A1

Fork of IDW01M1_Cloud_IBM by ST Expansion SW Team

To start the demo the following expansion boards are required

X_NUCLEO_IDW01M1v2, X_NUCLEO_IKS01A1, X_NUCLEO_NFC01A1

After having mounted the board stack on the Nucleo board the below steps should be followed:

  • Program in the application source code you local WiFi SSID and password and flash the binary. Make sure the Wifi network has visible SSID.
  • Reset the Nucleo board and after few seconds the Nucleo green led will be on (it means the Nucleo is connected to the local Wifi and to the IBM cloud server)
  • Read the NFC tag with an Android device and the browser will be automatically opened and directed to the specific brocker IBM demo page where the environmental values are displayed in form of a x-y graph. The values are updated every few seconds. On the Hyperterminal is possible to see the values sent to the IBM cloud server and the board mac address to be entered on the IBM quickstart web page if a manual connection is needed (eg. to connect from a PC browser).
Revision:
17:83d0cd810ed3
Parent:
16:233b89a6b72f
Child:
18:2c229fa282fa
--- a/main.cpp	Wed Sep 28 15:36:07 2016 +0000
+++ b/main.cpp	Tue Oct 04 11:58:37 2016 +0000
@@ -30,21 +30,33 @@
 DigitalOut myled(LED1);
 SpwfSAInterface spwf(D8, D2, false);
 bool quickstartMode = true;    
+
+#define ORG_QUICKSTART  // comment to connect to play.internetofthings.ibmcloud.com
     
 #define MQTT_MAX_PACKET_SIZE 250   
 #define MQTT_MAX_PAYLOAD_SIZE 300 
+
  // Configuration values needed to connect to IBM IoT Cloud
-#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
+#ifdef ORG_QUICKSTART
+#define ORG "quickstart"     // connect to quickstart.internetofthings.ibmcloud.com/ For a registered connection, replace with your org 
+#define ID ""
+#define AUTH_TOKEN ""
 #define DEFAULT_TYPE_NAME "iotsample-mbed-NucleoF401RE"
+
+#else
+#define ORG "play"             // connect to play.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
+#define ID "testnucleo2"       // For a registered connection, replace with your id
+#define AUTH_TOKEN "centrallab"// For a registered connection, replace with your auth-token
+#define DEFAULT_TYPE_NAME "sensor"
+#endif
+
 #define TYPE DEFAULT_TYPE_NAME       // For a registered connection, replace with your type
 #define MQTT_PORT 1883
 #define MQTT_TLS_PORT 8883
 #define IBM_IOT_PORT MQTT_PORT
 // WiFi network credential
 #define SSID   "crespan"   // Network must be visible otherwise it can't connect
-#define PASSW  ""
+#define PASSW  "Elfrontal0"
 #warning "Wifi password empty"
     
 char id[30] = ID;                 // mac without colons  
@@ -85,9 +97,9 @@
     LOG("IP ADDRESS: %s\n\r", WiFi.get_ip_address());
     LOG("MAC ADDRESS: %s\n\r", WiFi.get_mac_address());
     LOG("Server Hostname: %s\n\r", hostname);
-    for(int i = 0; clientId[i]; i++){
-       clientId[i] = tolower(clientId[i]);
-    }    
+//    for(int i = 0; clientId[i]; i++){
+//       clientId[i] = tolower(clientId[i]);
+//    }    
     LOG("Client ID: %s\n\r", clientId);
     LOG("=====================================\n\r");
     
@@ -105,7 +117,8 @@
     // MQTT Connect
     mqttConnecting = true;
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
-    data.MQTTVersion = 3;
+    data.MQTTVersion = 4;
+    data.struct_version=0;
     data.clientID.cstring = clientId;
  
     if (!quickstartMode) 
@@ -199,12 +212,14 @@
     pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
     pc.printf("\r\nconnecting to AP\r\n");            
 
+   quickstartMode=false;
    if (strcmp(org, "quickstart") == 0){quickstartMode = true;}
    MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2);
    MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
    if (quickstartMode){
         char mac[50];
-        char *digit=NULL;        
+        char *digit=NULL;
+        sprintf (id,"%s", "");                
         sprintf (mac,"%s",ipstack.getWiFi().get_mac_address()); 
         strcpy (mac, ipstack.getWiFi().get_mac_address());
         digit = strtok (mac,":");
@@ -212,7 +227,7 @@
         {
             strcat (id, digit);
             digit = strtok (NULL, ":");
-        }        
+        }                
    }
    attemptConnect(&client, &ipstack);
    if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)