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