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 33:5b90257d2d57, committed 2018-01-21
- Comitter:
- DuyLionTran
- Date:
- Sun Jan 21 13:04:06 2018 +0000
- Parent:
- 32:8226837c56ae
- Child:
- 34:601effb7b7fe
- Commit message:
- version 2.3
Changed in this revision
--- a/Application/main.cpp Mon Jan 08 18:07:56 2018 +0000
+++ b/Application/main.cpp Sun Jan 21 13:04:06 2018 +0000
@@ -7,12 +7,15 @@
#include "ReadSensor.h"
#include "SimpleMQTT.h"
+#include "CommandExecution.h"
#include "flash_programming.h"
/***************************************************************
* Definitions
***************************************************************/
-
+#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 RECONNECT_WIFI 60 /* Try to reconnect to wifi */
/***************************************************************
* Variables
@@ -20,12 +23,12 @@
bool isUploading = false;
uint8_t uploadType = ADC_VALUE;
-uint32_t lastRead = 0;
-uint16_t readSecond = 5; /* Read timer every 5 second(s) */
-uint32_t uploadPeriod = 5; /* Period between each time upload all data = uploadPeriod*readSecond = 25 s */
-uint32_t uploadPeriodCounter = 0;
+uint32_t lastRead = 0;
+uint32_t noWiFilastRead = 0;
+uint16_t intervalSecondCounter = 0;
+uint32_t uploadPeriodCounter = 0;
-struct UploadValue DataStruct = {0.8, 0.9, 8.0, 89.9, 0, 1, 0, 50, 75, 25};
+struct UploadValue DataStruct;
/***************************************************************
@@ -39,11 +42,27 @@
/***************************************************************
* Unity function definitions
***************************************************************/
+void ReadAllFlashValues();
/***************************************************************
* Unity function declarations
***************************************************************/
-
+void ReadAllFlashValues() {
+ DataStruct.ADC_PHVal = 0.85;
+ DataStruct.ADC_DOVal = 0.95;
+ DataStruct.SENSOR_PHVal = 17.8;
+ DataStruct.SENSOR_D0Val = 18.9;
+ DataStruct.RELAY_State_1 = FP_ReadValue(RELAY1_ADDRESS);
+ DataStruct.RELAY_State_2 = FP_ReadValue(RELAY2_ADDRESS);
+ DataStruct.CONFIG_Mode = FP_ReadValue(MODE_ADDRESS);
+ DataStruct.CONFIG_MinOxi = FP_ReadValue(MIN_OXI_ADDRESS);
+ DataStruct.CONFIG_MaxOxi = FP_ReadValue(MAX_OXI_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_Mode, DataStruct.CONFIG_MinOxi,
+ DataStruct.CONFIG_MaxOxi, DataStruct.CONFIG_UploadInterval);
+ CE_HandleRelays(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2);
+}
/***************************************************************
* Main
@@ -65,51 +84,74 @@
printf ("ATTEMPT CONNECT\n\r");
MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct);
if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
- printf ("---ERROR line : %d\n\r", __LINE__);
- while (true)
- wait(1.0); // Permanent failures - don't retry
+ printf ("---ERROR line : %d, error type %d\n\r", __LINE__, connack_rc);
+ wifiConnected = false;
+// while (true)
+// wait(1.0); // Permanent failures - don't retry
}
+ ReadAllFlashValues();
+ myled=1;
- myled=1;
- FP_WriteConfigValues(25, 26, 27);
while (true) {
- time_t seconds = time(NULL);
-
- if(connected == true) {
- if ((uint32_t)(UploadTimer.read() - lastRead) >= readSecond) { // Publish a message every 3 second
- printf("Upload time %ds\r\n", FP_ReadValue(UPLOAD_PERIOD_ARRESS));
- if (!isUploading) {
- uploadPeriodCounter++;
- if (uploadPeriodCounter == uploadPeriod) {
- uploadPeriodCounter = 0;
- isUploading = true;
- }
- }
- else {
- if (MQTT_PublishAll(&client, seconds, uploadType, DataStruct) == MQTT::SUCCESS) {
- myled = 1;
- uploadType++;
- if (uploadType > CONFIG_VALUE) {
- isUploading = false;
- uploadType = ADC_VALUE;
- commandID++;
- UploadTimer.reset();
+ time_t seconds = time(NULL);
+ 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);
+ 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) {
+ if (MQTT_PublishAll(&client, seconds, uploadType, DataStruct) == MQTT::SUCCESS) {
+ myled = 1;
+ uploadType++;
+ if (uploadType > CONFIG_VALUE) {
+ isUploading = false;
+ uploadType = ADC_VALUE;
+ commandID++;
+ UploadTimer.reset();
+ }
+ }
+ else {
+ myled = 0;
+ client.disconnect();
+ mqttNetwork.disconnect();
+ MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct); // if we have lost the connection
+ }
+ intervalSecondCounter = 0;
+ }
+ else {
+ intervalSecondCounter++;
}
}
- else {
- myled = 0;
- client.disconnect();
- mqttNetwork.disconnect();
- MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct); // if we have lost the connection
- }
- }
- lastRead = UploadTimer.read();
- }
- client.yield(500); // allow the MQTT client to receive subscribe messages and manage keep alive
- }
- else if (connected == false) {
- connected = true;
- }
+ lastRead = UploadTimer.read();
+ }
+ client.yield(500); // allow the MQTT client to receive subscribe messages and manage keep alive
+ }
+ else if (connected == false) {
+ connected = true;
+ }
+ }
+ else {
+ if ((uint32_t)(UploadTimer.read() - noWiFilastRead) >= RECONNECT_WIFI) {
+ wifiConnected = true;
+ network = easy_connect(true);
+ MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct);
+ if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
+ printf ("---ERROR line : %d, error type %d\n\r", __LINE__, connack_rc);
+ wifiConnected = false;
+ // while (true)
+ // wait(1.0); // Permanent failures - don't retry
+ }
+ noWiFilastRead = UploadTimer.read();
+ }
+ }
}
}
--- a/Flash/flash_programming.cpp Mon Jan 08 18:07:56 2018 +0000
+++ b/Flash/flash_programming.cpp Sun Jan 21 13:04:06 2018 +0000
@@ -38,9 +38,9 @@
return ReturnValue;
}
-int FP_WriteMode(uint32_t WriteModeValue) {
- uint8_t CurrentPage = FP_GetPage(MODE_BASE_ADDRESS);
- uint32_t CurrentAddress = (MODE_BASE_ADDRESS + STEP_ADDRESS);
+int FP_WriteRelayStates(uint8_t RelayState1, uint8_t RelayState2) {
+ uint8_t CurrentPage = FP_GetPage(RELAY_BASE_ADDRESS);
+ uint32_t CurrentAddress = RELAY1_ADDRESS;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = FLASH_BANK_1;
@@ -52,34 +52,68 @@
}
HAL_FLASH_Unlock();
- if (FP_ReadValue(CurrentAddress) == WriteModeValue) {
- printf("Value does not change, no need to write\r\n");
+ if ((FP_ReadValue(RELAY1_ADDRESS) == RelayState1) &&
+ (FP_ReadValue(RELAY2_ADDRESS) == RelayState2)) {
+ 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());
}
- if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, WriteModeValue) == HAL_OK) {
- printf("Write OK\r\n");
- }
- else {
- printf("Write failed, error num %d\r\n", HAL_FLASH_GetError());
+ while (CurrentAddress < (RELAY2_ADDRESS + STEP_ADDRESS)) {
+ switch (CurrentAddress) {
+ case (RELAY1_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, RelayState1) == HAL_OK) {
+ printf("Write Relay 1 State OK\r\n");
+ }
+ else {
+ printf("Write Relay 1 State failed, error num %d\r\n", HAL_FLASH_GetError());
+ }
+ break;
+ case (RELAY2_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, RelayState2) == HAL_OK) {
+ printf("Write Relay 2 State OK\r\n");
+ }
+ else {
+ printf("Write Relay 2 State failed, error num %d\r\n", HAL_FLASH_GetError());
+ }
+ break;
+ default: break;
+ }
+ CurrentAddress = CurrentAddress + STEP_ADDRESS;
}
- uint32_t readBack = FP_ReadValue(CurrentAddress);
- printf("Read back: %d\r\n", readBack);
- if (readBack != WriteModeValue) {
- printf("Write failed, wrong read back value\r\n");
- HAL_FLASH_Lock();
- return FAILED;
+
+ CurrentAddress = RELAY1_ADDRESS;
+ while (CurrentAddress < (RELAY2_ADDRESS + STEP_ADDRESS)) {
+ switch (CurrentAddress) {
+ case (RELAY1_ADDRESS): if (FP_ReadValue(CurrentAddress) == RelayState1) {
+ printf("Read back Relay 1 State: %d\r\n", FP_ReadValue(CurrentAddress));
+ }
+ else {
+ printf("Write Relay 1 State failed, wrong read back value\r\n");
+ HAL_FLASH_Lock();
+ return FAILED;
+ }
+ break;
+ case (RELAY2_ADDRESS): if (FP_ReadValue(CurrentAddress) == RelayState2) {
+ printf("Read back Relay 2 State: %d\r\n", FP_ReadValue(CurrentAddress));
+ }
+ else {
+ printf("Write Relay 2 State failed, wrong read back value\r\n");
+ HAL_FLASH_Lock();
+ return FAILED;
+ }
+ break;
+ default: break;
+ }
+ CurrentAddress = CurrentAddress + STEP_ADDRESS;
}
HAL_FLASH_Lock();
return PASSED;
}
-int FP_WriteConfigValues(uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod) {
+int FP_WriteConfigValues(uint8_t Mode, uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod) {
uint8_t CurrentPage = FP_GetPage(CONF_BASE_ADDRESS);
- uint32_t CurrentAddress = MIN_OXI_ADDRESS;
+ uint32_t CurrentAddress = MODE_ADDRESS;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = FLASH_BANK_1;
EraseInitStruct.Page = CurrentPage;
@@ -90,18 +124,26 @@
}
HAL_FLASH_Unlock();
- if ((FP_ReadValue(MIN_OXI_ADDRESS) == MinOxi) &&
+ if ((FP_ReadValue(MODE_ADDRESS) == Mode) &&
+ (FP_ReadValue(MIN_OXI_ADDRESS) == MinOxi) &&
(FP_ReadValue(MAX_OXI_ADDRESS) == MaxOxi) &&
- (FP_ReadValue(UPLOAD_PERIOD_ARRESS) == UploadPeriod)) {
- printf("Value does not change, no need to write\r\n");
+ (FP_ReadValue(UPLOAD_PERIOD_ADDRESS) == UploadPeriod)) {
+ printf("Configuration 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 < (UPLOAD_PERIOD_ARRESS + STEP_ADDRESS)) {
+ while (CurrentAddress < (UPLOAD_PERIOD_ADDRESS + STEP_ADDRESS)) {
switch (CurrentAddress) {
+ case (MODE_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, Mode) == HAL_OK) {
+ printf("Write Mode OK\r\n");
+ }
+ else {
+ printf("Write Mode failed, error num %d\r\n", HAL_FLASH_GetError());
+ }
+ break;
case (MIN_OXI_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, MinOxi) == HAL_OK) {
printf("Write MinOxi OK\r\n");
}
@@ -116,7 +158,7 @@
printf("Write MaxOxi failed, error num %d\r\n", HAL_FLASH_GetError());
}
break;
- case (UPLOAD_PERIOD_ARRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, UploadPeriod) == HAL_OK) {
+ case (UPLOAD_PERIOD_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, UploadPeriod) == HAL_OK) {
printf("Write UploadPeriod OK\r\n");
}
else {
@@ -128,9 +170,18 @@
CurrentAddress = CurrentAddress + STEP_ADDRESS;
}
- CurrentAddress = MIN_OXI_ADDRESS;
- while (CurrentAddress < (UPLOAD_PERIOD_ARRESS + STEP_ADDRESS)) {
+ CurrentAddress = MODE_ADDRESS;
+ while (CurrentAddress < (UPLOAD_PERIOD_ADDRESS + STEP_ADDRESS)) {
switch (CurrentAddress) {
+ case (MODE_ADDRESS): if (FP_ReadValue(CurrentAddress) == Mode) {
+ printf("Read back Mode: %d\r\n", FP_ReadValue(CurrentAddress));
+ }
+ else {
+ printf("Write Mode failed, wrong read back value\r\n");
+ HAL_FLASH_Lock();
+ return FAILED;
+ }
+ break;
case (MIN_OXI_ADDRESS): if (FP_ReadValue(CurrentAddress) == MinOxi) {
printf("Read back MinOxi: %d\r\n", FP_ReadValue(CurrentAddress));
}
@@ -149,7 +200,7 @@
return FAILED;
}
break;
- case (UPLOAD_PERIOD_ARRESS): if (FP_ReadValue(CurrentAddress) == UploadPeriod) {
+ case (UPLOAD_PERIOD_ADDRESS): if (FP_ReadValue(CurrentAddress) == UploadPeriod) {
printf("Read back UploadPeriod: %d\r\n", FP_ReadValue(CurrentAddress));
}
else {
--- a/Flash/flash_programming.h Mon Jan 08 18:07:56 2018 +0000
+++ b/Flash/flash_programming.h Sun Jan 21 13:04:06 2018 +0000
@@ -133,14 +133,18 @@
#define ADDR_FLASH_PAGE_127 ((uint32_t)0x0803F800) /* Base @ of Page 127, 2 Kbytes */
#define STEP_ADDRESS 0x00000008
-#define MODE_BASE_ADDRESS ADDR_FLASH_PAGE_101 /* USER DEFINE */
-#define TIME_BASE_ADDRESS ADDR_FLASH_PAGE_102 /* USER DEFINE */
-#define CONF_BASE_ADDRESS ADDR_FLASH_PAGE_103 /* USER DEFINE */
-#define EMPT_BASE_ADDRESS ADDR_FLASH_PAGE_104 /* USER DEFINE */
+#define RELAY_BASE_ADDRESS ADDR_FLASH_PAGE_102 /* USER DEFINE */
+#define TIME_BASE_ADDRESS ADDR_FLASH_PAGE_103 /* USER DEFINE */
+#define CONF_BASE_ADDRESS ADDR_FLASH_PAGE_104 /* USER DEFINE */
+#define EMPT_BASE_ADDRESS ADDR_FLASH_PAGE_105 /* USER DEFINE */
-#define MIN_OXI_ADDRESS (CONF_BASE_ADDRESS + STEP_ADDRESS)
-#define MAX_OXI_ADDRESS (MIN_OXI_ADDRESS + STEP_ADDRESS)
-#define UPLOAD_PERIOD_ARRESS (MAX_OXI_ADDRESS + STEP_ADDRESS)
+#define RELAY1_ADDRESS (RELAY_BASE_ADDRESS + STEP_ADDRESS)
+#define RELAY2_ADDRESS (RELAY1_ADDRESS + STEP_ADDRESS)
+
+#define MODE_ADDRESS (CONF_BASE_ADDRESS + STEP_ADDRESS)
+#define MIN_OXI_ADDRESS (MODE_ADDRESS + STEP_ADDRESS)
+#define MAX_OXI_ADDRESS (MIN_OXI_ADDRESS + STEP_ADDRESS)
+#define UPLOAD_PERIOD_ADDRESS (MAX_OXI_ADDRESS + STEP_ADDRESS)
typedef enum {
FAILED = 1,
@@ -163,11 +167,12 @@
uint32_t FP_ReadValue(uint32_t Addr);
/**
- * @brief Write the current working mode of the device into the flash memory
- * @param WriteModeValue: Current working mode of the device
+ * @brief Write the states of relays into the flash memory
+ * @param RelayState1: Relay 1 state
+ * @param RelayState2: Relay 2 state
* @retval FLASH result
*/
-int FP_WriteMode(uint32_t WriteModeValue);
+int FP_WriteRelayStates(uint8_t RelayState1, uint8_t RelayState2);
/**
* @brief Write the time to activate an event into the flash memory
@@ -178,9 +183,12 @@
/**
* @brief Write the value to control the switches into the flash memory
- * @param WriteTimeValue: Current working mode of the device
+ * @param Mode: Current working mode of the device
+ * @param MinOxi: Current working mode of the device
+ * @param MaxOxi: Current working mode of the device
+ * @param UploadPeriod: Current working mode of the device
* @retval FLASH result
*/
-int FP_WriteConfigValues(uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod);
+int FP_WriteConfigValues(uint8_t Mode ,uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod);
#endif /* __FLASH_PROGRAMMING_H__ */
\ No newline at end of file
--- a/Simple-MQTT/SimpleMQTT.h Mon Jan 08 18:07:56 2018 +0000
+++ b/Simple-MQTT/SimpleMQTT.h Sun Jan 21 13:04:06 2018 +0000
@@ -47,8 +47,10 @@
float SENSOR_PHVal;
float SENSOR_D0Val;
- int RELAY_State_1;
- int RELAY_State_2;
+ uint8_t RELAY_State_1;
+ uint8_t RELAY_State_2;
+
+ uint32_t CONFIG_Time;
uint8_t CONFIG_Mode;
uint8_t CONFIG_MinOxi;
@@ -62,6 +64,7 @@
static char type[30] = TYPE;
static char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
static int connack_rc = 0; // MQTT connack return code
+static bool wifiConnected = true;
static bool netConnecting = false;
static bool mqttConnecting = false;
static bool netConnected = false;
@@ -181,25 +184,26 @@
printf("Command Type: %d, error %d\r\n", CommandType, ret);
switch (CommandType) {
- case 3: int relayState0, relayState1;
+ case 3: int relayState1, relayState2;
KeyIndex = json.findKeyIndexIn("cmdID", 0);
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
- KeyIndex = json.findKeyIndexIn("relayState0", 0);
- KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
- ret = json.tokenIntegerValue(KeyValueIndex, relayState0);
-
KeyIndex = json.findKeyIndexIn("relayState1", 0);
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
- ret = json.tokenIntegerValue(KeyValueIndex, relayState1);
+ ret = json.tokenIntegerValue(KeyValueIndex, relayState1);
- DataStruct.RELAY_State_1 = relayState0;
- DataStruct.RELAY_State_2 = relayState1;
- CE_HandleRelays(relayState0, relayState1);
+ KeyIndex = json.findKeyIndexIn("relayState2", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, relayState2);
+
+ DataStruct.RELAY_State_1 = relayState1;
+ DataStruct.RELAY_State_2 = relayState2;
+ FP_WriteRelayStates(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2);
+ CE_HandleRelays(relayState1, relayState2);
break;
- case 4: int mode, minOxiVal, maxOxiVal;
+ case 4: int mode, minOxiVal, maxOxiVal, uploadInterval;
KeyIndex = json.findKeyIndexIn("cmdID", 0);
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
@@ -215,11 +219,16 @@
KeyIndex = json.findKeyIndexIn("maxOxygenVal", 0);
KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
ret = json.tokenIntegerValue(KeyValueIndex, maxOxiVal);
+
+ KeyIndex = json.findKeyIndexIn("uploadInterval", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, uploadInterval);
- DataStruct.CONFIG_Mode = mode;
- DataStruct.CONFIG_MinOxi = minOxiVal;
- DataStruct.CONFIG_MaxOxi = maxOxiVal;
- //DataStruct.CONFIG_UploadInterval = relayState0;
+ 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);
break;
default: break;