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 08 16:38:35 2018 +0000
Parent:
47:c3707a4f69a7
Child:
49:d45c534ec444
Commit message:
* version 2.7.5a 03-08-2018: DS18B20 problem discovered at line 42th ReadSensor.cpp; Upload RTC time converted to epoch

Changed in this revision

Application/CommandExecution.cpp 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
Sensor/ReadSensor.cpp Show annotated file Show diff for this revision Revisions of this file
Sensor/ReadSensor.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
--- a/Application/CommandExecution.cpp	Thu Mar 08 14:19:48 2018 +0000
+++ b/Application/CommandExecution.cpp	Thu Mar 08 16:38:35 2018 +0000
@@ -7,6 +7,7 @@
 DigitalOut  relay3(RELAY_3_PIN);
 
 extern bool isCalibrating;
+extern bool isUploadImmediately;
 
 void CE_HandleRelays(int Relay1State, int Relay2State, int Relay3State) {
     relay1 = Relay1State;
@@ -28,5 +29,5 @@
 }
 
 void CE_UpdateImmediately() {
-    
+    isUploadImmediately = true;
 }
\ No newline at end of file
--- a/Application/main.cpp	Thu Mar 08 14:19:48 2018 +0000
+++ b/Application/main.cpp	Thu Mar 08 16:38:35 2018 +0000
@@ -44,6 +44,8 @@
  ***************************************************************/
 bool        isUploading         = false;
 bool		isSendingData       = false;
+bool        isUploadImmediately = false;
+bool 		isFirstUpload       = true;
 uint8_t 	uploadType          = SENSOR_VALUE;
 uint8_t     currentCalibMode    = 1;
 uint8_t     calibStateCounter   = 0;
@@ -151,6 +153,7 @@
 	}
    	myled = 0;	
 }
+
 /***************************************************************
  * Main
  ***************************************************************/ 
@@ -189,6 +192,9 @@
 	DisplayDO.attach(&SensorRun, PROCESS_SENSOR_VALUE_S);
 	ReadAllFlashValues();
 	SENSOR_ReadDoCharacteristicValues();
+	BinkLEDStart();
+	lastRead = UploadTimer.read();	
+	
    	myled = 1;   
 	while (true) {
 		time_t seconds = time(NULL);	
@@ -200,8 +206,9 @@
 		#endif
 		if (wifiConnected) {
 			if(connected == true) {
-		       	if ((uint32_t)(UploadTimer.read() - lastRead) >= READ_SECOND) {               // Read timer every readSecond(s)	
-		       		printf("Upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);		
+				/* Upload for the first time */
+				if (isFirstUpload) {
+			       	printf("Upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);		
 					if (!isUploading) {
 						uploadPeriodCounter++;
 						if (uploadPeriodCounter == DataStruct.CONFIG_UploadInterval) {	
@@ -237,8 +244,52 @@
 							intervalSecondCounter++;
 						}
 					}
-					lastRead = UploadTimer.read();
-		    	}        
+					lastRead = UploadTimer.read();					
+					isFirstUpload = 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);	
+			       		/* Start uploading data */	
+						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;		
+							}
+							else {
+								intervalSecondCounter++;
+							}
+						}
+						lastRead = UploadTimer.read();
+			    	}					
+				}        
 		    	/* allow the MQTT client to receive subscribe messages and manage keep alive */
 		    	client.yield(500);                                                        
 			} 
--- a/Sensor/ReadSensor.cpp	Thu Mar 08 14:19:48 2018 +0000
+++ b/Sensor/ReadSensor.cpp	Thu Mar 08 16:38:35 2018 +0000
@@ -39,7 +39,7 @@
 AnalogIn     DOSensor(SENSOR_1_PIN);
 
 /* Error often occurs here */
-//DS1820       probe(SENSOR_2_PIN);
+DS1820       probe(SENSOR_2_PIN);
 
 int getMedianNum(int bArray[], int iFilterLen) {
     int bTab[iFilterLen];
--- a/Sensor/ReadSensor.h	Thu Mar 08 14:19:48 2018 +0000
+++ b/Sensor/ReadSensor.h	Thu Mar 08 16:38:35 2018 +0000
@@ -2,7 +2,7 @@
 #define __READSENSOR_H__
 
 #define SENSOR_1_PIN  (A5)
-#define SENSOR_2_PIN  (A4)
+#define SENSOR_2_PIN  (D8)
 #define SENSOR_3_PIN  (A3)
 
 #define VREF           5000
--- a/Simple-MQTT/SimpleMQTT.h	Thu Mar 08 14:19:48 2018 +0000
+++ b/Simple-MQTT/SimpleMQTT.h	Thu Mar 08 16:38:35 2018 +0000
@@ -397,7 +397,7 @@
     }
     
 //    strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
-    sprintf(buf, "{\"type\":2,\"deviceId\":\"PROEVN\",\"time\":\"%d\",\"cmdId\":%d,\"temperature0\":%.2f,\"oxygen0\":%.2f}",
+    sprintf(buf, "{\"type\":2,\"deviceId\":\"PROEVN\",\"time\":%d,\"cmdId\":%d,\"temperature0\":%.2f,\"oxygen0\":%.2f}",
                 inputTime, commandID, TempVal ,DOVal);
     message.qos        = MQTT::QOS0;
     message.retained   = false;
@@ -423,7 +423,7 @@
         return MQTT::FAILURE; 
     }
 //    strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
-    sprintf(buf, "{\"type\":3,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"relay1\":%d,\"relay2\":%d,\"relay3\":%d}",
+    sprintf(buf, "{\"type\":3,\"deviceId\":\"PROEVN\",\"time\":%d,\"cmdId\":%d,\"relay1\":%d,\"relay2\":%d,\"relay3\":%d}",
                 inputTime, commandID, relay1, relay2, relay3);
     message.qos        = MQTT::QOS0;
     message.retained   = false;
@@ -450,7 +450,7 @@
         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,\"OxygenThreshold\":%d,\"TemperatureThreshold\":%d,\"uploadInterval\":%d}",
+    sprintf(buf, "{\"type\":4,\"deviceId\":\"PROEVN\",\"time\":%d,\"cmdId\":%d,\"mode\":%d,\"OxygenThreshold\":%d,\"TemperatureThreshold\":%d,\"uploadInterval\":%d}",
                 inputTime, commandID, mode, OxyThreshold, TemperatureThreshold, UploadInterval);
     message.qos        = MQTT::QOS0;
     message.retained   = false;