Duy tran / Mbed OS iot_water_monitor_v2

Dependencies:   easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code

Files at this revision

API Documentation at this revision

Comitter:
DuyLionTran
Date:
Thu Jan 04 17:17:42 2018 +0000
Parent:
27:8db8de505c47
Child:
29:3636503b2539
Commit message:
version 1.8.8: json parser works fine, next update will turn on/off a led online

Changed in this revision

Flash/flash_programming.cpp Show annotated file Show diff for this revision Revisions of this file
Flash/flash_programming.h Show annotated file Show diff for this revision Revisions of this file
Simple-MQTT/MQTTNetwork.h Show annotated file Show diff for this revision Revisions of this file
Simple-MQTT/SimpleMQTT.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flash/flash_programming.cpp	Thu Jan 04 17:17:42 2018 +0000
@@ -0,0 +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;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flash/flash_programming.h	Thu Jan 04 17:17:42 2018 +0000
@@ -0,0 +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);
+
+#endif /* __FLASH_PROGRAMMING_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simple-MQTT/MQTTNetwork.h	Thu Jan 04 17:17:42 2018 +0000
@@ -0,0 +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_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simple-MQTT/SimpleMQTT.h	Thu Jan 04 17:17:42 2018 +0000
@@ -0,0 +1,365 @@
+#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"
+/***************************************************************
+ * 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
+ ***************************************************************/
+/** 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) {
+    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);
+            
+            char DeviceID[32];
+            KeyIndex   = json.findKeyIndexIn("deviceId", 0);
+            KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
+            
+            const char *valueStart  = json.tokenAddress(KeyValueIndex);
+            int         valueLength = json.tokenLength(KeyValueIndex);
+            strncpy(DeviceID, valueStart, valueLength);
+            DeviceID[valueLength] = 0;
+            printf("Device ID: %s\r\n", DeviceID);
+        }    	
+    }    
+}
+
+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__ */