iot_water_monitor_v2

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

Committer:
DuyLionTran
Date:
Wed Feb 14 03:56:34 2018 +0000
Revision:
34:601effb7b7fe
Parent:
33:5b90257d2d57
Child:
35:fc800d35c1ba
* version 2.5 02-14-2017: 3rd relay is added

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 32:8226837c56ae 30 #define MQTT_MAX_PACKET_SIZE 400
DuyLionTran 32:8226837c56ae 31 #define MQTT_MAX_PAYLOAD_SIZE 300
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 32:8226837c56ae 42
DuyLionTran 32:8226837c56ae 43 struct UploadValue {
DuyLionTran 32:8226837c56ae 44 float ADC_PHVal;
DuyLionTran 32:8226837c56ae 45 float ADC_DOVal;
DuyLionTran 32:8226837c56ae 46
DuyLionTran 32:8226837c56ae 47 float SENSOR_PHVal;
DuyLionTran 32:8226837c56ae 48 float SENSOR_D0Val;
DuyLionTran 32:8226837c56ae 49
DuyLionTran 33:5b90257d2d57 50 uint8_t RELAY_State_1;
DuyLionTran 33:5b90257d2d57 51 uint8_t RELAY_State_2;
DuyLionTran 34:601effb7b7fe 52 uint8_t RELAY_State_3;
DuyLionTran 34:601effb7b7fe 53
DuyLionTran 33:5b90257d2d57 54 uint32_t CONFIG_Time;
DuyLionTran 32:8226837c56ae 55
DuyLionTran 32:8226837c56ae 56 uint8_t CONFIG_Mode;
DuyLionTran 32:8226837c56ae 57 uint8_t CONFIG_MinOxi;
DuyLionTran 32:8226837c56ae 58 uint8_t CONFIG_MaxOxi;
DuyLionTran 32:8226837c56ae 59 uint16_t CONFIG_UploadInterval;
DuyLionTran 32:8226837c56ae 60 } UploadValue;
DuyLionTran 32:8226837c56ae 61
DuyLionTran 32:8226837c56ae 62 char *projectName = "WaterMonitor";
DuyLionTran 32:8226837c56ae 63 static char id[30] = ID; // mac without colons
DuyLionTran 32:8226837c56ae 64 static char org[12] = ORG;
DuyLionTran 32:8226837c56ae 65 static char type[30] = TYPE;
DuyLionTran 32:8226837c56ae 66 static char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
DuyLionTran 32:8226837c56ae 67 static int connack_rc = 0; // MQTT connack return code
DuyLionTran 33:5b90257d2d57 68 static bool wifiConnected = true;
DuyLionTran 32:8226837c56ae 69 static bool netConnecting = false;
DuyLionTran 32:8226837c56ae 70 static bool mqttConnecting = false;
DuyLionTran 32:8226837c56ae 71 static bool netConnected = false;
DuyLionTran 32:8226837c56ae 72 static bool connected = false;
DuyLionTran 32:8226837c56ae 73 static int retryAttempt = 0;
DuyLionTran 32:8226837c56ae 74 static int connectTimeout = 1000;
DuyLionTran 32:8226837c56ae 75 uint16_t commandID = 0;
DuyLionTran 32:8226837c56ae 76 static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 77
DuyLionTran 32:8226837c56ae 78 extern struct UploadValue DataStruct;
DuyLionTran 32:8226837c56ae 79 /***************************************************************
DuyLionTran 32:8226837c56ae 80 * Unity function definitions
DuyLionTran 32:8226837c56ae 81 ***************************************************************/
DuyLionTran 32:8226837c56ae 82 void MQTT_MessageHandles(uint8_t ControlSignal);
DuyLionTran 32:8226837c56ae 83
DuyLionTran 32:8226837c56ae 84 /** brief Callback function when MQTT message arrives
DuyLionTran 32:8226837c56ae 85 * param[in] msgMQTT
DuyLionTran 32:8226837c56ae 86 * retral None
DuyLionTran 32:8226837c56ae 87 */
DuyLionTran 32:8226837c56ae 88 void MQTT_SubscribeCallback(MQTT::MessageData &msgMQTT);
DuyLionTran 32:8226837c56ae 89
DuyLionTran 32:8226837c56ae 90 /** brief Subscribe to a MQTT topic and set the MQTT callback function
DuyLionTran 32:8226837c56ae 91 * param[in] subscribeTopic Topic to be subscribed
DuyLionTran 32:8226837c56ae 92 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 93 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 94 */
DuyLionTran 32:8226837c56ae 95 int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, struct UploadValue uploadStruct);
DuyLionTran 32:8226837c56ae 96
DuyLionTran 32:8226837c56ae 97 /** brief Connect to the internet then the MQTT network
DuyLionTran 32:8226837c56ae 98 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 99 * param[in] mqttNetwork MQTT network
DuyLionTran 32:8226837c56ae 100 * param[in] network The internet network interface (ethernet, wifi...)
DuyLionTran 32:8226837c56ae 101 * retral Internet connect result and returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 102 */
DuyLionTran 32:8226837c56ae 103 int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct);
DuyLionTran 32:8226837c56ae 104
DuyLionTran 32:8226837c56ae 105 /** brief Setup the number of attempt to re-connect to the internet
DuyLionTran 32:8226837c56ae 106 * param[in] attemptNumber The number of attemp
DuyLionTran 32:8226837c56ae 107 */
DuyLionTran 32:8226837c56ae 108 int MQTT_GetConnTimeout(int attemptNumber);
DuyLionTran 32:8226837c56ae 109
DuyLionTran 32:8226837c56ae 110 /** brief Try to reconnect to the internet and MQTT network
DuyLionTran 32:8226837c56ae 111 * retral None
DuyLionTran 32:8226837c56ae 112 */
DuyLionTran 32:8226837c56ae 113 void MQTT_AttemptConnect(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 Publish ADC values to the server
DuyLionTran 32:8226837c56ae 116 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 117 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 118 * param[in] adcVal_0 The ADC value to be sent
DuyLionTran 32:8226837c56ae 119 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 120 */
DuyLionTran 32:8226837c56ae 121 int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0);
DuyLionTran 32:8226837c56ae 122
DuyLionTran 32:8226837c56ae 123 /** brief Publish Sensor values to the server
DuyLionTran 32:8226837c56ae 124 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 125 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 126 * param[in] pHVal The pHVal value to be sent
DuyLionTran 32:8226837c56ae 127 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 128 */
DuyLionTran 32:8226837c56ae 129 int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float pHVal);
DuyLionTran 32:8226837c56ae 130
DuyLionTran 32:8226837c56ae 131 /** brief Publish relay states to the server
DuyLionTran 32:8226837c56ae 132 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 133 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 134 * param[in] relay1 Relay 1 state
DuyLionTran 32:8226837c56ae 135 * param[in] relay2 Relay 2 state
DuyLionTran 32:8226837c56ae 136 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 137 */
DuyLionTran 32:8226837c56ae 138 int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2);
DuyLionTran 32:8226837c56ae 139
DuyLionTran 32:8226837c56ae 140 /** brief Publish relay states to the server
DuyLionTran 32:8226837c56ae 141 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 142 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 143 * param[in] mode current mode: automatic (0) or manual (1)
DuyLionTran 32:8226837c56ae 144 * param[in] maxOxi Maximum Oxygen value
DuyLionTran 32:8226837c56ae 145 * param[in] minOxi Minimum Oxygen value
DuyLionTran 32:8226837c56ae 146 // * param[in] uploadInterval Interval between upload turns
DuyLionTran 32:8226837c56ae 147 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 148 */
DuyLionTran 32:8226837c56ae 149 int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t minOxi, uint8_t maxOxi);
DuyLionTran 32:8226837c56ae 150
DuyLionTran 32:8226837c56ae 151 /** brief Upload all the data to the MQTT server
DuyLionTran 32:8226837c56ae 152 * param[in] client MQTT client
DuyLionTran 32:8226837c56ae 153 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 32:8226837c56ae 154 * param[in] uploadInterval The period between each upload moment
DuyLionTran 32:8226837c56ae 155 * retral returnCode from MQTTClient.h
DuyLionTran 32:8226837c56ae 156 */
DuyLionTran 32:8226837c56ae 157 int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct);
DuyLionTran 32:8226837c56ae 158
DuyLionTran 32:8226837c56ae 159 /********************************************************************************************************************************************************************************************/
DuyLionTran 32:8226837c56ae 160 /***************************************************************
DuyLionTran 32:8226837c56ae 161 * Unity function declarations
DuyLionTran 32:8226837c56ae 162 ***************************************************************/
DuyLionTran 32:8226837c56ae 163 void MQTT_SubscribeCallback(MQTT::MessageData &msgMQTT) {
DuyLionTran 32:8226837c56ae 164 // Message Handles
DuyLionTran 32:8226837c56ae 165 char msg[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 166 msg[0]='\0';
DuyLionTran 32:8226837c56ae 167 strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
DuyLionTran 32:8226837c56ae 168 printf ("--->>> MQTT_SubscribeCallback msg: %s\n\r", msg);
DuyLionTran 32:8226837c56ae 169 //{"type":"3","deviceId":"string"}
DuyLionTran 32:8226837c56ae 170 Json json(msg, msgMQTT.message.payloadlen);
DuyLionTran 32:8226837c56ae 171 if (!json.isValidJson()) {
DuyLionTran 32:8226837c56ae 172 printf("Invalid JSON: %s", msg);
DuyLionTran 32:8226837c56ae 173 }
DuyLionTran 32:8226837c56ae 174 else {
DuyLionTran 32:8226837c56ae 175 if (json.type(0) != JSMN_OBJECT ) {
DuyLionTran 32:8226837c56ae 176 printf("Invalid JSON. ROOT element is not Object: %s", msg);
DuyLionTran 32:8226837c56ae 177 }
DuyLionTran 32:8226837c56ae 178 else {
DuyLionTran 32:8226837c56ae 179 int CommandType;
DuyLionTran 32:8226837c56ae 180 int KeyIndex = json.findKeyIndexIn("type", 0);
DuyLionTran 32:8226837c56ae 181 int KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 182 int ret = json.tokenIntegerValue(KeyValueIndex, CommandType);
DuyLionTran 32:8226837c56ae 183
DuyLionTran 32:8226837c56ae 184 int receiveCmdID;
DuyLionTran 32:8226837c56ae 185 printf("Command Type: %d, error %d\r\n", CommandType, ret);
DuyLionTran 32:8226837c56ae 186
DuyLionTran 32:8226837c56ae 187 switch (CommandType) {
DuyLionTran 34:601effb7b7fe 188 case 3: int relayState1, relayState2, relayState3;
DuyLionTran 32:8226837c56ae 189 KeyIndex = json.findKeyIndexIn("cmdID", 0);
DuyLionTran 32:8226837c56ae 190 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 191 ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
DuyLionTran 32:8226837c56ae 192
DuyLionTran 32:8226837c56ae 193 KeyIndex = json.findKeyIndexIn("relayState1", 0);
DuyLionTran 32:8226837c56ae 194 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 33:5b90257d2d57 195 ret = json.tokenIntegerValue(KeyValueIndex, relayState1);
DuyLionTran 32:8226837c56ae 196
DuyLionTran 33:5b90257d2d57 197 KeyIndex = json.findKeyIndexIn("relayState2", 0);
DuyLionTran 33:5b90257d2d57 198 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 33:5b90257d2d57 199 ret = json.tokenIntegerValue(KeyValueIndex, relayState2);
DuyLionTran 33:5b90257d2d57 200
DuyLionTran 34:601effb7b7fe 201 KeyIndex = json.findKeyIndexIn("relayState3", 0);
DuyLionTran 34:601effb7b7fe 202 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 34:601effb7b7fe 203 ret = json.tokenIntegerValue(KeyValueIndex, relayState3);
DuyLionTran 34:601effb7b7fe 204
DuyLionTran 33:5b90257d2d57 205 DataStruct.RELAY_State_1 = relayState1;
DuyLionTran 33:5b90257d2d57 206 DataStruct.RELAY_State_2 = relayState2;
DuyLionTran 34:601effb7b7fe 207 DataStruct.RELAY_State_3 = relayState3;
DuyLionTran 34:601effb7b7fe 208
DuyLionTran 34:601effb7b7fe 209 FP_WriteRelayStates(DataStruct.RELAY_State_1, DataStruct.RELAY_State_2, DataStruct.RELAY_State_3);
DuyLionTran 34:601effb7b7fe 210 CE_HandleRelays(relayState1, relayState2, relayState3);
DuyLionTran 32:8226837c56ae 211 break;
DuyLionTran 32:8226837c56ae 212
DuyLionTran 33:5b90257d2d57 213 case 4: int mode, minOxiVal, maxOxiVal, uploadInterval;
DuyLionTran 32:8226837c56ae 214 KeyIndex = json.findKeyIndexIn("cmdID", 0);
DuyLionTran 32:8226837c56ae 215 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 216 ret = json.tokenIntegerValue(KeyValueIndex, receiveCmdID);
DuyLionTran 32:8226837c56ae 217
DuyLionTran 32:8226837c56ae 218 KeyIndex = json.findKeyIndexIn("mode", 0);
DuyLionTran 32:8226837c56ae 219 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 220 ret = json.tokenIntegerValue(KeyValueIndex, mode);
DuyLionTran 32:8226837c56ae 221
DuyLionTran 32:8226837c56ae 222 KeyIndex = json.findKeyIndexIn("minOxygenVal", 0);
DuyLionTran 32:8226837c56ae 223 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 224 ret = json.tokenIntegerValue(KeyValueIndex, minOxiVal);
DuyLionTran 32:8226837c56ae 225
DuyLionTran 32:8226837c56ae 226 KeyIndex = json.findKeyIndexIn("maxOxygenVal", 0);
DuyLionTran 32:8226837c56ae 227 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 32:8226837c56ae 228 ret = json.tokenIntegerValue(KeyValueIndex, maxOxiVal);
DuyLionTran 33:5b90257d2d57 229
DuyLionTran 33:5b90257d2d57 230 KeyIndex = json.findKeyIndexIn("uploadInterval", 0);
DuyLionTran 33:5b90257d2d57 231 KeyValueIndex = json.findChildIndexOf(KeyIndex, 0);
DuyLionTran 33:5b90257d2d57 232 ret = json.tokenIntegerValue(KeyValueIndex, uploadInterval);
DuyLionTran 32:8226837c56ae 233
DuyLionTran 33:5b90257d2d57 234 DataStruct.CONFIG_Mode = mode;
DuyLionTran 33:5b90257d2d57 235 DataStruct.CONFIG_MinOxi = minOxiVal;
DuyLionTran 33:5b90257d2d57 236 DataStruct.CONFIG_MaxOxi = maxOxiVal;
DuyLionTran 33:5b90257d2d57 237 DataStruct.CONFIG_UploadInterval = uploadInterval;
DuyLionTran 33:5b90257d2d57 238 FP_WriteConfigValues(DataStruct.CONFIG_Mode, DataStruct.CONFIG_MinOxi, DataStruct.CONFIG_MaxOxi, DataStruct.CONFIG_UploadInterval);
DuyLionTran 32:8226837c56ae 239 break;
DuyLionTran 32:8226837c56ae 240
DuyLionTran 32:8226837c56ae 241 default: break;
DuyLionTran 32:8226837c56ae 242 }
DuyLionTran 32:8226837c56ae 243 }
DuyLionTran 32:8226837c56ae 244 }
DuyLionTran 32:8226837c56ae 245 }
DuyLionTran 32:8226837c56ae 246
DuyLionTran 32:8226837c56ae 247 int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, struct UploadValue uploadStruct) {
DuyLionTran 32:8226837c56ae 248 return client->subscribe(subscribeTopic, MQTT::QOS1, MQTT_SubscribeCallback);
DuyLionTran 32:8226837c56ae 249 }
DuyLionTran 32:8226837c56ae 250
DuyLionTran 32:8226837c56ae 251 int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct) {
DuyLionTran 32:8226837c56ae 252 const char* iot_ibm = MQTT_BROKER_URL;
DuyLionTran 32:8226837c56ae 253 char hostname[strlen(org) + strlen(iot_ibm) + 1];
DuyLionTran 32:8226837c56ae 254
DuyLionTran 32:8226837c56ae 255 sprintf(hostname, "%s%s", org, iot_ibm);
DuyLionTran 32:8226837c56ae 256 // Construct clientId - d:org:type:id
DuyLionTran 32:8226837c56ae 257 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
DuyLionTran 32:8226837c56ae 258 sprintf(clientId, "d:%s:%s:%s", org, type, id);
DuyLionTran 32:8226837c56ae 259 sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, DEFAULT_TYPE_NAME);
DuyLionTran 32:8226837c56ae 260
DuyLionTran 32:8226837c56ae 261 // Network debug statements
DuyLionTran 32:8226837c56ae 262 LOG("=====================================\n\r");
DuyLionTran 32:8226837c56ae 263 LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
DuyLionTran 32:8226837c56ae 264 LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
DuyLionTran 32:8226837c56ae 265 LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
DuyLionTran 32:8226837c56ae 266 LOG("Client ID: %s\n\r", clientId);
DuyLionTran 32:8226837c56ae 267 LOG("Topic: %s\n\r",MQTT_EVENT_TOPIC);
DuyLionTran 32:8226837c56ae 268 LOG("Subscription URL: %s\n\r", subscription_url);
DuyLionTran 32:8226837c56ae 269 LOG("=====================================\n\r");
DuyLionTran 32:8226837c56ae 270 netConnecting = true;
DuyLionTran 32:8226837c56ae 271 int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
DuyLionTran 32:8226837c56ae 272 if (rc != 0) {
DuyLionTran 32:8226837c56ae 273 printf("rc from TCP connect is %d\r\n", rc);
DuyLionTran 32:8226837c56ae 274 return rc;
DuyLionTran 32:8226837c56ae 275 }
DuyLionTran 32:8226837c56ae 276
DuyLionTran 32:8226837c56ae 277 printf ("--->TCP Connected\n\r");
DuyLionTran 32:8226837c56ae 278 netConnected = true;
DuyLionTran 32:8226837c56ae 279 netConnecting = false;
DuyLionTran 32:8226837c56ae 280
DuyLionTran 32:8226837c56ae 281 // MQTT Connect
DuyLionTran 32:8226837c56ae 282 mqttConnecting = true;
DuyLionTran 32:8226837c56ae 283 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
DuyLionTran 32:8226837c56ae 284 data.MQTTVersion = 4;
DuyLionTran 32:8226837c56ae 285 data.struct_version = 0;
DuyLionTran 32:8226837c56ae 286 data.clientID.cstring = clientId;
DuyLionTran 32:8226837c56ae 287 data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec
DuyLionTran 32:8226837c56ae 288 data.username.cstring = AUTH_METHOD;
DuyLionTran 32:8226837c56ae 289 data.password.cstring = auth_token;
DuyLionTran 32:8226837c56ae 290 printf ("AutToken: %s\n\r", auth_token);
DuyLionTran 32:8226837c56ae 291
DuyLionTran 32:8226837c56ae 292 if ((rc = client->connect(data)) != 0) {
DuyLionTran 32:8226837c56ae 293 printf("rc from MQTT connect is %d\r\n", rc);
DuyLionTran 32:8226837c56ae 294 connack_rc = rc;
DuyLionTran 32:8226837c56ae 295 return rc;
DuyLionTran 32:8226837c56ae 296 }
DuyLionTran 32:8226837c56ae 297 connected = true;
DuyLionTran 32:8226837c56ae 298 printf ("--->MQTT Connected\n\r");
DuyLionTran 32:8226837c56ae 299 if ((rc = MQTT_Subscribe(MQTT_COMMAND_TOPIC, client, uploadStruct)) == 0) {
DuyLionTran 32:8226837c56ae 300 LOG ("--->>>MQTT subscribed to: %s\n\r", MQTT_COMMAND_TOPIC);
DuyLionTran 32:8226837c56ae 301 } else {
DuyLionTran 32:8226837c56ae 302 LOG ("--->>>ERROR MQTT subscribe : %s\n\r", MQTT_COMMAND_TOPIC);
DuyLionTran 32:8226837c56ae 303 }
DuyLionTran 32:8226837c56ae 304 mqttConnecting = false;
DuyLionTran 32:8226837c56ae 305 connack_rc = rc;
DuyLionTran 32:8226837c56ae 306 return rc;
DuyLionTran 32:8226837c56ae 307 }
DuyLionTran 32:8226837c56ae 308
DuyLionTran 32:8226837c56ae 309
DuyLionTran 32:8226837c56ae 310 int MQTT_GetConnTimeout(int attemptNumber) { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
DuyLionTran 32:8226837c56ae 311 // after 20 attempts, retry every 10 minutes
DuyLionTran 32:8226837c56ae 312 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
DuyLionTran 32:8226837c56ae 313 }
DuyLionTran 32:8226837c56ae 314
DuyLionTran 32:8226837c56ae 315
DuyLionTran 32:8226837c56ae 316 void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network, struct UploadValue uploadStruct) {
DuyLionTran 32:8226837c56ae 317 connected = false;
DuyLionTran 32:8226837c56ae 318
DuyLionTran 32:8226837c56ae 319 while (MQTT_Connect(client, mqttNetwork, network, uploadStruct) != MQTT_CONNECTION_ACCEPTED) {
DuyLionTran 32:8226837c56ae 320 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
DuyLionTran 32:8226837c56ae 321 printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
DuyLionTran 32:8226837c56ae 322 return; // don't reattempt to connect if credentials are wrong
DuyLionTran 32:8226837c56ae 323 }
DuyLionTran 32:8226837c56ae 324 int timeout = MQTT_GetConnTimeout(++retryAttempt);
DuyLionTran 32:8226837c56ae 325 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
DuyLionTran 32:8226837c56ae 326
DuyLionTran 32:8226837c56ae 327 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
DuyLionTran 32:8226837c56ae 328 // or maybe just add the proper members to do this disconnect and call MQTT_AttemptConnect(...)
DuyLionTran 32:8226837c56ae 329 // this works - reset the system when the retry count gets to a threshold
DuyLionTran 32:8226837c56ae 330 if (retryAttempt == 5)
DuyLionTran 32:8226837c56ae 331 NVIC_SystemReset();
DuyLionTran 32:8226837c56ae 332 else
DuyLionTran 32:8226837c56ae 333 wait(timeout);
DuyLionTran 32:8226837c56ae 334 }
DuyLionTran 32:8226837c56ae 335 }
DuyLionTran 32:8226837c56ae 336
DuyLionTran 32:8226837c56ae 337 int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0) {
DuyLionTran 32:8226837c56ae 338 MQTT::Message message;
DuyLionTran 32:8226837c56ae 339 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 32:8226837c56ae 340
DuyLionTran 32:8226837c56ae 341 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 342 char timeBuf[50];
DuyLionTran 32:8226837c56ae 343
DuyLionTran 32:8226837c56ae 344 if (!client->isConnected()) {
DuyLionTran 32:8226837c56ae 345 printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
DuyLionTran 32:8226837c56ae 346 }
DuyLionTran 32:8226837c56ae 347
DuyLionTran 32:8226837c56ae 348 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 32:8226837c56ae 349 sprintf(buf, "{\"type\":1,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"adc0\":%.1f}",
DuyLionTran 32:8226837c56ae 350 timeBuf, commandID, adcVal_0);
DuyLionTran 32:8226837c56ae 351 message.qos = MQTT::QOS0;
DuyLionTran 32:8226837c56ae 352 message.retained = false;
DuyLionTran 32:8226837c56ae 353 message.dup = false;
DuyLionTran 32:8226837c56ae 354 message.payload = (void*)buf;
DuyLionTran 32:8226837c56ae 355 message.payloadlen = strlen(buf);
DuyLionTran 32:8226837c56ae 356
DuyLionTran 32:8226837c56ae 357 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 32:8226837c56ae 358 printf("message too long!\r\n");
DuyLionTran 32:8226837c56ae 359
DuyLionTran 32:8226837c56ae 360 LOG("Publishing %s\n\r", buf);
DuyLionTran 32:8226837c56ae 361 return client->publish(pubTopic, message);
DuyLionTran 32:8226837c56ae 362 }
DuyLionTran 32:8226837c56ae 363
DuyLionTran 32:8226837c56ae 364 int MQTT_PublishSensorVal(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float pHVal) {
DuyLionTran 32:8226837c56ae 365 MQTT::Message message;
DuyLionTran 32:8226837c56ae 366 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 32:8226837c56ae 367
DuyLionTran 32:8226837c56ae 368 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 369 char timeBuf[50];
DuyLionTran 32:8226837c56ae 370
DuyLionTran 32:8226837c56ae 371 if (!client->isConnected()) {
DuyLionTran 32:8226837c56ae 372 printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
DuyLionTran 32:8226837c56ae 373 }
DuyLionTran 32:8226837c56ae 374
DuyLionTran 32:8226837c56ae 375 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 32:8226837c56ae 376 sprintf(buf, "{\"type\":2,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"pH0\":%.1f}",
DuyLionTran 32:8226837c56ae 377 timeBuf, commandID, pHVal);
DuyLionTran 32:8226837c56ae 378 message.qos = MQTT::QOS0;
DuyLionTran 32:8226837c56ae 379 message.retained = false;
DuyLionTran 32:8226837c56ae 380 message.dup = false;
DuyLionTran 32:8226837c56ae 381 message.payload = (void*)buf;
DuyLionTran 32:8226837c56ae 382 message.payloadlen = strlen(buf);
DuyLionTran 32:8226837c56ae 383
DuyLionTran 32:8226837c56ae 384 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 32:8226837c56ae 385 printf("message too long!\r\n");
DuyLionTran 32:8226837c56ae 386
DuyLionTran 32:8226837c56ae 387 LOG("Publishing %s\n\r", buf);
DuyLionTran 32:8226837c56ae 388 return client->publish(pubTopic, message);
DuyLionTran 32:8226837c56ae 389 }
DuyLionTran 32:8226837c56ae 390
DuyLionTran 32:8226837c56ae 391 int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2) {
DuyLionTran 32:8226837c56ae 392 MQTT::Message message;
DuyLionTran 32:8226837c56ae 393 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 32:8226837c56ae 394 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 395 char timeBuf[50];
DuyLionTran 32:8226837c56ae 396
DuyLionTran 32:8226837c56ae 397 if (!client->isConnected()) {
DuyLionTran 32:8226837c56ae 398 printf ("---> MQTT DISCONNECTED\n\r");
DuyLionTran 32:8226837c56ae 399 return MQTT::FAILURE;
DuyLionTran 32:8226837c56ae 400 }
DuyLionTran 32:8226837c56ae 401 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 32:8226837c56ae 402 sprintf(buf, "{\"type\":3,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"relay1\":%d,\"relay2\":%d}",
DuyLionTran 32:8226837c56ae 403 timeBuf, commandID, relay1, relay2);
DuyLionTran 32:8226837c56ae 404 message.qos = MQTT::QOS0;
DuyLionTran 32:8226837c56ae 405 message.retained = false;
DuyLionTran 32:8226837c56ae 406 message.dup = false;
DuyLionTran 32:8226837c56ae 407 message.payload = (void*)buf;
DuyLionTran 32:8226837c56ae 408 message.payloadlen = strlen(buf);
DuyLionTran 32:8226837c56ae 409
DuyLionTran 32:8226837c56ae 410 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 32:8226837c56ae 411 printf("message too long!\r\n");
DuyLionTran 32:8226837c56ae 412
DuyLionTran 32:8226837c56ae 413 LOG("Publishing %s\n\r", buf);
DuyLionTran 32:8226837c56ae 414 return client->publish(pubTopic, message);
DuyLionTran 32:8226837c56ae 415 }
DuyLionTran 32:8226837c56ae 416
DuyLionTran 32:8226837c56ae 417 int MQTT_PublishConfigValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t mode, uint8_t minOxi, uint8_t maxOxi) {
DuyLionTran 32:8226837c56ae 418 MQTT::Message message;
DuyLionTran 32:8226837c56ae 419 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 32:8226837c56ae 420 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 32:8226837c56ae 421 char timeBuf[50];
DuyLionTran 32:8226837c56ae 422
DuyLionTran 32:8226837c56ae 423 if (!client->isConnected()) {
DuyLionTran 32:8226837c56ae 424 printf ("---> MQTT DISCONNECTED\n\r");
DuyLionTran 32:8226837c56ae 425 return MQTT::FAILURE;
DuyLionTran 32:8226837c56ae 426 }
DuyLionTran 32:8226837c56ae 427 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 32:8226837c56ae 428 sprintf(buf, "{\"type\":4,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"mode\":%d,\"minOxygenVal\":%d,\"maxOxygenVal\":%d}",
DuyLionTran 32:8226837c56ae 429 timeBuf, commandID, mode, minOxi, maxOxi);
DuyLionTran 32:8226837c56ae 430 message.qos = MQTT::QOS0;
DuyLionTran 32:8226837c56ae 431 message.retained = false;
DuyLionTran 32:8226837c56ae 432 message.dup = false;
DuyLionTran 32:8226837c56ae 433 message.payload = (void*)buf;
DuyLionTran 32:8226837c56ae 434 message.payloadlen = strlen(buf);
DuyLionTran 32:8226837c56ae 435
DuyLionTran 32:8226837c56ae 436 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 32:8226837c56ae 437 printf("message too long!\r\n");
DuyLionTran 32:8226837c56ae 438
DuyLionTran 32:8226837c56ae 439 LOG("Publishing %s\n\r", buf);
DuyLionTran 32:8226837c56ae 440 return client->publish(pubTopic, message);
DuyLionTran 32:8226837c56ae 441 }
DuyLionTran 32:8226837c56ae 442
DuyLionTran 32:8226837c56ae 443 int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct) {
DuyLionTran 32:8226837c56ae 444 int retVal;
DuyLionTran 32:8226837c56ae 445 switch (uploadType) {
DuyLionTran 32:8226837c56ae 446 case (ADC_VALUE): retVal = MQTT_PublishADC(client, inputTime, uploadStruct.ADC_PHVal);
DuyLionTran 32:8226837c56ae 447 break;
DuyLionTran 32:8226837c56ae 448 case (SENSOR_VALUE): retVal = MQTT_PublishSensorVal(client, inputTime, uploadStruct.SENSOR_PHVal);
DuyLionTran 32:8226837c56ae 449 break;
DuyLionTran 32:8226837c56ae 450 case (RELAY_STATE): retVal = MQTT_PublishRelayState(client, inputTime, uploadStruct.RELAY_State_1, uploadStruct.RELAY_State_2);
DuyLionTran 32:8226837c56ae 451 break;
DuyLionTran 32:8226837c56ae 452 case (CONFIG_VALUE): retVal = MQTT_PublishConfigValue(client, inputTime, uploadStruct.CONFIG_Mode, uploadStruct.CONFIG_MinOxi, uploadStruct.CONFIG_MaxOxi);
DuyLionTran 32:8226837c56ae 453 break;
DuyLionTran 32:8226837c56ae 454 default: break;
DuyLionTran 32:8226837c56ae 455 }
DuyLionTran 32:8226837c56ae 456 return retVal;
DuyLionTran 32:8226837c56ae 457 }
DuyLionTran 32:8226837c56ae 458
DuyLionTran 32:8226837c56ae 459 #endif /* __SIMPLEMQTT_H__ */