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:
Wed Nov 29 15:09:42 2017 +0000
Parent:
6:3efc470a9f35
Child:
8:844796296dea
Commit message:
version 1.1 - code is re-organized

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Nov 28 14:32:53 2017 +0000
+++ b/main.cpp	Wed Nov 29 15:09:42 2017 +0000
@@ -13,8 +13,6 @@
  ***************************************************************/
 #define logMessage printf
 #define MQTTCLIENT_QOS2 (0)
-#define MQTT_OK         (0)
-#define MQTT_FAIL       (-1)
  
 #define SENSOR_1_PIN  (A0)
 #define SENSOR_2_PIN  (A1)
@@ -29,11 +27,17 @@
 #define DEVICE_ID     "PROEVN"
 #define TOKEN         "PROEVN2017"
 
+typedef enum {
+    MQTT_SUCCESS      =  0,
+    MQTT_NETWORK_FAIL = -1,
+    MQTT_FAIL         = -2    
+};
+
 /***************************************************************
  * Variables
  ***************************************************************/
-float firmwareVersion = 0.86; 
- 
+/* MQTT Varialbles */
+float firmwareVersion = 0.86;  
 char server[]     = ORG ".messaging.internetofthings.ibmcloud.com";
 char topicCMD[]   = "iot-2/cmd/test/fmt/json";
 char topicEvent[] = "iot-2/evt/status/fmt/json";
@@ -42,22 +46,50 @@
 char clientId[]   = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
 int  ibmPort      = 1883;
 
-int arrivedcount = 0;
-uint32_t lastRead = 0;
-uint32_t timer_last_read = 0;
+/* Internet Varialbles */
+bool internetState = false;
 
-uint8_t readVal1 = 0, readVal2 = 7;;
+/* Time Handles */
+uint32_t lastRead;
+/***************************************************************
+ * Structs/Classess
+ ***************************************************************/
+NetworkInterface* network = easy_connect(true);
+MQTTNetwork mqttNetwork(network);
+MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
 
 Timer  readTime;
 Thread thread;
 Serial serial(USBTX, USBRX);
 
 /***************************************************************
- * Structs/Classess
+ * Unity function definitions
  ***************************************************************/
-NetworkInterface* network = easy_connect(true);
-MQTTNetwork mqttNetwork(network);
-MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+/**
+  * @brief  Establish a connection to the internet
+  * @retval MQTT_SUCCESS if connected to the internet, MQTT_NETWORK_FAIL if failed to establish the connection
+  */
+int MQTT_internetConnect();
+
+/**
+  * @brief  Connect to the MQTT data and subscribe to the topic
+  * @retval MQTT_SUCCESS if connected to the data and subscribed the topic successfully.
+  */
+int MQTT_networkConnect();
+
+/**
+  * @brief  Connect to the internet and then MQTT network.
+  * @retval MQTT_SUCCESS if the 2 above functions succeeded.
+  */
+int MQTT_init();
+
+/**
+  * @brief  Publish a message to the MQTT topic
+  * @param  sendMessage[in]: the message to be sent.
+  * @retval MQTT_SUCCESS if the message is sent.
+  */
+int MQTT_publish(char *sendMessage);
+
 
 /***************************************************************
  * Callbacks
@@ -68,12 +100,77 @@
     logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
 }
 
+
 /***************************************************************
- * Unity function definitions
+ * Unity function declarations
  ***************************************************************/
-int MQTT_publish(char *sendMessage);
-void clientYield();
+int MQTT_internetConnect() {
+    network = easy_connect(true);
+    
+    if (!network) {
+        printf("Cannot connect to the internet\r\n");
+        internetState = false;
+        return MQTT_SUCCESS;
+    }
+    else {
+        printf("Reconnected to the internet\r\n");
+        internetState = true;
+        return MQTT_NETWORK_FAIL;
+    }
+} 
 
+int MQTT_networkConnect() {
+    logMessage("Connecting to %s:%d\r\n", server, ibmPort);
+    int rc = mqttNetwork.connect(server, ibmPort);
+    if (rc != 0) {
+        logMessage("rc from TCP connect is %d\r\n", rc);
+        return rc;   
+    }
+          
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = 3;
+    data.clientID.cstring = clientId;
+    data.username.cstring = authMethod;
+    data.password.cstring = token;
+    
+    if ((rc = client.connect(data)) != 0) {
+        logMessage("rc from MQTT connect is %d\r\n", rc);
+        return rc;
+    }
+    
+    if ((rc = client.subscribe(topicCMD, MQTT::QOS0, messageArrived)) != 0) {
+        logMessage("rc from MQTT subscribe is %d\r\n", rc);        
+        return rc;
+    }
+}
+
+int MQTT_init() {
+    int ret = MQTT_SUCCESS;
+    if (!network) {
+        logMessage("Failed to connect to the internet, retrying\r\n");
+        ret = MQTT_internetConnect();
+        if (ret != MQTT_SUCCESS) {
+            return ret;
+        }
+    }
+    
+    ret = MQTT_networkConnect();
+    if (ret != MQTT_SUCCESS) {
+        printf("Fail to connect to MQTT or fail to subscribe");
+    }
+    
+    return ret;
+}
+
+int MQTT_publish(char *sendMessage) {
+    MQTT::Message msg;
+    msg.qos = MQTT::QOS0;
+    msg.retained = false;
+    msg.dup = false;
+    msg.payload = sendMessage;
+    msg.payloadlen = strlen(sendMessage) + 1;
+    return client.publish(topicEvent, msg);        
+}
 
 /***************************************************************
  * Main
@@ -83,29 +180,10 @@
     
     logMessage("IoT Water Monitor project, firmware version is %.2f\r\n", firmwareVersion);
     
-    if (!network) {
-        logMessage("Failed to connect to the internet\r\n");
-        return ;
-    }
-    
-    logMessage("Connecting to %s:%d\r\n", server, ibmPort);
-    int rc = mqttNetwork.connect(server, ibmPort);
-    if (rc != 0)
-        logMessage("rc from TCP connect is %d\r\n", rc);
-          
-    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
-    data.MQTTVersion = 3;
-    data.clientID.cstring = clientId;
-    data.username.cstring = authMethod;
-    data.password.cstring = token;
-    
-    if ((rc = client.connect(data)) != 0)
-        logMessage("rc from MQTT connect is %d\r\n", rc);
-    if ((rc = client.subscribe(topicCMD, MQTT::QOS0, messageArrived)) != 0)
-        logMessage("rc from MQTT subscribe is %d\r\n", rc);
+    MQTT_init();
        
     char greetingMessage[100];
-    sprintf(greetingMessage, "{\"d\":{\"Device ID\":\"PROEVN\",\"Firmware Version\":\"%f\"}}", firmwareVersion);
+    sprintf(greetingMessage, "{\"Device ID\":\"PROEVN\",\"Firmware Version\":\"%f\"}", firmwareVersion);
     
     printf("Sending payload: %s\r\n", greetingMessage);
     if (!MQTT_publish((char *)greetingMessage)) {
@@ -127,7 +205,7 @@
         if ((uint32_t)(readTime.read() - lastRead) > 10) {
             counter++;
             char payload[100];
-            sprintf(payload, "{\"d\":{\"Type\":\"1\",\"Command ID\":\"%d\",\"ADC0\":\"%d\",\"ADC1\":\"%d\"}}", cmdID, counter, counter++);
+            sprintf(payload, "{\"Type\":\"1\",\"Command ID\":\"%d\",\"ADC0\":\"%d\",\"ADC1\":\"%d\"}", cmdID, counter, counter++);
             printf("MQTT publish %s\r\n", payload);
             if (!MQTT_publish((char *)payload)) {
                 printf("Publish ok\r\n");
@@ -145,16 +223,4 @@
         
 }
 
-/***************************************************************
- * Unity function declarations
- ***************************************************************/
-int MQTT_publish(char *sendMessage) {
-    MQTT::Message msg;
-    msg.qos = MQTT::QOS0;
-    msg.retained = false;
-    msg.dup = false;
-    msg.payload = sendMessage;
-    msg.payloadlen = strlen(sendMessage) + 1;
-    return client.publish(topicEvent, msg);        
-}