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:
4:df4138621205
Parent:
2:e3846f091b6b
Child:
5:efa13fc5d99a
--- a/main.cpp	Wed Jan 03 17:06:48 2018 +0100
+++ b/main.cpp	Fri Jan 05 14:53:46 2018 +0100
@@ -19,8 +19,9 @@
 #include "MQTTClient.h"
 #include "XNucleoIKS01A2.h"
 #include "XNucleoNFC01A1.h"
-#include "NDefLib/NDefNfcTag.h"
+#include "NDefNfcTag.h"
 #include "NDefLib/RecordType/RecordURI.h"
+#include "RecordWifiConf.h"
 #include "MQTTNetwork.h"
 #include "MQTTmbed.h"
 
@@ -46,7 +47,8 @@
 #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-Nucleo"
+//#define DEFAULT_TYPE_NAME "iotsample-mbed-Nucleo"
+#define DEFAULT_TYPE_NAME "sensor"
 #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
@@ -56,7 +58,10 @@
 
 #define TYPE DEFAULT_TYPE_NAME       // For a registered connection, replace with your type
 #define IBM_IOT_PORT MQTT_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
+ 
 static char id[30] = ID;                 // mac without colons  
 static char org[12] = ORG;        
 static int connack_rc = 0; // MQTT connack return code
@@ -69,11 +74,87 @@
 static bool connected = false;
 static int retryAttempt = 0;
 static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
+static char  ssid[MAXLEN_MBED_CONF_APP_WIFI_SSID];     // Network must be visible otherwise it can't connect
+static char  seckey[MAXLEN_MBED_CONF_APP_WIFI_PASSWORD]; 
 
 static LPS22HBSensor *pressure_sensor;
 static HTS221Sensor  *humidity_sensor;
 static HTS221Sensor  *temp_sensor1;
 
+#ifdef X_NUCLEO_NFC01A1_PRESENT  
+// Read from NFC the Wifi record
+void NFCReadRecordWIFI (XNucleoNFC01A1 *nfcNucleo) {   
+   
+    NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
+
+    printf ("Write to NFC tag the WiFi record ...\n\r");   
+    for (int ReadSSIDPassw=0; ReadSSIDPassw!=1; wait_ms(1000)) {   
+    //open the i2c session with the nfc chip
+	    NDefLib::Message readMsg;
+        if(tag.open_session(1)){
+        tag.read(&readMsg);
+//        printf ("---- N record %d\n\r", readMsg.get_N_records());		
+        if(readMsg.get_N_records()==0){
+            printf("Error Read\r\n");
+        }else {
+            for(uint32_t i=0;i<readMsg.get_N_records();i++){
+                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());
+                    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;
+                    }                        
+                    else if (r->get_type() == NDefLib::Record::TYPE_UNKNOWN) { printf ("NFC RECORD TYPE_UNKNOWN\n\r"); }
+                }
+                if (r != NULL) delete r;
+            }//for all the NFC records
+        }//nfc n records              
+        //close the i2c session
+        if(!tag.close_session()){
+            printf("Error Closing the session\r\n");
+        }
+     }else printf("Error open Session\r\n");  
+   }    
+}
+
+// add to NFC the HTTP_BROKER_URL URL swapping the two NFC records in order to set the HTTP_BROKER_URL as a first 
+// record allowing to read URL from any device. 
+void NFCWriteRecordURI (XNucleoNFC01A1 *nfcNucleo) {
+         
+    NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
+    //open the i2c session with the nfc chip
+    if(tag.open_session()){
+       //create the NDef message and record
+        NDefLib::Message msg;
+		tag.read(&msg);
+//		printf ("---- N record present: %d\n\r", msg.get_N_records());	
+		NDefLib::Record *r = msg[0];
+        if ((r != NULL) && (r->get_type() == NDefLib::Record::TYPE_WIFI_CONF)) {  // first time record 0 must be wifi
+ 			NDefLib::RecordWifiConf * const pWIFI = (NDefLib::RecordWifiConf*)r;  
+            NDefLib::Message msg2;				
+            NDefLib::RecordURI rURL(NDefLib::RecordURI::HTTPS, subscription_url);
+            msg2.add_record(&rURL);   // add a new HTTP record to msg2
+            msg2.add_record(pWIFI);	  // copy a Wifi record to msg2
+            //write the tag
+            if(tag.write(msg2)){
+                printf("Tag writed \r\n");
+            }
+		    NDefLib::Message::remove_and_delete_all_record(msg);										                
+		}
+        //close the i2c session
+        if(!tag.close_session()){
+            printf("Error Closing the session\r\n");
+        }				
+     }else printf("Error open Session\r\n"); 
+}        
+
+#endif
+
 #ifndef TARGET_SENSOR_TILE
 static void BlueButtonPressed ()
 {
@@ -104,7 +185,7 @@
     // Construct clientId - d:org:type:id
     char clientId[strlen(org) + strlen(type) + strlen(id) + 5];  
     sprintf(clientId, "d:%s:%s:%s", org, type, id);  
-    sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, DEFAULT_TYPE_NAME);
+    sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, TYPE);
 
     // Network debug statements 
     LOG("=====================================\n\r");
@@ -199,7 +280,7 @@
     char buf[MQTT_MAX_PAYLOAD_SIZE];
     float temp, press, hum;
 
-	if (!client->isConnected()) { printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE; }
+	if (!client->isConnected()) { printf ("publish failed: MQTT disconnected\n\r"); return MQTT::FAILURE; }
     temp_sensor1->get_temperature(&temp);
     pressure_sensor->get_pressure(&press);
     humidity_sensor->get_humidity(&hum);
@@ -239,16 +320,27 @@
 	  BlueButtonToggle = false;
 #endif    
 	
-   pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
+   pc.printf("\r\nCloud_IBM_MbedOS Application\r\n");     
    pc.printf("\r\nconnecting to AP\r\n");            
 
    quickstartMode=false;
    if (strcmp(org, "quickstart") == 0){quickstartMode = true;}
-   NetworkInterface* network = easy_connect(true);
+   
+#ifdef X_NUCLEO_NFC01A1_PRESENT      
+   // program NFC with broker URL        
+   XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(*i2c, NULL, XNucleoNFC01A1::DEFAULT_GPO_PIN, XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN, NC,NC,NC);  
+   NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
+   printf("NFC Init done: !\r\n");
+   //open the i2c session with the nfc chip
+   NFCReadRecordWIFI (nfcNucleo);
+   NetworkInterface* network = easy_connect(true, ssid, seckey);  // Wifi SSID and passw from NFC tag
+#else
+   NetworkInterface* network = easy_connect(true);   // // Wifi SSID and passw from mbed_app.json
+#endif    
    if (!network) {
        printf ("Error easy_connect\n\r");
        return -1;
-   } 
+   }   
    MQTTNetwork mqttNetwork(network);	
    MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
 		
@@ -272,27 +364,10 @@
        while (true)
        wait(1.0); // Permanent failures - don't retry
    }
-#ifdef X_NUCLEO_NFC01A1_PRESENT      
-   // program NFC with broker URL        
-   XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(*i2c, NULL, XNucleoNFC01A1::DEFAULT_GPO_PIN, XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN, NC,NC,NC);  
-   NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
-   printf("NFC Init done: !\r\n");
-   //open the i2c session with the nfc chip
-   if(tag.open_session()){
-       //create the NDef message and record
-       NDefLib::Message msg;
-       NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTPS, subscription_url);
-       msg.add_record(&rUri);
-       //write the tag
-       if(tag.write(msg)){
-           printf("Tag writed \r\n");
-       }
-       //close the i2c session
-       if(!tag.close_session()){
-           printf("Error Closing the session\r\n");
-       }
-   }else printf("Error open Session\r\n");             
-#endif    
+
+#ifdef X_NUCLEO_NFC01A1_PRESENT 
+   NFCWriteRecordURI (nfcNucleo);
+#endif   
    myled=1;         
    int count = 0;    
    while (true)
@@ -313,14 +388,15 @@
        }        
        client.yield(500);  // allow the MQTT client to receive subscribe messages and manage keep alive
 		 } else if (BlueButtonToggle == true && connected == true){   // disconnect MQTT
-			 printf ("--->> Disconnect\n\r");
-       connected = false;
+			 printf ("--->> MQTT Disconnect\n\r");
+             connected = false;
 			 count = 0;
 			 BlueButtonToggle = false;
 #ifdef SUBSCRIBE			 
   //			 unsubscribe(const char* topicFilter);   // unsubscribe if subscribed
 #endif			 
 			 client.disconnect();
+             printf ("--->> TCP Disconnect\n\r");
 			 mqttNetwork.disconnect();
 		 } else if (BlueButtonToggle == true && connected == false) {
 			 connected = true;