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