Duy_new_test_Water_Monitor

Dependencies:   MQTT NDefLib

Fork of Cloud_IBM_MbedOS by ST

Revision:
3:e1f6c5af437e
Parent:
2:e3846f091b6b
--- a/main.cpp	Fri Dec 01 11:05:04 2017 +0100
+++ b/main.cpp	Tue Dec 05 17:12:57 2017 +0000
@@ -17,8 +17,8 @@
 #include "mbed.h"
 #include "easy-connect.h"
 #include "MQTTClient.h"
-#include "XNucleoIKS01A2.h"
-#include "XNucleoNFC01A1.h"
+//#include "XNucleoIKS01A2.h"
+//#include "XNucleoNFC01A1.h"
 #include "NDefLib/NDefNfcTag.h"
 #include "NDefLib/RecordType/RecordURI.h"
 #include "MQTTNetwork.h"
@@ -31,19 +31,19 @@
 //------------------------------------
 static Serial pc(SERIAL_TX, SERIAL_RX); 
 static DigitalOut myled(LED1);
-static bool quickstartMode = true;      // set to false to connect with authentication tocken
+static bool quickstartMode = false;      // 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 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 
 
  // Configuration values needed to connect to IBM IoT Cloud
 #ifdef ORG_QUICKSTART
-#define ORG "quickstart"     // connect to quickstart.internetofthings.ibmcloud.com/ For a registered connection, replace with your org 
+#define ORG "a4nvkh"     // 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"
@@ -69,17 +69,18 @@
 static bool connected = false;
 static int retryAttempt = 0;
 static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
+char topicCMD[]   = "iot-2/cmd/command/fmt/json";
 
-static LPS22HBSensor *pressure_sensor;
-static HTS221Sensor  *humidity_sensor;
-static HTS221Sensor  *temp_sensor1;
+//static LPS22HBSensor *pressure_sensor;
+//static HTS221Sensor  *humidity_sensor;
+//static HTS221Sensor  *temp_sensor1;
 
-#ifndef TARGET_SENSOR_TILE
+//#ifndef TARGET_SENSOR_TILE
 static void BlueButtonPressed ()
 {
 	BlueButtonToggle = true;	
 }
-#endif
+//#endif
 
 #ifdef SUBSCRIBE
 void subscribe_cb(MQTT::MessageData & msgMQTT) {
@@ -151,9 +152,13 @@
     connected = true;
     printf ("--->MQTT Connected\n\r"); 
 #ifdef SUBSCRIBE
-		    int rc=0;
-        if ((rc=subscribe(MQTT_TOPIC, client)) == 0) LOG ("--->>>MQTT subscribed to: %s\n\r",MQTT_TOPIC);
-		    else LOG ("--->>>ERROR MQTT subscribe : %s\n\r",MQTT_TOPIC);
+//		    int rc=0;
+        if ((rc=subscribe(topicCMD, client)) == 0) { 
+        	LOG ("--->>>MQTT subscribed to: %s\n\r",topicCMD);
+        }
+		else {
+			LOG ("--->>>ERROR MQTT subscribe : %s\n\r",topicCMD);
+		}
 #endif    
     mqttConnecting = false;
     connack_rc = rc;
@@ -200,9 +205,12 @@
     float temp, press, hum;
 
 	if (!client->isConnected()) { printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE; }
-    temp_sensor1->get_temperature(&temp);
-    pressure_sensor->get_pressure(&press);
-    humidity_sensor->get_humidity(&hum);
+//    temp_sensor1->get_temperature(&temp);
+//    pressure_sensor->get_pressure(&press);
+//    humidity_sensor->get_humidity(&hum);
+	temp = 17.7;
+	press = 70.7;
+	hum = 80.9;
     sprintf(buf,
      "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}",
               temp, press, hum);
@@ -221,24 +229,24 @@
  
 int main()
 {   
-   myled=0;
-   DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
-   i2c->frequency(400000);    
-	
-   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; 
-   pressure_sensor->enable();
-   temp_sensor1->enable();
-   humidity_sensor->enable();
+//   myled=0;
+//   DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
+//   i2c->frequency(400000);    
+//	
+//   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; 
+//   pressure_sensor->enable();
+//   temp_sensor1->enable();
+//   humidity_sensor->enable();
     
-#if !defined (TARGET_SENSOR_TILE)
-    InterruptIn BlueButton(USER_BUTTON);    
-    BlueButton.fall(&BlueButtonPressed);
-	  BlueButtonToggle = false;
-#endif    
-	
+//#if !defined (TARGET_SENSOR_TILE)
+//    InterruptIn BlueButton(USER_BUTTON);    
+//    BlueButton.fall(&BlueButtonPressed);
+	BlueButtonToggle = false;
+//#endif    
+   pc.baud(115200);
    pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
    pc.printf("\r\nconnecting to AP\r\n");            
 
@@ -297,26 +305,26 @@
    int count = 0;    
    while (true)
    {
-		 if (BlueButtonToggle == false && connected == true) {
+	if (BlueButtonToggle == false && connected == true) {
        if (++count == 6)
        {               // Publish a message every 3 second
-           if (publish(&client) != MQTT::SUCCESS) { 
-               myled=0;
-			   count=0;
-             client.disconnect();
-			 mqttNetwork.disconnect();			   
-               attemptConnect(&client, &mqttNetwork, network);   // if we have lost the connection                
-           } else {					 
-			  myled=1;
-              count=0;
-					 }
+    	if (publish(&client) != MQTT::SUCCESS) { 
+        	myled=0;
+			count=0;
+            client.disconnect();
+			mqttNetwork.disconnect();			   
+            attemptConnect(&client, &mqttNetwork, network);   // if we have lost the connection                
+        } else {					 
+			myled=1;
+            count=0;
+		}
        }        
        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;
-			 count = 0;
-			 BlueButtonToggle = false;
+	} else if (BlueButtonToggle == true && connected == true){   // disconnect MQTT
+		printf ("--->> Disconnect\n\r");
+       	connected = false;
+		count = 0;
+		BlueButtonToggle = false;
 #ifdef SUBSCRIBE			 
   //			 unsubscribe(const char* topicFilter);   // unsubscribe if subscribed
 #endif