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 32:8226837c56ae, committed 2018-01-08
- Comitter:
- DuyLionTran
- Date:
- Mon Jan 08 18:07:56 2018 +0000
- Parent:
- 31:0f7ea3981668
- Child:
- 33:5b90257d2d57
- Commit message:
- version 2.1;
Changed in this revision
--- a/Application/CommandExecution.cpp Mon Jan 08 22:09:56 2018 +0700
+++ b/Application/CommandExecution.cpp Mon Jan 08 18:07:56 2018 +0000
@@ -1,2 +1,10 @@
-#include "CommandExecution.h"
-#include "mbed.h"
\ No newline at end of file
+#include "CommandExecution.h"
+#include "mbed.h"
+
+DigitalOut relay1(RELAY_1_PIN);
+DigitalOut relay2(RELAY_2_PIN);
+
+void CE_HandleRelays(int Relay1State, int Relay2State) {
+ relay1 = Relay1State;
+ relay2 = Relay2State;
+}
--- a/Application/CommandExecution.h Mon Jan 08 22:09:56 2018 +0700 +++ b/Application/CommandExecution.h Mon Jan 08 18:07:56 2018 +0000 @@ -1,7 +1,9 @@ -#ifndef __COMMANDEXECUTION_H__ -#define __COMMANDEXECUTION_H__ - -void CE_HandleRelays(); -void CE_HandleCondig(); - -#endif __COMMANDEXECUTION_H__ \ No newline at end of file +#ifndef __COMMANDEXECUTION_H__ +#define __COMMANDEXECUTION_H__ + +#define RELAY_1_PIN (D12) +#define RELAY_2_PIN (D11) + +void CE_HandleRelays(int Relay1State, int Relat2State); + +#endif /* __COMMANDEXECUTION_H__ */ \ No newline at end of file
--- a/Application/main.cpp Mon Jan 08 22:09:56 2018 +0700
+++ b/Application/main.cpp Mon Jan 08 18:07:56 2018 +0000
@@ -1,130 +1,115 @@
-// For receiving message handles, search "Message Handles" in SimpleMQTT.h
-
-/***************************************************************
- * Includes
- ***************************************************************/
-#include "mbed.h"
-
-#include "ReadSensor.h"
-#include "SimpleMQTT.h"
-#include "flash_programming.h"
-
-/***************************************************************
- * Definitions
- ***************************************************************/
-#define RELAY_1_PIN (D11)
-#define RELAY_2_PIN (D12)
-
-/***************************************************************
- * Variables
- ***************************************************************/
-bool isUploading = false;
-uint8_t uploadType = ADC_VALUE;
-
-uint32_t lastRead = 0;
-uint16_t readSecond = 5; /* Read timer every 2 second(s) */
-uint32_t uploadPeriod = 4; /* Period between each time upload all data = uploadPeriod*readSecond = 20 s */
-uint32_t uploadPeriodCounter = 0;
-
-struct UploadValue DataStruct = {19, 25, 0, 1, 0, 65, 80, 10};
-
-/* Analog Handles */
-float ADC_PHVal;
-float ADC_DOVal;
-
-/***************************************************************
- * Structs/Classess
- ***************************************************************/
-static Serial pc(SERIAL_TX, SERIAL_RX);
-
-DigitalOut myled(LED1);
-DigitalOut led1(RELAY_1_PIN);
-DigitalOut led2(RELAY_2_PIN);
-
-Timer UploadTimer;
-/***************************************************************
- * Unity function definitions
- ***************************************************************/
-
-/***************************************************************
- * Unity function declarations
- ***************************************************************/
-void MQTT_MessageHandles(uint8_t ControlSignal) {
- switch(ControlSignal) {
- case (1): led1 = !led1;
- break;
- case (2): led2 = !led2;
- break;
- default: break;
- }
-}
-
-
-/***************************************************************
- * Main
- ***************************************************************/
-int main() {
- pc.baud(115200);
- UploadTimer.start();
- lastRead = 0;
- set_time(1515445620);
- pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");
- pc.printf("\r\nconnecting to AP\r\n");
-
- NetworkInterface* network = easy_connect(true);
- if (!network) {
- printf ("Error easy_connect\n\r");
- }
- MQTTNetwork mqttNetwork(network);
- MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
- printf ("ATTEMPT CONNECT\n\r");
- MQTT_AttemptConnect(&client, &mqttNetwork, network);
- 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
- }
-
- myled=1;
- FP_WriteConfigValues(25, 26, 27);
- while (true) {
- time_t seconds = time(NULL);
- ADC_PHVal = SENSOR_ReadPHADC();
- 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();
- }
- }
- else {
- myled = 0;
- client.disconnect();
- mqttNetwork.disconnect();
- MQTT_AttemptConnect(&client, &mqttNetwork, network); // 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;
- }
- }
-}
-
+// For receiving message handles, search "Message Handles" in SimpleMQTT.h
+
+/***************************************************************
+ * Includes
+ ***************************************************************/
+#include "mbed.h"
+
+#include "ReadSensor.h"
+#include "SimpleMQTT.h"
+#include "flash_programming.h"
+
+/***************************************************************
+ * Definitions
+ ***************************************************************/
+
+
+/***************************************************************
+ * Variables
+ ***************************************************************/
+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;
+
+struct UploadValue DataStruct = {0.8, 0.9, 8.0, 89.9, 0, 1, 0, 50, 75, 25};
+
+
+/***************************************************************
+ * Structs/Classess
+ ***************************************************************/
+static Serial pc(SERIAL_TX, SERIAL_RX);
+
+DigitalOut myled(LED1);
+
+Timer UploadTimer;
+/***************************************************************
+ * Unity function definitions
+ ***************************************************************/
+
+/***************************************************************
+ * Unity function declarations
+ ***************************************************************/
+
+
+/***************************************************************
+ * Main
+ ***************************************************************/
+int main() {
+ pc.baud(115200);
+ UploadTimer.start();
+ lastRead = 0;
+// set_time(1515453021);
+ pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");
+ pc.printf("\r\nconnecting to AP\r\n");
+
+ NetworkInterface* network = easy_connect(true);
+ if (!network) {
+ printf ("Error easy_connect\n\r");
+ }
+ MQTTNetwork mqttNetwork(network);
+ MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
+ 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
+ }
+
+ 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();
+ }
+ }
+ 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;
+ }
+ }
+}
+
--- a/Flash/flash_programming.cpp Mon Jan 08 22:09:56 2018 +0700
+++ b/Flash/flash_programming.cpp Mon Jan 08 18:07:56 2018 +0000
@@ -1,167 +1,167 @@
-#include "flash_programming.h"
-#include "stm32l4xx_hal_flash.h"
-
-uint32_t PageError = 0;
-
-FLASH_EraseInitTypeDef EraseInitStruct;
-
-uint32_t FP_GetPage(uint32_t Addr) {
- uint32_t page = 0;
-
- if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
- {
- /* Bank 1 */
- page = (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;
- }
- else
- {
- /* Bank 2 */
- page = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
- }
-
- return page;
-}
-
-int FP_ClearFlags() {
- __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGAERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_PGSERR);
- if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
- (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
- (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))) {
- printf("Clear flag error\r\n");
- return FAILED;
- }
- return PASSED;
-}
-
-uint32_t FP_ReadValue(uint32_t Addr) {
- uint32_t ReturnValue = *(__IO uint32_t*)Addr;
- return ReturnValue;
-}
-
-int FP_WriteMode(uint32_t WriteModeValue) {
- uint8_t CurrentPage = FP_GetPage(MODE_BASE_ADDRESS);
- uint32_t CurrentAddress = (MODE_BASE_ADDRESS + STEP_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(CurrentAddress) == WriteModeValue) {
- printf("Value does not 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());
- }
- 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;
- }
- HAL_FLASH_Lock();
- return PASSED;
-}
-
-int FP_WriteConfigValues(uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod) {
- uint8_t CurrentPage = FP_GetPage(CONF_BASE_ADDRESS);
- uint32_t CurrentAddress = MIN_OXI_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(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");
- 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)) {
- switch (CurrentAddress) {
- case (MIN_OXI_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, MinOxi) == HAL_OK) {
- printf("Write MinOxi OK\r\n");
- }
- else {
- printf("Write MinOxi failed, error num %d\r\n", HAL_FLASH_GetError());
- }
- break;
- case (MAX_OXI_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, MaxOxi) == HAL_OK) {
- printf("Write MaxOxi OK\r\n");
- }
- else {
- 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) {
- printf("Write UploadPeriod OK\r\n");
- }
- else {
- printf("Write UploadPeriod failed, error num %d\r\n", HAL_FLASH_GetError());
- }
- break;
- default: break;
- }
- CurrentAddress = CurrentAddress + STEP_ADDRESS;
- }
-
- CurrentAddress = MIN_OXI_ADDRESS;
- while (CurrentAddress < (UPLOAD_PERIOD_ARRESS + STEP_ADDRESS)) {
- switch (CurrentAddress) {
- case (MIN_OXI_ADDRESS): if (FP_ReadValue(CurrentAddress) == MinOxi) {
- printf("Read back MinOxi: %d\r\n", FP_ReadValue(CurrentAddress));
- }
- else {
- printf("Write MinOxi failed, wrong read back value\r\n");
- HAL_FLASH_Lock();
- return FAILED;
- }
- break;
- case (MAX_OXI_ADDRESS): if (FP_ReadValue(CurrentAddress) == MaxOxi) {
- printf("Read back MaxOxi: %d\r\n", FP_ReadValue(CurrentAddress));
- }
- else {
- printf("Write MaxOxi failed, wrong read back value\r\n");
- HAL_FLASH_Lock();
- return FAILED;
- }
- break;
- case (UPLOAD_PERIOD_ARRESS): if (FP_ReadValue(CurrentAddress) == UploadPeriod) {
- printf("Read back UploadPeriod: %d\r\n", FP_ReadValue(CurrentAddress));
- }
- else {
- printf("Write UploadPeriod failed, wrong read back value\r\n");
- HAL_FLASH_Lock();
- return FAILED;
- }
- break;
- default: break;
- }
- CurrentAddress = CurrentAddress + STEP_ADDRESS;
- }
- HAL_FLASH_Lock();
- return PASSED;
+#include "flash_programming.h"
+#include "stm32l4xx_hal_flash.h"
+
+uint32_t PageError = 0;
+
+FLASH_EraseInitTypeDef EraseInitStruct;
+
+uint32_t FP_GetPage(uint32_t Addr) {
+ uint32_t page = 0;
+
+ if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
+ {
+ /* Bank 1 */
+ page = (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;
+ }
+ else
+ {
+ /* Bank 2 */
+ page = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
+ }
+
+ return page;
+}
+
+int FP_ClearFlags() {
+ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGAERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_PGSERR);
+ if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
+ (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
+ (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))) {
+ printf("Clear flag error\r\n");
+ return FAILED;
+ }
+ return PASSED;
+}
+
+uint32_t FP_ReadValue(uint32_t Addr) {
+ uint32_t ReturnValue = *(__IO uint32_t*)Addr;
+ return ReturnValue;
+}
+
+int FP_WriteMode(uint32_t WriteModeValue) {
+ uint8_t CurrentPage = FP_GetPage(MODE_BASE_ADDRESS);
+ uint32_t CurrentAddress = (MODE_BASE_ADDRESS + STEP_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(CurrentAddress) == WriteModeValue) {
+ printf("Value does not 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());
+ }
+ 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;
+ }
+ HAL_FLASH_Lock();
+ return PASSED;
+}
+
+int FP_WriteConfigValues(uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod) {
+ uint8_t CurrentPage = FP_GetPage(CONF_BASE_ADDRESS);
+ uint32_t CurrentAddress = MIN_OXI_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(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");
+ 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)) {
+ switch (CurrentAddress) {
+ case (MIN_OXI_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, MinOxi) == HAL_OK) {
+ printf("Write MinOxi OK\r\n");
+ }
+ else {
+ printf("Write MinOxi failed, error num %d\r\n", HAL_FLASH_GetError());
+ }
+ break;
+ case (MAX_OXI_ADDRESS): if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CurrentAddress, MaxOxi) == HAL_OK) {
+ printf("Write MaxOxi OK\r\n");
+ }
+ else {
+ 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) {
+ printf("Write UploadPeriod OK\r\n");
+ }
+ else {
+ printf("Write UploadPeriod failed, error num %d\r\n", HAL_FLASH_GetError());
+ }
+ break;
+ default: break;
+ }
+ CurrentAddress = CurrentAddress + STEP_ADDRESS;
+ }
+
+ CurrentAddress = MIN_OXI_ADDRESS;
+ while (CurrentAddress < (UPLOAD_PERIOD_ARRESS + STEP_ADDRESS)) {
+ switch (CurrentAddress) {
+ case (MIN_OXI_ADDRESS): if (FP_ReadValue(CurrentAddress) == MinOxi) {
+ printf("Read back MinOxi: %d\r\n", FP_ReadValue(CurrentAddress));
+ }
+ else {
+ printf("Write MinOxi failed, wrong read back value\r\n");
+ HAL_FLASH_Lock();
+ return FAILED;
+ }
+ break;
+ case (MAX_OXI_ADDRESS): if (FP_ReadValue(CurrentAddress) == MaxOxi) {
+ printf("Read back MaxOxi: %d\r\n", FP_ReadValue(CurrentAddress));
+ }
+ else {
+ printf("Write MaxOxi failed, wrong read back value\r\n");
+ HAL_FLASH_Lock();
+ return FAILED;
+ }
+ break;
+ case (UPLOAD_PERIOD_ARRESS): if (FP_ReadValue(CurrentAddress) == UploadPeriod) {
+ printf("Read back UploadPeriod: %d\r\n", FP_ReadValue(CurrentAddress));
+ }
+ else {
+ printf("Write UploadPeriod failed, wrong read back value\r\n");
+ HAL_FLASH_Lock();
+ return FAILED;
+ }
+ break;
+ default: break;
+ }
+ CurrentAddress = CurrentAddress + STEP_ADDRESS;
+ }
+ HAL_FLASH_Lock();
+ return PASSED;
}
\ No newline at end of file
--- a/Flash/flash_programming.h Mon Jan 08 22:09:56 2018 +0700
+++ b/Flash/flash_programming.h Mon Jan 08 18:07:56 2018 +0000
@@ -1,186 +1,186 @@
-#ifndef __FLASH_PROGRAMMING_H__
-#define __FLASH_PROGRAMMING_H__
-
-#include "stm32l4xx_hal.h"
-
-#define ADDR_FLASH_PAGE_0 ((uint32_t)0x08000000) /* Base @ of Page 0, 2 Kbytes */
-#define ADDR_FLASH_PAGE_1 ((uint32_t)0x08000800) /* Base @ of Page 1, 2 Kbytes */
-#define ADDR_FLASH_PAGE_2 ((uint32_t)0x08001000) /* Base @ of Page 2, 2 Kbytes */
-#define ADDR_FLASH_PAGE_3 ((uint32_t)0x08001800) /* Base @ of Page 3, 2 Kbytes */
-#define ADDR_FLASH_PAGE_4 ((uint32_t)0x08002000) /* Base @ of Page 4, 2 Kbytes */
-#define ADDR_FLASH_PAGE_5 ((uint32_t)0x08002800) /* Base @ of Page 5, 2 Kbytes */
-#define ADDR_FLASH_PAGE_6 ((uint32_t)0x08003000) /* Base @ of Page 6, 2 Kbytes */
-#define ADDR_FLASH_PAGE_7 ((uint32_t)0x08003800) /* Base @ of Page 7, 2 Kbytes */
-#define ADDR_FLASH_PAGE_8 ((uint32_t)0x08004000) /* Base @ of Page 8, 2 Kbytes */
-#define ADDR_FLASH_PAGE_9 ((uint32_t)0x08004800) /* Base @ of Page 9, 2 Kbytes */
-#define ADDR_FLASH_PAGE_10 ((uint32_t)0x08005000) /* Base @ of Page 10, 2 Kbytes */
-#define ADDR_FLASH_PAGE_11 ((uint32_t)0x08005800) /* Base @ of Page 11, 2 Kbytes */
-#define ADDR_FLASH_PAGE_12 ((uint32_t)0x08006000) /* Base @ of Page 12, 2 Kbytes */
-#define ADDR_FLASH_PAGE_13 ((uint32_t)0x08006800) /* Base @ of Page 13, 2 Kbytes */
-#define ADDR_FLASH_PAGE_14 ((uint32_t)0x08007000) /* Base @ of Page 14, 2 Kbytes */
-#define ADDR_FLASH_PAGE_15 ((uint32_t)0x08007800) /* Base @ of Page 15, 2 Kbytes */
-#define ADDR_FLASH_PAGE_16 ((uint32_t)0x08008000) /* Base @ of Page 16, 2 Kbytes */
-#define ADDR_FLASH_PAGE_17 ((uint32_t)0x08008800) /* Base @ of Page 17, 2 Kbytes */
-#define ADDR_FLASH_PAGE_18 ((uint32_t)0x08009000) /* Base @ of Page 18, 2 Kbytes */
-#define ADDR_FLASH_PAGE_19 ((uint32_t)0x08009800) /* Base @ of Page 19, 2 Kbytes */
-#define ADDR_FLASH_PAGE_20 ((uint32_t)0x0800A000) /* Base @ of Page 20, 2 Kbytes */
-#define ADDR_FLASH_PAGE_21 ((uint32_t)0x0800A800) /* Base @ of Page 21, 2 Kbytes */
-#define ADDR_FLASH_PAGE_22 ((uint32_t)0x0800B000) /* Base @ of Page 22, 2 Kbytes */
-#define ADDR_FLASH_PAGE_23 ((uint32_t)0x0800B800) /* Base @ of Page 23, 2 Kbytes */
-#define ADDR_FLASH_PAGE_24 ((uint32_t)0x0800C000) /* Base @ of Page 24, 2 Kbytes */
-#define ADDR_FLASH_PAGE_25 ((uint32_t)0x0800C800) /* Base @ of Page 25, 2 Kbytes */
-#define ADDR_FLASH_PAGE_26 ((uint32_t)0x0800D000) /* Base @ of Page 26, 2 Kbytes */
-#define ADDR_FLASH_PAGE_27 ((uint32_t)0x0800D800) /* Base @ of Page 27, 2 Kbytes */
-#define ADDR_FLASH_PAGE_28 ((uint32_t)0x0800E000) /* Base @ of Page 28, 2 Kbytes */
-#define ADDR_FLASH_PAGE_29 ((uint32_t)0x0800E800) /* Base @ of Page 29, 2 Kbytes */
-#define ADDR_FLASH_PAGE_30 ((uint32_t)0x0800F000) /* Base @ of Page 30, 2 Kbytes */
-#define ADDR_FLASH_PAGE_31 ((uint32_t)0x0800F800) /* Base @ of Page 31, 2 Kbytes */
-#define ADDR_FLASH_PAGE_32 ((uint32_t)0x08010000) /* Base @ of Page 32, 2 Kbytes */
-#define ADDR_FLASH_PAGE_33 ((uint32_t)0x08010800) /* Base @ of Page 33, 2 Kbytes */
-#define ADDR_FLASH_PAGE_34 ((uint32_t)0x08011000) /* Base @ of Page 34, 2 Kbytes */
-#define ADDR_FLASH_PAGE_35 ((uint32_t)0x08011800) /* Base @ of Page 35, 2 Kbytes */
-#define ADDR_FLASH_PAGE_36 ((uint32_t)0x08012000) /* Base @ of Page 36, 2 Kbytes */
-#define ADDR_FLASH_PAGE_37 ((uint32_t)0x08012800) /* Base @ of Page 37, 2 Kbytes */
-#define ADDR_FLASH_PAGE_38 ((uint32_t)0x08013000) /* Base @ of Page 38, 2 Kbytes */
-#define ADDR_FLASH_PAGE_39 ((uint32_t)0x08013800) /* Base @ of Page 39, 2 Kbytes */
-#define ADDR_FLASH_PAGE_40 ((uint32_t)0x08014000) /* Base @ of Page 40, 2 Kbytes */
-#define ADDR_FLASH_PAGE_41 ((uint32_t)0x08014800) /* Base @ of Page 41, 2 Kbytes */
-#define ADDR_FLASH_PAGE_42 ((uint32_t)0x08015000) /* Base @ of Page 42, 2 Kbytes */
-#define ADDR_FLASH_PAGE_43 ((uint32_t)0x08015800) /* Base @ of Page 43, 2 Kbytes */
-#define ADDR_FLASH_PAGE_44 ((uint32_t)0x08016000) /* Base @ of Page 44, 2 Kbytes */
-#define ADDR_FLASH_PAGE_45 ((uint32_t)0x08016800) /* Base @ of Page 45, 2 Kbytes */
-#define ADDR_FLASH_PAGE_46 ((uint32_t)0x08017000) /* Base @ of Page 46, 2 Kbytes */
-#define ADDR_FLASH_PAGE_47 ((uint32_t)0x08017800) /* Base @ of Page 47, 2 Kbytes */
-#define ADDR_FLASH_PAGE_48 ((uint32_t)0x08018000) /* Base @ of Page 48, 2 Kbytes */
-#define ADDR_FLASH_PAGE_49 ((uint32_t)0x08018800) /* Base @ of Page 49, 2 Kbytes */
-#define ADDR_FLASH_PAGE_50 ((uint32_t)0x08019000) /* Base @ of Page 50, 2 Kbytes */
-#define ADDR_FLASH_PAGE_51 ((uint32_t)0x08019800) /* Base @ of Page 51, 2 Kbytes */
-#define ADDR_FLASH_PAGE_52 ((uint32_t)0x0801A000) /* Base @ of Page 52, 2 Kbytes */
-#define ADDR_FLASH_PAGE_53 ((uint32_t)0x0801A800) /* Base @ of Page 53, 2 Kbytes */
-#define ADDR_FLASH_PAGE_54 ((uint32_t)0x0801B000) /* Base @ of Page 54, 2 Kbytes */
-#define ADDR_FLASH_PAGE_55 ((uint32_t)0x0801B800) /* Base @ of Page 55, 2 Kbytes */
-#define ADDR_FLASH_PAGE_56 ((uint32_t)0x0801C000) /* Base @ of Page 56, 2 Kbytes */
-#define ADDR_FLASH_PAGE_57 ((uint32_t)0x0801C800) /* Base @ of Page 57, 2 Kbytes */
-#define ADDR_FLASH_PAGE_58 ((uint32_t)0x0801D000) /* Base @ of Page 58, 2 Kbytes */
-#define ADDR_FLASH_PAGE_59 ((uint32_t)0x0801D800) /* Base @ of Page 59, 2 Kbytes */
-#define ADDR_FLASH_PAGE_60 ((uint32_t)0x0801E000) /* Base @ of Page 60, 2 Kbytes */
-#define ADDR_FLASH_PAGE_61 ((uint32_t)0x0801E800) /* Base @ of Page 61, 2 Kbytes */
-#define ADDR_FLASH_PAGE_62 ((uint32_t)0x0801F000) /* Base @ of Page 62, 2 Kbytes */
-#define ADDR_FLASH_PAGE_63 ((uint32_t)0x0801F800) /* Base @ of Page 63, 2 Kbytes */
-#define ADDR_FLASH_PAGE_64 ((uint32_t)0x08020000) /* Base @ of Page 64, 2 Kbytes */
-#define ADDR_FLASH_PAGE_65 ((uint32_t)0x08020800) /* Base @ of Page 65, 2 Kbytes */
-#define ADDR_FLASH_PAGE_66 ((uint32_t)0x08021000) /* Base @ of Page 66, 2 Kbytes */
-#define ADDR_FLASH_PAGE_67 ((uint32_t)0x08021800) /* Base @ of Page 67, 2 Kbytes */
-#define ADDR_FLASH_PAGE_68 ((uint32_t)0x08022000) /* Base @ of Page 68, 2 Kbytes */
-#define ADDR_FLASH_PAGE_69 ((uint32_t)0x08022800) /* Base @ of Page 69, 2 Kbytes */
-#define ADDR_FLASH_PAGE_70 ((uint32_t)0x08023000) /* Base @ of Page 70, 2 Kbytes */
-#define ADDR_FLASH_PAGE_71 ((uint32_t)0x08023800) /* Base @ of Page 71, 2 Kbytes */
-#define ADDR_FLASH_PAGE_72 ((uint32_t)0x08024000) /* Base @ of Page 72, 2 Kbytes */
-#define ADDR_FLASH_PAGE_73 ((uint32_t)0x08024800) /* Base @ of Page 73, 2 Kbytes */
-#define ADDR_FLASH_PAGE_74 ((uint32_t)0x08025000) /* Base @ of Page 74, 2 Kbytes */
-#define ADDR_FLASH_PAGE_75 ((uint32_t)0x08025800) /* Base @ of Page 75, 2 Kbytes */
-#define ADDR_FLASH_PAGE_76 ((uint32_t)0x08026000) /* Base @ of Page 76, 2 Kbytes */
-#define ADDR_FLASH_PAGE_77 ((uint32_t)0x08026800) /* Base @ of Page 77, 2 Kbytes */
-#define ADDR_FLASH_PAGE_78 ((uint32_t)0x08027000) /* Base @ of Page 78, 2 Kbytes */
-#define ADDR_FLASH_PAGE_79 ((uint32_t)0x08027800) /* Base @ of Page 79, 2 Kbytes */
-#define ADDR_FLASH_PAGE_80 ((uint32_t)0x08028000) /* Base @ of Page 80, 2 Kbytes */
-#define ADDR_FLASH_PAGE_81 ((uint32_t)0x08028800) /* Base @ of Page 81, 2 Kbytes */
-#define ADDR_FLASH_PAGE_82 ((uint32_t)0x08029000) /* Base @ of Page 82, 2 Kbytes */
-#define ADDR_FLASH_PAGE_83 ((uint32_t)0x08029800) /* Base @ of Page 83, 2 Kbytes */
-#define ADDR_FLASH_PAGE_84 ((uint32_t)0x0802A000) /* Base @ of Page 84, 2 Kbytes */
-#define ADDR_FLASH_PAGE_85 ((uint32_t)0x0802A800) /* Base @ of Page 85, 2 Kbytes */
-#define ADDR_FLASH_PAGE_86 ((uint32_t)0x0802B000) /* Base @ of Page 86, 2 Kbytes */
-#define ADDR_FLASH_PAGE_87 ((uint32_t)0x0802B800) /* Base @ of Page 87, 2 Kbytes */
-#define ADDR_FLASH_PAGE_88 ((uint32_t)0x0802C000) /* Base @ of Page 88, 2 Kbytes */
-#define ADDR_FLASH_PAGE_89 ((uint32_t)0x0802C800) /* Base @ of Page 89, 2 Kbytes */
-#define ADDR_FLASH_PAGE_90 ((uint32_t)0x0802D000) /* Base @ of Page 90, 2 Kbytes */
-#define ADDR_FLASH_PAGE_91 ((uint32_t)0x0802D800) /* Base @ of Page 91, 2 Kbytes */
-#define ADDR_FLASH_PAGE_92 ((uint32_t)0x0802E000) /* Base @ of Page 92, 2 Kbytes */
-#define ADDR_FLASH_PAGE_93 ((uint32_t)0x0802E800) /* Base @ of Page 93, 2 Kbytes */
-#define ADDR_FLASH_PAGE_94 ((uint32_t)0x0802F000) /* Base @ of Page 94, 2 Kbytes */
-#define ADDR_FLASH_PAGE_95 ((uint32_t)0x0802F800) /* Base @ of Page 95, 2 Kbytes */
-#define ADDR_FLASH_PAGE_96 ((uint32_t)0x08030000) /* Base @ of Page 96, 2 Kbytes */
-#define ADDR_FLASH_PAGE_97 ((uint32_t)0x08030800) /* Base @ of Page 97, 2 Kbytes */
-#define ADDR_FLASH_PAGE_98 ((uint32_t)0x08031000) /* Base @ of Page 98, 2 Kbytes */
-#define ADDR_FLASH_PAGE_99 ((uint32_t)0x08031800) /* Base @ of Page 99, 2 Kbytes */
-#define ADDR_FLASH_PAGE_100 ((uint32_t)0x08032000) /* Base @ of Page 100, 2 Kbytes */
-#define ADDR_FLASH_PAGE_101 ((uint32_t)0x08032800) /* Base @ of Page 101, 2 Kbytes */
-#define ADDR_FLASH_PAGE_102 ((uint32_t)0x08033000) /* Base @ of Page 102, 2 Kbytes */
-#define ADDR_FLASH_PAGE_103 ((uint32_t)0x08033800) /* Base @ of Page 103, 2 Kbytes */
-#define ADDR_FLASH_PAGE_104 ((uint32_t)0x08034000) /* Base @ of Page 104, 2 Kbytes */
-#define ADDR_FLASH_PAGE_105 ((uint32_t)0x08034800) /* Base @ of Page 105, 2 Kbytes */
-#define ADDR_FLASH_PAGE_106 ((uint32_t)0x08035000) /* Base @ of Page 106, 2 Kbytes */
-#define ADDR_FLASH_PAGE_107 ((uint32_t)0x08035800) /* Base @ of Page 107, 2 Kbytes */
-#define ADDR_FLASH_PAGE_108 ((uint32_t)0x08036000) /* Base @ of Page 108, 2 Kbytes */
-#define ADDR_FLASH_PAGE_109 ((uint32_t)0x08036800) /* Base @ of Page 109, 2 Kbytes */
-#define ADDR_FLASH_PAGE_110 ((uint32_t)0x08037000) /* Base @ of Page 110, 2 Kbytes */
-#define ADDR_FLASH_PAGE_111 ((uint32_t)0x08037800) /* Base @ of Page 111, 2 Kbytes */
-#define ADDR_FLASH_PAGE_112 ((uint32_t)0x08038000) /* Base @ of Page 112, 2 Kbytes */
-#define ADDR_FLASH_PAGE_113 ((uint32_t)0x08038800) /* Base @ of Page 113, 2 Kbytes */
-#define ADDR_FLASH_PAGE_114 ((uint32_t)0x08039000) /* Base @ of Page 114, 2 Kbytes */
-#define ADDR_FLASH_PAGE_115 ((uint32_t)0x08039800) /* Base @ of Page 115, 2 Kbytes */
-#define ADDR_FLASH_PAGE_116 ((uint32_t)0x0803A000) /* Base @ of Page 116, 2 Kbytes */
-#define ADDR_FLASH_PAGE_117 ((uint32_t)0x0803A800) /* Base @ of Page 117, 2 Kbytes */
-#define ADDR_FLASH_PAGE_118 ((uint32_t)0x0803B000) /* Base @ of Page 118, 2 Kbytes */
-#define ADDR_FLASH_PAGE_119 ((uint32_t)0x0803B800) /* Base @ of Page 119, 2 Kbytes */
-#define ADDR_FLASH_PAGE_120 ((uint32_t)0x0803C000) /* Base @ of Page 120, 2 Kbytes */
-#define ADDR_FLASH_PAGE_121 ((uint32_t)0x0803C800) /* Base @ of Page 121, 2 Kbytes */
-#define ADDR_FLASH_PAGE_122 ((uint32_t)0x0803D000) /* Base @ of Page 122, 2 Kbytes */
-#define ADDR_FLASH_PAGE_123 ((uint32_t)0x0803D800) /* Base @ of Page 123, 2 Kbytes */
-#define ADDR_FLASH_PAGE_124 ((uint32_t)0x0803E000) /* Base @ of Page 124, 2 Kbytes */
-#define ADDR_FLASH_PAGE_125 ((uint32_t)0x0803E800) /* Base @ of Page 125, 2 Kbytes */
-#define ADDR_FLASH_PAGE_126 ((uint32_t)0x0803F000) /* Base @ of Page 126, 2 Kbytes */
-#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 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)
-
-typedef enum {
- FAILED = 1,
- PASSED = 0
-} FlashReturnStatus;
-
-
-/**
- * @brief Gets the page of a given address
- * @param Addr: Address of the FLASH Memory
- * @retval The page of a given address
- */
-uint32_t FP_GetPage(uint32_t Addr);
-
-/**
- * @brief Read the value stored in a specific address
- * @param Addr: Address of the FLASH Memory
- * @retval The result read from the memory
- */
-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
- * @retval FLASH result
- */
-int FP_WriteMode(uint32_t WriteModeValue);
-
-/**
- * @brief Write the time to activate an event into the flash memory
- * @param WriteTimeValue: Current working mode of the device
- * @retval FLASH result
- */
-int FP_WriteTime(uint32_t WriteTimeValue);
-
-/**
- * @brief Write the value to control the switches into the flash memory
- * @param WriteTimeValue: Current working mode of the device
- * @retval FLASH result
- */
-int FP_WriteConfigValues(uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod);
-
+#ifndef __FLASH_PROGRAMMING_H__
+#define __FLASH_PROGRAMMING_H__
+
+#include "stm32l4xx_hal.h"
+
+#define ADDR_FLASH_PAGE_0 ((uint32_t)0x08000000) /* Base @ of Page 0, 2 Kbytes */
+#define ADDR_FLASH_PAGE_1 ((uint32_t)0x08000800) /* Base @ of Page 1, 2 Kbytes */
+#define ADDR_FLASH_PAGE_2 ((uint32_t)0x08001000) /* Base @ of Page 2, 2 Kbytes */
+#define ADDR_FLASH_PAGE_3 ((uint32_t)0x08001800) /* Base @ of Page 3, 2 Kbytes */
+#define ADDR_FLASH_PAGE_4 ((uint32_t)0x08002000) /* Base @ of Page 4, 2 Kbytes */
+#define ADDR_FLASH_PAGE_5 ((uint32_t)0x08002800) /* Base @ of Page 5, 2 Kbytes */
+#define ADDR_FLASH_PAGE_6 ((uint32_t)0x08003000) /* Base @ of Page 6, 2 Kbytes */
+#define ADDR_FLASH_PAGE_7 ((uint32_t)0x08003800) /* Base @ of Page 7, 2 Kbytes */
+#define ADDR_FLASH_PAGE_8 ((uint32_t)0x08004000) /* Base @ of Page 8, 2 Kbytes */
+#define ADDR_FLASH_PAGE_9 ((uint32_t)0x08004800) /* Base @ of Page 9, 2 Kbytes */
+#define ADDR_FLASH_PAGE_10 ((uint32_t)0x08005000) /* Base @ of Page 10, 2 Kbytes */
+#define ADDR_FLASH_PAGE_11 ((uint32_t)0x08005800) /* Base @ of Page 11, 2 Kbytes */
+#define ADDR_FLASH_PAGE_12 ((uint32_t)0x08006000) /* Base @ of Page 12, 2 Kbytes */
+#define ADDR_FLASH_PAGE_13 ((uint32_t)0x08006800) /* Base @ of Page 13, 2 Kbytes */
+#define ADDR_FLASH_PAGE_14 ((uint32_t)0x08007000) /* Base @ of Page 14, 2 Kbytes */
+#define ADDR_FLASH_PAGE_15 ((uint32_t)0x08007800) /* Base @ of Page 15, 2 Kbytes */
+#define ADDR_FLASH_PAGE_16 ((uint32_t)0x08008000) /* Base @ of Page 16, 2 Kbytes */
+#define ADDR_FLASH_PAGE_17 ((uint32_t)0x08008800) /* Base @ of Page 17, 2 Kbytes */
+#define ADDR_FLASH_PAGE_18 ((uint32_t)0x08009000) /* Base @ of Page 18, 2 Kbytes */
+#define ADDR_FLASH_PAGE_19 ((uint32_t)0x08009800) /* Base @ of Page 19, 2 Kbytes */
+#define ADDR_FLASH_PAGE_20 ((uint32_t)0x0800A000) /* Base @ of Page 20, 2 Kbytes */
+#define ADDR_FLASH_PAGE_21 ((uint32_t)0x0800A800) /* Base @ of Page 21, 2 Kbytes */
+#define ADDR_FLASH_PAGE_22 ((uint32_t)0x0800B000) /* Base @ of Page 22, 2 Kbytes */
+#define ADDR_FLASH_PAGE_23 ((uint32_t)0x0800B800) /* Base @ of Page 23, 2 Kbytes */
+#define ADDR_FLASH_PAGE_24 ((uint32_t)0x0800C000) /* Base @ of Page 24, 2 Kbytes */
+#define ADDR_FLASH_PAGE_25 ((uint32_t)0x0800C800) /* Base @ of Page 25, 2 Kbytes */
+#define ADDR_FLASH_PAGE_26 ((uint32_t)0x0800D000) /* Base @ of Page 26, 2 Kbytes */
+#define ADDR_FLASH_PAGE_27 ((uint32_t)0x0800D800) /* Base @ of Page 27, 2 Kbytes */
+#define ADDR_FLASH_PAGE_28 ((uint32_t)0x0800E000) /* Base @ of Page 28, 2 Kbytes */
+#define ADDR_FLASH_PAGE_29 ((uint32_t)0x0800E800) /* Base @ of Page 29, 2 Kbytes */
+#define ADDR_FLASH_PAGE_30 ((uint32_t)0x0800F000) /* Base @ of Page 30, 2 Kbytes */
+#define ADDR_FLASH_PAGE_31 ((uint32_t)0x0800F800) /* Base @ of Page 31, 2 Kbytes */
+#define ADDR_FLASH_PAGE_32 ((uint32_t)0x08010000) /* Base @ of Page 32, 2 Kbytes */
+#define ADDR_FLASH_PAGE_33 ((uint32_t)0x08010800) /* Base @ of Page 33, 2 Kbytes */
+#define ADDR_FLASH_PAGE_34 ((uint32_t)0x08011000) /* Base @ of Page 34, 2 Kbytes */
+#define ADDR_FLASH_PAGE_35 ((uint32_t)0x08011800) /* Base @ of Page 35, 2 Kbytes */
+#define ADDR_FLASH_PAGE_36 ((uint32_t)0x08012000) /* Base @ of Page 36, 2 Kbytes */
+#define ADDR_FLASH_PAGE_37 ((uint32_t)0x08012800) /* Base @ of Page 37, 2 Kbytes */
+#define ADDR_FLASH_PAGE_38 ((uint32_t)0x08013000) /* Base @ of Page 38, 2 Kbytes */
+#define ADDR_FLASH_PAGE_39 ((uint32_t)0x08013800) /* Base @ of Page 39, 2 Kbytes */
+#define ADDR_FLASH_PAGE_40 ((uint32_t)0x08014000) /* Base @ of Page 40, 2 Kbytes */
+#define ADDR_FLASH_PAGE_41 ((uint32_t)0x08014800) /* Base @ of Page 41, 2 Kbytes */
+#define ADDR_FLASH_PAGE_42 ((uint32_t)0x08015000) /* Base @ of Page 42, 2 Kbytes */
+#define ADDR_FLASH_PAGE_43 ((uint32_t)0x08015800) /* Base @ of Page 43, 2 Kbytes */
+#define ADDR_FLASH_PAGE_44 ((uint32_t)0x08016000) /* Base @ of Page 44, 2 Kbytes */
+#define ADDR_FLASH_PAGE_45 ((uint32_t)0x08016800) /* Base @ of Page 45, 2 Kbytes */
+#define ADDR_FLASH_PAGE_46 ((uint32_t)0x08017000) /* Base @ of Page 46, 2 Kbytes */
+#define ADDR_FLASH_PAGE_47 ((uint32_t)0x08017800) /* Base @ of Page 47, 2 Kbytes */
+#define ADDR_FLASH_PAGE_48 ((uint32_t)0x08018000) /* Base @ of Page 48, 2 Kbytes */
+#define ADDR_FLASH_PAGE_49 ((uint32_t)0x08018800) /* Base @ of Page 49, 2 Kbytes */
+#define ADDR_FLASH_PAGE_50 ((uint32_t)0x08019000) /* Base @ of Page 50, 2 Kbytes */
+#define ADDR_FLASH_PAGE_51 ((uint32_t)0x08019800) /* Base @ of Page 51, 2 Kbytes */
+#define ADDR_FLASH_PAGE_52 ((uint32_t)0x0801A000) /* Base @ of Page 52, 2 Kbytes */
+#define ADDR_FLASH_PAGE_53 ((uint32_t)0x0801A800) /* Base @ of Page 53, 2 Kbytes */
+#define ADDR_FLASH_PAGE_54 ((uint32_t)0x0801B000) /* Base @ of Page 54, 2 Kbytes */
+#define ADDR_FLASH_PAGE_55 ((uint32_t)0x0801B800) /* Base @ of Page 55, 2 Kbytes */
+#define ADDR_FLASH_PAGE_56 ((uint32_t)0x0801C000) /* Base @ of Page 56, 2 Kbytes */
+#define ADDR_FLASH_PAGE_57 ((uint32_t)0x0801C800) /* Base @ of Page 57, 2 Kbytes */
+#define ADDR_FLASH_PAGE_58 ((uint32_t)0x0801D000) /* Base @ of Page 58, 2 Kbytes */
+#define ADDR_FLASH_PAGE_59 ((uint32_t)0x0801D800) /* Base @ of Page 59, 2 Kbytes */
+#define ADDR_FLASH_PAGE_60 ((uint32_t)0x0801E000) /* Base @ of Page 60, 2 Kbytes */
+#define ADDR_FLASH_PAGE_61 ((uint32_t)0x0801E800) /* Base @ of Page 61, 2 Kbytes */
+#define ADDR_FLASH_PAGE_62 ((uint32_t)0x0801F000) /* Base @ of Page 62, 2 Kbytes */
+#define ADDR_FLASH_PAGE_63 ((uint32_t)0x0801F800) /* Base @ of Page 63, 2 Kbytes */
+#define ADDR_FLASH_PAGE_64 ((uint32_t)0x08020000) /* Base @ of Page 64, 2 Kbytes */
+#define ADDR_FLASH_PAGE_65 ((uint32_t)0x08020800) /* Base @ of Page 65, 2 Kbytes */
+#define ADDR_FLASH_PAGE_66 ((uint32_t)0x08021000) /* Base @ of Page 66, 2 Kbytes */
+#define ADDR_FLASH_PAGE_67 ((uint32_t)0x08021800) /* Base @ of Page 67, 2 Kbytes */
+#define ADDR_FLASH_PAGE_68 ((uint32_t)0x08022000) /* Base @ of Page 68, 2 Kbytes */
+#define ADDR_FLASH_PAGE_69 ((uint32_t)0x08022800) /* Base @ of Page 69, 2 Kbytes */
+#define ADDR_FLASH_PAGE_70 ((uint32_t)0x08023000) /* Base @ of Page 70, 2 Kbytes */
+#define ADDR_FLASH_PAGE_71 ((uint32_t)0x08023800) /* Base @ of Page 71, 2 Kbytes */
+#define ADDR_FLASH_PAGE_72 ((uint32_t)0x08024000) /* Base @ of Page 72, 2 Kbytes */
+#define ADDR_FLASH_PAGE_73 ((uint32_t)0x08024800) /* Base @ of Page 73, 2 Kbytes */
+#define ADDR_FLASH_PAGE_74 ((uint32_t)0x08025000) /* Base @ of Page 74, 2 Kbytes */
+#define ADDR_FLASH_PAGE_75 ((uint32_t)0x08025800) /* Base @ of Page 75, 2 Kbytes */
+#define ADDR_FLASH_PAGE_76 ((uint32_t)0x08026000) /* Base @ of Page 76, 2 Kbytes */
+#define ADDR_FLASH_PAGE_77 ((uint32_t)0x08026800) /* Base @ of Page 77, 2 Kbytes */
+#define ADDR_FLASH_PAGE_78 ((uint32_t)0x08027000) /* Base @ of Page 78, 2 Kbytes */
+#define ADDR_FLASH_PAGE_79 ((uint32_t)0x08027800) /* Base @ of Page 79, 2 Kbytes */
+#define ADDR_FLASH_PAGE_80 ((uint32_t)0x08028000) /* Base @ of Page 80, 2 Kbytes */
+#define ADDR_FLASH_PAGE_81 ((uint32_t)0x08028800) /* Base @ of Page 81, 2 Kbytes */
+#define ADDR_FLASH_PAGE_82 ((uint32_t)0x08029000) /* Base @ of Page 82, 2 Kbytes */
+#define ADDR_FLASH_PAGE_83 ((uint32_t)0x08029800) /* Base @ of Page 83, 2 Kbytes */
+#define ADDR_FLASH_PAGE_84 ((uint32_t)0x0802A000) /* Base @ of Page 84, 2 Kbytes */
+#define ADDR_FLASH_PAGE_85 ((uint32_t)0x0802A800) /* Base @ of Page 85, 2 Kbytes */
+#define ADDR_FLASH_PAGE_86 ((uint32_t)0x0802B000) /* Base @ of Page 86, 2 Kbytes */
+#define ADDR_FLASH_PAGE_87 ((uint32_t)0x0802B800) /* Base @ of Page 87, 2 Kbytes */
+#define ADDR_FLASH_PAGE_88 ((uint32_t)0x0802C000) /* Base @ of Page 88, 2 Kbytes */
+#define ADDR_FLASH_PAGE_89 ((uint32_t)0x0802C800) /* Base @ of Page 89, 2 Kbytes */
+#define ADDR_FLASH_PAGE_90 ((uint32_t)0x0802D000) /* Base @ of Page 90, 2 Kbytes */
+#define ADDR_FLASH_PAGE_91 ((uint32_t)0x0802D800) /* Base @ of Page 91, 2 Kbytes */
+#define ADDR_FLASH_PAGE_92 ((uint32_t)0x0802E000) /* Base @ of Page 92, 2 Kbytes */
+#define ADDR_FLASH_PAGE_93 ((uint32_t)0x0802E800) /* Base @ of Page 93, 2 Kbytes */
+#define ADDR_FLASH_PAGE_94 ((uint32_t)0x0802F000) /* Base @ of Page 94, 2 Kbytes */
+#define ADDR_FLASH_PAGE_95 ((uint32_t)0x0802F800) /* Base @ of Page 95, 2 Kbytes */
+#define ADDR_FLASH_PAGE_96 ((uint32_t)0x08030000) /* Base @ of Page 96, 2 Kbytes */
+#define ADDR_FLASH_PAGE_97 ((uint32_t)0x08030800) /* Base @ of Page 97, 2 Kbytes */
+#define ADDR_FLASH_PAGE_98 ((uint32_t)0x08031000) /* Base @ of Page 98, 2 Kbytes */
+#define ADDR_FLASH_PAGE_99 ((uint32_t)0x08031800) /* Base @ of Page 99, 2 Kbytes */
+#define ADDR_FLASH_PAGE_100 ((uint32_t)0x08032000) /* Base @ of Page 100, 2 Kbytes */
+#define ADDR_FLASH_PAGE_101 ((uint32_t)0x08032800) /* Base @ of Page 101, 2 Kbytes */
+#define ADDR_FLASH_PAGE_102 ((uint32_t)0x08033000) /* Base @ of Page 102, 2 Kbytes */
+#define ADDR_FLASH_PAGE_103 ((uint32_t)0x08033800) /* Base @ of Page 103, 2 Kbytes */
+#define ADDR_FLASH_PAGE_104 ((uint32_t)0x08034000) /* Base @ of Page 104, 2 Kbytes */
+#define ADDR_FLASH_PAGE_105 ((uint32_t)0x08034800) /* Base @ of Page 105, 2 Kbytes */
+#define ADDR_FLASH_PAGE_106 ((uint32_t)0x08035000) /* Base @ of Page 106, 2 Kbytes */
+#define ADDR_FLASH_PAGE_107 ((uint32_t)0x08035800) /* Base @ of Page 107, 2 Kbytes */
+#define ADDR_FLASH_PAGE_108 ((uint32_t)0x08036000) /* Base @ of Page 108, 2 Kbytes */
+#define ADDR_FLASH_PAGE_109 ((uint32_t)0x08036800) /* Base @ of Page 109, 2 Kbytes */
+#define ADDR_FLASH_PAGE_110 ((uint32_t)0x08037000) /* Base @ of Page 110, 2 Kbytes */
+#define ADDR_FLASH_PAGE_111 ((uint32_t)0x08037800) /* Base @ of Page 111, 2 Kbytes */
+#define ADDR_FLASH_PAGE_112 ((uint32_t)0x08038000) /* Base @ of Page 112, 2 Kbytes */
+#define ADDR_FLASH_PAGE_113 ((uint32_t)0x08038800) /* Base @ of Page 113, 2 Kbytes */
+#define ADDR_FLASH_PAGE_114 ((uint32_t)0x08039000) /* Base @ of Page 114, 2 Kbytes */
+#define ADDR_FLASH_PAGE_115 ((uint32_t)0x08039800) /* Base @ of Page 115, 2 Kbytes */
+#define ADDR_FLASH_PAGE_116 ((uint32_t)0x0803A000) /* Base @ of Page 116, 2 Kbytes */
+#define ADDR_FLASH_PAGE_117 ((uint32_t)0x0803A800) /* Base @ of Page 117, 2 Kbytes */
+#define ADDR_FLASH_PAGE_118 ((uint32_t)0x0803B000) /* Base @ of Page 118, 2 Kbytes */
+#define ADDR_FLASH_PAGE_119 ((uint32_t)0x0803B800) /* Base @ of Page 119, 2 Kbytes */
+#define ADDR_FLASH_PAGE_120 ((uint32_t)0x0803C000) /* Base @ of Page 120, 2 Kbytes */
+#define ADDR_FLASH_PAGE_121 ((uint32_t)0x0803C800) /* Base @ of Page 121, 2 Kbytes */
+#define ADDR_FLASH_PAGE_122 ((uint32_t)0x0803D000) /* Base @ of Page 122, 2 Kbytes */
+#define ADDR_FLASH_PAGE_123 ((uint32_t)0x0803D800) /* Base @ of Page 123, 2 Kbytes */
+#define ADDR_FLASH_PAGE_124 ((uint32_t)0x0803E000) /* Base @ of Page 124, 2 Kbytes */
+#define ADDR_FLASH_PAGE_125 ((uint32_t)0x0803E800) /* Base @ of Page 125, 2 Kbytes */
+#define ADDR_FLASH_PAGE_126 ((uint32_t)0x0803F000) /* Base @ of Page 126, 2 Kbytes */
+#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 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)
+
+typedef enum {
+ FAILED = 1,
+ PASSED = 0
+} FlashReturnStatus;
+
+
+/**
+ * @brief Gets the page of a given address
+ * @param Addr: Address of the FLASH Memory
+ * @retval The page of a given address
+ */
+uint32_t FP_GetPage(uint32_t Addr);
+
+/**
+ * @brief Read the value stored in a specific address
+ * @param Addr: Address of the FLASH Memory
+ * @retval The result read from the memory
+ */
+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
+ * @retval FLASH result
+ */
+int FP_WriteMode(uint32_t WriteModeValue);
+
+/**
+ * @brief Write the time to activate an event into the flash memory
+ * @param WriteTimeValue: Current working mode of the device
+ * @retval FLASH result
+ */
+int FP_WriteTime(uint32_t WriteTimeValue);
+
+/**
+ * @brief Write the value to control the switches into the flash memory
+ * @param WriteTimeValue: Current working mode of the device
+ * @retval FLASH result
+ */
+int FP_WriteConfigValues(uint8_t MinOxi, uint8_t MaxOxi, uint32_t UploadPeriod);
+
#endif /* __FLASH_PROGRAMMING_H__ */
\ No newline at end of file
--- a/Sensor/ReadSensor.cpp Mon Jan 08 22:09:56 2018 +0700
+++ b/Sensor/ReadSensor.cpp Mon Jan 08 18:07:56 2018 +0000
@@ -1,33 +1,33 @@
-#include "mbed.h"
-#include "ReadSensor.h"
-
-const float saturationValueTab[41] = { //saturation dissolved oxygen concentrations at various temperatures
-14.46, 14.22, 13.82, 13.44, 13.09,
-12.74, 12.42, 12.11, 11.81, 11.53,
-11.26, 11.01, 10.77, 10.53, 10.30,
-10.08, 9.86, 9.66, 9.46, 9.27,
-9.08, 8.90, 8.73, 8.57, 8.41,
-8.25, 8.11, 7.96, 7.82, 7.69,
-7.56, 7.43, 7.30, 7.18, 7.07,
-6.95, 6.84, 6.73, 6.63, 6.53,
-6.41,
-};
-
-float saturationDoVoltage;
-float saturationDoTemperature;
-float averageVoltage;
-
-AnalogIn phSensor(SENSOR_1_PIN);
-AnalogIn DOSensor(SENSOR_2_PIN);
-
-void SENSOR_PHCalib() {
-
-}
-
-float SENSOR_ReadPHADC() {
- return phSensor.read();
-}
-
-float SENSOR_GetPHValue() {
-
+#include "mbed.h"
+#include "ReadSensor.h"
+
+const float saturationValueTab[41] = { //saturation dissolved oxygen concentrations at various temperatures
+14.46, 14.22, 13.82, 13.44, 13.09,
+12.74, 12.42, 12.11, 11.81, 11.53,
+11.26, 11.01, 10.77, 10.53, 10.30,
+10.08, 9.86, 9.66, 9.46, 9.27,
+9.08, 8.90, 8.73, 8.57, 8.41,
+8.25, 8.11, 7.96, 7.82, 7.69,
+7.56, 7.43, 7.30, 7.18, 7.07,
+6.95, 6.84, 6.73, 6.63, 6.53,
+6.41,
+};
+
+float saturationDoVoltage;
+float saturationDoTemperature;
+float averageVoltage;
+
+AnalogIn phSensor(SENSOR_1_PIN);
+AnalogIn DOSensor(SENSOR_2_PIN);
+
+void SENSOR_Calib() {
+
+}
+
+float SENSOR_ReadPHADC() {
+ return phSensor.read();
+}
+
+float SENSOR_GetPHValue() {
+
}
\ No newline at end of file
--- a/Sensor/ReadSensor.h Mon Jan 08 22:09:56 2018 +0700 +++ b/Sensor/ReadSensor.h Mon Jan 08 18:07:56 2018 +0000 @@ -1,30 +1,30 @@ -#ifndef __READSENSOR_H__ -#define __READSENSOR_H__ - -#define SENSOR_1_PIN (A0) -#define SENSOR_2_PIN (A1) -#define SENSOR_3_PIN (A3) -#define SENSOR_4_PIN (A4) - -#define SaturationDoVoltageAddress 12 //the address of the Saturation Oxygen voltage stored in the Flash -#define SaturationDoTemperatureAddress 16 //the address of the Saturation Oxygen temperature stored in the Flash - -#define VREF 3.3 -#define SCOUNT 30 - -/** brief Perform calibration for pH sensor - * retral None - */ -void SENSOR_PHCalib(); - -/** brief Read the analog value of pH sensor - * retral pH ADC value - */ -float SENSOR_ReadPHADC(); - -/** brief Convert the ADC value read from pH sensor into pH value - * retral pH value - */ -float SENSOR_GetPHValue(); - +#ifndef __READSENSOR_H__ +#define __READSENSOR_H__ + +#define SENSOR_1_PIN (A0) +#define SENSOR_2_PIN (A1) +#define SENSOR_3_PIN (A3) +#define SENSOR_4_PIN (A4) + +#define SaturationDoVoltageAddress 12 //the address of the Saturation Oxygen voltage stored in the Flash +#define SaturationDoTemperatureAddress 16 //the address of the Saturation Oxygen temperature stored in the Flash + +#define VREF 3.3 +#define SCOUNT 30 + +/** brief Perform calibration for pH sensor + * retral None + */ +void SENSOR_Calib(); + +/** brief Read the analog value of pH sensor + * retral pH ADC value + */ +float SENSOR_ReadPHADC(); + +/** brief Convert the ADC value read from pH sensor into pH value + * retral pH value + */ +float SENSOR_GetPHValue(); + #endif /* __READSENSOR_H__ */ \ No newline at end of file
--- a/Simple-MQTT/MQTTNetwork.h Mon Jan 08 22:09:56 2018 +0700
+++ b/Simple-MQTT/MQTTNetwork.h Mon Jan 08 18:07:56 2018 +0000
@@ -1,40 +1,40 @@
-#ifndef _MQTTNETWORK_H_
-#define _MQTTNETWORK_H_
-
-#include "NetworkInterface.h"
-
-class MQTTNetwork {
-public:
- MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
- socket = new TCPSocket();
- }
-
- ~MQTTNetwork() {
- delete socket;
- }
-
- int read(unsigned char* buffer, int len, int timeout) {
- socket->set_timeout(timeout);
- return socket->recv(buffer, len);
- }
-
- int write(unsigned char* buffer, int len, int timeout) {
- socket->set_timeout(timeout);
- return socket->send(buffer, len);
- }
-
- int connect(const char* hostname, int port) {
- socket->open(network);
- return socket->connect(hostname, port);
- }
-
- int disconnect() {
- return socket->close();
- }
-
-private:
- NetworkInterface* network;
- TCPSocket* socket;
-};
-
-#endif // _MQTTNETWORK_H_
+#ifndef _MQTTNETWORK_H_
+#define _MQTTNETWORK_H_
+
+#include "NetworkInterface.h"
+
+class MQTTNetwork {
+public:
+ MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
+ socket = new TCPSocket();
+ }
+
+ ~MQTTNetwork() {
+ delete socket;
+ }
+
+ int read(unsigned char* buffer, int len, int timeout) {
+ socket->set_timeout(timeout);
+ return socket->recv(buffer, len);
+ }
+
+ int write(unsigned char* buffer, int len, int timeout) {
+ socket->set_timeout(timeout);
+ return socket->send(buffer, len);
+ }
+
+ int connect(const char* hostname, int port) {
+ socket->open(network);
+ return socket->connect(hostname, port);
+ }
+
+ int disconnect() {
+ return socket->close();
+ }
+
+private:
+ NetworkInterface* network;
+ TCPSocket* socket;
+};
+
+#endif // _MQTTNETWORK_H_
--- a/Simple-MQTT/SimpleMQTT.h Mon Jan 08 22:09:56 2018 +0700
+++ b/Simple-MQTT/SimpleMQTT.h Mon Jan 08 18:07:56 2018 +0000
@@ -1,369 +1,443 @@
-#ifndef __SIMPLEMQTT_H__
-#define __SIMPLEMQTT_H__
-
-/***************************************************************
- * Includes
- ***************************************************************/
-#include "easy-connect.h"
-#include "MQTTClient.h"
-#include "NDefLib/NDefNfcTag.h"
-#include "NDefLib/RecordType/RecordURI.h"
-#include "MQTTNetwork.h"
-#include "MQTTmbed.h"
-
-#include "Json.h"
-#include "CommandExecution.h"
-/***************************************************************
- * Definitions
- ***************************************************************/
- // Configuration values needed to connect to IBM IoT Cloud
-#define ORG MQTT_ORG_ID // connect to ORG.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
-#define ID MQTT_DEVICE_ID // For a registered connection is your device id
-#define AUTH_TOKEN MQTT_DEVICE_PASSWORD // For a registered connection is a device auth-token
-#define DEFAULT_TYPE_NAME MQTT_DEVICE_TYPE // For a registered connection is device type
-#define AUTH_METHOD MQTT_USERNAME
-
-#define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
-#define IBM_IOT_PORT MQTT_PORT
-
-#define MQTT_MAX_PACKET_SIZE 400
-#define MQTT_MAX_PAYLOAD_SIZE 300
-
-/***************************************************************
- * Variables
- ***************************************************************/
-typedef enum {
- ADC_VALUE = 0,
- SENSOR_VALUE,
- RELAY_STATE,
- CONFIG_VALUE
-} UploadType;
-
-struct UploadValue {
- float ADC_PHVal;
- float ADC_DOVal;
-
- int RELAY_State_1;
- int RELAY_State_2;
-
- uint8_t CONFIG_Mode;
- uint8_t CONFIG_MinOxi;
- uint8_t CONFIG_MaxOxi;
- uint16_t CONFIG_UploadInterval;
-} UploadValue;
-
-char *projectName = "WaterMonitor";
-static char id[30] = ID; // mac without colons
-static char org[12] = ORG;
-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 netConnecting = false;
-static bool mqttConnecting = false;
-static bool netConnected = false;
-static bool connected = false;
-static int retryAttempt = 0;
-static int connectTimeout = 1000;
-uint16_t commandID = 0;
-static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
-
-/***************************************************************
- * Unity function definitions
- ***************************************************************/
-void MQTT_MessageHandles(uint8_t ControlSignal);
-
-/** brief Callback function when MQTT message arrives
- * param[in] msgMQTT
- * retral None
- */
-void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT);
-
-/** brief Subscribe to a MQTT topic and set the MQTT callback function
- * param[in] subscribeTopic Topic to be subscribed
- * param[in] client MQTT client
- * retral returnCode from MQTTClient.h
- */
-int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client);
-
-/** brief Connect to the internet then the MQTT network
- * param[in] client MQTT client
- * param[in] mqttNetwork MQTT network
- * param[in] network The internet network interface (ethernet, wifi...)
- * retral Internet connect result and returnCode from MQTTClient.h
- */
-int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
-
-/** brief Setup the number of attempt to re-connect to the internet
- * param[in] attemptNumber The number of attemp
- */
-int MQTT_GetConnTimeout(int attemptNumber);
-
-/** brief Try to reconnect to the internet and MQTT network
- * retral None
- */
-void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
-
-/** brief Publish ADC values to the server
- * param[in] client MQTT client
- * param[in] inputTime The time when the data is attempt to be sent
- * param[in] adcVal_0 The ADC value to be sent
- * retral returnCode from MQTTClient.h
- */
-int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0);
-
-/** brief Publish relay states to the server
- * param[in] client MQTT client
- * param[in] inputTime The time when the data is attempt to be sent
- * param[in] relay1 Relay 1 state
- * param[in] relay2 Relay 2 state
- * retral returnCode from MQTTClient.h
- */
-int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2);
-
-/** brief Publish relay states to the server
- * param[in] client MQTT client
- * param[in] inputTime The time when the data is attempt to be sent
- * param[in] mode current mode: automatic (0) or manual (1)
- * param[in] maxOxi Maximum Oxygen value
- * param[in] minOxi Minimum Oxygen value
-// * param[in] uploadInterval Interval between upload turns
- * retral returnCode from MQTTClient.h
- */
-int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t minOxi, uint8_t maxOxi);
-
-/** brief Upload all the data to the MQTT server
- * param[in] client MQTT client
- * param[in] inputTime The time when the data is attempt to be sent
- * param[in] uploadInterval The period between each upload moment
- * retral returnCode from MQTTClient.h
- */
-int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct);
-
-/********************************************************************************************************************************************************************************************/
-/***************************************************************
- * Unity function declarations
- ***************************************************************/
-void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT) {
- // Message Handles
- char msg[MQTT_MAX_PAYLOAD_SIZE];
- msg[0]='\0';
- strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
- printf ("--->>> MQTT_SubscribeCallback msg: %s\n\r", msg);
- //{"type":"3","deviceId":"string"}
- Json json(msg, msgMQTT.message.payloadlen);
- if (!json.isValidJson()) {
- printf("Invalid JSON: %s", msg);
- }
- else {
- if (json.type(0) != JSMN_OBJECT ) {
- printf("Invalid JSON. ROOT element is not Object: %s", msg);
- }
- else {
- int CommandType;
- int KeyIndex = json.findKeyIndexIn("type", 0);
- int KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
- int ret = json.tokenIntegerValue(KeyValueIndex, CommandType);
- printf("Command Type: %d, error %d\r\n", CommandType, ret);
-
- switch (CommandType) {
- case 3:
- break;
-
- case 4:
- break;
-
- default: break;
- }
- }
- }
-}
-
-int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) {
- return client->subscribe(subscribeTopic, MQTT::QOS1, MQTT_SubscribeCallback);
-}
-
-int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
- const char* iot_ibm = MQTT_BROKER_URL;
- char hostname[strlen(org) + strlen(iot_ibm) + 1];
-
- sprintf(hostname, "%s%s", org, iot_ibm);
- // Construct clientId - d:org:type:id
- char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
- sprintf(clientId, "d:%s:%s:%s", org, type, id);
- sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, DEFAULT_TYPE_NAME);
-
- // Network debug statements
- LOG("=====================================\n\r");
- LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
- LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
- LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
- LOG("Client ID: %s\n\r", clientId);
- LOG("Topic: %s\n\r",MQTT_EVENT_TOPIC);
- LOG("Subscription URL: %s\n\r", subscription_url);
- LOG("=====================================\n\r");
- netConnecting = true;
- int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
- if (rc != 0) {
- printf("rc from TCP connect is %d\r\n", rc);
- return rc;
- }
-
- printf ("--->TCP Connected\n\r");
- netConnected = true;
- netConnecting = false;
-
- // MQTT Connect
- mqttConnecting = true;
- MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
- data.MQTTVersion = 4;
- data.struct_version = 0;
- data.clientID.cstring = clientId;
- data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec
- data.username.cstring = AUTH_METHOD;
- data.password.cstring = auth_token;
- printf ("AutToken: %s\n\r", auth_token);
-
- if ((rc = client->connect(data)) != 0) {
- printf("rc from MQTT connect is %d\r\n", rc);
- connack_rc = rc;
- return rc;
- }
- connected = true;
- printf ("--->MQTT Connected\n\r");
- if ((rc = MQTT_Subscribe(MQTT_COMMAND_TOPIC, client)) == 0) {
- LOG ("--->>>MQTT subscribed to: %s\n\r", MQTT_COMMAND_TOPIC);
- } else {
- LOG ("--->>>ERROR MQTT subscribe : %s\n\r", MQTT_COMMAND_TOPIC);
- }
- mqttConnecting = false;
- connack_rc = rc;
- return rc;
-}
-
-
-int MQTT_GetConnTimeout(int attemptNumber) { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
- // after 20 attempts, retry every 10 minutes
- return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
-}
-
-
-void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
- connected = false;
-
- while (MQTT_Connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED) {
- if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
- printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
- return; // don't reattempt to connect if credentials are wrong
- }
- int timeout = MQTT_GetConnTimeout(++retryAttempt);
- WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
-
- // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
- // 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();
- else
- wait(timeout);
- }
-}
-
-int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0) {
- MQTT::Message message;
- const char* pubTopic = MQTT_EVENT_TOPIC;
-
- char buf[MQTT_MAX_PAYLOAD_SIZE];
- char timeBuf[50];
-
- if (!client->isConnected()) {
- printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
- }
-
- strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
-// sprintf(buf,
-// "{\"Project\":\"%s\",\"Time\":\"%s\",\"Type\":1,\"cmdID\":%d,\"ADC0\":%0.2f}",
-// projectName, timeBuf, commandID, adcVal_0);
- sprintf(buf, "{\"type\":1,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"adc0\":%0.2f}",
- timeBuf, commandID, adcVal_0);
- message.qos = MQTT::QOS0;
- message.retained = false;
- message.dup = false;
- message.payload = (void*)buf;
- message.payloadlen = strlen(buf);
-
- if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
- printf("message too long!\r\n");
-
- LOG("Publishing %s\n\r", buf);
- return client->publish(pubTopic, message);
-}
-
-int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2) {
- MQTT::Message message;
- const char* pubTopic = MQTT_EVENT_TOPIC;
- char buf[MQTT_MAX_PAYLOAD_SIZE];
- char timeBuf[50];
-
- if (!client->isConnected()) {
- printf ("---> MQTT DISCONNECTED\n\r");
- 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}",
- timeBuf, commandID, relay1, relay2);
- message.qos = MQTT::QOS0;
- message.retained = false;
- message.dup = false;
- message.payload = (void*)buf;
- message.payloadlen = strlen(buf);
-
- if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
- printf("message too long!\r\n");
-
- LOG("Publishing %s\n\r", buf);
- return client->publish(pubTopic, message);
-}
-
-int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t minOxi, uint8_t maxOxi) {
- MQTT::Message message;
- const char* pubTopic = MQTT_EVENT_TOPIC;
- char buf[MQTT_MAX_PAYLOAD_SIZE];
- char timeBuf[50];
-
- if (!client->isConnected()) {
- printf ("---> MQTT DISCONNECTED\n\r");
- 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,\"minOxygenVal\":%d,\"maxOxygenVal\":%d}",
- timeBuf, commandID, mode, minOxi, maxOxi);
- message.qos = MQTT::QOS0;
- message.retained = false;
- message.dup = false;
- message.payload = (void*)buf;
- message.payloadlen = strlen(buf);
-
- if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
- printf("message too long!\r\n");
-
- LOG("Publishing %s\n\r", buf);
- return client->publish(pubTopic, message);
-}
-
-int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct) {
- int retVal;
- switch (uploadType) {
- case (ADC_VALUE): retVal = MQTT_PublishADC(client, inputTime, uploadStruct.ADC_PHVal);
- break;
- case (SENSOR_VALUE): retVal = MQTT::SUCCESS;
- break;
- case (RELAY_STATE): retVal = MQTT_PublishRelayState(client, inputTime, uploadStruct.RELAY_State_1, uploadStruct.RELAY_State_2);
- break;
- case (CONFIG_VALUE): retVal = MQTT_PublishConfigValue(client, inputTime, uploadStruct.CONFIG_Mode, uploadStruct.CONFIG_MinOxi, uploadStruct.CONFIG_MaxOxi);
- break;
- default: break;
- }
- return retVal;
-}
-
-#endif /* __SIMPLEMQTT_H__ */
+#ifndef __SIMPLEMQTT_H__
+#define __SIMPLEMQTT_H__
+
+/***************************************************************
+ * Includes
+ ***************************************************************/
+#include "easy-connect.h"
+#include "MQTTClient.h"
+#include "NDefLib/NDefNfcTag.h"
+#include "NDefLib/RecordType/RecordURI.h"
+#include "MQTTNetwork.h"
+#include "MQTTmbed.h"
+
+#include "Json.h"
+#include "CommandExecution.h"
+#include "flash_programming.h"
+/***************************************************************
+ * Definitions
+ ***************************************************************/
+ // Configuration values needed to connect to IBM IoT Cloud
+#define ORG MQTT_ORG_ID // connect to ORG.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
+#define ID MQTT_DEVICE_ID // For a registered connection is your device id
+#define AUTH_TOKEN MQTT_DEVICE_PASSWORD // For a registered connection is a device auth-token
+#define DEFAULT_TYPE_NAME MQTT_DEVICE_TYPE // For a registered connection is device type
+#define AUTH_METHOD MQTT_USERNAME
+
+#define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
+#define IBM_IOT_PORT MQTT_PORT
+
+#define MQTT_MAX_PACKET_SIZE 400
+#define MQTT_MAX_PAYLOAD_SIZE 300
+
+/***************************************************************
+ * Variables
+ ***************************************************************/
+typedef enum {
+ ADC_VALUE = 0,
+ SENSOR_VALUE,
+ RELAY_STATE,
+ CONFIG_VALUE
+} UploadType;
+
+struct UploadValue {
+ float ADC_PHVal;
+ float ADC_DOVal;
+
+ float SENSOR_PHVal;
+ float SENSOR_D0Val;
+
+ int RELAY_State_1;
+ int RELAY_State_2;
+
+ uint8_t CONFIG_Mode;
+ uint8_t CONFIG_MinOxi;
+ uint8_t CONFIG_MaxOxi;
+ uint16_t CONFIG_UploadInterval;
+} UploadValue;
+
+char *projectName = "WaterMonitor";
+static char id[30] = ID; // mac without colons
+static char org[12] = ORG;
+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 netConnecting = false;
+static bool mqttConnecting = false;
+static bool netConnected = false;
+static bool connected = false;
+static int retryAttempt = 0;
+static int connectTimeout = 1000;
+uint16_t commandID = 0;
+static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
+
+extern struct UploadValue DataStruct;
+/***************************************************************
+ * Unity function definitions
+ ***************************************************************/
+void MQTT_MessageHandles(uint8_t ControlSignal);
+
+/** brief Callback function when MQTT message arrives
+ * param[in] msgMQTT
+ * retral None
+ */
+void MQTT_SubscribeCallback(MQTT::MessageData &msgMQTT);
+
+/** brief Subscribe to a MQTT topic and set the MQTT callback function
+ * param[in] subscribeTopic Topic to be subscribed
+ * param[in] client MQTT client
+ * retral returnCode from MQTTClient.h
+ */
+int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, struct UploadValue uploadStruct);
+
+/** brief Connect to the internet then the MQTT network
+ * param[in] client MQTT client
+ * param[in] mqttNetwork MQTT network
+ * param[in] network The internet network interface (ethernet, wifi...)
+ * retral Internet connect result and returnCode from MQTTClient.h
+ */
+int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct);
+
+/** brief Setup the number of attempt to re-connect to the internet
+ * param[in] attemptNumber The number of attemp
+ */
+int MQTT_GetConnTimeout(int attemptNumber);
+
+/** brief Try to reconnect to the internet and MQTT network
+ * retral None
+ */
+void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct);
+
+/** brief Publish ADC values to the server
+ * param[in] client MQTT client
+ * param[in] inputTime The time when the data is attempt to be sent
+ * param[in] adcVal_0 The ADC value to be sent
+ * retral returnCode from MQTTClient.h
+ */
+int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0);
+
+/** brief Publish Sensor values to the server
+ * param[in] client MQTT client
+ * param[in] inputTime The time when the data is attempt to be sent
+ * param[in] pHVal The pHVal value to be sent
+ * retral returnCode from MQTTClient.h
+ */
+int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float pHVal);
+
+/** brief Publish relay states to the server
+ * param[in] client MQTT client
+ * param[in] inputTime The time when the data is attempt to be sent
+ * param[in] relay1 Relay 1 state
+ * param[in] relay2 Relay 2 state
+ * retral returnCode from MQTTClient.h
+ */
+int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2);
+
+/** brief Publish relay states to the server
+ * param[in] client MQTT client
+ * param[in] inputTime The time when the data is attempt to be sent
+ * param[in] mode current mode: automatic (0) or manual (1)
+ * param[in] maxOxi Maximum Oxygen value
+ * param[in] minOxi Minimum Oxygen value
+// * param[in] uploadInterval Interval between upload turns
+ * retral returnCode from MQTTClient.h
+ */
+int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t minOxi, uint8_t maxOxi);
+
+/** brief Upload all the data to the MQTT server
+ * param[in] client MQTT client
+ * param[in] inputTime The time when the data is attempt to be sent
+ * param[in] uploadInterval The period between each upload moment
+ * retral returnCode from MQTTClient.h
+ */
+int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct);
+
+/********************************************************************************************************************************************************************************************/
+/***************************************************************
+ * Unity function declarations
+ ***************************************************************/
+void MQTT_SubscribeCallback(MQTT::MessageData &msgMQTT) {
+ // Message Handles
+ char msg[MQTT_MAX_PAYLOAD_SIZE];
+ msg[0]='\0';
+ strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
+ printf ("--->>> MQTT_SubscribeCallback msg: %s\n\r", msg);
+ //{"type":"3","deviceId":"string"}
+ Json json(msg, msgMQTT.message.payloadlen);
+ if (!json.isValidJson()) {
+ printf("Invalid JSON: %s", msg);
+ }
+ else {
+ if (json.type(0) != JSMN_OBJECT ) {
+ printf("Invalid JSON. ROOT element is not Object: %s", msg);
+ }
+ else {
+ int CommandType;
+ int KeyIndex = json.findKeyIndexIn("type", 0);
+ int KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ int ret = json.tokenIntegerValue(KeyValueIndex, CommandType);
+
+ int receiveCmdID;
+ printf("Command Type: %d, error %d\r\n", CommandType, ret);
+
+ switch (CommandType) {
+ case 3: int relayState0, relayState1;
+ 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);
+
+ DataStruct.RELAY_State_1 = relayState0;
+ DataStruct.RELAY_State_2 = relayState1;
+ CE_HandleRelays(relayState0, relayState1);
+ break;
+
+ case 4: int mode, minOxiVal, maxOxiVal;
+ KeyIndex = json.findKeyIndexIn("cmdID", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
+
+ KeyIndex = json.findKeyIndexIn("mode", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, mode);
+
+ KeyIndex = json.findKeyIndexIn("minOxygenVal", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, minOxiVal);
+
+ KeyIndex = json.findKeyIndexIn("maxOxygenVal", 0);
+ KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+ ret = json.tokenIntegerValue(KeyValueIndex, maxOxiVal);
+
+ DataStruct.CONFIG_Mode = mode;
+ DataStruct.CONFIG_MinOxi = minOxiVal;
+ DataStruct.CONFIG_MaxOxi = maxOxiVal;
+ //DataStruct.CONFIG_UploadInterval = relayState0;
+ break;
+
+ default: break;
+ }
+ }
+ }
+}
+
+int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, struct UploadValue uploadStruct) {
+ return client->subscribe(subscribeTopic, MQTT::QOS1, MQTT_SubscribeCallback);
+}
+
+int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct) {
+ const char* iot_ibm = MQTT_BROKER_URL;
+ char hostname[strlen(org) + strlen(iot_ibm) + 1];
+
+ sprintf(hostname, "%s%s", org, iot_ibm);
+ // Construct clientId - d:org:type:id
+ char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
+ sprintf(clientId, "d:%s:%s:%s", org, type, id);
+ sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, DEFAULT_TYPE_NAME);
+
+ // Network debug statements
+ LOG("=====================================\n\r");
+ LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
+ LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
+ LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
+ LOG("Client ID: %s\n\r", clientId);
+ LOG("Topic: %s\n\r",MQTT_EVENT_TOPIC);
+ LOG("Subscription URL: %s\n\r", subscription_url);
+ LOG("=====================================\n\r");
+ netConnecting = true;
+ int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
+ if (rc != 0) {
+ printf("rc from TCP connect is %d\r\n", rc);
+ return rc;
+ }
+
+ printf ("--->TCP Connected\n\r");
+ netConnected = true;
+ netConnecting = false;
+
+ // MQTT Connect
+ mqttConnecting = true;
+ MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+ data.MQTTVersion = 4;
+ data.struct_version = 0;
+ data.clientID.cstring = clientId;
+ data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec
+ data.username.cstring = AUTH_METHOD;
+ data.password.cstring = auth_token;
+ printf ("AutToken: %s\n\r", auth_token);
+
+ if ((rc = client->connect(data)) != 0) {
+ printf("rc from MQTT connect is %d\r\n", rc);
+ connack_rc = rc;
+ return rc;
+ }
+ connected = true;
+ printf ("--->MQTT Connected\n\r");
+ if ((rc = MQTT_Subscribe(MQTT_COMMAND_TOPIC, client, uploadStruct)) == 0) {
+ LOG ("--->>>MQTT subscribed to: %s\n\r", MQTT_COMMAND_TOPIC);
+ } else {
+ LOG ("--->>>ERROR MQTT subscribe : %s\n\r", MQTT_COMMAND_TOPIC);
+ }
+ mqttConnecting = false;
+ connack_rc = rc;
+ return rc;
+}
+
+
+int MQTT_GetConnTimeout(int attemptNumber) { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
+ // after 20 attempts, retry every 10 minutes
+ return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
+}
+
+
+void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct) {
+ connected = false;
+
+ while (MQTT_Connect(client, mqttNetwork, network, uploadStruct) != MQTT_CONNECTION_ACCEPTED) {
+ if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
+ printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
+ return; // don't reattempt to connect if credentials are wrong
+ }
+ int timeout = MQTT_GetConnTimeout(++retryAttempt);
+ WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
+
+ // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
+ // 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();
+ else
+ wait(timeout);
+ }
+}
+
+int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0) {
+ MQTT::Message message;
+ const char* pubTopic = MQTT_EVENT_TOPIC;
+
+ char buf[MQTT_MAX_PAYLOAD_SIZE];
+ char timeBuf[50];
+
+ if (!client->isConnected()) {
+ printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
+ }
+
+ strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
+ sprintf(buf, "{\"type\":1,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"adc0\":%.1f}",
+ timeBuf, commandID, adcVal_0);
+ message.qos = MQTT::QOS0;
+ message.retained = false;
+ message.dup = false;
+ message.payload = (void*)buf;
+ message.payloadlen = strlen(buf);
+
+ if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
+ printf("message too long!\r\n");
+
+ LOG("Publishing %s\n\r", buf);
+ return client->publish(pubTopic, message);
+}
+
+int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float pHVal) {
+ MQTT::Message message;
+ const char* pubTopic = MQTT_EVENT_TOPIC;
+
+ char buf[MQTT_MAX_PAYLOAD_SIZE];
+ char timeBuf[50];
+
+ if (!client->isConnected()) {
+ printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
+ }
+
+ strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
+ sprintf(buf, "{\"type\":2,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"pH0\":%.1f}",
+ timeBuf, commandID, pHVal);
+ message.qos = MQTT::QOS0;
+ message.retained = false;
+ message.dup = false;
+ message.payload = (void*)buf;
+ message.payloadlen = strlen(buf);
+
+ if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
+ printf("message too long!\r\n");
+
+ LOG("Publishing %s\n\r", buf);
+ return client->publish(pubTopic, message);
+}
+
+int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2) {
+ MQTT::Message message;
+ const char* pubTopic = MQTT_EVENT_TOPIC;
+ char buf[MQTT_MAX_PAYLOAD_SIZE];
+ char timeBuf[50];
+
+ if (!client->isConnected()) {
+ printf ("---> MQTT DISCONNECTED\n\r");
+ 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}",
+ timeBuf, commandID, relay1, relay2);
+ message.qos = MQTT::QOS0;
+ message.retained = false;
+ message.dup = false;
+ message.payload = (void*)buf;
+ message.payloadlen = strlen(buf);
+
+ if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
+ printf("message too long!\r\n");
+
+ LOG("Publishing %s\n\r", buf);
+ return client->publish(pubTopic, message);
+}
+
+int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t minOxi, uint8_t maxOxi) {
+ MQTT::Message message;
+ const char* pubTopic = MQTT_EVENT_TOPIC;
+ char buf[MQTT_MAX_PAYLOAD_SIZE];
+ char timeBuf[50];
+
+ if (!client->isConnected()) {
+ printf ("---> MQTT DISCONNECTED\n\r");
+ 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,\"minOxygenVal\":%d,\"maxOxygenVal\":%d}",
+ timeBuf, commandID, mode, minOxi, maxOxi);
+ message.qos = MQTT::QOS0;
+ message.retained = false;
+ message.dup = false;
+ message.payload = (void*)buf;
+ message.payloadlen = strlen(buf);
+
+ if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
+ printf("message too long!\r\n");
+
+ LOG("Publishing %s\n\r", buf);
+ return client->publish(pubTopic, message);
+}
+
+int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct) {
+ int retVal;
+ switch (uploadType) {
+ case (ADC_VALUE): retVal = MQTT_PublishADC(client, inputTime, uploadStruct.ADC_PHVal);
+ break;
+ case (SENSOR_VALUE): retVal = MQTT_PublishSensorVal(client, inputTime, uploadStruct.SENSOR_PHVal);
+ break;
+ case (RELAY_STATE): retVal = MQTT_PublishRelayState(client, inputTime, uploadStruct.RELAY_State_1, uploadStruct.RELAY_State_2);
+ break;
+ case (CONFIG_VALUE): retVal = MQTT_PublishConfigValue(client, inputTime, uploadStruct.CONFIG_Mode, uploadStruct.CONFIG_MinOxi, uploadStruct.CONFIG_MaxOxi);
+ break;
+ default: break;
+ }
+ return retVal;
+}
+
+#endif /* __SIMPLEMQTT_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/easy-connect-v16.lib Mon Jan 08 18:07:56 2018 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/users/DuyLionTran/code/easy-connect-v16/#9fa9929d1a8c
--- a/easy-connect.lib Mon Jan 08 22:09:56 2018 +0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://github.com/ARMmbed/easy-connect/#542e3ab6d180e362b1344d9736ac1e8e4986934b
--- a/main.cpp Mon Jan 08 22:09:56 2018 +0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,130 +0,0 @@
-// For receiving message handles, search "Message Handles" in SimpleMQTT.h
-
-/***************************************************************
- * Includes
- ***************************************************************/
-#include "mbed.h"
-
-#include "ReadSensor.h"
-#include "SimpleMQTT.h"
-#include "flash_programming.h"
-
-/***************************************************************
- * Definitions
- ***************************************************************/
-#define RELAY_1_PIN (D11)
-#define RELAY_2_PIN (D12)
-
-/***************************************************************
- * Variables
- ***************************************************************/
-bool isUploading = false;
-uint8_t uploadType = ADC_VALUE;
-
-uint32_t lastRead = 0;
-uint16_t readSecond = 5; /* Read timer every 2 second(s) */
-uint32_t uploadPeriod = 4; /* Period between each time upload all data = uploadPeriod*readSecond = 20 s */
-uint32_t uploadPeriodCounter = 0;
-
-struct UploadValue DataStruct = {19, 25, 0, 1, 0, 65, 80, 10};
-
-/* Analog Handles */
-float ADC_PHVal;
-float ADC_DOVal;
-
-/***************************************************************
- * Structs/Classess
- ***************************************************************/
-static Serial pc(SERIAL_TX, SERIAL_RX);
-
-DigitalOut myled(LED1);
-DigitalOut led1(RELAY_1_PIN);
-DigitalOut led2(RELAY_2_PIN);
-
-Timer UploadTimer;
-/***************************************************************
- * Unity function definitions
- ***************************************************************/
-
-/***************************************************************
- * Unity function declarations
- ***************************************************************/
-void MQTT_MessageHandles(uint8_t ControlSignal) {
- switch(ControlSignal) {
- case (1): led1 = !led1;
- break;
- case (2): led2 = !led2;
- break;
- default: break;
- }
-}
-
-
-/***************************************************************
- * Main
- ***************************************************************/
-int main() {
- pc.baud(115200);
- UploadTimer.start();
- lastRead = 0;
- set_time(1515445620);
- pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");
- pc.printf("\r\nconnecting to AP\r\n");
-
- NetworkInterface* network = easy_connect(true);
- if (!network) {
- printf ("Error easy_connect\n\r");
- }
- MQTTNetwork mqttNetwork(network);
- MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
- printf ("ATTEMPT CONNECT\n\r");
- MQTT_AttemptConnect(&client, &mqttNetwork, network);
- 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
- }
-
- myled=1;
- FP_WriteConfigValues(25, 26, 27);
- while (true) {
- time_t seconds = time(NULL);
- ADC_PHVal = SENSOR_ReadPHADC();
- 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();
- }
- }
- else {
- myled = 0;
- client.disconnect();
- mqttNetwork.disconnect();
- MQTT_AttemptConnect(&client, &mqttNetwork, network); // 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;
- }
- }
-}
-
--- a/mbed_app.json Mon Jan 08 22:09:56 2018 +0700
+++ b/mbed_app.json Mon Jan 08 18:07:56 2018 +0000
@@ -1,59 +1,59 @@
-{
- "config": {
- "network-interface":{
- "help": "options are ETHERNET, WIFI_ESP8266, WIFI_IDW0XX1, WIFI_ODIN, WIFI_RTW, MESH_LOWPAN_ND, MESH_THREAD, CELLULAR_ONBOARD",
- "value": "WIFI_ESP8266"
- },
- "mesh_radio_type": {
- "help": "options are ATMEL, MCR20",
- "value": "ATMEL"
- },
- "esp8266-tx": {
- "help": "Pin used as TX (connects to ESP8266 RX)",
- "value": "D1"
- },
- "esp8266-rx": {
- "help": "Pin used as RX (connects to ESP8266 TX)",
- "value": "D0"
- },
- "esp8266-ssid": {
- "value": "\"GreenLantern\""
- },
- "esp8266-password": {
- "value": "\"deAlembert\""
- },
- "esp8266-debug": {
- "value": true
- }
- },
- "macros": [ "MQTT_BROKER_URL=\".messaging.internetofthings.ibmcloud.com\"",
- "MQTT_CLIENT_ID=\"d:a4nvkh:Nucleo_8266:PROEVN\"",
- "MQTT_ORG_ID=\"a4nvkh\"",
- "MQTT_USERNAME=\"use-token-auth\"",
- "MQTT_DEVICE_PASSWORD=\"PROEVN2017\"",
- "MQTT_EVENT_TOPIC=\"iot-2/evt/status/fmt/json\"",
- "MQTT_COMMAND_TOPIC=\"iot-2/cmd/command/fmt/json\"",
- "MQTT_DEVICE_ID=\"PROEVN\"",
- "MQTT_DEVICE_TYPE=\"Nucleo_8266\"",
- "MQTT_KEEPALIVE 10",
- "MQTT_PORT 1883",
- "MQTT_TLS_PORT 8883"],
- "target_overrides": {
- "*": {
- "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER", "COMMON_PAL"],
- "mbed-mesh-api.6lowpan-nd-channel-page": 0,
- "mbed-mesh-api.6lowpan-nd-channel": 12
- },
- "K64F": {
- },
- "NUCLEO_F401RE": {
- "wifi-tx": "D8",
- "wifi-rx": "D2",
- "target.macros_add": ["IKS01A2"]
- },
- "DISCO_L475VG_IOT01A": {
- },
- "NUCLEO_L476RG": {
- }
- }
-}
+{
+ "config": {
+ "network-interface":{
+ "help": "options are ETHERNET, WIFI_ESP8266, WIFI_IDW0XX1, WIFI_ODIN, WIFI_RTW, MESH_LOWPAN_ND, MESH_THREAD, CELLULAR_ONBOARD",
+ "value": "WIFI_ESP8266"
+ },
+ "mesh_radio_type": {
+ "help": "options are ATMEL, MCR20",
+ "value": "ATMEL"
+ },
+ "esp8266-tx": {
+ "help": "Pin used as TX (connects to ESP8266 RX)",
+ "value": "D1"
+ },
+ "esp8266-rx": {
+ "help": "Pin used as RX (connects to ESP8266 TX)",
+ "value": "D0"
+ },
+ "esp8266-ssid": {
+ "value": "\"GreenLantern\""
+ },
+ "esp8266-password": {
+ "value": "\"deAlembert\""
+ },
+ "esp8266-debug": {
+ "value": true
+ }
+ },
+ "macros": [ "MQTT_BROKER_URL=\".messaging.internetofthings.ibmcloud.com\"",
+ "MQTT_CLIENT_ID=\"d:a4nvkh:Nucleo_8266:PROEVN\"",
+ "MQTT_ORG_ID=\"a4nvkh\"",
+ "MQTT_USERNAME=\"use-token-auth\"",
+ "MQTT_DEVICE_PASSWORD=\"PROEVN2017\"",
+ "MQTT_EVENT_TOPIC=\"iot-2/evt/status/fmt/json\"",
+ "MQTT_COMMAND_TOPIC=\"iot-2/cmd/command/fmt/json\"",
+ "MQTT_DEVICE_ID=\"PROEVN\"",
+ "MQTT_DEVICE_TYPE=\"Nucleo_8266\"",
+ "MQTT_KEEPALIVE 10",
+ "MQTT_PORT 1883",
+ "MQTT_TLS_PORT 8883"],
+ "target_overrides": {
+ "*": {
+ "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER", "COMMON_PAL"],
+ "mbed-mesh-api.6lowpan-nd-channel-page": 0,
+ "mbed-mesh-api.6lowpan-nd-channel": 12
+ },
+ "K64F": {
+ },
+ "NUCLEO_F401RE": {
+ "wifi-tx": "D8",
+ "wifi-rx": "D2",
+ "target.macros_add": ["IKS01A2"]
+ },
+ "DISCO_L475VG_IOT01A": {
+ },
+ "NUCLEO_L476RG": {
+ }
+ }
+}