Publisher for IBM Quickstart and Watson IoT cloud.

Dependencies:   MQTT NDefLib X_NUCLEO_IKS01A2 X_NUCLEO_NFC01A1

Fork of Cloud_IBM_MbedOS by ST Expansion SW Team

To start the demo the following expansion boards are required

X_NUCLEO_IDW01M1v2, X_NUCLEO_IKS01A2, X_NUCLEO_NFC01A1

and as MCU board the NUCLEO-L476RG as it include a True Random Number Generator needed for TLS.

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

  • In case the X-NUCLEO-NFC-01A1 is on the board stack the WiFi SSID and password can be passed through the NFC tag by means of: 1) enabling the NFC support defining the X_NUCLEO_NFC01A1_PRESENT and recompiling, 2) when prompted on hyperterminal, programming the SSID and password to NFC using the Android app "NFCtools"
  • In case the NFC is not present, you local WiFi SSID and password can be programmed to mbed_app.json file and compiling and flashing 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 IBM quickstart 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).

In case of registered connection ( internetofthings.ibmcloud.com ) is needed ( no TLS ) comment the #define ORG_QUICKSTART than check in the mbed_app.json the following fields and change them according to your IBM MQTT broker account, MQTT_ORG_ID, MQTT_DEVICE_PASSWORD, MQTT_DEVICE_ID, MQTT_DEVICE_TYPE.

In case of registered connection ( internetofthings.ibmcloud.com ) with TLS encryption is needed, uncomment the #define TLS_EN and make sure the certificate (SSL_CA_PEM) is still valid.

In the default case the application connect to quickstart.internetofthings.ibmcloud.com without any encryption not authentication.

Revision:
5:efa13fc5d99a
Parent:
4:df4138621205
Child:
7:d18775ea6734
--- a/main.cpp	Fri Jan 05 14:53:46 2018 +0100
+++ b/main.cpp	Wed Jan 24 10:42:08 2018 +0100
@@ -15,6 +15,7 @@
  */
 
 #include "mbed.h"
+#include <string.h>
 #include "easy-connect.h"
 #include "MQTTClient.h"
 #include "XNucleoIKS01A2.h"
@@ -22,9 +23,41 @@
 #include "NDefNfcTag.h"
 #include "NDefLib/RecordType/RecordURI.h"
 #include "RecordWifiConf.h"
+#include "MQTTmbed.h"
 #include "MQTTNetwork.h"
-#include "MQTTmbed.h"
+
+/****  System configuration define   ****/
+#define ORG_QUICKSTART           // comment to connect to play.internetofthings.ibmcloud.com
+//#define SUBSCRIBE              // uncomment to subscribe to broker msgs (not to be used with IBM broker) 
+#define X_NUCLEO_NFC01A1_PRESENT // uncomment to add NFC support
+#ifndef ORG_QUICKSTART
+//#define TLS_EN                   // uncomment to add TLS to NON quickstart connections
+#endif
 
+#ifdef TLS_EN     // Digicert Root Certificate in PEM format (from IBM website)
+const char SSL_CA_PEM[] ="-----BEGIN CERTIFICATE-----\n"
+"MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n"
+"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
+"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n"
+"QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n"
+"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
+"b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n"
+"9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n"
+"CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n"
+"nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n"
+"43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n"
+"T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n"
+"gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n"
+"BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n"
+"TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n"
+"DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n"
+"hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n"
+"06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n"
+"PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n"
+"YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n"
+"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n"
+"-----END CERTIFICATE-----\n";
+#endif
 
 //------------------------------------
 // Hyperterminal configuration
@@ -34,10 +67,6 @@
 static DigitalOut myled(LED1);
 static bool quickstartMode = true;      // set to false to connect with authentication tocken
 static bool BlueButtonToggle = false;  
-
-#define ORG_QUICKSTART           // comment to connect to play.internetofthings.ibmcloud.com
-//#define SUBSCRIBE              // uncomment to subscribe to broker msgs (not to be used with IBM broker) 
-#define X_NUCLEO_NFC01A1_PRESENT // uncomment to add NFC support
     
 #define MQTT_MAX_PACKET_SIZE 400   
 #define MQTT_MAX_PAYLOAD_SIZE 300 
@@ -49,15 +78,21 @@
 #define AUTH_TOKEN ""
 //#define DEFAULT_TYPE_NAME "iotsample-mbed-Nucleo"
 #define DEFAULT_TYPE_NAME "sensor"
+#define DEFAULT_PORT  MQTT_PORT
 #else   // not def ORG_QUICKSTART
 #define ORG MQTT_ORG_ID            // connect to ORG.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
 #define ID MQTT_DEVICE_ID          // For a registered connection is your device id
 #define AUTH_TOKEN  MQTT_DEVICE_PASSWORD  // For a registered connection is a device auth-token
 #define DEFAULT_TYPE_NAME  MQTT_DEVICE_TYPE  // For a registered connection is device type
+#ifdef TLS_EN
+#define DEFAULT_PORT  MQTT_TLS_PORT
+#else
+#define DEFAULT_PORT  MQTT_PORT
+#endif
 #endif
 
 #define TYPE DEFAULT_TYPE_NAME       // For a registered connection, replace with your type
-#define IBM_IOT_PORT MQTT_PORT
+#define IBM_IOT_PORT  DEFAULT_PORT
  
 #define MAXLEN_MBED_CONF_APP_WIFI_SSID       32  // same as WIFI_SSID_MAX_LEN in easy_connect
 #define MAXLEN_MBED_CONF_APP_WIFI_PASSWORD   64  // same as WIFI_PASSWORD_MAX_LEN
@@ -101,13 +136,13 @@
                 NDefLib::Record *r = readMsg[i];
 //              printf ("N record %d\n\r", readMsg.get_N_records());
 				if (r != NULL) {
-					printf ("Record RecordType_t: %d\n\r", r->get_type());
+//					printf ("Record RecordType_t: %d\n\r", r->get_type());
                     if (r->get_type() == NDefLib::Record::TYPE_WIFI_CONF) {
                         NDefLib::RecordWifiConf * temp = (NDefLib::RecordWifiConf*)r;
                         sprintf (ssid, "%s", temp->get_network_ssid().c_str());
                         sprintf (seckey, "%s", temp->get_network_key().c_str());
                         printf ("Read SSID: %s Passw: %s\n\r", ssid, seckey); 
-						ReadSSIDPassw =1;
+						            ReadSSIDPassw =1;
                     }                        
                     else if (r->get_type() == NDefLib::Record::TYPE_UNKNOWN) { printf ("NFC RECORD TYPE_UNKNOWN\n\r"); }
                 }
@@ -200,31 +235,46 @@
     LOG("Subscription URL: %s\n\r", subscription_url);
     LOG("=====================================\n\r");    
     netConnecting = true;
-    int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
+
+#ifdef ORG_QUICKSTART
+int tls = TLS_OFF;
+const char * cert = NULL;
+unsigned int sizeof_cert = 0;
+#else  // if !QUICKSTART possible to connect with TLS or not
+#ifdef TLS_EN
+int tls = TLS_ON;
+const char * cert = SSL_CA_PEM;
+unsigned int sizeof_cert = sizeof(SSL_CA_PEM);
+#else
+int tls = TLS_OFF;
+const char * cert = 0;
+unsigned int sizeof_cert = 0;
+#endif
+#endif
+
+    int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT, tls, cert, sizeof_cert);
     if (rc != 0)
     {
         printf("rc from TCP connect is %d\r\n", rc);
         return rc;
     }
-    
-    printf ("--->TCP Connected\n\r");
     netConnected = true;
     netConnecting = false;		
-		
+
     // MQTT Connect
     mqttConnecting = true;
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
     data.MQTTVersion = 4;
     data.struct_version=0;
     data.clientID.cstring = clientId; 
-	data.keepAliveInterval = MQTT_KEEPALIVE;  // in Sec
+	  data.keepAliveInterval = 0; //MQTT_KEEPALIVE;  // in Sec
     if (!quickstartMode) 
     {        
         data.username.cstring = "use-token-auth";
         data.password.cstring = auth_token;
         printf ("AutToken: %s\n\r", auth_token);
     }   
-    if ((rc = client->connect(data)) != 0) {
+    if ((rc = client->connect(data)) != MQTT::SUCCESS) {
         printf("rc from MQTT connect is %d\r\n", rc);
         connack_rc = rc;
         return rc;
@@ -256,7 +306,7 @@
     while (connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED) 
     {    
         if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
-            printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);        
+            printf ("Error MQTT_BAD_USERNAME_OR_PASSWORDFile: %s, Line: %d Error: %d \n\r",__FILE__,__LINE__, connack_rc);        
             return; // don't reattempt to connect if credentials are wrong
         } 
         int timeout = getConnTimeout(++retryAttempt);
@@ -299,14 +349,15 @@
     LOG("Publishing %s\n\r", buf);
     return client->publish(pubTopic, message);    
 }
+
  
 int main()
 {   
    myled=0;
    DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
    i2c->frequency(400000);    
-	
-   XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(i2c);   
+
+   XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(i2c);  
    pressure_sensor = mems_expansion_board->pt_sensor;
    temp_sensor1 = mems_expansion_board->ht_sensor;  
    humidity_sensor = mems_expansion_board->ht_sensor; 
@@ -321,6 +372,11 @@
 #endif    
 	
    pc.printf("\r\nCloud_IBM_MbedOS Application\r\n");     
+#if defined(MBED_MAJOR_VERSION)
+    printf("Using Mbed OS %d.%d.%d\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
+#else
+    printf("Using Mbed OS from master.\n");
+#endif	
    pc.printf("\r\nconnecting to AP\r\n");            
 
    quickstartMode=false;
@@ -340,7 +396,24 @@
    if (!network) {
        printf ("Error easy_connect\n\r");
        return -1;
-   }   
+   }  
+//=================  TODO Set System Time ideally from NTP srv or from shell
+#if 0
+    time_t ctTime;
+    ctTime = time(NULL);             
+    printf ("Start Secure Socket connection with one way server autentication test\n\r");                
+    printf("Initial System Time is: %s\r\n", ctime(&ctTime));   
+    printf("Need to adjust time? if yes enter time in seconds elapsed since Epoch (cmd: date +'%%s'), otherwise enter 0 ");                
+    int t=0;
+    scanf("%d",&t);
+    printf ("entered time is: %d \n\r", t);
+    if (t != 0) { time_t txTm = t; set_time(txTm); }  // set Nucleo system time
+    ctTime = time(NULL);    
+    printf ("The current system time is: %s", ctime (&ctTime));  // set WiFi module systm time
+    WiFiInterface* wifi = easy_get_wifi(0);
+    if (!((SpwfSAInterface*)wifi)->set_time(ctTime)) printf ("ERROR set_time\n\r");  
+#endif    
+//=================   
    MQTTNetwork mqttNetwork(network);	
    MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
 		
@@ -374,31 +447,34 @@
    {
 		 if (BlueButtonToggle == false && connected == true) {
        if (++count == 6)
-       {               // Publish a message every 3 second
+       {   
+				 // Publish a message every 3 second
            if (publish(&client) != MQTT::SUCCESS) { 
                myled=0;
-			   count=0;
-             client.disconnect();
-			 mqttNetwork.disconnect();			   
+			         count=0;
+               client.disconnect();
+			         mqttNetwork.disconnect();			   
                attemptConnect(&client, &mqttNetwork, network);   // if we have lost the connection                
            } else {					 
-			  myled=1;
+			        myled=1;
               count=0;
 					 }
        }        
-       client.yield(500);  // allow the MQTT client to receive subscribe messages and manage keep alive
+       if (client.isConnected()) client.yield(500);  // allow the MQTT client to receive subscribe messages and manage keep alive
 		 } else if (BlueButtonToggle == true && connected == true){   // disconnect MQTT
 			 printf ("--->> MQTT Disconnect\n\r");
-             connected = false;
+       connected = false;
+			 myled=0;
 			 count = 0;
 			 BlueButtonToggle = false;
 #ifdef SUBSCRIBE			 
-  //			 unsubscribe(const char* topicFilter);   // unsubscribe if subscribed
+//			 unsubscribe(const char* topicFilter);   // unsubscribe if subscribed
 #endif			 
 			 client.disconnect();
-             printf ("--->> TCP Disconnect\n\r");
+       printf ("--->> TCP Disconnect\n\r");
 			 mqttNetwork.disconnect();
 		 } else if (BlueButtonToggle == true && connected == false) {
+			 attemptConnect(&client, &mqttNetwork, network); 
 			 connected = true;
 			 BlueButtonToggle = false;
 	 } else wait (0.5);