iot_water_monitor_v2

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

Committer:
DuyLionTran
Date:
Tue Apr 03 17:03:01 2018 +0000
Revision:
57:898fcb6692cd
Parent:
55:c405323f8d5a
;   * version 2.9.8  	03-04-2018  Minor changes. Time frame updated to IBM Watson every 60s

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 32:8226837c56ae 1 #ifndef __SIMPLEMQTT_H__
DuyLionTran 32:8226837c56ae 2 #define __SIMPLEMQTT_H__
DuyLionTran 32:8226837c56ae 3
DuyLionTran 32:8226837c56ae 4 /***************************************************************
DuyLionTran 32:8226837c56ae 5 * Includes
DuyLionTran 32:8226837c56ae 6 ***************************************************************/
DuyLionTran 32:8226837c56ae 7 #include "easy-connect.h"
DuyLionTran 32:8226837c56ae 8 #include "MQTTClient.h"
DuyLionTran 32:8226837c56ae 9 #include "NDefLib/NDefNfcTag.h"
DuyLionTran 32:8226837c56ae 10 #include "NDefLib/RecordType/RecordURI.h"
DuyLionTran 32:8226837c56ae 11 #include "MQTTNetwork.h"
DuyLionTran 32:8226837c56ae 12 #include "MQTTmbed.h"
DuyLionTran 32:8226837c56ae 13
DuyLionTran 32:8226837c56ae 14 #include "Json.h"
DuyLionTran 32:8226837c56ae 15 #include "CommandExecution.h"
DuyLionTran 32:8226837c56ae 16 #include "flash_programming.h"
DuyLionTran 32:8226837c56ae 17 /***************************************************************
DuyLionTran 32:8226837c56ae 18 * Definitions
DuyLionTran 32:8226837c56ae 19 ***************************************************************/
DuyLionTran 32:8226837c56ae 20 // Configuration values needed to connect to IBM IoT Cloud
DuyLionTran 32:8226837c56ae 21 #define ORG MQTT_ORG_ID // connect to ORG.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
DuyLionTran 32:8226837c56ae 22 #define ID MQTT_DEVICE_ID // For a registered connection is your device id
DuyLionTran 32:8226837c56ae 23 #define AUTH_TOKEN MQTT_DEVICE_PASSWORD // For a registered connection is a device auth-token
DuyLionTran 32:8226837c56ae 24 #define DEFAULT_TYPE_NAME MQTT_DEVICE_TYPE // For a registered connection is device type
DuyLionTran 32:8226837c56ae 25 #define AUTH_METHOD MQTT_USERNAME
DuyLionTran 32:8226837c56ae 26
DuyLionTran 32:8226837c56ae 27 #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
DuyLionTran 32:8226837c56ae 28 #define IBM_IOT_PORT MQTT_PORT
DuyLionTran 32:8226837c56ae 29
DuyLionTran 43:dcde0e66874a 30 #define MQTT_MAX_PACKET_SIZE 450
DuyLionTran 43:dcde0e66874a 31 #define MQTT_MAX_PAYLOAD_SIZE 350
DuyLionTran 32:8226837c56ae 32
DuyLionTran 32:8226837c56ae 33 /***************************************************************
DuyLionTran 32:8226837c56ae 34 * Variables
DuyLionTran 32:8226837c56ae 35 ***************************************************************/
DuyLionTran 32:8226837c56ae 36 typedef enum {
DuyLionTran 32:8226837c56ae 37 ADC_VALUE = 0,
DuyLionTran 32:8226837c56ae 38 SENSOR_VALUE,
DuyLionTran 32:8226837c56ae 39 RELAY_STATE,
DuyLionTran 32:8226837c56ae 40 CONFIG_VALUE
DuyLionTran 32:8226837c56ae 41 } UploadType;
DuyLionTran 39:a5ee98bd0050 42
DuyLionTran 39:a5ee98bd0050 43 typedef enum {
DuyLionTran 39:a5ee98bd0050 44 CONTROL_CMD = 0,
DuyLionTran 39:a5ee98bd0050 45 READ_CMD,
DuyLionTran 39:a5ee98bd0050 46 SETUP_CMD
DuyLionTran 39:a5ee98bd0050 47 } CommandType;
DuyLionTran 32:8226837c56ae 48
DuyLionTran 32:8226837c56ae 49 struct UploadValue {
DuyLionTran 43:dcde0e66874a 50 float ADC_TEMPVal;
DuyLionTran 32:8226837c56ae 51 float ADC_DOVal;
DuyLionTran 32:8226837c56ae 52
DuyLionTran 43:dcde0e66874a 53 float SENSOR_TEMPVal;
DuyLionTran 36:d0b628087ac8 54 float SENSOR_DOVal;
DuyLionTran 32:8226837c56ae 55
DuyLionTran 33:5b90257d2d57 56 uint8_t RELAY_State_1;
DuyLionTran 33:5b90257d2d57 57 uint8_t RELAY_State_2;
DuyLionTran 34:601effb7b7fe 58 uint8_t RELAY_State_3;
DuyLionTran 34:601effb7b7fe 59
DuyLionTran 33:5b90257d2d57 60 uint32_t CONFIG_Time;
DuyLionTran 32:8226837c56ae 61
DuyLionTran 32:8226837c56ae 62 uint8_t CONFIG_Mode;
DuyLionTran 43:dcde0e66874a 63 uint8_t CONFIG_OxyThreshold;
DuyLionTran 43:dcde0e66874a 64 uint8_t CONFIG_TemperatureThreshold;
DuyLionTran 50:5a19fc4b41d9 65 uint16_t CONFIG_UploadInterval; /* in second */
DuyLionTran 50:5a19fc4b41d9 66
DuyLionTran 50:5a19fc4b41d9 67 uint32_t CONFIG_AlarmTime;
DuyLionTran 50:5a19fc4b41d9 68 uint8_t CONFIG_SetRelayState_1;
DuyLionTran 50:5a19fc4b41d9 69 uint8_t CONFIG_SetRelayState_2;
DuyLionTran 32:8226837c56ae 70 } UploadValue;
DuyLionTran 32:8226837c56ae 71
DuyLionTran 32:8226837c56ae 72 char *projectName = "WaterMonitor";
DuyLionTran 32:8226837c56ae 73 static char id[30] = ID; // mac without colons
DuyLionTran 32:8226837c56ae 74 static char org[12] = ORG;
DuyLionTran 32:8226837c56ae 75 static char type[30] = TYPE;
DuyLionTran 32:8226837c56ae 76 static char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
DuyLionTran 32:8226837c56ae 77 static int connack_rc = 0; // MQTT connack return code
DuyLionTran 33:5b90257d2d57 78 static bool wifiConnected = true;
DuyLionTran 32:8226837c56ae 79 static bool netConnecting = false;
DuyLionTran 32:8226837c56ae 80 static bool mqttConnecting = false;
DuyLionTran 32:8226837c56ae 81 static bool netConnected = false;
DuyLionTran 32:8226837c56ae 82 static bool connected = false;
DuyLionTran 32:8226837c56ae 83 static int retryAttempt = 0;
DuyLionTran 32:8226837c56ae 84 static int connectTimeout = 1000;
DuyLionTran 32:8226837c56ae 85 uint16_t commandID = 0;
DuyLionTran 32:8226837c56ae 86 static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 87
DuyLionTran 32:8226837c56ae 88 extern struct UploadValue DataStruct;
DuyLionTran 32:8226837c56ae 89 /***************************************************************
DuyLionTran 32:8226837c56ae 90 * Unity function definitions
DuyLionTran 32:8226837c56ae 91 ***************************************************************/
DuyLionTran 32:8226837c56ae 92 void MQTT_MessageHandles(uint8_t ControlSignal);
DuyLionTran 32:8226837c56ae 93
DuyLionTran 32:8226837c56ae 94 /** brief Callback function when MQTT message arrives
DuyLionTran 32:8226837c56ae 95 * param[in] msgMQTT
DuyLionTran 32:8226837c56ae 96 * retral None
DuyLionTran 32:8226837c56ae 97 */
DuyLionTran 32:8226837c56ae 98 void MQTT_SubscribeCallback(MQTT::MessageData &msgMQTT);
DuyLionTran 32:8226837c56ae 99
DuyLionTran 32:8226837c56ae 100 /** brief Subscribe to a MQTT topic and set the MQTT callback function
DuyLionTran 32:8226837c56ae 101 * param[in] subscribeTopic Topic to be subscribed
DuyLionTran 32:8226837c56ae 102 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 103 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 104 */
DuyLionTran 32:8226837c56ae 105 int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, struct UploadValue uploadStruct);
DuyLionTran 32:8226837c56ae 106
DuyLionTran 32:8226837c56ae 107 /** brief Connect to the internet then the MQTT network
DuyLionTran 32:8226837c56ae 108 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 109 * param[in] mqttNetwork MQTT network
DuyLionTran 32:8226837c56ae 110 * param[in] network The internet network interface (ethernet, wifi...)
DuyLionTran 32:8226837c56ae 111 * retral Internet connect result and returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 112 */
DuyLionTran 32:8226837c56ae 113 int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct);
DuyLionTran 32:8226837c56ae 114
DuyLionTran 32:8226837c56ae 115 /** brief Setup the number of attempt to re-connect to the internet
DuyLionTran 32:8226837c56ae 116 * param[in] attemptNumber The number of attemp
DuyLionTran 32:8226837c56ae 117 */
DuyLionTran 32:8226837c56ae 118 int MQTT_GetConnTimeout(int attemptNumber);
DuyLionTran 32:8226837c56ae 119
DuyLionTran 32:8226837c56ae 120 /** brief Try to reconnect to the internet and MQTT network
DuyLionTran 32:8226837c56ae 121 * retral None
DuyLionTran 32:8226837c56ae 122 */
DuyLionTran 32:8226837c56ae 123 void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct);
DuyLionTran 32:8226837c56ae 124
DuyLionTran 32:8226837c56ae 125 /** brief Publish ADC values to the server
DuyLionTran 32:8226837c56ae 126 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 127 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 128 * param[in] adcVal_0 The ADC value to be sent
DuyLionTran 32:8226837c56ae 129 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 130 */
DuyLionTran 32:8226837c56ae 131 int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0);
DuyLionTran 32:8226837c56ae 132
DuyLionTran 32:8226837c56ae 133 /** brief Publish Sensor values to the server
DuyLionTran 32:8226837c56ae 134 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 135 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 136 * param[in] pHVal The pHVal value to be sent
DuyLionTran 32:8226837c56ae 137 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 138 */
DuyLionTran 43:dcde0e66874a 139 int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float TempVal, float DOVal);
DuyLionTran 32:8226837c56ae 140
DuyLionTran 32:8226837c56ae 141 /** brief Publish relay states to the server
DuyLionTran 32:8226837c56ae 142 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 143 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 144 * param[in] relay1 Relay 1 state
DuyLionTran 32:8226837c56ae 145 * param[in] relay2 Relay 2 state
DuyLionTran 32:8226837c56ae 146 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 147 */
DuyLionTran 36:d0b628087ac8 148 int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2, int relay3);
DuyLionTran 32:8226837c56ae 149
DuyLionTran 32:8226837c56ae 150 /** brief Publish relay states to the server
DuyLionTran 32:8226837c56ae 151 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 152 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 153 * param[in] mode current mode: automatic (0) or manual (1)
DuyLionTran 32:8226837c56ae 154 * param[in] maxOxi Maximum Oxygen value
DuyLionTran 32:8226837c56ae 155 * param[in] minOxi Minimum Oxygen value
DuyLionTran 32:8226837c56ae 156 // * param[in] uploadInterval Interval between upload turns
DuyLionTran 32:8226837c56ae 157 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 158 */
DuyLionTran 43:dcde0e66874a 159 int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t OxyThreshold, uint8_t TemperatureThreshold, uint16_t UploadInterval);
DuyLionTran 32:8226837c56ae 160
DuyLionTran 32:8226837c56ae 161 /** brief Upload all the data to the MQTT server
DuyLionTran 32:8226837c56ae 162 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 163 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 164 * param[in] uploadInterval The period between each upload moment
DuyLionTran 32:8226837c56ae 165 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 166 */
DuyLionTran 32:8226837c56ae 167 int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct);
DuyLionTran 32:8226837c56ae 168
DuyLionTran 57:898fcb6692cd 169 int MQTT_PublishDeviceManage(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint32_t uploadPeriod, uint32_t sendTimePeriod);
DuyLionTran 32:8226837c56ae 170 /********************************************************************************************************************************************************************************************/
DuyLionTran 32:8226837c56ae 171 /***************************************************************
DuyLionTran 32:8226837c56ae 172 * Unity function declarations
DuyLionTran 32:8226837c56ae 173 ***************************************************************/
DuyLionTran 32:8226837c56ae 174 void MQTT_SubscribeCallback(MQTT::MessageData &msgMQTT) {
DuyLionTran 32:8226837c56ae 175 // Message Handles
DuyLionTran 32:8226837c56ae 176 char msg[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 177 msg[0]='\0';
DuyLionTran 32:8226837c56ae 178 strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
DuyLionTran 32:8226837c56ae 179 printf ("--->>> MQTT_SubscribeCallback msg: %s\n\r", msg);
DuyLionTran 39:a5ee98bd0050 180 /* {"type":"3","deviceId":"string"} */
DuyLionTran 32:8226837c56ae 181 Json json(msg, msgMQTT.message.payloadlen);
DuyLionTran 32:8226837c56ae 182 if (!json.isValidJson()) {
DuyLionTran 32:8226837c56ae 183 printf("Invalid JSON: %s", msg);
DuyLionTran 32:8226837c56ae 184 }
DuyLionTran 32:8226837c56ae 185 else {
DuyLionTran 32:8226837c56ae 186 if (json.type(0) != JSMN_OBJECT ) {
DuyLionTran 32:8226837c56ae 187 printf("Invalid JSON. ROOT element is not Object: %s", msg);
DuyLionTran 32:8226837c56ae 188 }
DuyLionTran 32:8226837c56ae 189 else {
DuyLionTran 32:8226837c56ae 190 int CommandType;
DuyLionTran 32:8226837c56ae 191 int KeyIndex = json.findKeyIndexIn("type", 0);
DuyLionTran 32:8226837c56ae 192 int KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 193 int ret = json.tokenIntegerValue(KeyValueIndex, CommandType);
DuyLionTran 32:8226837c56ae 194
DuyLionTran 32:8226837c56ae 195 int receiveCmdID;
DuyLionTran 32:8226837c56ae 196 printf("Command Type: %d, error %d\r\n", CommandType, ret);
DuyLionTran 32:8226837c56ae 197
DuyLionTran 32:8226837c56ae 198 switch (CommandType) {
DuyLionTran 45:92da959d624b 199 case 0: printf("Calibrate\r\n");
DuyLionTran 45:92da959d624b 200 CE_Calibrate();
DuyLionTran 39:a5ee98bd0050 201 break;
DuyLionTran 39:a5ee98bd0050 202
DuyLionTran 45:92da959d624b 203 case 1: printf("Set relay alarm state\r\n");
DuyLionTran 50:5a19fc4b41d9 204 int AlarmTime, SetRelayState1, SetRelayState2;
DuyLionTran 50:5a19fc4b41d9 205 KeyIndex = json.findKeyIndexIn("cmdID", 0);
DuyLionTran 50:5a19fc4b41d9 206 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 50:5a19fc4b41d9 207 ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
DuyLionTran 50:5a19fc4b41d9 208
DuyLionTran 50:5a19fc4b41d9 209 KeyIndex = json.findKeyIndexIn("AlarmTime", 0);
DuyLionTran 50:5a19fc4b41d9 210 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 50:5a19fc4b41d9 211 ret = json.tokenIntegerValue(KeyValueIndex, AlarmTime);
DuyLionTran 50:5a19fc4b41d9 212
DuyLionTran 50:5a19fc4b41d9 213 KeyIndex = json.findKeyIndexIn("SetRelayState1", 0);
DuyLionTran 50:5a19fc4b41d9 214 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 50:5a19fc4b41d9 215 ret = json.tokenIntegerValue(KeyValueIndex, SetRelayState1);
DuyLionTran 50:5a19fc4b41d9 216
DuyLionTran 50:5a19fc4b41d9 217 KeyIndex = json.findKeyIndexIn("SetRelayState2", 0);
DuyLionTran 50:5a19fc4b41d9 218 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 50:5a19fc4b41d9 219 ret = json.tokenIntegerValue(KeyValueIndex, SetRelayState2);
DuyLionTran 50:5a19fc4b41d9 220
DuyLionTran 50:5a19fc4b41d9 221 DataStruct.CONFIG_AlarmTime = AlarmTime;
DuyLionTran 50:5a19fc4b41d9 222 DataStruct.CONFIG_SetRelayState_1 = SetRelayState1;
DuyLionTran 50:5a19fc4b41d9 223 DataStruct.CONFIG_SetRelayState_2 = SetRelayState2;
DuyLionTran 50:5a19fc4b41d9 224 FP_SetAlarmValues(DataStruct.CONFIG_AlarmTime, DataStruct.CONFIG_SetRelayState_1, DataStruct.CONFIG_SetRelayState_2);
DuyLionTran 45:92da959d624b 225 break;
DuyLionTran 43:dcde0e66874a 226
DuyLionTran 45:92da959d624b 227 case 2: printf("Set relay state\r\n");
DuyLionTran 45:92da959d624b 228 int relayState1, relayState2, relayState3;
DuyLionTran 32:8226837c56ae 229 KeyIndex = json.findKeyIndexIn("cmdID", 0);
DuyLionTran 32:8226837c56ae 230 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 231 ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
DuyLionTran 32:8226837c56ae 232
DuyLionTran 55:c405323f8d5a 233 KeyIndex = json.findKeyIndexIn("relayState0", 0);
DuyLionTran 55:c405323f8d5a 234 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 55:c405323f8d5a 235 ret = json.tokenIntegerValue(KeyValueIndex, relayState1);
DuyLionTran 55:c405323f8d5a 236
DuyLionTran 32:8226837c56ae 237 KeyIndex = json.findKeyIndexIn("relayState1", 0);
DuyLionTran 32:8226837c56ae 238 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 55:c405323f8d5a 239 ret = json.tokenIntegerValue(KeyValueIndex, relayState2);
DuyLionTran 32:8226837c56ae 240
DuyLionTran 33:5b90257d2d57 241 KeyIndex = json.findKeyIndexIn("relayState2", 0);
DuyLionTran 33:5b90257d2d57 242 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 34:601effb7b7fe 243 ret = json.tokenIntegerValue(KeyValueIndex, relayState3);
DuyLionTran 34:601effb7b7fe 244
DuyLionTran 55:c405323f8d5a 245 if (DataStruct.CONFIG_Mode == 0) {
DuyLionTran 50:5a19fc4b41d9 246 DataStruct.RELAY_State_1 = relayState1;
DuyLionTran 50:5a19fc4b41d9 247 DataStruct.RELAY_State_2 = relayState2;
DuyLionTran 50:5a19fc4b41d9 248 DataStruct.RELAY_State_3 = relayState3;
DuyLionTran 50:5a19fc4b41d9 249 FP_WriteRelayStates(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);
DuyLionTran 50:5a19fc4b41d9 250 CE_HandleRelays(relayState1, relayState2, relayState3);
DuyLionTran 50:5a19fc4b41d9 251 }
DuyLionTran 50:5a19fc4b41d9 252
DuyLionTran 32:8226837c56ae 253 break;
DuyLionTran 32:8226837c56ae 254
DuyLionTran 45:92da959d624b 255 case 3: printf("Set config values\r\n");
DuyLionTran 55:c405323f8d5a 256 int mode, OxiThres, TempThres, uploadInterval;
DuyLionTran 32:8226837c56ae 257 KeyIndex = json.findKeyIndexIn("cmdID", 0);
DuyLionTran 32:8226837c56ae 258 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 259 ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
DuyLionTran 32:8226837c56ae 260
DuyLionTran 32:8226837c56ae 261 KeyIndex = json.findKeyIndexIn("mode", 0);
DuyLionTran 32:8226837c56ae 262 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 263 ret = json.tokenIntegerValue(KeyValueIndex, mode);
DuyLionTran 32:8226837c56ae 264
DuyLionTran 43:dcde0e66874a 265 KeyIndex = json.findKeyIndexIn("OxygenThreshold", 0);
DuyLionTran 32:8226837c56ae 266 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 43:dcde0e66874a 267 ret = json.tokenIntegerValue(KeyValueIndex, OxiThres);
DuyLionTran 32:8226837c56ae 268
DuyLionTran 43:dcde0e66874a 269 KeyIndex = json.findKeyIndexIn("TemperatureThreshold", 0);
DuyLionTran 32:8226837c56ae 270 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 43:dcde0e66874a 271 ret = json.tokenIntegerValue(KeyValueIndex, TempThres);
DuyLionTran 33:5b90257d2d57 272
DuyLionTran 33:5b90257d2d57 273 KeyIndex = json.findKeyIndexIn("uploadInterval", 0);
DuyLionTran 33:5b90257d2d57 274 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 35:fc800d35c1ba 275 ret = json.tokenIntegerValue(KeyValueIndex, uploadInterval);
DuyLionTran 55:c405323f8d5a 276
DuyLionTran 43:dcde0e66874a 277 DataStruct.CONFIG_Mode = mode;
DuyLionTran 43:dcde0e66874a 278 DataStruct.CONFIG_OxyThreshold = OxiThres;
DuyLionTran 43:dcde0e66874a 279 DataStruct.CONFIG_TemperatureThreshold = TempThres;
DuyLionTran 55:c405323f8d5a 280 DataStruct.CONFIG_UploadInterval = uploadInterval * 60;
DuyLionTran 55:c405323f8d5a 281 printf("OMode %d\r\n", DataStruct.CONFIG_Mode );
DuyLionTran 55:c405323f8d5a 282 printf("Oxygen Threshold*10 %dppm(*100)\r\n", DataStruct.CONFIG_OxyThreshold);
DuyLionTran 55:c405323f8d5a 283 printf("Temperature Threshold %d*C\r\n", DataStruct.CONFIG_TemperatureThreshold);
DuyLionTran 55:c405323f8d5a 284 printf("Periodic upload time %ds\r\n", DataStruct.CONFIG_UploadInterval);
DuyLionTran 43:dcde0e66874a 285 FP_WriteConfigValues(DataStruct.CONFIG_Mode, DataStruct.CONFIG_OxyThreshold, DataStruct.CONFIG_TemperatureThreshold, DataStruct.CONFIG_UploadInterval);
DuyLionTran 55:c405323f8d5a 286
DuyLionTran 32:8226837c56ae 287 break;
DuyLionTran 32:8226837c56ae 288
DuyLionTran 45:92da959d624b 289 case 4: printf("Synchronize data\r\n");
DuyLionTran 45:92da959d624b 290 CE_UpdateImmediately();
DuyLionTran 55:c405323f8d5a 291 case 5: printf("Set time\r\n");
DuyLionTran 55:c405323f8d5a 292 int setRTCTime;
DuyLionTran 55:c405323f8d5a 293 KeyIndex = json.findKeyIndexIn("cmdID", 0);
DuyLionTran 55:c405323f8d5a 294 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 55:c405323f8d5a 295 ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
DuyLionTran 55:c405323f8d5a 296
DuyLionTran 55:c405323f8d5a 297 KeyIndex = json.findKeyIndexIn("setRTCTime", 0);
DuyLionTran 55:c405323f8d5a 298 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 55:c405323f8d5a 299 ret = json.tokenIntegerValue(KeyValueIndex, setRTCTime);
DuyLionTran 55:c405323f8d5a 300
DuyLionTran 55:c405323f8d5a 301 printf("Epoch time %d\r\n", (uint32_t)setRTCTime);
DuyLionTran 55:c405323f8d5a 302 CE_SetRTCTime((uint32_t)setRTCTime);
DuyLionTran 55:c405323f8d5a 303
DuyLionTran 43:dcde0e66874a 304 break;
DuyLionTran 43:dcde0e66874a 305
DuyLionTran 32:8226837c56ae 306 default: break;
DuyLionTran 32:8226837c56ae 307 }
DuyLionTran 32:8226837c56ae 308 }
DuyLionTran 32:8226837c56ae 309 }
DuyLionTran 32:8226837c56ae 310 }
DuyLionTran 32:8226837c56ae 311
DuyLionTran 32:8226837c56ae 312 int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, struct UploadValue uploadStruct) {
DuyLionTran 32:8226837c56ae 313 return client->subscribe(subscribeTopic, MQTT::QOS1, MQTT_SubscribeCallback);
DuyLionTran 32:8226837c56ae 314 }
DuyLionTran 32:8226837c56ae 315
DuyLionTran 32:8226837c56ae 316 int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct) {
DuyLionTran 32:8226837c56ae 317 const char* iot_ibm = MQTT_BROKER_URL;
DuyLionTran 32:8226837c56ae 318 char hostname[strlen(org) + strlen(iot_ibm) + 1];
DuyLionTran 32:8226837c56ae 319
DuyLionTran 32:8226837c56ae 320 sprintf(hostname, "%s%s", org, iot_ibm);
DuyLionTran 32:8226837c56ae 321 // Construct clientId - d:org:type:id
DuyLionTran 32:8226837c56ae 322 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
DuyLionTran 32:8226837c56ae 323 sprintf(clientId, "d:%s:%s:%s", org, type, id);
DuyLionTran 32:8226837c56ae 324 sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, DEFAULT_TYPE_NAME);
DuyLionTran 32:8226837c56ae 325
DuyLionTran 32:8226837c56ae 326 // Network debug statements
DuyLionTran 32:8226837c56ae 327 LOG("=====================================\n\r");
DuyLionTran 32:8226837c56ae 328 LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
DuyLionTran 32:8226837c56ae 329 LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
DuyLionTran 32:8226837c56ae 330 LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
DuyLionTran 32:8226837c56ae 331 LOG("Client ID: %s\n\r", clientId);
DuyLionTran 32:8226837c56ae 332 LOG("Topic: %s\n\r",MQTT_EVENT_TOPIC);
DuyLionTran 32:8226837c56ae 333 LOG("Subscription URL: %s\n\r", subscription_url);
DuyLionTran 32:8226837c56ae 334 LOG("=====================================\n\r");
DuyLionTran 32:8226837c56ae 335 netConnecting = true;
DuyLionTran 32:8226837c56ae 336 int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
DuyLionTran 32:8226837c56ae 337 if (rc != 0) {
DuyLionTran 32:8226837c56ae 338 printf("rc from TCP connect is %d\r\n", rc);
DuyLionTran 32:8226837c56ae 339 return rc;
DuyLionTran 32:8226837c56ae 340 }
DuyLionTran 32:8226837c56ae 341
DuyLionTran 32:8226837c56ae 342 printf ("--->TCP Connected\n\r");
DuyLionTran 32:8226837c56ae 343 netConnected = true;
DuyLionTran 32:8226837c56ae 344 netConnecting = false;
DuyLionTran 32:8226837c56ae 345
DuyLionTran 32:8226837c56ae 346 // MQTT Connect
DuyLionTran 32:8226837c56ae 347 mqttConnecting = true;
DuyLionTran 32:8226837c56ae 348 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
DuyLionTran 32:8226837c56ae 349 data.MQTTVersion = 4;
DuyLionTran 32:8226837c56ae 350 data.struct_version = 0;
DuyLionTran 32:8226837c56ae 351 data.clientID.cstring = clientId;
DuyLionTran 32:8226837c56ae 352 data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec
DuyLionTran 32:8226837c56ae 353 data.username.cstring = AUTH_METHOD;
DuyLionTran 32:8226837c56ae 354 data.password.cstring = auth_token;
DuyLionTran 32:8226837c56ae 355 printf ("AutToken: %s\n\r", auth_token);
DuyLionTran 32:8226837c56ae 356
DuyLionTran 32:8226837c56ae 357 if ((rc = client->connect(data)) != 0) {
DuyLionTran 32:8226837c56ae 358 printf("rc from MQTT connect is %d\r\n", rc);
DuyLionTran 32:8226837c56ae 359 connack_rc = rc;
DuyLionTran 32:8226837c56ae 360 return rc;
DuyLionTran 32:8226837c56ae 361 }
DuyLionTran 32:8226837c56ae 362 connected = true;
DuyLionTran 32:8226837c56ae 363 printf ("--->MQTT Connected\n\r");
DuyLionTran 32:8226837c56ae 364 if ((rc = MQTT_Subscribe(MQTT_COMMAND_TOPIC, client, uploadStruct)) == 0) {
DuyLionTran 32:8226837c56ae 365 LOG ("--->>>MQTT subscribed to: %s\n\r", MQTT_COMMAND_TOPIC);
DuyLionTran 32:8226837c56ae 366 } else {
DuyLionTran 32:8226837c56ae 367 LOG ("--->>>ERROR MQTT subscribe : %s\n\r", MQTT_COMMAND_TOPIC);
DuyLionTran 32:8226837c56ae 368 }
DuyLionTran 32:8226837c56ae 369 mqttConnecting = false;
DuyLionTran 32:8226837c56ae 370 connack_rc = rc;
DuyLionTran 32:8226837c56ae 371 return rc;
DuyLionTran 32:8226837c56ae 372 }
DuyLionTran 32:8226837c56ae 373
DuyLionTran 32:8226837c56ae 374
DuyLionTran 32:8226837c56ae 375 int MQTT_GetConnTimeout(int attemptNumber) { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
DuyLionTran 32:8226837c56ae 376 // after 20 attempts, retry every 10 minutes
DuyLionTran 32:8226837c56ae 377 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
DuyLionTran 32:8226837c56ae 378 }
DuyLionTran 32:8226837c56ae 379
DuyLionTran 32:8226837c56ae 380
DuyLionTran 32:8226837c56ae 381 void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct) {
DuyLionTran 32:8226837c56ae 382 connected = false;
DuyLionTran 32:8226837c56ae 383
DuyLionTran 32:8226837c56ae 384 while (MQTT_Connect(client, mqttNetwork, network, uploadStruct) != MQTT_CONNECTION_ACCEPTED) {
DuyLionTran 32:8226837c56ae 385 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
DuyLionTran 32:8226837c56ae 386 printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
DuyLionTran 32:8226837c56ae 387 return; // don't reattempt to connect if credentials are wrong
DuyLionTran 32:8226837c56ae 388 }
DuyLionTran 32:8226837c56ae 389 int timeout = MQTT_GetConnTimeout(++retryAttempt);
DuyLionTran 32:8226837c56ae 390 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
DuyLionTran 32:8226837c56ae 391
DuyLionTran 32:8226837c56ae 392 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
DuyLionTran 32:8226837c56ae 393 // or maybe just add the proper members to do this disconnect and call MQTT_AttemptConnect(...)
DuyLionTran 32:8226837c56ae 394 // this works - reset the system when the retry count gets to a threshold
DuyLionTran 32:8226837c56ae 395 if (retryAttempt == 5)
DuyLionTran 55:c405323f8d5a 396 {
DuyLionTran 55:c405323f8d5a 397 /* RESET ESP */
DuyLionTran 55:c405323f8d5a 398 NVIC_SystemReset();
DuyLionTran 55:c405323f8d5a 399 }
DuyLionTran 32:8226837c56ae 400 else
DuyLionTran 32:8226837c56ae 401 wait(timeout);
DuyLionTran 32:8226837c56ae 402 }
DuyLionTran 32:8226837c56ae 403 }
DuyLionTran 32:8226837c56ae 404
DuyLionTran 32:8226837c56ae 405 int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0) {
DuyLionTran 32:8226837c56ae 406 MQTT::Message message;
DuyLionTran 32:8226837c56ae 407 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 32:8226837c56ae 408
DuyLionTran 32:8226837c56ae 409 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 410 char timeBuf[50];
DuyLionTran 32:8226837c56ae 411
DuyLionTran 32:8226837c56ae 412 if (!client->isConnected()) {
DuyLionTran 32:8226837c56ae 413 printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
DuyLionTran 32:8226837c56ae 414 }
DuyLionTran 32:8226837c56ae 415
DuyLionTran 32:8226837c56ae 416 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 32:8226837c56ae 417 sprintf(buf, "{\"type\":1,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"adc0\":%.1f}",
DuyLionTran 32:8226837c56ae 418 timeBuf, commandID, adcVal_0);
DuyLionTran 32:8226837c56ae 419 message.qos = MQTT::QOS0;
DuyLionTran 32:8226837c56ae 420 message.retained = false;
DuyLionTran 32:8226837c56ae 421 message.dup = false;
DuyLionTran 32:8226837c56ae 422 message.payload = (void*)buf;
DuyLionTran 32:8226837c56ae 423 message.payloadlen = strlen(buf);
DuyLionTran 32:8226837c56ae 424
DuyLionTran 32:8226837c56ae 425 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 32:8226837c56ae 426 printf("message too long!\r\n");
DuyLionTran 32:8226837c56ae 427
DuyLionTran 32:8226837c56ae 428 LOG("Publishing %s\n\r", buf);
DuyLionTran 32:8226837c56ae 429 return client->publish(pubTopic, message);
DuyLionTran 32:8226837c56ae 430 }
DuyLionTran 32:8226837c56ae 431
DuyLionTran 43:dcde0e66874a 432 int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float TempVal, float DOVal) {
DuyLionTran 32:8226837c56ae 433 MQTT::Message message;
DuyLionTran 32:8226837c56ae 434 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 32:8226837c56ae 435
DuyLionTran 32:8226837c56ae 436 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 47:c3707a4f69a7 437 // char timeBuf[50];
DuyLionTran 32:8226837c56ae 438
DuyLionTran 32:8226837c56ae 439 if (!client->isConnected()) {
DuyLionTran 32:8226837c56ae 440 printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
DuyLionTran 32:8226837c56ae 441 }
DuyLionTran 32:8226837c56ae 442
DuyLionTran 47:c3707a4f69a7 443 // strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 48:a7ed665844cd 444 sprintf(buf, "{\"type\":2,\"deviceId\":\"PROEVN\",\"time\":%d,\"cmdId\":%d,\"temperature0\":%.2f,\"oxygen0\":%.2f}",
DuyLionTran 47:c3707a4f69a7 445 inputTime, commandID, TempVal ,DOVal);
DuyLionTran 32:8226837c56ae 446 message.qos = MQTT::QOS0;
DuyLionTran 32:8226837c56ae 447 message.retained = false;
DuyLionTran 32:8226837c56ae 448 message.dup = false;
DuyLionTran 32:8226837c56ae 449 message.payload = (void*)buf;
DuyLionTran 32:8226837c56ae 450 message.payloadlen = strlen(buf);
DuyLionTran 32:8226837c56ae 451
DuyLionTran 32:8226837c56ae 452 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 32:8226837c56ae 453 printf("message too long!\r\n");
DuyLionTran 32:8226837c56ae 454
DuyLionTran 32:8226837c56ae 455 LOG("Publishing %s\n\r", buf);
DuyLionTran 32:8226837c56ae 456 return client->publish(pubTopic, message);
DuyLionTran 32:8226837c56ae 457 }
DuyLionTran 32:8226837c56ae 458
DuyLionTran 36:d0b628087ac8 459 int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2, int relay3) {
DuyLionTran 32:8226837c56ae 460 MQTT::Message message;
DuyLionTran 32:8226837c56ae 461 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 32:8226837c56ae 462 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 47:c3707a4f69a7 463 // char timeBuf[50];
DuyLionTran 32:8226837c56ae 464
DuyLionTran 32:8226837c56ae 465 if (!client->isConnected()) {
DuyLionTran 32:8226837c56ae 466 printf ("---> MQTT DISCONNECTED\n\r");
DuyLionTran 32:8226837c56ae 467 return MQTT::FAILURE;
DuyLionTran 32:8226837c56ae 468 }
DuyLionTran 47:c3707a4f69a7 469 // strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 48:a7ed665844cd 470 sprintf(buf, "{\"type\":3,\"deviceId\":\"PROEVN\",\"time\":%d,\"cmdId\":%d,\"relay1\":%d,\"relay2\":%d,\"relay3\":%d}",
DuyLionTran 47:c3707a4f69a7 471 inputTime, commandID, relay1, relay2, relay3);
DuyLionTran 32:8226837c56ae 472 message.qos = MQTT::QOS0;
DuyLionTran 32:8226837c56ae 473 message.retained = false;
DuyLionTran 32:8226837c56ae 474 message.dup = false;
DuyLionTran 32:8226837c56ae 475 message.payload = (void*)buf;
DuyLionTran 32:8226837c56ae 476 message.payloadlen = strlen(buf);
DuyLionTran 32:8226837c56ae 477
DuyLionTran 32:8226837c56ae 478 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 32:8226837c56ae 479 printf("message too long!\r\n");
DuyLionTran 32:8226837c56ae 480
DuyLionTran 32:8226837c56ae 481 LOG("Publishing %s\n\r", buf);
DuyLionTran 32:8226837c56ae 482 return client->publish(pubTopic, message);
DuyLionTran 32:8226837c56ae 483 }
DuyLionTran 32:8226837c56ae 484
DuyLionTran 43:dcde0e66874a 485 int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode,
DuyLionTran 43:dcde0e66874a 486 uint8_t OxyThreshold, uint8_t TemperatureThreshold, uint16_t UploadInterval) {
DuyLionTran 32:8226837c56ae 487 MQTT::Message message;
DuyLionTran 32:8226837c56ae 488 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 32:8226837c56ae 489 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 47:c3707a4f69a7 490 // char timeBuf[50];
DuyLionTran 32:8226837c56ae 491
DuyLionTran 32:8226837c56ae 492 if (!client->isConnected()) {
DuyLionTran 32:8226837c56ae 493 printf ("---> MQTT DISCONNECTED\n\r");
DuyLionTran 32:8226837c56ae 494 return MQTT::FAILURE;
DuyLionTran 32:8226837c56ae 495 }
DuyLionTran 47:c3707a4f69a7 496 // strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 48:a7ed665844cd 497 sprintf(buf, "{\"type\":4,\"deviceId\":\"PROEVN\",\"time\":%d,\"cmdId\":%d,\"mode\":%d,\"OxygenThreshold\":%d,\"TemperatureThreshold\":%d,\"uploadInterval\":%d}",
DuyLionTran 47:c3707a4f69a7 498 inputTime, commandID, mode, OxyThreshold, TemperatureThreshold, UploadInterval);
DuyLionTran 32:8226837c56ae 499 message.qos = MQTT::QOS0;
DuyLionTran 32:8226837c56ae 500 message.retained = false;
DuyLionTran 32:8226837c56ae 501 message.dup = false;
DuyLionTran 32:8226837c56ae 502 message.payload = (void*)buf;
DuyLionTran 32:8226837c56ae 503 message.payloadlen = strlen(buf);
DuyLionTran 32:8226837c56ae 504
DuyLionTran 32:8226837c56ae 505 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 32:8226837c56ae 506 printf("message too long!\r\n");
DuyLionTran 32:8226837c56ae 507
DuyLionTran 32:8226837c56ae 508 LOG("Publishing %s\n\r", buf);
DuyLionTran 32:8226837c56ae 509 return client->publish(pubTopic, message);
DuyLionTran 32:8226837c56ae 510 }
DuyLionTran 32:8226837c56ae 511
DuyLionTran 32:8226837c56ae 512 int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct) {
DuyLionTran 32:8226837c56ae 513 int retVal;
DuyLionTran 32:8226837c56ae 514 switch (uploadType) {
DuyLionTran 36:d0b628087ac8 515 case (ADC_VALUE): retVal = MQTT_PublishADC(client, inputTime, uploadStruct.ADC_DOVal);
DuyLionTran 32:8226837c56ae 516 break;
DuyLionTran 43:dcde0e66874a 517 case (SENSOR_VALUE): retVal = MQTT_PublishSensorVal(client, inputTime, uploadStruct.SENSOR_TEMPVal ,uploadStruct.SENSOR_DOVal);
DuyLionTran 32:8226837c56ae 518 break;
DuyLionTran 36:d0b628087ac8 519 case (RELAY_STATE): retVal = MQTT_PublishRelayState(client, inputTime, uploadStruct.RELAY_State_1, uploadStruct.RELAY_State_2, uploadStruct.RELAY_State_3);
DuyLionTran 32:8226837c56ae 520 break;
DuyLionTran 43:dcde0e66874a 521 case (CONFIG_VALUE): retVal = MQTT_PublishConfigValue(client, inputTime, uploadStruct.CONFIG_Mode, uploadStruct.CONFIG_OxyThreshold, uploadStruct.CONFIG_TemperatureThreshold, uploadStruct.CONFIG_UploadInterval);
DuyLionTran 32:8226837c56ae 522 break;
DuyLionTran 32:8226837c56ae 523 default: break;
DuyLionTran 32:8226837c56ae 524 }
DuyLionTran 32:8226837c56ae 525 return retVal;
DuyLionTran 32:8226837c56ae 526 }
DuyLionTran 32:8226837c56ae 527
DuyLionTran 57:898fcb6692cd 528 int MQTT_PublishDeviceManage(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint32_t uploadPeriod, uint32_t sendTimePeriod) {
DuyLionTran 57:898fcb6692cd 529 MQTT::Message message;
DuyLionTran 57:898fcb6692cd 530 const char* pubTopic = MQTT_MANAGE_TOPIC;
DuyLionTran 57:898fcb6692cd 531 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 57:898fcb6692cd 532 char timeBuf[50];
DuyLionTran 57:898fcb6692cd 533
DuyLionTran 57:898fcb6692cd 534 if (!client->isConnected()) {
DuyLionTran 57:898fcb6692cd 535 printf ("---> MQTT DISCONNECTED\n\r");
DuyLionTran 57:898fcb6692cd 536 return MQTT::FAILURE;
DuyLionTran 57:898fcb6692cd 537 }
DuyLionTran 57:898fcb6692cd 538 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 57:898fcb6692cd 539 // sprintf(buf, "{\"d\":{\"metadata\":{\"Device ID\":\"PROEVN\",\"Time\":%s,\"Upload Interval\":%d,\"Send Time Frame Interval\":%d},\"supports\":{\"deviceActions\":true}}}",
DuyLionTran 57:898fcb6692cd 540 // timeBuf, uploadPeriod, sendTimePeriod);
DuyLionTran 57:898fcb6692cd 541 sprintf(buf, "{\"d\":{\"metadata\":{\"Upload Interval\":%d},\"supports\":{\"deviceActions\":true}}}",
DuyLionTran 57:898fcb6692cd 542 uploadPeriod);
DuyLionTran 57:898fcb6692cd 543 message.qos = MQTT::QOS0;
DuyLionTran 57:898fcb6692cd 544 message.retained = false;
DuyLionTran 57:898fcb6692cd 545 message.dup = false;
DuyLionTran 57:898fcb6692cd 546 message.payload = (void*)buf;
DuyLionTran 57:898fcb6692cd 547 message.payloadlen = strlen(buf);
DuyLionTran 57:898fcb6692cd 548
DuyLionTran 57:898fcb6692cd 549 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 57:898fcb6692cd 550 printf("message too long!\r\n");
DuyLionTran 57:898fcb6692cd 551
DuyLionTran 57:898fcb6692cd 552 LOG("Publishing %s\n\r", buf);
DuyLionTran 57:898fcb6692cd 553 return client->publish(pubTopic, message);
DuyLionTran 57:898fcb6692cd 554 }
DuyLionTran 57:898fcb6692cd 555
DuyLionTran 32:8226837c56ae 556 #endif /* __SIMPLEMQTT_H__ */