iot_water_monitor_v2

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

Revision:
43:dcde0e66874a
Parent:
40:4356c209c58d
Child:
45:92da959d624b
--- a/Simple-MQTT/SimpleMQTT.h	Sun Mar 04 07:58:23 2018 +0000
+++ b/Simple-MQTT/SimpleMQTT.h	Thu Mar 08 13:13:53 2018 +0000
@@ -27,8 +27,8 @@
 #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 MQTT_MAX_PACKET_SIZE    450   
+#define MQTT_MAX_PAYLOAD_SIZE   350 
 
 /***************************************************************
  * Variables
@@ -47,10 +47,10 @@
 } CommandType;
  
 struct UploadValue {
-    float ADC_PHVal;
+    float ADC_TEMPVal;
     float ADC_DOVal;
     
-    float SENSOR_PHVal;
+    float SENSOR_TEMPVal;
     float SENSOR_DOVal;
     
     uint8_t   RELAY_State_1;
@@ -60,8 +60,8 @@
     uint32_t CONFIG_Time;
 	
 	uint8_t  CONFIG_Mode;
-	uint8_t  CONFIG_MinOxi;
-	uint8_t  CONFIG_MaxOxi;
+	uint8_t  CONFIG_OxyThreshold;
+	uint8_t  CONFIG_TemperatureThreshold;
 	uint16_t CONFIG_UploadInterval;
 } UploadValue; 
  
@@ -132,7 +132,7 @@
  *  param[in]   pHVal         The pHVal value to be sent
  *  retral      returnCode from MQTTClient.h 
  */
-int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float DOVal);
+int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float TempVal, float DOVal);
 
 /** brief       Publish relay states to the server
  *  param[in]   client        MQTT client 
@@ -152,7 +152,7 @@
 // *  param[in]   uploadInterval	Interval between upload turns
  *  retral      returnCode from MQTTClient.h 
  */
-int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t minOxi, uint8_t maxOxi);
+int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t OxyThreshold, uint8_t TemperatureThreshold, uint16_t UploadInterval);
 
 /** brief       Upload all the data to the MQTT server
  *  param[in]   client          MQTT client 
@@ -191,10 +191,12 @@
             printf("Command Type: %d, error %d\r\n", CommandType, ret);
             
             switch (CommandType) {
-            	case CONTROL_CMD: CE_Calibrate();
+            	case 0: CE_Calibrate();
             	break;
             	
-            	case 3: int relayState1, relayState2, relayState3;
+            	case 1:
+            	
+            	case 2: int relayState1, relayState2, relayState3;
             			KeyIndex      = json.findKeyIndexIn("cmdID", 0);
             			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
             			ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID); 
@@ -219,7 +221,7 @@
             			CE_HandleRelays(relayState1, relayState2, relayState3);
             	break;
             	
-            	case 4: int mode, minOxiVal, maxOxiVal, uploadInterval, setRTCTime;
+            	case 3: int mode, OxiThres, TempThres, uploadInterval, setRTCTime;
             	
             			KeyIndex      = json.findKeyIndexIn("cmdID", 0);
             			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
@@ -229,13 +231,13 @@
             			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
             			ret = json.tokenIntegerValue(KeyValueIndex, mode);     
             			        			
-            			KeyIndex      = json.findKeyIndexIn("minOxygenVal", 0);
+            			KeyIndex      = json.findKeyIndexIn("OxygenThreshold", 0);
             			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
-            			ret = json.tokenIntegerValue(KeyValueIndex, minOxiVal); 
+            			ret = json.tokenIntegerValue(KeyValueIndex, OxiThres); 
             			
-            			KeyIndex      = json.findKeyIndexIn("maxOxygenVal", 0);
+            			KeyIndex      = json.findKeyIndexIn("TemperatureThreshold", 0);
             			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
-            			ret = json.tokenIntegerValue(KeyValueIndex, maxOxiVal);     
+            			ret = json.tokenIntegerValue(KeyValueIndex, TempThres);     
             		
             			KeyIndex      = json.findKeyIndexIn("uploadInterval", 0);
             			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
@@ -245,14 +247,17 @@
             			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
             			ret = json.tokenIntegerValue(KeyValueIndex, setRTCTime);      
             			
-            			DataStruct.CONFIG_Mode   		 = mode;   
-            			DataStruct.CONFIG_MinOxi 		 = minOxiVal;   
-            			DataStruct.CONFIG_MaxOxi 		 = maxOxiVal;   
-            			DataStruct.CONFIG_UploadInterval = uploadInterval;			
-            			FP_WriteConfigValues(DataStruct.CONFIG_Mode, DataStruct.CONFIG_MinOxi, DataStruct.CONFIG_MaxOxi, DataStruct.CONFIG_UploadInterval);  	
+            			DataStruct.CONFIG_Mode   		 		= mode;   
+            			DataStruct.CONFIG_OxyThreshold 		 	= OxiThres;   
+            			DataStruct.CONFIG_TemperatureThreshold	= TempThres;   
+            			DataStruct.CONFIG_UploadInterval 		= uploadInterval;			
+            			FP_WriteConfigValues(DataStruct.CONFIG_Mode, DataStruct.CONFIG_OxyThreshold, DataStruct.CONFIG_TemperatureThreshold, DataStruct.CONFIG_UploadInterval);  	
             			CE_SetRTCTime(setRTCTime);		
             	break;
             	
+            	case 4: CE_UpdateImmediately();
+            	break;
+            	
             	default: break;
             }
         }    	
@@ -376,7 +381,7 @@
     return client->publish(pubTopic, message);    
 }
 
-int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float DOVal) {
+int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float TempVal, float DOVal) {
     MQTT::Message message;
     const char* pubTopic = MQTT_EVENT_TOPIC;
             
@@ -388,8 +393,8 @@
     }
     
     strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
-    sprintf(buf, "{\"type\":2,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"DO(ppm)\":%.2f}",
-                timeBuf, commandID, DOVal);
+    sprintf(buf, "{\"type\":2,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"temperature0\":%.2f,\"oxygen0\":%.2f}",
+                timeBuf, commandID, TempVal ,DOVal);
     message.qos        = MQTT::QOS0;
     message.retained   = false;
     message.dup        = false;
@@ -429,7 +434,8 @@
     return client->publish(pubTopic, message);       
 }
 
-int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t minOxi, uint8_t maxOxi) {
+int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, 
+                                                    uint8_t OxyThreshold, uint8_t TemperatureThreshold, uint16_t UploadInterval) {
     MQTT::Message message;
     const char* pubTopic = MQTT_EVENT_TOPIC;         
     char buf[MQTT_MAX_PAYLOAD_SIZE];
@@ -440,8 +446,8 @@
         return MQTT::FAILURE; 
     }
     strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
-    sprintf(buf, "{\"type\":4,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"mode\":%d,\"minOxygenVal\":%d,\"maxOxygenVal\":%d}",
-                timeBuf, commandID, mode, minOxi, maxOxi);
+    sprintf(buf, "{\"type\":4,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"mode\":%d,\"OxygenThreshold\":%d,\"TemperatureThreshold\":%d,\"uploadInterval\":%d}",
+                timeBuf, commandID, mode, OxyThreshold, TemperatureThreshold, UploadInterval);
     message.qos        = MQTT::QOS0;
     message.retained   = false;
     message.dup        = false;
@@ -460,11 +466,11 @@
     switch (uploadType) {
         case (ADC_VALUE): 		retVal = MQTT_PublishADC(client, inputTime, uploadStruct.ADC_DOVal);
             break;
-        case (SENSOR_VALUE): 	retVal =  MQTT_PublishSensorVal(client, inputTime, uploadStruct.SENSOR_DOVal);
+        case (SENSOR_VALUE): 	retVal =  MQTT_PublishSensorVal(client, inputTime, uploadStruct.SENSOR_TEMPVal ,uploadStruct.SENSOR_DOVal);
             break;
         case (RELAY_STATE):		retVal = MQTT_PublishRelayState(client, inputTime, uploadStruct.RELAY_State_1, uploadStruct.RELAY_State_2, uploadStruct.RELAY_State_3);
             break;
-        case (CONFIG_VALUE): 	retVal = MQTT_PublishConfigValue(client, inputTime, uploadStruct.CONFIG_Mode, uploadStruct.CONFIG_MinOxi, uploadStruct.CONFIG_MaxOxi);
+        case (CONFIG_VALUE): 	retVal = MQTT_PublishConfigValue(client, inputTime, uploadStruct.CONFIG_Mode, uploadStruct.CONFIG_OxyThreshold, uploadStruct.CONFIG_TemperatureThreshold, uploadStruct.CONFIG_UploadInterval);
         	break;
         default: break;
     }