Duy tran / Mbed OS iot_water_monitor_v2

Dependencies:   easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code

Files at this revision

API Documentation at this revision

Comitter:
DuyLionTran
Date:
Tue Dec 12 17:53:40 2017 +0000
Parent:
16:a4118bfd7b41
Child:
18:5585365d6951
Commit message:
version 1.6: Project is re-organized, RTC is added, Sensor Calibration and Flash is being developed

Changed in this revision

Sensor/ReadSensor.cpp Show diff for this revision Revisions of this file
Sensor/ReadSensor.h Show diff for this revision Revisions of this file
Simple-MQTT/SimpleMQTT.h Show annotated file Show diff for this revision Revisions of this file
SimpleMQTT.cpp Show diff for this revision Revisions of this file
SimpleMQTT.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- a/Sensor/ReadSensor.cpp	Tue Dec 12 16:00:32 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-#include "mbed.h"
-#include "ReadSensor.h"
-
-const float saturationValueTab[41] = {      //saturation dissolved oxygen concentrations at various temperatures
-14.46, 14.22, 13.82, 13.44, 13.09,
-12.74, 12.42, 12.11, 11.81, 11.53,
-11.26, 11.01, 10.77, 10.53, 10.30,
-10.08, 9.86,  9.66,  9.46,  9.27,
-9.08,  8.90,  8.73,  8.57,  8.41,
-8.25,  8.11,  7.96,  7.82,  7.69,
-7.56,  7.43,  7.30,  7.18,  7.07,
-6.95,  6.84,  6.73,  6.63,  6.53,
-6.41,
-};
-
-float saturationDoVoltage; 
-float saturationDoTemperature;
-float averageVoltage;
-
-AnalogIn    phSensor(SENSOR_1_PIN);
-AnalogIn    DOSensor(SENSOR_2_PIN);
-
-void SENSOR_Calib() {
-    
-}
-
-float SENSOR_ReadPHValue() {
-    return phSensor.read();
-}
\ No newline at end of file
--- a/Sensor/ReadSensor.h	Tue Dec 12 16:00:32 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,18 +0,0 @@
-#ifndef __READSENSOR_H__
-#define __READSENSOR_H__
-
-#define SENSOR_1_PIN  (A0)
-#define SENSOR_2_PIN  (A1)
-#define SENSOR_3_PIN  (A3)
-#define SENSOR_4_PIN  (A4)
-
-#define SaturationDoVoltageAddress     12          //the address of the Saturation Oxygen voltage stored in the Flash
-#define SaturationDoTemperatureAddress 16      //the address of the Saturation Oxygen temperature stored in the Flash
-
-#define VREF           3.3 
-#define SCOUNT         30 
-
-void SENSOR_Calib();
-float SENSOR_ReadPHValue();
-
-#endif /* __READSENSOR_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simple-MQTT/SimpleMQTT.h	Tue Dec 12 17:53:40 2017 +0000
@@ -0,0 +1,213 @@
+#ifndef __SIMPLEMQTT_H__
+#define __SIMPLEMQTT_H__
+
+/***************************************************************
+ * Includes
+ ***************************************************************/
+#include "easy-connect.h"
+#include "MQTTClient.h"
+#include "NDefLib/NDefNfcTag.h"
+#include "NDefLib/RecordType/RecordURI.h"
+#include "MQTTNetwork.h"
+#include "MQTTmbed.h"
+
+/***************************************************************
+ * Definitions
+ ***************************************************************/
+ // Configuration values needed to connect to IBM IoT Cloud
+#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
+#define AUTH_METHOD         MQTT_USERNAME
+
+#define TYPE                DEFAULT_TYPE_NAME       // For a registered connection, replace with your type
+#define IBM_IOT_PORT        MQTT_PORT
+
+#define MQTT_MAX_PACKET_SIZE    400   
+#define MQTT_MAX_PAYLOAD_SIZE   300 
+
+/***************************************************************
+ * Variables
+ ***************************************************************/
+char       *projectName     = "WaterMonitor";
+static char id[30]          = ID;               // mac without colons  
+static char org[12]         = ORG;        
+static char type[30]        = TYPE;
+static char auth_token[30]  = AUTH_TOKEN;       // Auth_token is only used in non-quickstart mode
+static int  connack_rc      = 0;                // MQTT connack return code
+static bool netConnecting   = false;
+static bool mqttConnecting  = false;
+static bool netConnected    = false;
+static bool connected       = false;
+static int  retryAttempt    = 0;
+static int  connectTimeout  = 1000;
+static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
+
+/***************************************************************
+ * Unity function definitions
+ ***************************************************************/
+/** brief       Callback function when MQTT message arrives
+ *  param[in]   msgMQTT
+ *  retral      None 
+ */ 
+void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT); 
+
+/** brief       Subscribe to a MQTT topic and set the MQTT callback function
+ *  param[in]   subscribeTopic Topic to be subscribed
+ *  param[in]   client         MQTT client 
+ *  retral      returnCode from MQTTClient.h 
+ */ 
+int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client);
+
+/** brief       Connect to the internet then the MQTT network
+ *  param[in]   client          MQTT client 
+ *  param[in]   mqttNetwork     MQTT network 
+ *  param[in]   network         The internet network interface (ethernet, wifi...)
+ *  retral      Internet connect result and returnCode from MQTTClient.h 
+ */ 
+int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
+
+/** brief       Setup the number of attempt to re-connect to the internet
+ *  param[in]   attemptNumber The number of attemp
+ */ 
+int MQTT_GetConnTimeout(int attemptNumber);
+void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
+
+/** brief       Connect to the internet then the MQTT network
+ *  param[in]   client        MQTT client 
+ *  param[in]   inputTime     The time when the data is attempt to be sent 
+ *  param[in]   commandID     Sequence number of the frame
+ *  param[in]   adcVal_0      The ADC value to be sent
+ *  retral      returnCode from MQTTClient.h 
+ */
+int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint16_t commandID, float adcVal_0);
+
+/***************************************************************
+ * Unity function declarations
+ ***************************************************************/ 
+void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT) {
+    char msg[MQTT_MAX_PAYLOAD_SIZE];
+    msg[0]='\0';
+    strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
+    printf ("--->>> MQTT_SubscribeCallback msg: %s\n\r", msg);
+}
+
+int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) {
+    return client->subscribe(subscribeTopic, MQTT::QOS1, MQTT_SubscribeCallback);
+}
+
+int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
+    const char* iot_ibm = MQTT_BROKER_URL;     
+    char hostname[strlen(org) + strlen(iot_ibm) + 1];
+    
+    sprintf(hostname, "%s%s", org, iot_ibm);
+    // 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);
+
+    // Network debug statements 
+    LOG("=====================================\n\r");
+    LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
+    LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
+    LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
+    LOG("Client ID: %s\n\r", clientId);
+    LOG("Topic: %s\n\r",MQTT_EVENT_TOPIC);
+    LOG("Subscription URL: %s\n\r", subscription_url);
+    LOG("=====================================\n\r");    
+    netConnecting = true;
+    int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
+    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.username.cstring       = AUTH_METHOD;
+    data.password.cstring       = auth_token;
+    printf ("AutToken: %s\n\r", auth_token);
+       
+    if ((rc = client->connect(data)) != 0) {
+        printf("rc from MQTT connect is %d\r\n", rc);
+        connack_rc = rc;
+        return rc;
+    }
+    connected = true;
+    printf ("--->MQTT Connected\n\r"); 
+    if ((rc = MQTT_Subscribe(MQTT_COMMAND_TOPIC, client)) == 0) { 
+        LOG ("--->>>MQTT subscribed to: %s\n\r", MQTT_COMMAND_TOPIC);
+    } else {
+        LOG ("--->>>ERROR MQTT subscribe : %s\n\r", MQTT_COMMAND_TOPIC);
+    }  
+    mqttConnecting = false;
+    connack_rc = rc;
+    return rc;       
+}
+
+
+int MQTT_GetConnTimeout(int attemptNumber) {  // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
+   // after 20 attempts, retry every 10 minutes
+    return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
+}
+
+
+void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
+    connected = false;
+           
+    while (MQTT_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);        
+            return; // don't reattempt to connect if credentials are wrong
+        } 
+        int timeout = MQTT_GetConnTimeout(++retryAttempt);
+        WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
+        
+        // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
+        //  or maybe just add the proper members to do this disconnect and call MQTT_AttemptConnect(...)        
+        // this works - reset the system when the retry count gets to a threshold
+        if (retryAttempt == 5)
+            NVIC_SystemReset(); 
+        else
+            wait(timeout);
+    }    
+}
+
+int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint16_t commandID, float adcVal_0) {
+    MQTT::Message message;
+    const char* pubTopic = MQTT_EVENT_TOPIC;
+            
+    char buf[MQTT_MAX_PAYLOAD_SIZE];
+    char timeBuf[50];
+
+    if (!client->isConnected()) { 
+        printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE; 
+    }
+    
+    strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
+    sprintf(buf,
+     "{\"Project\":\"%s\",\"Time\":\"%s\",\"Type\":1,\"cmdID\":%d,\"ADC0\":%0.2f}",
+              projectName, timeBuf, commandID, adcVal_0);
+    message.qos        = MQTT::QOS0;
+    message.retained   = false;
+    message.dup        = false;
+    message.payload    = (void*)buf;
+    message.payloadlen = strlen(buf);
+
+    if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
+        printf("message too long!\r\n");
+    
+    LOG("Publishing %s\n\r", buf);
+    return client->publish(pubTopic, message);    
+}
+#endif /* __SIMPLEMQTT_H__ */
\ No newline at end of file
--- a/SimpleMQTT.cpp	Tue Dec 12 16:00:32 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-#include "SimpleMQTT.h"
-
-#include "mbed.h"
--- a/SimpleMQTT.h	Tue Dec 12 16:00:32 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-#ifndef __SIMPLEMQTT_H__
-#define __SIMPLEMQTT_H__
-
-#endif /* __SIMPLEMQTT_H__ */
\ No newline at end of file
--- a/main.cpp	Tue Dec 12 16:00:32 2017 +0000
+++ b/main.cpp	Tue Dec 12 17:53:40 2017 +0000
@@ -2,57 +2,23 @@
  * Includes
  ***************************************************************/
 #include "mbed.h"
-#include "easy-connect.h"
-#include "MQTTClient.h"
-#include "NDefLib/NDefNfcTag.h"
-#include "NDefLib/RecordType/RecordURI.h"
-#include "MQTTNetwork.h"
-#include "MQTTmbed.h"
+
 #include "ReadSensor.h"
 #include "SimpleMQTT.h"
 
 /***************************************************************
  * Definitions
  ***************************************************************/
-// Configuration values needed to connect to IBM IoT Cloud
-#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
-#define AUTH_METHOD         MQTT_USERNAME
-
-#define TYPE 				DEFAULT_TYPE_NAME       // For a registered connection, replace with your type
-#define IBM_IOT_PORT 		MQTT_PORT
-
-#define MQTT_MAX_PACKET_SIZE 	400   
-#define MQTT_MAX_PAYLOAD_SIZE 	300 
-
 #define RELAY_1_PIN   (D11)
 #define RELAY_2_PIN   (D12)
 
 /***************************************************************
  * Variables
  ***************************************************************/
-char       *projectName     = "WaterMonitor";
 uint16_t    cmdID           = 0;
 uint32_t 	lastRead        = 0;
 uint32_t    uploadInterval  = 5;
 
-/* MQTT Varialbles */ 
-static char id[30] 			= ID;               // mac without colons  
-static char org[12] 		= ORG;        
-static char type[30] 		= TYPE;
-static char auth_token[30] 	= AUTH_TOKEN; 		// Auth_token is only used in non-quickstart mode
-static int 	connack_rc 		= 0; 				// MQTT connack return code
-static bool netConnecting 	= false;
-static bool mqttConnecting 	= false;
-static bool netConnected 	= false;
-static bool connected 		= false;
-static int 	retryAttempt 	= 0;
-static int 	connectTimeout 	= 1000;
-static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
-char 		topicCMD[]   	= "iot-2/cmd/command/fmt/json";
-
 /* Analog Handles */
 float ADC_PHVal;
 float ADC_DOVal;
@@ -65,144 +31,15 @@
 DigitalOut 	myled(LED1);
 
 Timer timer;
-
 /***************************************************************
  * Unity function definitions
  ***************************************************************/
-void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT); 
-int MQTT_Subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client);
-int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
-int MQTT_GetConnTimeout(int attemptNumber);
-void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
-int MQTT_Publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime);
-
+ 
 /***************************************************************
  * Unity function declarations
  ***************************************************************/ 
-void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT) {
-    char msg[MQTT_MAX_PAYLOAD_SIZE];
-    msg[0]='\0';
-    strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
-    printf ("--->>> MQTT_SubscribeCallback msg: %s\n\r", msg);
-}
-
-int MQTT_Subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) {
-    return client->subscribe(pubTopic, MQTT::QOS1, MQTT_SubscribeCallback);
-}
-
-int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
-    const char* iot_ibm = MQTT_BROKER_URL;     
-    char hostname[strlen(org) + strlen(iot_ibm) + 1];
-    
-    sprintf(hostname, "%s%s", org, iot_ibm);
-    // 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);
-
-    // Network debug statements 
-    LOG("=====================================\n\r");
-    LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
-    LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
-    LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
-    LOG("Client ID: %s\n\r", clientId);
-    LOG("Topic: %s\n\r",MQTT_EVENT_TOPIC);
-    LOG("Subscription URL: %s\n\r", subscription_url);
-    LOG("=====================================\n\r");    
-    netConnecting = true;
-    int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
-    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.username.cstring 		= AUTH_METHOD;
-    data.password.cstring 		= auth_token;
-    printf ("AutToken: %s\n\r", auth_token);
-       
-    if ((rc = client->connect(data)) != 0) {
-        printf("rc from MQTT connect is %d\r\n", rc);
-        connack_rc = rc;
-        return rc;
-    }
-    connected = true;
-    printf ("--->MQTT Connected\n\r"); 
-    if ((rc = MQTT_Subscribe(topicCMD, client)) == 0) { 
-       	LOG ("--->>>MQTT subscribed to: %s\n\r", topicCMD);
-    } else {
-		LOG ("--->>>ERROR MQTT subscribe : %s\n\r", topicCMD);
-	}  
-    mqttConnecting = false;
-    connack_rc = rc;
-    return rc;       
-}
 
 
-int MQTT_GetConnTimeout(int attemptNumber) {  // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
-   // after 20 attempts, retry every 10 minutes
-    return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
-}
-
-
-void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
-    connected = false;
-           
-    while (MQTT_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);        
-            return; // don't reattempt to connect if credentials are wrong
-        } 
-        int timeout = MQTT_GetConnTimeout(++retryAttempt);
-        WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
-        
-        // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
-        //  or maybe just add the proper members to do this disconnect and call MQTT_AttemptConnect(...)        
-        // this works - reset the system when the retry count gets to a threshold
-        if (retryAttempt == 5)
-            NVIC_SystemReset(); 
-        else
-            wait(timeout);
-    }    
-}
-
-int MQTT_Publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime) {
-    MQTT::Message message;
-    const char* pubTopic = MQTT_EVENT_TOPIC;
-            
-    char buf[MQTT_MAX_PAYLOAD_SIZE];
-    char timeBuf[50];
-
-	if (!client->isConnected()) { 
-		printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE; 
-	}
-	
-	strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
-    sprintf(buf,
-     "{\"Project\":\"%s\",\"Time\":\"%s\",\"Type\":1,\"cmdID\":%d,\"ADC0\":%0.4f}",
-              projectName, timeBuf, cmdID, ADC_PHVal);
-    message.qos        = MQTT::QOS0;
-    message.retained   = false;
-    message.dup        = false;
-    message.payload    = (void*)buf;
-    message.payloadlen = strlen(buf);
-
-    if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
-        printf("message too long!\r\n");
-    
-    LOG("Publishing %s\n\r", buf);
-    return client->publish(pubTopic, message);    
-}
 
 /***************************************************************
  * Main
@@ -211,14 +48,13 @@
    pc.baud(115200);
    timer.start();
    lastRead = 0;
-//   set_time(1512950250);
+//   set_time(1513125870);
    pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
    pc.printf("\r\nconnecting to AP\r\n");            
 
    NetworkInterface* network = easy_connect(true);
    if (!network) {
        printf ("Error easy_connect\n\r");
-       return -1;
    } 
    MQTTNetwork mqttNetwork(network);	
    MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
@@ -234,10 +70,10 @@
    myled=1;          
 	while (true) {
 		time_t seconds = time(NULL);
-		ADC_PHVal = SENSOR_ReadPHValue();
+		ADC_PHVal = SENSOR_ReadPHADC();
 		if (connected == true) {
 	       	if ((uint32_t)(timer.read() - lastRead) >= uploadInterval) {               // Publish a message every 3 second
-	    		if (MQTT_Publish(&client, seconds) != MQTT::SUCCESS) {    			 
+	    		if (MQTT_PublishADC(&client, seconds, cmdID, ADC_PHVal) != MQTT::SUCCESS) {    			 
 	        		myled=0;
 	            	client.disconnect();
 					mqttNetwork.disconnect();			   
--- a/mbed_app.json	Tue Dec 12 16:00:32 2017 +0000
+++ b/mbed_app.json	Tue Dec 12 17:53:40 2017 +0000
@@ -32,6 +32,7 @@
                 "MQTT_USERNAME=\"use-token-auth\"",
                 "MQTT_DEVICE_PASSWORD=\"PROEVN2017\"",
                 "MQTT_EVENT_TOPIC=\"iot-2/evt/status/fmt/json\"",
+                "MQTT_COMMAND_TOPIC=\"iot-2/cmd/command/fmt/json\"",
                 "MQTT_DEVICE_ID=\"PROEVN\"",
                 "MQTT_DEVICE_TYPE=\"Nucleo_8266\"",                
                 "MQTT_KEEPALIVE  10",