iot_water_monitor_v2
Dependencies: easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code
Revision 38:64e3e9acb49c, committed 2018-02-19
- Comitter:
- DuyLionTran
- Date:
- Mon Feb 19 14:18:22 2018 +0000
- Parent:
- 37:338e3bc04e57
- Child:
- 39:a5ee98bd0050
- Commit message:
- * version 2.6.3 02-19-2017: developing calibration. the average voltage is ok
Changed in this revision
--- a/Application/main.cpp Wed Feb 14 14:56:00 2018 +0000 +++ b/Application/main.cpp Mon Feb 19 14:18:22 2018 +0000 @@ -4,6 +4,7 @@ * .... * version 2.5 02-14-2017: 3rd relay and remote time setting are added * version 2.6 02-14-2017: DO Sensor added, calibration is still on the way + * version 2.6.3 02-19-2017: developing calibration. the average voltage is ok */ /*************************************************************** @@ -114,7 +115,7 @@ } ReadAllFlashValues(); - SENSOR_Calib(); + SENSOR_ReadDoCharacteristicValues(); myled=1; while (true) { @@ -151,7 +152,8 @@ myled = 0; client.disconnect(); mqttNetwork.disconnect(); - MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct); // if we have lost the connection + /* if we have lost the connection */ + MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct); } intervalSecondCounter = 0; } @@ -161,7 +163,8 @@ } lastRead = UploadTimer.read(); } - client.yield(500); // allow the MQTT client to receive subscribe messages and manage keep alive + /* allow the MQTT client to receive subscribe messages and manage keep alive */ + client.yield(500); } else if (connected == false) { connected = true;
--- a/Flash/flash_programming.cpp Wed Feb 14 14:56:00 2018 +0000 +++ b/Flash/flash_programming.cpp Mon Feb 19 14:18:22 2018 +0000 @@ -232,4 +232,43 @@ } HAL_FLASH_Lock(); return PASSED; +} + +int FP_WriteConfigValues(uint32_t SaturationDoVoltage) { + uint8_t CurrentPage = FP_GetPage(SAT_DO_VOLT_ADDRESS); + uint32_t CurrentAddress = SAT_DO_VOLT_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(SAT_DO_VOLT_ADDRESS) == SaturationDoVoltage)) { + printf("Configuration values doesn'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, SaturationDoVoltage) == HAL_OK) { + printf("Write SaturationDoVoltage OK\r\n"); + } + else { + printf("Write SaturationDoVoltage failed, error num %d\r\n", HAL_FLASH_GetError()); + } + if (FP_ReadValue(CurrentAddress) == SaturationDoVoltage) { + printf("Read back SaturationDoVoltage: %d\r\n", FP_ReadValue(CurrentAddress)); + } + else { + printf("Write SaturationDoVoltage failed, wrong read back value\r\n"); + HAL_FLASH_Lock(); + return FAILED; + } + HAL_FLASH_Lock(); + return PASSED; } \ No newline at end of file
--- a/Flash/flash_programming.h Wed Feb 14 14:56:00 2018 +0000 +++ b/Flash/flash_programming.h Mon Feb 19 14:18:22 2018 +0000 @@ -136,7 +136,8 @@ #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 CALIB_BASE_ADDRESS ADDR_FLASH_PAGE_105 /* USER DEFINE */ +#define EMPT_BASE_ADDRESS ADDR_FLASH_PAGE_106 /* USER DEFINE */ #define RELAY1_ADDRESS (RELAY_BASE_ADDRESS + STEP_ADDRESS) #define RELAY2_ADDRESS (RELAY1_ADDRESS + STEP_ADDRESS) @@ -147,6 +148,9 @@ #define MAX_OXI_ADDRESS (MIN_OXI_ADDRESS + STEP_ADDRESS) #define UPLOAD_PERIOD_ADDRESS (MAX_OXI_ADDRESS + STEP_ADDRESS) +#define SAT_DO_VOLT_ADDRESS (CALIB_BASE_ADDRESS + STEP_ADDRESS) + + typedef enum { FAILED = 1, PASSED = 0 @@ -193,4 +197,11 @@ */ int FP_WriteConfigValues(uint8_t Mode ,uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod); +/** + * @brief Write the calibration value to the flash memory + * @param SaturationDoVoltagex10: + * @retval FLASH result + */ +int FP_WriteConfigValues(uint32_t SaturationDoVoltage); + #endif /* __FLASH_PROGRAMMING_H__ */ \ No newline at end of file
--- a/Sensor/ReadSensor.cpp Wed Feb 14 14:56:00 2018 +0000 +++ b/Sensor/ReadSensor.cpp Mon Feb 19 14:18:22 2018 +0000 @@ -1,5 +1,6 @@ #include "mbed.h" #include "ReadSensor.h" +#include "flash_programming.h" const float SaturationValueTab[41] = { /* saturation dissolved oxygen concentrations at various temperatures */ 14.46, 14.22, 13.82, 13.44, 13.09, @@ -19,17 +20,18 @@ int analogBufferTemp[SCOUNT]; int analogBufferIndex = 0; int copyIndex = 0; -float SaturationDoVoltage = 1127.6; /* mV */ -float SaturationDoTemperature = 25.0; /* ^C */ -float averageVoltage; -AnalogIn DOSensor(SENSOR_1_PIN); +float SaturationDoVoltage; /* mV */ +float SaturationDoTemperature = 25.0; /* ^C */ +float averageVoltage; +AnalogIn DOSensor(SENSOR_1_PIN); int getMedianNum(int bArray[], int iFilterLen) { int bTab[iFilterLen]; for (uint8_t i = 0; i < iFilterLen; i++) { bTab[i] = bArray[i]; } - int i, j, bTemp; + int i, j; + int bTemp; for (j = 0; j < iFilterLen - 1; j++) { for (i = 0; i < iFilterLen - j - 1; i++) { if (bTab[i] > bTab[i + 1]) { @@ -49,8 +51,10 @@ } void SENSOR_AnalogRead() { - analogBuffer[analogBufferIndex] = (int)(DOSensor.read() * 100); - printf("Analog read %d (%.2f)\r\n", analogBuffer[analogBufferIndex], DOSensor.read()); + float analogReadRaw = DOSensor.read(); + float voltageRaw = analogReadRaw*3.3; + analogBuffer[analogBufferIndex] = (int)((voltageRaw * 1023.0)/5.0); + printf("Analog read %d %d\r\n", analogBufferIndex, analogBuffer[analogBufferIndex]); analogBufferIndex++; if(analogBufferIndex == SCOUNT) { analogBufferIndex = 0; @@ -62,13 +66,32 @@ analogBufferTemp[copyIndex]= analogBuffer[copyIndex]; } /* read the value more stable by the median filtering algorithm */ - averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF; + averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF/1024.0; printf("Average voltage %.2f\r\n", averageVoltage); /* calculate the do value, doValue = Voltage / SaturationDoVoltage * SaturationDoValue(with temperature compensation) */ - doValue = (SaturationValueTab[0] + (int)(SaturationDoTemperature + 0.5)) * averageVoltage / SaturationDoVoltage; - printf("DO Value %.2f mg/L\r\n", doValue); + doValue = (SaturationValueTab[0] + (int)(SaturationDoTemperature + 0.5)) * averageVoltage / (float)(SaturationDoVoltage*10.0); + float a = 1; + printf("SaturationDoVoltage %.2f; DO Value %.2f mg/L\r\n", SaturationDoVoltage, doValue/a); } void SENSOR_Calib() { +} + +void SENSOR_ReadDoCharacteristicValues() { + FP_WriteConfigValues(1508); + uint32_t int_SaturationDoVoltage = FP_ReadValue(SAT_DO_VOLT_ADDRESS); + printf("Read SaturationDoVoltage %d\r\n", int_SaturationDoVoltage); + SaturationDoVoltage = (float)int_SaturationDoVoltage; + printf("SaturationDoVoltage %f\r\n", SaturationDoVoltage); + if (FP_ReadValue(SAT_DO_VOLT_ADDRESS) == 0xFF) { + /* default voltage: 1127V */ + SaturationDoVoltage = 1127.6; + int_SaturationDoVoltage = (uint32_t)SaturationDoVoltage; + if ((SaturationDoVoltage - 0.5) >= (float)(int_SaturationDoVoltage)) { + int_SaturationDoVoltage++; + } + printf("SaturationDoVoltage %d\r\n", (uint32_t)int_SaturationDoVoltage); + FP_WriteConfigValues((uint32_t)(int_SaturationDoVoltage)); + } } \ No newline at end of file
--- a/Sensor/ReadSensor.h Wed Feb 14 14:56:00 2018 +0000 +++ b/Sensor/ReadSensor.h Mon Feb 19 14:18:22 2018 +0000 @@ -5,11 +5,12 @@ #define SENSOR_2_PIN (A4) #define SENSOR_3_PIN (A3) -#define VREF 33 +#define VREF 5000 #define SCOUNT 30 /* sum of sample point */ void SENSOR_Calib(); void SENSOR_AnalogRead(); void SENSOR_GetDOValue(); +void SENSOR_ReadDoCharacteristicValues(); #endif /* __READSENSOR_H__ */ \ No newline at end of file