Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code
Revision 55:c405323f8d5a, committed 2018-03-29
- Comitter:
- DuyLionTran
- Date:
- Thu Mar 29 16:52:35 2018 +0000
- Parent:
- 54:d8dda8394353
- Child:
- 56:3729bedac5ab
- Commit message:
- * version 2.9.7 03-29-2018: Try to upload 1 more time if upload fail
Changed in this revision
--- a/Application/main.cpp Fri Mar 23 17:18:08 2018 +0000
+++ b/Application/main.cpp Thu Mar 29 16:52:35 2018 +0000
@@ -14,6 +14,7 @@
* 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
+ * version 2.9.7 03-29-2018: Try to upload 1 more time if upload fail
*/
/***************************************************************
@@ -34,7 +35,7 @@
//#define READ_ANALOG_SOFTTIMER
#define READ_SECOND 1 /* Read timer every 1 second(s) */
-#define INTERVAL_BETWEEN_EACH_UPLOAD_TYPE 5 /* The interval between each upload type in second */
+#define INTERVAL_BETWEEN_EACH_UPLOAD_TYPE 10 /* The interval between each upload type in second */
#define RECONNECT_WIFI 60 /* Try to reconnect to wifi */
#ifdef READ_ANALOG_SOFTTIMER
#define READ_ANALOG_MS 30
@@ -121,6 +122,18 @@
DataStruct.CONFIG_TemperatureThreshold, DataStruct.CONFIG_UploadInterval,
DataStruct.CONFIG_AlarmTime,
DataStruct.CONFIG_SetRelayState_1, DataStruct.CONFIG_SetRelayState_2);
+ if (DataStruct.CONFIG_Mode == 0xFFFFFFFF) {
+ DataStruct.CONFIG_Mode = 0;
+ }
+ if (DataStruct.CONFIG_OxyThreshold == 0xFFFFFFFF) {
+ DataStruct.CONFIG_OxyThreshold = 50;
+ }
+ if (DataStruct.CONFIG_TemperatureThreshold == 0xFFFFFFFF) {
+ DataStruct.CONFIG_TemperatureThreshold = 25;
+ }
+ if (DataStruct.CONFIG_UploadInterval == 0xFFFFFFFF) {
+ DataStruct.CONFIG_UploadInterval = 300;
+ }
CE_HandleRelays(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);
}
@@ -153,6 +166,9 @@
}
void enableESP() {
+ espRs = 0;
+ espEn = 0;
+ wait(1);
espEn = 1;
espRs = 1;
printf("ESP enabled\r\n");
@@ -168,8 +184,9 @@
}
void AutomaticHandle() {
- if (DataStruct.CONFIG_Mode == 0) {
- if ((uint32_t)(DataStruct.SENSOR_DOVal*100) >= DataStruct.CONFIG_OxyThreshold) {
+ if (DataStruct.CONFIG_Mode == 1) { /* Automatic mode */
+// printf("DataStruct.SENSOR_DOVal*100 %d\r\n",(uint32_t)(DataStruct.SENSOR_DOVal*100));
+ if ((uint32_t)(DataStruct.SENSOR_DOVal*10) >= DataStruct.CONFIG_OxyThreshold) {
/* Turn on the pumps */
DataStruct.RELAY_State_1 = 1;
DataStruct.RELAY_State_2 = 1;
@@ -188,7 +205,7 @@
}
void TimeAlarmHandle(time_t CurrentTime) {
- if (CurrentTime >= (DataStruct.CONFIG_AlarmTime - ALARM_TIME_ODD)) {
+ if (CurrentTime == (DataStruct.CONFIG_AlarmTime)) {
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);
@@ -201,6 +218,7 @@
int main() {
pc.baud(115200);
printf("PROGRAM STARTS\r\n");
+ set_time(1514768400); //01-01-2018 8:30AM
enableESP();
UploadTimer.start();
@@ -255,12 +273,14 @@
if(connected == true) {
/* Upload for the first time */
if (isFirstUpload) {
- MQTT_PublishAll(&client, seconds, CONFIG_VALUE, DataStruct);
+ if (MQTT_PublishAll(&client, seconds, SENSOR_VALUE, DataStruct) != MQTT::SUCCESS) {
+ wait(2);
+ MQTT_PublishAll(&client, seconds, SENSOR_VALUE, DataStruct);
+ }
isFirstUpload = false;
}
/* Periodic upload */
- if ((uint32_t)(UploadTimer.read() - lastRead) >= READ_SECOND) { // Read timer every readSecond(s)
- printf("Periodic upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);
+ if ((uint32_t)(UploadTimer.read() - lastRead) >= READ_SECOND) { // Read timer every readSecond(s)
/* Start uploading data */
if (!isUploading) {
wd.Service();
@@ -286,11 +306,24 @@
}
}
else {
- myled = 0;
- client.disconnect();
- mqttNetwork.disconnect();
- /* if we have lost the connection */
- MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct);
+ /* Try to reupload */
+ if (MQTT_PublishAll(&client, seconds, uploadType, DataStruct) == MQTT::SUCCESS) {
+ myled = 1;
+ uploadType++;
+ if (uploadType > RELAY_STATE) {
+ 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;
--- a/Sensor/ReadSensor.cpp Fri Mar 23 17:18:08 2018 +0000
+++ b/Sensor/ReadSensor.cpp Thu Mar 29 16:52:35 2018 +0000
@@ -146,6 +146,7 @@
printf("Exit Calibration Mode<<<\r\n\r\n");
doCalibrationFinishFlag = false;
enterCalibrationFlag = false;
+ NVIC_SystemReset();
}
break;
--- a/Simple-MQTT/SimpleMQTT.h Fri Mar 23 17:18:08 2018 +0000
+++ b/Simple-MQTT/SimpleMQTT.h Thu Mar 29 16:52:35 2018 +0000
@@ -229,19 +229,19 @@
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
+ KeyIndex = json.findKeyIndexIn("relayState0", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, relayState1);
+
KeyIndex = json.findKeyIndexIn("relayState1", 0);
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
- ret = json.tokenIntegerValue(KeyValueIndex, relayState1);
+ ret = json.tokenIntegerValue(KeyValueIndex, relayState2);
KeyIndex = json.findKeyIndexIn("relayState2", 0);
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
- ret = json.tokenIntegerValue(KeyValueIndex, relayState2);
-
- KeyIndex = json.findKeyIndexIn("relayState3", 0);
- KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
ret = json.tokenIntegerValue(KeyValueIndex, relayState3);
- if (DataStruct.CONFIG_Mode == 1) {
+ if (DataStruct.CONFIG_Mode == 0) {
DataStruct.RELAY_State_1 = relayState1;
DataStruct.RELAY_State_2 = relayState2;
DataStruct.RELAY_State_3 = relayState3;
@@ -252,7 +252,7 @@
break;
case 3: printf("Set config values\r\n");
- int mode, OxiThres, TempThres, uploadInterval, setRTCTime;
+ int mode, OxiThres, TempThres, uploadInterval;
KeyIndex = json.findKeyIndexIn("cmdID", 0);
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
@@ -272,21 +272,34 @@
KeyIndex = json.findKeyIndexIn("uploadInterval", 0);
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
ret = json.tokenIntegerValue(KeyValueIndex, uploadInterval);
-
- KeyIndex = json.findKeyIndexIn("setRTCTime", 0);
- KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
- ret = json.tokenIntegerValue(KeyValueIndex, setRTCTime);
-
+
DataStruct.CONFIG_Mode = mode;
DataStruct.CONFIG_OxyThreshold = OxiThres;
DataStruct.CONFIG_TemperatureThreshold = TempThres;
- DataStruct.CONFIG_UploadInterval = uploadInterval * 60;
+ DataStruct.CONFIG_UploadInterval = uploadInterval * 60;
+ printf("OMode %d\r\n", DataStruct.CONFIG_Mode );
+ printf("Oxygen Threshold*10 %dppm(*100)\r\n", DataStruct.CONFIG_OxyThreshold);
+ printf("Temperature Threshold %d*C\r\n", DataStruct.CONFIG_TemperatureThreshold);
+ printf("Periodic upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);
FP_WriteConfigValues(DataStruct.CONFIG_Mode, DataStruct.CONFIG_OxyThreshold, DataStruct.CONFIG_TemperatureThreshold, DataStruct.CONFIG_UploadInterval);
- CE_SetRTCTime(setRTCTime);
+
break;
case 4: printf("Synchronize data\r\n");
CE_UpdateImmediately();
+ case 5: printf("Set time\r\n");
+ int setRTCTime;
+ KeyIndex = json.findKeyIndexIn("cmdID", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
+
+ KeyIndex = json.findKeyIndexIn("setRTCTime", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, setRTCTime);
+
+ printf("Epoch time %d\r\n", (uint32_t)setRTCTime);
+ CE_SetRTCTime((uint32_t)setRTCTime);
+
break;
default: break;
@@ -379,7 +392,10 @@
// or maybe just add the proper members to do this disconnect and call MQTT_AttemptConnect(...)
// this works - reset the system when the retry count gets to a threshold
if (retryAttempt == 5)
- NVIC_SystemReset();
+ {
+ /* RESET ESP */
+ NVIC_SystemReset();
+ }
else
wait(timeout);
}
--- a/jsonString.md Fri Mar 23 17:18:08 2018 +0000
+++ b/jsonString.md Thu Mar 29 16:52:35 2018 +0000
@@ -33,8 +33,8 @@
{"type":2, "deviceId": "string", "cmdID":uint16_t,"relayState1":int 0 or 1, "relayState2":int 0 or 1} //Ði?u khi?n relay nhu cu
{"type":3, "deviceId": "string", "cmdID":"uint16_t","mode":int 0 or 1,"OxygenThreshold":int,"TemperatureThreshold":int, "uploadInterval":int, "setRTCTime":epoch}
- 0 for automatic //field cuoi là cài dat gio cho
- 1 for manual //thiet bi
+ 1 for automatic //field cuoi là cài dat gio cho
+ 0 for manual //thiet bi
{"type":4, "deviceId": "string", "cmdID":uint16_t} //Cái này là nút synchronize, nh?n vô phát là c?p nh?t h?t giá tr? c?m bi?n
--- a/mbed-os.lib Fri Mar 23 17:18:08 2018 +0000 +++ b/mbed-os.lib Thu Mar 29 16:52:35 2018 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#91e6db1ea251ffcc973001ed90477f42fdca5751 +https://github.com/ARMmbed/mbed-os/#16bac101a6b7b4724023dcf86ece1548e3a23cbf