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:
24:a59eb8bdc431
Parent:
23:43f9e34781f2
--- a/main.cpp	Wed Oct 26 15:28:35 2016 +0000
+++ b/main.cpp	Thu Nov 24 13:22:03 2016 +0000
@@ -31,7 +31,6 @@
 //------------------------------------
 Serial pc(SERIAL_TX, SERIAL_RX); 
 DigitalOut myled(LED1);
-SpwfSAInterface spwf(D8, D2, false);
 bool quickstartMode = true;    
 
 #define ORG_QUICKSTART           // comment to connect to play.internetofthings.ibmcloud.com
@@ -109,7 +108,7 @@
     char hostname[strlen(org) + strlen(iot_ibm) + 1];
     sprintf(hostname, "%s%s", org, iot_ibm);
     SpwfSAInterface& WiFi = ipstack->getWiFi();
-    ip_addr = WiFi.get_ip_address();
+//    ip_addr = WiFi.get_ip_address();
     // Construct clientId - d:org:type:id
     char clientId[strlen(org) + strlen(type) + strlen(id) + 5];  
     sprintf(clientId, "d:%s:%s:%s", org, type, id);  
@@ -121,10 +120,10 @@
     LOG("Nucleo IP ADDRESS: %s\n\r", WiFi.get_ip_address());
     LOG("Nucleo MAC ADDRESS: %s\n\r", WiFi.get_mac_address());
     LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
-//    for(int i = 0; clientId[i]; i++){
-//       clientId[i] = tolower(clientId[i]);
+//    for(int i = 0; clientId[i]; i++){  // set lowercase mac
+//       clientId[i] = tolower(clientId[i]); 
 //    }    
-    LOG("Client ID: %s id: %s\n\r", clientId, id);
+    LOG("Client ID: %s\n\r", clientId);
     LOG("Topic: %s\n\r",TOPIC);
     LOG("Subscription URL: %s\n\r", subscription_url);
     LOG("=====================================\n\r");
@@ -218,7 +217,8 @@
     message.payload = (void*)buf;
     message.payloadlen = strlen(buf);
     
-    LOG("Publishing %s\n\r", buf);
+//    LOG("Publishing %s\n\r", buf);
+    printf("Publishing %s\n\r", buf);
     return client->publish(pubTopic, message);
 } 
     
@@ -226,9 +226,10 @@
 {
     const char * ssid = SSID; // Network must be visible otherwise it can't connect
     const char * seckey = PASSW;
+    SpwfSAInterface spwf(D8, D2, false);
     
 //    Timer tyeld;
-
+    myled=0;
     DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
     i2c->frequency(400000);    
     
@@ -245,17 +246,16 @@
    MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2);
    MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
    if (quickstartMode){
-        char mac[50];
+        char mac[50];  // remove all : from mac
         char *digit=NULL;
         sprintf (id,"%s", "");                
         sprintf (mac,"%s",ipstack.getWiFi().get_mac_address()); 
-        strcpy (mac, ipstack.getWiFi().get_mac_address());
         digit = strtok (mac,":");
         while (digit != NULL)
         {
             strcat (id, digit);
             digit = strtok (NULL, ":");
-        }                
+        }     
    }
    attemptConnect(&client, &ipstack);
    if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)    
@@ -283,20 +283,22 @@
             printf("Error Closing the session\r\n");
         }
     }else printf("Error open Session\r\n");             
-#endif             
+#endif    
+   myled=1;         
    int count = 0;    
 //    tyeld.start();    
     while (true)
     {
-        if (++count == /*100*/2)
+        if (++count == 100)
         {               // Publish a message every second
             if (publish(&client, &ipstack) != 0) { 
+                myled=0;
                 attemptConnect(&client, &ipstack);   // if we have lost the connection                
-            }
+            } else myled=1;
             count = 0;
         }        
 //        int start = tyeld.read_ms();
-        client.yield(/*10*/1);  // allow the MQTT client to receive messages
+        client.yield(10);  // allow the MQTT client to receive messages
 //        printf ("tyeld: %d\n\r",tyeld.read_ms()-start);
     }
 }