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:
Thu Mar 22 04:11:22 2018 +0000
Parent:
50:5a19fc4b41d9
Child:
52:5546fa1b67b5
Commit message:
* version 2.9 03-22-2018: Watchdog worked. Some hardware bugs found

Changed in this revision

Application/main.cpp Show annotated file Show diff for this revision Revisions of this file
Watchdog.lib Show annotated file Show diff for this revision Revisions of this file
--- a/Application/main.cpp	Sun Mar 18 17:54:50 2018 +0000
+++ b/Application/main.cpp	Thu Mar 22 04:11:22 2018 +0000
@@ -13,6 +13,7 @@
                                 Upload RTC time converted to epoch
   * version 2.8  	03-18-2018: DS18B20 problem solved
   * version 2.8.5  	03-19-2018: Set time to turn on/off the relay
+  * version 2.9  	03-22-2018: Watchdog worked. Some hardware bugs found
   */
 
 /***************************************************************
@@ -25,7 +26,7 @@
 #include "CommandExecution.h"
 #include "flash_programming.h"
 
-#include "stm32l4xx_hal_iwdg.h"
+#include "Watchdog.h"
 
 /***************************************************************
  * Definitions
@@ -63,7 +64,7 @@
 #endif
 
 struct UploadValue DataStruct;
-IWDG_HandleTypeDef IwdgHandle;
+Watchdog           wd;
 
 extern float doValue;
 extern float temperature;
@@ -125,6 +126,7 @@
 
 void SensorRun() {
 	if (!isSendingData) {
+		wd.Service();
 		for (uint8_t j = 0; j < SCOUNT; j++) {
 			SENSOR_AnalogRead();
 		}
@@ -227,11 +229,15 @@
 //	    while (true)
 //	    	wait(1.0); // Permanent failures - don't retry
 	}
-	
+	if (wd.WatchdogCausedReset()) {
+		printf("Watchdog caused reset.\r\n");
+	}
+	wd.Configure(24.8);
 	DisplayDO.attach(&SensorRun, PROCESS_SENSOR_VALUE_S);
 	ReadAllFlashValues();
 	SENSOR_ReadDoCharacteristicValues();
 	BinkLEDStart();
+	wd.Service();
 	lastRead = UploadTimer.read();	
 	
    	myled = 1;   
@@ -249,52 +255,32 @@
 			if(connected == true) {
 				/* Upload for the first time */
 				if (isFirstUpload) {
-			       	printf("Upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);		
-					if (!isUploading) {
-						uploadPeriodCounter++;
-						if (uploadPeriodCounter == DataStruct.CONFIG_UploadInterval) {	
-							uploadPeriodCounter 	= 0;
-							isUploading         	= true;
-							intervalSecondCounter 	= INTERVAL_BETWEEN_EACH_UPLOAD_TYPE;
-						}					
-					}
-					else {
-						if (intervalSecondCounter == INTERVAL_BETWEEN_EACH_UPLOAD_TYPE) {
-							isSendingData = true;
-							if (MQTT_PublishAll(&client, seconds, uploadType, DataStruct) ==  MQTT::SUCCESS) {
-								myled = 1;
-								uploadType++;
-								if (uploadType > CONFIG_VALUE) {
-									isUploading = false;
-									uploadType  = SENSOR_VALUE;
-									commandID++;
-									UploadTimer.reset();
-								}
-							}
-							else {
-								myled = 0;
-								client.disconnect();
-								mqttNetwork.disconnect();
-								/* if we have lost the connection */ 
-								MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct);   
-							}
-							isSendingData = false;						
-							intervalSecondCounter = 0;		
+			       	printf("First upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);		
+					for (uint8_t firstUploadType = 1; firstUploadType < 4; firstUploadType++) {
+						seconds = time(NULL);
+						wd.Service();
+						if (MQTT_PublishAll(&client, seconds, firstUploadType, DataStruct) ==  MQTT::SUCCESS) {
+							myled = 1;
 						}
 						else {
-							intervalSecondCounter++;
+							myled = 0;	
 						}
-					}
+						wait(5);
+						if (firstUploadType == CONFIG_VALUE) {
+							uploadType    = SENSOR_VALUE;
+							isFirstUpload = false;
+							lastRead      = UploadTimer.read();	
+						}
+					}	
 					lastRead = UploadTimer.read();					
-					isFirstUpload = false;
-					isUploading = false;
 				}
 				else {
 					/* Periodic upload */
 			       	if ((uint32_t)(UploadTimer.read() - lastRead) >= READ_SECOND) {               // Read timer every readSecond(s)	
-			       		printf("Upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);	
+			       		printf("Periodic upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);	
 			       		/* Start uploading data */	
 						if (!isUploading) {
+							wd.Service();
 							uploadPeriodCounter++;
 							if (uploadPeriodCounter == DataStruct.CONFIG_UploadInterval) {	
 								uploadPeriodCounter 	= 0;
@@ -303,6 +289,7 @@
 							}					
 						}
 						else {
+							wd.Service();
 							if (intervalSecondCounter == INTERVAL_BETWEEN_EACH_UPLOAD_TYPE) {
 								isSendingData = true;
 								if (MQTT_PublishAll(&client, seconds, uploadType, DataStruct) ==  MQTT::SUCCESS) {
@@ -340,6 +327,7 @@
 			} 			
 		}
 		else {
+			wd.Service();
 			if ((uint32_t)(UploadTimer.read() - noWiFilastRead) >= RECONNECT_WIFI) {
 			   	wifiConnected = true;
 			   	network = easy_connect(true);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Watchdog.lib	Thu Mar 22 04:11:22 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/salarian/code/Watchdog/#84f7c088c261