iot_water_monitor_v2

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

Committer:
DuyLionTran
Date:
Fri Dec 29 19:20:23 2017 +0000
Revision:
25:cf7a3e31622a
Parent:
24:3de3cf978e60
Child:
26:f40cc4d011b0
version 1.8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 24:3de3cf978e60 1 #ifndef __SIMPLEMQTT_H__
DuyLionTran 24:3de3cf978e60 2 #define __SIMPLEMQTT_H__
DuyLionTran 24:3de3cf978e60 3
DuyLionTran 24:3de3cf978e60 4 /***************************************************************
DuyLionTran 24:3de3cf978e60 5 * Includes
DuyLionTran 24:3de3cf978e60 6 ***************************************************************/
DuyLionTran 24:3de3cf978e60 7 #include "easy-connect.h"
DuyLionTran 24:3de3cf978e60 8 #include "MQTTClient.h"
DuyLionTran 24:3de3cf978e60 9 #include "NDefLib/NDefNfcTag.h"
DuyLionTran 24:3de3cf978e60 10 #include "NDefLib/RecordType/RecordURI.h"
DuyLionTran 24:3de3cf978e60 11 #include "MQTTNetwork.h"
DuyLionTran 24:3de3cf978e60 12 #include "MQTTmbed.h"
DuyLionTran 24:3de3cf978e60 13
DuyLionTran 24:3de3cf978e60 14 /***************************************************************
DuyLionTran 24:3de3cf978e60 15 * Definitions
DuyLionTran 24:3de3cf978e60 16 ***************************************************************/
DuyLionTran 24:3de3cf978e60 17 // Configuration values needed to connect to IBM IoT Cloud
DuyLionTran 24:3de3cf978e60 18 #define ORG MQTT_ORG_ID // connect to ORG.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
DuyLionTran 24:3de3cf978e60 19 #define ID MQTT_DEVICE_ID // For a registered connection is your device id
DuyLionTran 24:3de3cf978e60 20 #define AUTH_TOKEN MQTT_DEVICE_PASSWORD // For a registered connection is a device auth-token
DuyLionTran 24:3de3cf978e60 21 #define DEFAULT_TYPE_NAME MQTT_DEVICE_TYPE // For a registered connection is device type
DuyLionTran 24:3de3cf978e60 22 #define AUTH_METHOD MQTT_USERNAME
DuyLionTran 24:3de3cf978e60 23
DuyLionTran 24:3de3cf978e60 24 #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
DuyLionTran 24:3de3cf978e60 25 #define IBM_IOT_PORT MQTT_PORT
DuyLionTran 24:3de3cf978e60 26
DuyLionTran 24:3de3cf978e60 27 #define MQTT_MAX_PACKET_SIZE 400
DuyLionTran 24:3de3cf978e60 28 #define MQTT_MAX_PAYLOAD_SIZE 300
DuyLionTran 24:3de3cf978e60 29
DuyLionTran 24:3de3cf978e60 30 /***************************************************************
DuyLionTran 24:3de3cf978e60 31 * Variables
DuyLionTran 24:3de3cf978e60 32 ***************************************************************/
DuyLionTran 24:3de3cf978e60 33 typedef enum {
DuyLionTran 24:3de3cf978e60 34 ADC_VALUE = 0,
DuyLionTran 24:3de3cf978e60 35 SENSOR_VALUE,
DuyLionTran 24:3de3cf978e60 36 RELAY_STATE,
DuyLionTran 24:3de3cf978e60 37 CONFIG_VALUE
DuyLionTran 24:3de3cf978e60 38 } UploadType;
DuyLionTran 24:3de3cf978e60 39
DuyLionTran 24:3de3cf978e60 40 struct UploadValue {
DuyLionTran 24:3de3cf978e60 41 float ADC_PHVal;
DuyLionTran 24:3de3cf978e60 42 float ADC_DOVal;
DuyLionTran 24:3de3cf978e60 43
DuyLionTran 25:cf7a3e31622a 44 float SENSOR_PHVal;
DuyLionTran 25:cf7a3e31622a 45 float SENSOR_DOVal;
DuyLionTran 25:cf7a3e31622a 46
DuyLionTran 24:3de3cf978e60 47 int RELAY_State_1;
DuyLionTran 24:3de3cf978e60 48 int RELAY_State_2;
DuyLionTran 24:3de3cf978e60 49
DuyLionTran 24:3de3cf978e60 50 uint8_t CONFIG_Mode;
DuyLionTran 24:3de3cf978e60 51 uint8_t CONFIG_MinOxi;
DuyLionTran 24:3de3cf978e60 52 uint8_t CONFIG_MaxOxi;
DuyLionTran 24:3de3cf978e60 53 uint16_t CONFIG_UploadInterval;
DuyLionTran 24:3de3cf978e60 54 } UploadValue;
DuyLionTran 24:3de3cf978e60 55
DuyLionTran 24:3de3cf978e60 56 char *projectName = "WaterMonitor";
DuyLionTran 24:3de3cf978e60 57 static char id[30] = ID; // mac without colons
DuyLionTran 24:3de3cf978e60 58 static char org[12] = ORG;
DuyLionTran 24:3de3cf978e60 59 static char type[30] = TYPE;
DuyLionTran 24:3de3cf978e60 60 static char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
DuyLionTran 24:3de3cf978e60 61 static int connack_rc = 0; // MQTT connack return code
DuyLionTran 24:3de3cf978e60 62 static bool netConnecting = false;
DuyLionTran 24:3de3cf978e60 63 static bool mqttConnecting = false;
DuyLionTran 24:3de3cf978e60 64 static bool netConnected = false;
DuyLionTran 24:3de3cf978e60 65 static bool connected = false;
DuyLionTran 24:3de3cf978e60 66 static int retryAttempt = 0;
DuyLionTran 24:3de3cf978e60 67 static int connectTimeout = 1000;
DuyLionTran 24:3de3cf978e60 68 uint16_t commandID = 0;
DuyLionTran 24:3de3cf978e60 69 static char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 24:3de3cf978e60 70
DuyLionTran 24:3de3cf978e60 71 /***************************************************************
DuyLionTran 24:3de3cf978e60 72 * Unity function definitions
DuyLionTran 24:3de3cf978e60 73 ***************************************************************/
DuyLionTran 24:3de3cf978e60 74 /** brief Callback function when MQTT message arrives
DuyLionTran 24:3de3cf978e60 75 * param[in] msgMQTT
DuyLionTran 24:3de3cf978e60 76 * retral None
DuyLionTran 24:3de3cf978e60 77 */
DuyLionTran 24:3de3cf978e60 78 void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT);
DuyLionTran 24:3de3cf978e60 79
DuyLionTran 24:3de3cf978e60 80 /** brief Subscribe to a MQTT topic and set the MQTT callback function
DuyLionTran 24:3de3cf978e60 81 * param[in] subscribeTopic Topic to be subscribed
DuyLionTran 24:3de3cf978e60 82 * param[in] client MQTT client
DuyLionTran 24:3de3cf978e60 83 * retral returnCode from MQTTClient.h
DuyLionTran 24:3de3cf978e60 84 */
DuyLionTran 24:3de3cf978e60 85 int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client);
DuyLionTran 24:3de3cf978e60 86
DuyLionTran 24:3de3cf978e60 87 /** brief Connect to the internet then the MQTT network
DuyLionTran 24:3de3cf978e60 88 * param[in] client MQTT client
DuyLionTran 24:3de3cf978e60 89 * param[in] mqttNetwork MQTT network
DuyLionTran 24:3de3cf978e60 90 * param[in] network The internet network interface (ethernet, wifi...)
DuyLionTran 24:3de3cf978e60 91 * retral Internet connect result and returnCode from MQTTClient.h
DuyLionTran 24:3de3cf978e60 92 */
DuyLionTran 24:3de3cf978e60 93 int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
DuyLionTran 24:3de3cf978e60 94
DuyLionTran 24:3de3cf978e60 95 /** brief Setup the number of attempt to re-connect to the internet
DuyLionTran 24:3de3cf978e60 96 * param[in] attemptNumber The number of attemp
DuyLionTran 24:3de3cf978e60 97 */
DuyLionTran 24:3de3cf978e60 98 int MQTT_GetConnTimeout(int attemptNumber);
DuyLionTran 24:3de3cf978e60 99
DuyLionTran 24:3de3cf978e60 100 /** brief Try to reconnect to the internet and MQTT network
DuyLionTran 24:3de3cf978e60 101 * retral None
DuyLionTran 24:3de3cf978e60 102 */
DuyLionTran 24:3de3cf978e60 103 void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
DuyLionTran 24:3de3cf978e60 104
DuyLionTran 24:3de3cf978e60 105 /** brief Publish ADC values to the server
DuyLionTran 24:3de3cf978e60 106 * param[in] client MQTT client
DuyLionTran 24:3de3cf978e60 107 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 24:3de3cf978e60 108 * param[in] adcVal_0 The ADC value to be sent
DuyLionTran 24:3de3cf978e60 109 * retral returnCode from MQTTClient.h
DuyLionTran 24:3de3cf978e60 110 */
DuyLionTran 24:3de3cf978e60 111 int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0);
DuyLionTran 24:3de3cf978e60 112
DuyLionTran 25:cf7a3e31622a 113 /** brief Publish sensor values to the server
DuyLionTran 25:cf7a3e31622a 114 * param[in] client MQTT client
DuyLionTran 25:cf7a3e31622a 115 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 25:cf7a3e31622a 116 * param[in] pH pH Value
DuyLionTran 25:cf7a3e31622a 117 * param[in] DO Oxygen value
DuyLionTran 25:cf7a3e31622a 118 * retral returnCode from MQTTClient.h
DuyLionTran 25:cf7a3e31622a 119 */
DuyLionTran 25:cf7a3e31622a 120 int MQTT_PublishSensorValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float pH, float DO);
DuyLionTran 25:cf7a3e31622a 121
DuyLionTran 24:3de3cf978e60 122 /** brief Publish relay states to the server
DuyLionTran 24:3de3cf978e60 123 * param[in] client MQTT client
DuyLionTran 24:3de3cf978e60 124 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 24:3de3cf978e60 125 * param[in] relay1 Relay 1 state
DuyLionTran 24:3de3cf978e60 126 * param[in] relay2 Relay 2 state
DuyLionTran 24:3de3cf978e60 127 * retral returnCode from MQTTClient.h
DuyLionTran 24:3de3cf978e60 128 */
DuyLionTran 24:3de3cf978e60 129 int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2);
DuyLionTran 24:3de3cf978e60 130
DuyLionTran 25:cf7a3e31622a 131 /** brief Publish configuration values to the server
DuyLionTran 24:3de3cf978e60 132 * param[in] client MQTT client
DuyLionTran 24:3de3cf978e60 133 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 24:3de3cf978e60 134 * param[in] mode current mode: automatic (0) or manual (1)
DuyLionTran 24:3de3cf978e60 135 * param[in] maxOxi Maximum Oxygen value
DuyLionTran 24:3de3cf978e60 136 * param[in] minOxi Minimum Oxygen value
DuyLionTran 24:3de3cf978e60 137 // * param[in] uploadInterval Interval between upload turns
DuyLionTran 24:3de3cf978e60 138 * retral returnCode from MQTTClient.h
DuyLionTran 24:3de3cf978e60 139 */
DuyLionTran 24:3de3cf978e60 140 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 24:3de3cf978e60 141
DuyLionTran 24:3de3cf978e60 142 /** brief Upload all the data to the MQTT server
DuyLionTran 24:3de3cf978e60 143 * param[in] client MQTT client
DuyLionTran 24:3de3cf978e60 144 * param[in] inputTime The time when the data is attempt to be sent
DuyLionTran 24:3de3cf978e60 145 * param[in] uploadInterval The period between each upload moment
DuyLionTran 24:3de3cf978e60 146 * retral returnCode from MQTTClient.h
DuyLionTran 24:3de3cf978e60 147 */
DuyLionTran 24:3de3cf978e60 148 int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct);
DuyLionTran 24:3de3cf978e60 149
DuyLionTran 24:3de3cf978e60 150 /********************************************************************************************************************************************************************************************/
DuyLionTran 24:3de3cf978e60 151 /***************************************************************
DuyLionTran 24:3de3cf978e60 152 * Unity function declarations
DuyLionTran 24:3de3cf978e60 153 ***************************************************************/
DuyLionTran 24:3de3cf978e60 154 void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT) {
DuyLionTran 24:3de3cf978e60 155 char msg[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 24:3de3cf978e60 156 msg[0]='\0';
DuyLionTran 24:3de3cf978e60 157 strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
DuyLionTran 24:3de3cf978e60 158 printf ("--->>> MQTT_SubscribeCallback msg: %s\n\r", msg);
DuyLionTran 24:3de3cf978e60 159 }
DuyLionTran 24:3de3cf978e60 160
DuyLionTran 24:3de3cf978e60 161 int MQTT_Subscribe(char *subscribeTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) {
DuyLionTran 24:3de3cf978e60 162 return client->subscribe(subscribeTopic, MQTT::QOS1, MQTT_SubscribeCallback);
DuyLionTran 24:3de3cf978e60 163 }
DuyLionTran 24:3de3cf978e60 164
DuyLionTran 24:3de3cf978e60 165 int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
DuyLionTran 24:3de3cf978e60 166 const char* iot_ibm = MQTT_BROKER_URL;
DuyLionTran 24:3de3cf978e60 167 char hostname[strlen(org) + strlen(iot_ibm) + 1];
DuyLionTran 24:3de3cf978e60 168
DuyLionTran 24:3de3cf978e60 169 sprintf(hostname, "%s%s", org, iot_ibm);
DuyLionTran 24:3de3cf978e60 170 // Construct clientId - d:org:type:id
DuyLionTran 24:3de3cf978e60 171 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
DuyLionTran 24:3de3cf978e60 172 sprintf(clientId, "d:%s:%s:%s", org, type, id);
DuyLionTran 24:3de3cf978e60 173 sprintf(subscription_url, "%s.%s/#/device/%s/%s/", org, "internetofthings.ibmcloud.com", id, DEFAULT_TYPE_NAME);
DuyLionTran 24:3de3cf978e60 174
DuyLionTran 24:3de3cf978e60 175 // Network debug statements
DuyLionTran 24:3de3cf978e60 176 LOG("=====================================\n\r");
DuyLionTran 24:3de3cf978e60 177 LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
DuyLionTran 24:3de3cf978e60 178 LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
DuyLionTran 24:3de3cf978e60 179 LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
DuyLionTran 24:3de3cf978e60 180 LOG("Client ID: %s\n\r", clientId);
DuyLionTran 24:3de3cf978e60 181 LOG("Topic: %s\n\r",MQTT_EVENT_TOPIC);
DuyLionTran 24:3de3cf978e60 182 LOG("Subscription URL: %s\n\r", subscription_url);
DuyLionTran 24:3de3cf978e60 183 LOG("=====================================\n\r");
DuyLionTran 24:3de3cf978e60 184 netConnecting = true;
DuyLionTran 24:3de3cf978e60 185 int rc = mqttNetwork->connect(hostname, IBM_IOT_PORT);
DuyLionTran 24:3de3cf978e60 186 if (rc != 0) {
DuyLionTran 24:3de3cf978e60 187 printf("rc from TCP connect is %d\r\n", rc);
DuyLionTran 24:3de3cf978e60 188 return rc;
DuyLionTran 24:3de3cf978e60 189 }
DuyLionTran 24:3de3cf978e60 190
DuyLionTran 24:3de3cf978e60 191 printf ("--->TCP Connected\n\r");
DuyLionTran 24:3de3cf978e60 192 netConnected = true;
DuyLionTran 24:3de3cf978e60 193 netConnecting = false;
DuyLionTran 24:3de3cf978e60 194
DuyLionTran 24:3de3cf978e60 195 // MQTT Connect
DuyLionTran 24:3de3cf978e60 196 mqttConnecting = true;
DuyLionTran 24:3de3cf978e60 197 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
DuyLionTran 24:3de3cf978e60 198 data.MQTTVersion = 4;
DuyLionTran 24:3de3cf978e60 199 data.struct_version = 0;
DuyLionTran 24:3de3cf978e60 200 data.clientID.cstring = clientId;
DuyLionTran 24:3de3cf978e60 201 data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec
DuyLionTran 24:3de3cf978e60 202 data.username.cstring = AUTH_METHOD;
DuyLionTran 24:3de3cf978e60 203 data.password.cstring = auth_token;
DuyLionTran 24:3de3cf978e60 204 printf ("AutToken: %s\n\r", auth_token);
DuyLionTran 24:3de3cf978e60 205
DuyLionTran 24:3de3cf978e60 206 if ((rc = client->connect(data)) != 0) {
DuyLionTran 24:3de3cf978e60 207 printf("rc from MQTT connect is %d\r\n", rc);
DuyLionTran 24:3de3cf978e60 208 connack_rc = rc;
DuyLionTran 24:3de3cf978e60 209 return rc;
DuyLionTran 24:3de3cf978e60 210 }
DuyLionTran 24:3de3cf978e60 211 connected = true;
DuyLionTran 24:3de3cf978e60 212 printf ("--->MQTT Connected\n\r");
DuyLionTran 24:3de3cf978e60 213 if ((rc = MQTT_Subscribe(MQTT_COMMAND_TOPIC, client)) == 0) {
DuyLionTran 24:3de3cf978e60 214 LOG ("--->>>MQTT subscribed to: %s\n\r", MQTT_COMMAND_TOPIC);
DuyLionTran 24:3de3cf978e60 215 } else {
DuyLionTran 24:3de3cf978e60 216 LOG ("--->>>ERROR MQTT subscribe : %s\n\r", MQTT_COMMAND_TOPIC);
DuyLionTran 24:3de3cf978e60 217 }
DuyLionTran 24:3de3cf978e60 218 mqttConnecting = false;
DuyLionTran 24:3de3cf978e60 219 connack_rc = rc;
DuyLionTran 24:3de3cf978e60 220 return rc;
DuyLionTran 24:3de3cf978e60 221 }
DuyLionTran 24:3de3cf978e60 222
DuyLionTran 24:3de3cf978e60 223
DuyLionTran 24:3de3cf978e60 224 int MQTT_GetConnTimeout(int attemptNumber) { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
DuyLionTran 24:3de3cf978e60 225 // after 20 attempts, retry every 10 minutes
DuyLionTran 24:3de3cf978e60 226 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
DuyLionTran 24:3de3cf978e60 227 }
DuyLionTran 24:3de3cf978e60 228
DuyLionTran 24:3de3cf978e60 229
DuyLionTran 24:3de3cf978e60 230 void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
DuyLionTran 24:3de3cf978e60 231 connected = false;
DuyLionTran 24:3de3cf978e60 232
DuyLionTran 24:3de3cf978e60 233 while (MQTT_Connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED) {
DuyLionTran 24:3de3cf978e60 234 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
DuyLionTran 24:3de3cf978e60 235 printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
DuyLionTran 24:3de3cf978e60 236 return; // don't reattempt to connect if credentials are wrong
DuyLionTran 24:3de3cf978e60 237 }
DuyLionTran 24:3de3cf978e60 238 int timeout = MQTT_GetConnTimeout(++retryAttempt);
DuyLionTran 24:3de3cf978e60 239 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
DuyLionTran 24:3de3cf978e60 240
DuyLionTran 24:3de3cf978e60 241 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
DuyLionTran 24:3de3cf978e60 242 // or maybe just add the proper members to do this disconnect and call MQTT_AttemptConnect(...)
DuyLionTran 24:3de3cf978e60 243 // this works - reset the system when the retry count gets to a threshold
DuyLionTran 24:3de3cf978e60 244 if (retryAttempt == 5)
DuyLionTran 24:3de3cf978e60 245 NVIC_SystemReset();
DuyLionTran 24:3de3cf978e60 246 else
DuyLionTran 24:3de3cf978e60 247 wait(timeout);
DuyLionTran 24:3de3cf978e60 248 }
DuyLionTran 24:3de3cf978e60 249 }
DuyLionTran 24:3de3cf978e60 250
DuyLionTran 24:3de3cf978e60 251 int MQTT_PublishADC(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float adcVal_0) {
DuyLionTran 24:3de3cf978e60 252 MQTT::Message message;
DuyLionTran 24:3de3cf978e60 253 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 24:3de3cf978e60 254
DuyLionTran 24:3de3cf978e60 255 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 24:3de3cf978e60 256 char timeBuf[50];
DuyLionTran 24:3de3cf978e60 257
DuyLionTran 24:3de3cf978e60 258 if (!client->isConnected()) {
DuyLionTran 24:3de3cf978e60 259 printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
DuyLionTran 24:3de3cf978e60 260 }
DuyLionTran 24:3de3cf978e60 261
DuyLionTran 24:3de3cf978e60 262 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 24:3de3cf978e60 263 // sprintf(buf,
DuyLionTran 24:3de3cf978e60 264 // "{\"Project\":\"%s\",\"Time\":\"%s\",\"Type\":1,\"cmdID\":%d,\"ADC0\":%0.2f}",
DuyLionTran 24:3de3cf978e60 265 // projectName, timeBuf, commandID, adcVal_0);
DuyLionTran 24:3de3cf978e60 266 sprintf(buf, "{\"type\":1,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"adc0\":%0.2f}",
DuyLionTran 24:3de3cf978e60 267 timeBuf, commandID, adcVal_0);
DuyLionTran 24:3de3cf978e60 268 message.qos = MQTT::QOS0;
DuyLionTran 24:3de3cf978e60 269 message.retained = false;
DuyLionTran 24:3de3cf978e60 270 message.dup = false;
DuyLionTran 24:3de3cf978e60 271 message.payload = (void*)buf;
DuyLionTran 24:3de3cf978e60 272 message.payloadlen = strlen(buf);
DuyLionTran 24:3de3cf978e60 273
DuyLionTran 24:3de3cf978e60 274 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 24:3de3cf978e60 275 printf("message too long!\r\n");
DuyLionTran 24:3de3cf978e60 276
DuyLionTran 24:3de3cf978e60 277 LOG("Publishing %s\n\r", buf);
DuyLionTran 24:3de3cf978e60 278 return client->publish(pubTopic, message);
DuyLionTran 24:3de3cf978e60 279 }
DuyLionTran 24:3de3cf978e60 280
DuyLionTran 25:cf7a3e31622a 281 int MQTT_PublishSensorValue(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, float pH, float DO) {
DuyLionTran 25:cf7a3e31622a 282
DuyLionTran 25:cf7a3e31622a 283 }
DuyLionTran 25:cf7a3e31622a 284
DuyLionTran 24:3de3cf978e60 285 int MQTT_PublishRelayState(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, int relay1, int relay2) {
DuyLionTran 24:3de3cf978e60 286 MQTT::Message message;
DuyLionTran 24:3de3cf978e60 287 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 24:3de3cf978e60 288 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 24:3de3cf978e60 289 char timeBuf[50];
DuyLionTran 24:3de3cf978e60 290
DuyLionTran 24:3de3cf978e60 291 if (!client->isConnected()) {
DuyLionTran 24:3de3cf978e60 292 printf ("---> MQTT DISCONNECTED\n\r");
DuyLionTran 24:3de3cf978e60 293 return MQTT::FAILURE;
DuyLionTran 24:3de3cf978e60 294 }
DuyLionTran 24:3de3cf978e60 295 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 24:3de3cf978e60 296 sprintf(buf, "{\"type\":3,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"relay1\":%d,\"relay2\":%d}",
DuyLionTran 24:3de3cf978e60 297 timeBuf, commandID, relay1, relay2);
DuyLionTran 24:3de3cf978e60 298 message.qos = MQTT::QOS0;
DuyLionTran 24:3de3cf978e60 299 message.retained = false;
DuyLionTran 24:3de3cf978e60 300 message.dup = false;
DuyLionTran 24:3de3cf978e60 301 message.payload = (void*)buf;
DuyLionTran 24:3de3cf978e60 302 message.payloadlen = strlen(buf);
DuyLionTran 24:3de3cf978e60 303
DuyLionTran 24:3de3cf978e60 304 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 24:3de3cf978e60 305 printf("message too long!\r\n");
DuyLionTran 24:3de3cf978e60 306
DuyLionTran 24:3de3cf978e60 307 LOG("Publishing %s\n\r", buf);
DuyLionTran 24:3de3cf978e60 308 return client->publish(pubTopic, message);
DuyLionTran 24:3de3cf978e60 309 }
DuyLionTran 24:3de3cf978e60 310
DuyLionTran 24:3de3cf978e60 311 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 24:3de3cf978e60 312 MQTT::Message message;
DuyLionTran 24:3de3cf978e60 313 const char* pubTopic = MQTT_EVENT_TOPIC;
DuyLionTran 24:3de3cf978e60 314 char buf[MQTT_MAX_PAYLOAD_SIZE];
DuyLionTran 24:3de3cf978e60 315 char timeBuf[50];
DuyLionTran 24:3de3cf978e60 316
DuyLionTran 24:3de3cf978e60 317 if (!client->isConnected()) {
DuyLionTran 24:3de3cf978e60 318 printf ("---> MQTT DISCONNECTED\n\r");
DuyLionTran 24:3de3cf978e60 319 return MQTT::FAILURE;
DuyLionTran 24:3de3cf978e60 320 }
DuyLionTran 24:3de3cf978e60 321 strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
DuyLionTran 24:3de3cf978e60 322 sprintf(buf, "{\"type\":4,\"deviceId\":\"PROEVN\",\"time\":\"%s\",\"cmdId\":%d,\"mode\":%d,\"minOxygenVal\":%d,\"maxOxygenVal\":%d}",
DuyLionTran 24:3de3cf978e60 323 timeBuf, commandID, mode, minOxi, maxOxi);
DuyLionTran 24:3de3cf978e60 324 message.qos = MQTT::QOS0;
DuyLionTran 24:3de3cf978e60 325 message.retained = false;
DuyLionTran 24:3de3cf978e60 326 message.dup = false;
DuyLionTran 24:3de3cf978e60 327 message.payload = (void*)buf;
DuyLionTran 24:3de3cf978e60 328 message.payloadlen = strlen(buf);
DuyLionTran 24:3de3cf978e60 329
DuyLionTran 24:3de3cf978e60 330 if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE)
DuyLionTran 24:3de3cf978e60 331 printf("message too long!\r\n");
DuyLionTran 24:3de3cf978e60 332
DuyLionTran 24:3de3cf978e60 333 LOG("Publishing %s\n\r", buf);
DuyLionTran 24:3de3cf978e60 334 return client->publish(pubTopic, message);
DuyLionTran 24:3de3cf978e60 335 }
DuyLionTran 24:3de3cf978e60 336
DuyLionTran 24:3de3cf978e60 337 int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct) {
DuyLionTran 24:3de3cf978e60 338 int retVal;
DuyLionTran 24:3de3cf978e60 339 switch (uploadType) {
DuyLionTran 24:3de3cf978e60 340 case (ADC_VALUE): retVal = MQTT_PublishADC(client, inputTime, uploadStruct.ADC_PHVal);
DuyLionTran 24:3de3cf978e60 341 break;
DuyLionTran 25:cf7a3e31622a 342 case (SENSOR_VALUE): retVal = MQTT_PublishSensorValue(client, inputTime, uploadStruct.SENSOR_PHVal, uploadStruct.SENSOR_DOVal);
DuyLionTran 24:3de3cf978e60 343 break;
DuyLionTran 24:3de3cf978e60 344 case (RELAY_STATE): retVal = MQTT_PublishRelayState(client, inputTime, uploadStruct.RELAY_State_1, uploadStruct.RELAY_State_2);
DuyLionTran 24:3de3cf978e60 345 break;
DuyLionTran 24:3de3cf978e60 346 case (CONFIG_VALUE): retVal = MQTT_PublishConfigValue(client, inputTime, uploadStruct.CONFIG_Mode, uploadStruct.CONFIG_MinOxi, uploadStruct.CONFIG_MaxOxi);
DuyLionTran 24:3de3cf978e60 347 break;
DuyLionTran 24:3de3cf978e60 348 default: break;
DuyLionTran 24:3de3cf978e60 349 }
DuyLionTran 24:3de3cf978e60 350 return retVal;
DuyLionTran 24:3de3cf978e60 351 }
DuyLionTran 24:3de3cf978e60 352
DuyLionTran 24:3de3cf978e60 353 #endif /* __SIMPLEMQTT_H__ */