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:
Sun Mar 18 17:54:50 2018 +0000
Parent:
49:d45c534ec444
Child:
51:da1f795fc7bf
Commit message:
* version 2.8.5 03-19-2018: Set time to turn on/off the relay

Changed in this revision

Application/CommandExecution.cpp Show annotated file Show diff for this revision Revisions of this file
Application/CommandExecution.h Show annotated file Show diff for this revision Revisions of this file
Application/main.cpp Show annotated file Show diff for this revision Revisions of this file
Flash/flash_programming.cpp Show annotated file Show diff for this revision Revisions of this file
Flash/flash_programming.h Show annotated file 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
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- a/Application/CommandExecution.cpp	Sun Mar 18 04:30:18 2018 +0000
+++ b/Application/CommandExecution.cpp	Sun Mar 18 17:54:50 2018 +0000
@@ -1,4 +1,5 @@
 #include "CommandExecution.h"
+#include "flash_programming.h"
 #include "ReadSensor.h"
 #include "mbed.h"
 
@@ -6,8 +7,9 @@
 DigitalOut  relay2(RELAY_2_PIN);
 DigitalOut  relay3(RELAY_3_PIN);
 
-extern bool isCalibrating;
-extern bool isUploadImmediately;
+extern bool   isCalibrating;
+extern bool   isUploadImmediately;
+extern float  doValue;
 
 void CE_HandleRelays(int Relay1State, int Relay2State, int Relay3State) {
     relay1 = Relay1State;
@@ -30,4 +32,5 @@
 
 void CE_UpdateImmediately() {
     isUploadImmediately = true;
-}
\ No newline at end of file
+}
+
--- a/Application/CommandExecution.h	Sun Mar 18 04:30:18 2018 +0000
+++ b/Application/CommandExecution.h	Sun Mar 18 17:54:50 2018 +0000
@@ -7,7 +7,7 @@
 #define RELAY_2_PIN   (D11)
 #define RELAY_3_PIN   (D12)
 
-void CE_HandleRelays(int Relay1State, int Relat2State, int Relat3State);
+void CE_HandleRelays(int Relay1State, int Relay2State, int Relay3State);
 void CE_SetRTCTime(uint32_t CurrentEpochTime);
 void CE_Calibrate();
 void CE_SetAlarmRelays();
--- a/Application/main.cpp	Sun Mar 18 04:30:18 2018 +0000
+++ b/Application/main.cpp	Sun Mar 18 17:54:50 2018 +0000
@@ -11,7 +11,8 @@
   * version 2.7.5 	03-08-2018: DS18B20 & IWDG is being developed
   * version 2.7.5a	03-08-2018: DS18B20 problem discovered at line 42th ReadSensor.cpp
                                 Upload RTC time converted to epoch
-  * version 2.8  	03-17-2018: DS18B20 problem solved
+  * version 2.8  	03-18-2018: DS18B20 problem solved
+  * version 2.8.5  	03-19-2018: Set time to turn on/off the relay
   */
 
 /***************************************************************
@@ -39,6 +40,7 @@
 #endif
 #define PROCESS_SENSOR_VALUE_S			    2
 #define CALIB_STATE_CHANGE_PERIOD_S         5
+#define ALARM_TIME_ODD                      20
 
 /***************************************************************
  * Variables
@@ -92,6 +94,9 @@
 
 void BinkLEDStart();
 
+void AutomaticHandle();
+void TimeAlarmHandle();
+
 /***************************************************************
  * Unity function declarations
  ***************************************************************/ 
@@ -107,9 +112,14 @@
 	DataStruct.CONFIG_OxyThreshold			= FP_ReadValue(OXY_THRES_ADDRESS);
 	DataStruct.CONFIG_TemperatureThreshold	= FP_ReadValue(TEMP_THRES_ADDRESS);
 	DataStruct.CONFIG_UploadInterval		= FP_ReadValue(UPLOAD_PERIOD_ADDRESS);
-	printf("All values: %d %d %d %d %d %d\r\n", DataStruct.RELAY_State_1, DataStruct.RELAY_State_2,
+	DataStruct.CONFIG_AlarmTime             = FP_ReadValue(ALARM_TIME_ADDRESS);
+	DataStruct.CONFIG_SetRelayState_1       = FP_ReadValue(SET_RELAY_1_ADDRESS);
+	DataStruct.CONFIG_SetRelayState_2       = FP_ReadValue(SET_RELAY_2_ADDRESS);
+	printf("All values: %d %d %d %d %d %d %d %d %d\r\n", DataStruct.RELAY_State_1, DataStruct.RELAY_State_2,
 	                                            DataStruct.CONFIG_Mode,   DataStruct.CONFIG_OxyThreshold, 	
-	                                            DataStruct.CONFIG_TemperatureThreshold, DataStruct.CONFIG_UploadInterval);
+	                                            DataStruct.CONFIG_TemperatureThreshold, DataStruct.CONFIG_UploadInterval,
+	                                            DataStruct.CONFIG_AlarmTime, 
+	                                            DataStruct.CONFIG_SetRelayState_1, DataStruct.CONFIG_SetRelayState_2);
 	CE_HandleRelays(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);
 }
 
@@ -155,6 +165,34 @@
    	myled = 0;	
 }
 
+void AutomaticHandle() {
+	if (DataStruct.CONFIG_Mode == 0) {
+		if ((uint32_t)(DataStruct.SENSOR_DOVal*100) >= DataStruct.CONFIG_OxyThreshold) {
+			/* Turn on the pumps */
+			DataStruct.RELAY_State_1 = 1;      
+			DataStruct.RELAY_State_2 = 1;  
+			DataStruct.RELAY_State_3 = 1;   
+			 
+		}
+		else {
+			/* Turn off the pumps */
+			DataStruct.RELAY_State_1 = 0;      
+			DataStruct.RELAY_State_2 = 0;
+			DataStruct.RELAY_State_3 = 0;   			
+		}
+		CE_HandleRelays(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);   
+		FP_WriteRelayStates(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);
+	}	
+}
+
+void TimeAlarmHandle(time_t CurrentTime) {
+	if (CurrentTime >= (DataStruct.CONFIG_AlarmTime - ALARM_TIME_ODD)) {
+		DataStruct.RELAY_State_1 = DataStruct.CONFIG_SetRelayState_1; 
+		DataStruct.RELAY_State_2 = DataStruct.CONFIG_SetRelayState_2;
+		CE_HandleRelays(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);   
+		FP_WriteRelayStates(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);		
+	}
+}
 /***************************************************************
  * Main
  ***************************************************************/ 
@@ -199,6 +237,8 @@
    	myled = 1;   
 	while (true) {
 		time_t seconds = time(NULL);	
+		AutomaticHandle();
+		TimeAlarmHandle(seconds);
 		#ifdef READ_ANALOG_SOFTTIMER
 			if ((uint32_t)(ReadAnalogTimer.read_ms() - lastReadAnalog) > READ_ANALOG_MS) {
 				SENSOR_AnalogRead();
@@ -247,6 +287,7 @@
 					}
 					lastRead = UploadTimer.read();					
 					isFirstUpload = false;
+					isUploading = false;
 				}
 				else {
 					/* Periodic upload */
--- a/Flash/flash_programming.cpp	Sun Mar 18 04:30:18 2018 +0000
+++ b/Flash/flash_programming.cpp	Sun Mar 18 17:54:50 2018 +0000
@@ -271,4 +271,94 @@
     }
     HAL_FLASH_Lock();
     return PASSED;
+}
+
+int FP_SetAlarmValues(uint32_t WriteTimeValue, uint8_t SetRelayState1, uint8_t SetRelayState2) {
+    uint8_t  CurrentPage = FP_GetPage(ALRM_BASE_ADDRESS);
+    uint32_t CurrentAddress = ALARM_TIME_ADDRESS;
+    
+    EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
+    EraseInitStruct.Banks       = FLASH_BANK_1;
+    EraseInitStruct.Page        = CurrentPage;
+    EraseInitStruct.NbPages     = 1;      
+    
+    if (FP_ClearFlags() != PASSED) {
+        return FAILED;
+    }
+    HAL_FLASH_Unlock();  
+    
+    if ((FP_ReadValue(ALARM_TIME_ADDRESS)  == WriteTimeValue) &&
+        (FP_ReadValue(SET_RELAY_1_ADDRESS) == SetRelayState1) &&
+        (FP_ReadValue(SET_RELAY_2_ADDRESS) == SetRelayState2)) {
+        printf("Relay values don't change, no need to write\r\n");
+        HAL_FLASH_Lock();
+        return PASSED;
+    }
+    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) {
+        printf("Erase error, error num %d\r\n", HAL_FLASH_GetError());
+    }  
+    while (CurrentAddress < (SET_RELAY_2_ADDRESS + STEP_ADDRESS)) {
+        switch (CurrentAddress) {
+            case (ALARM_TIME_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, WriteTimeValue) == HAL_OK) {
+                                        printf("Write WriteTimeValue OK\r\n");
+                                   }
+                                   else {
+                                        printf("Write WriteTimeValue failed, error num %d\r\n", HAL_FLASH_GetError());
+                                   }
+            break;
+            case (SET_RELAY_1_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, SetRelayState1) == HAL_OK) {
+                                        printf("Write SetRelayState1 OK\r\n");
+                                   }
+                                   else {
+                                        printf("Write SetRelayState1 failed, error num %d\r\n", HAL_FLASH_GetError());
+                                   }
+            break;
+            case (SET_RELAY_2_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, SetRelayState2) == HAL_OK) {
+                                        printf("Write SetRelayState2 OK\r\n");
+                                   }
+                                   else {
+                                        printf("Write SetRelayState2 failed, error num %d\r\n", HAL_FLASH_GetError());
+                                   }
+            break;
+            default: break;   
+        }
+        CurrentAddress = CurrentAddress + STEP_ADDRESS;
+    }
+
+    CurrentAddress = ALARM_TIME_ADDRESS;
+    while (CurrentAddress < (SET_RELAY_2_ADDRESS + STEP_ADDRESS)) {
+        switch (CurrentAddress) {
+            case (ALARM_TIME_ADDRESS): if (FP_ReadValue(CurrentAddress) == WriteTimeValue) {
+                                    printf("Read back WriteTimeValue: %d\r\n", FP_ReadValue(CurrentAddress));
+                                 }
+                                 else {
+                                    printf("Write WriteTimeValue failed, wrong read back value\r\n"); 
+                                    HAL_FLASH_Lock();
+                                    return FAILED;
+                                 }
+            break;
+            case (SET_RELAY_1_ADDRESS): if (FP_ReadValue(CurrentAddress) == SetRelayState1) {
+                                        printf("Read back SetRelayState1: %d\r\n", FP_ReadValue(CurrentAddress));  
+                                    }
+                                    else {
+                                        printf("Write SetRelayState1 failed, wrong read back value\r\n");
+                                        HAL_FLASH_Lock();
+                                        return FAILED;
+                                    }
+            break;
+            case (SET_RELAY_2_ADDRESS): if (FP_ReadValue(CurrentAddress) == SetRelayState2) {
+                                        printf("Read back SetRelayState2: %d\r\n", FP_ReadValue(CurrentAddress));  
+                                    }
+                                    else {
+                                        printf("Write SetRelayState2 failed, wrong read back value\r\n");
+                                        HAL_FLASH_Lock();
+                                        return FAILED;
+                                    }
+            break;
+            default: break;   
+        }
+        CurrentAddress = CurrentAddress + STEP_ADDRESS;
+    }
+    HAL_FLASH_Lock();
+    return PASSED;    
 }
\ No newline at end of file
--- a/Flash/flash_programming.h	Sun Mar 18 04:30:18 2018 +0000
+++ b/Flash/flash_programming.h	Sun Mar 18 17:54:50 2018 +0000
@@ -137,7 +137,7 @@
 #define TIME_BASE_ADDRESS       ADDR_FLASH_PAGE_103                 /* USER DEFINE */
 #define CONF_BASE_ADDRESS       ADDR_FLASH_PAGE_104                 /* USER DEFINE */
 #define CALIB_BASE_ADDRESS      ADDR_FLASH_PAGE_105                 /* USER DEFINE */
-#define EMPT_BASE_ADDRESS       ADDR_FLASH_PAGE_106                 /* USER DEFINE */
+#define ALRM_BASE_ADDRESS       ADDR_FLASH_PAGE_106                 /* USER DEFINE */
 
 #define RELAY1_ADDRESS          (RELAY_BASE_ADDRESS + STEP_ADDRESS)
 #define RELAY2_ADDRESS          (RELAY1_ADDRESS     + STEP_ADDRESS)
@@ -150,6 +150,9 @@
 
 #define SAT_DO_VOLT_ADDRESS     (CALIB_BASE_ADDRESS + STEP_ADDRESS)
 
+#define ALARM_TIME_ADDRESS      (ALRM_BASE_ADDRESS   + STEP_ADDRESS)
+#define SET_RELAY_1_ADDRESS     (ALARM_TIME_ADDRESS  + STEP_ADDRESS)
+#define SET_RELAY_2_ADDRESS     (SET_RELAY_1_ADDRESS + STEP_ADDRESS)
 
 typedef enum {
     FAILED = 1, 
@@ -204,4 +207,5 @@
  */
 int FP_WriteConfigValues(uint32_t SaturationDoVoltage);
 
+int FP_SetAlarmValues(uint32_t WriteTimeValue, uint8_t SetRelayState1, uint8_t SetRelayState2);
 #endif /* __FLASH_PROGRAMMING_H__ */
\ No newline at end of file
--- a/Simple-MQTT/SimpleMQTT.h	Sun Mar 18 04:30:18 2018 +0000
+++ b/Simple-MQTT/SimpleMQTT.h	Sun Mar 18 17:54:50 2018 +0000
@@ -62,7 +62,11 @@
 	uint8_t  CONFIG_Mode;
 	uint8_t  CONFIG_OxyThreshold;
 	uint8_t  CONFIG_TemperatureThreshold;
-	uint16_t CONFIG_UploadInterval;
+	uint16_t CONFIG_UploadInterval;              /* in second */
+	
+	uint32_t CONFIG_AlarmTime;
+    uint8_t  CONFIG_SetRelayState_1;
+    uint8_t  CONFIG_SetRelayState_2;	
 } UploadValue; 
  
 char       *projectName     = "WaterMonitor";
@@ -196,6 +200,27 @@
             	break;
             	
             	case 1: printf("Set relay alarm state\r\n");
+            			int AlarmTime, SetRelayState1, SetRelayState2;
+            			KeyIndex      = json.findKeyIndexIn("cmdID", 0);
+            			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+            			ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID); 
+            			
+            			KeyIndex      = json.findKeyIndexIn("AlarmTime", 0);
+            			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+            			ret = json.tokenIntegerValue(KeyValueIndex, AlarmTime); 
+            			
+            			KeyIndex      = json.findKeyIndexIn("SetRelayState1", 0);
+            			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+            			ret = json.tokenIntegerValue(KeyValueIndex, SetRelayState1);
+            			
+            			KeyIndex      = json.findKeyIndexIn("SetRelayState2", 0);
+            			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+            			ret = json.tokenIntegerValue(KeyValueIndex, SetRelayState2);
+            			
+            			DataStruct.CONFIG_AlarmTime       = AlarmTime;
+            			DataStruct.CONFIG_SetRelayState_1 = SetRelayState1;
+            			DataStruct.CONFIG_SetRelayState_2 = SetRelayState2;
+            			FP_SetAlarmValues(DataStruct.CONFIG_AlarmTime, DataStruct.CONFIG_SetRelayState_1, DataStruct.CONFIG_SetRelayState_2);            			
             	break;
             	
             	case 2: printf("Set relay state\r\n");
@@ -216,12 +241,14 @@
             			KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
             			ret = json.tokenIntegerValue(KeyValueIndex, relayState3);
             			
-            			DataStruct.RELAY_State_1 = relayState1;     
-            			DataStruct.RELAY_State_2 = relayState2;   
-            			DataStruct.RELAY_State_3 = relayState3;   
-            			
-            			FP_WriteRelayStates(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);      		
-            			CE_HandleRelays(relayState1, relayState2, relayState3);
+            			if (DataStruct.CONFIG_Mode == 1) {
+	             			DataStruct.RELAY_State_1 = relayState1;     
+	            			DataStruct.RELAY_State_2 = relayState2;   
+	            			DataStruct.RELAY_State_3 = relayState3;     
+	            			FP_WriteRelayStates(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);      		
+	            			CE_HandleRelays(relayState1, relayState2, relayState3);       				
+            			}
+  
             	break;
             	
             	case 3: printf("Set config values\r\n");
--- a/mbed_app.json	Sun Mar 18 04:30:18 2018 +0000
+++ b/mbed_app.json	Sun Mar 18 17:54:50 2018 +0000
@@ -17,10 +17,10 @@
             "value": "D0"
         },
         "esp8266-ssid": {
-            "value": "\"KH\""
+            "value": "\"GreenLantern\""
         },
         "esp8266-password": {
-            "value": "\"kh123456\""
+            "value": "\"deAlembert\""
         },
         "esp8266-debug": {
             "value": true