BG96 Module MQTT client example
Dependencies: MQTT NetworkSocketAPI mbed
Fork of Nucleo_NbIotBG96_A2_cloud_IBM by
main.cpp@2:b72797c38464, 2017-11-20 (annotated)
- Committer:
- abci5961
- Date:
- Mon Nov 20 16:23:22 2017 +0100
- Revision:
- 2:b72797c38464
- Parent:
- 1:a622606a49fd
- Child:
- 3:e26690c0e2c7
update BG96 library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abci5961 | 0:ae6c6727b545 | 1 | /* BG96 NetworkSocketAPI Example Program |
abci5961 | 0:ae6c6727b545 | 2 | * Copyright (c) 2015 ARM Limited |
abci5961 | 0:ae6c6727b545 | 3 | * |
abci5961 | 0:ae6c6727b545 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
abci5961 | 0:ae6c6727b545 | 5 | * you may not use this file except in compliance with the License. |
abci5961 | 0:ae6c6727b545 | 6 | * You may obtain a copy of the License at |
abci5961 | 0:ae6c6727b545 | 7 | * |
abci5961 | 0:ae6c6727b545 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
abci5961 | 0:ae6c6727b545 | 9 | * |
abci5961 | 0:ae6c6727b545 | 10 | * Unless required by applicable law or agreed to in writing, software |
abci5961 | 0:ae6c6727b545 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
abci5961 | 0:ae6c6727b545 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
abci5961 | 0:ae6c6727b545 | 13 | * See the License for the specific language governing permissions and |
abci5961 | 0:ae6c6727b545 | 14 | * limitations under the License. |
abci5961 | 0:ae6c6727b545 | 15 | */ |
abci5961 | 0:ae6c6727b545 | 16 | |
abci5961 | 0:ae6c6727b545 | 17 | #include "mbed.h" |
abci5961 | 0:ae6c6727b545 | 18 | #include "BG96Interface.h" |
abci5961 | 0:ae6c6727b545 | 19 | #include "TCPSocket.h" |
abci5961 | 0:ae6c6727b545 | 20 | #include "MQTTClient.h" |
abci5961 | 0:ae6c6727b545 | 21 | #include "MQTT_GSM.h" |
abci5961 | 0:ae6c6727b545 | 22 | #include <ctype.h> |
abci5961 | 0:ae6c6727b545 | 23 | //#include "x_nucleo_iks01a1.h" |
abci5961 | 0:ae6c6727b545 | 24 | #include "XNucleoIKS01A2.h" |
abci5961 | 0:ae6c6727b545 | 25 | |
abci5961 | 0:ae6c6727b545 | 26 | #include "BG96.h" |
abci5961 | 0:ae6c6727b545 | 27 | |
abci5961 | 0:ae6c6727b545 | 28 | //------------------------------------ |
abci5961 | 0:ae6c6727b545 | 29 | // Hyperterminal default configuration |
abci5961 | 0:ae6c6727b545 | 30 | // 9600 bauds, 8-bit data, no parity |
abci5961 | 0:ae6c6727b545 | 31 | //------------------------------------ |
abci5961 | 0:ae6c6727b545 | 32 | Serial pc(SERIAL_TX, SERIAL_RX); |
abci5961 | 0:ae6c6727b545 | 33 | DigitalOut myled(LED1); |
abci5961 | 0:ae6c6727b545 | 34 | bool quickstartMode = true; |
abci5961 | 0:ae6c6727b545 | 35 | |
abci5961 | 0:ae6c6727b545 | 36 | #define MQTT_MAX_PACKET_SIZE 300 |
abci5961 | 0:ae6c6727b545 | 37 | #define MQTT_MAX_PAYLOAD_SIZE 500 |
abci5961 | 0:ae6c6727b545 | 38 | |
abci5961 | 0:ae6c6727b545 | 39 | |
abci5961 | 0:ae6c6727b545 | 40 | #define ORG_QUICKSTART // comment to connect to play.internetofthings.ibmcloud.com |
abci5961 | 0:ae6c6727b545 | 41 | //#define SUBSCRIBE // uncomment to subscribe to broker msgs (not to be used with IBM broker) |
abci5961 | 0:ae6c6727b545 | 42 | |
abci5961 | 0:ae6c6727b545 | 43 | // Configuration values needed to connect to IBM IoT Cloud |
abci5961 | 0:ae6c6727b545 | 44 | #define BROKER_URL ".messaging.internetofthings.ibmcloud.com"; |
abci5961 | 0:ae6c6727b545 | 45 | #ifdef ORG_QUICKSTART |
abci5961 | 0:ae6c6727b545 | 46 | #define ORG "quickstart" // connect to quickstart.internetofthings.ibmcloud.com/ For a registered connection, replace with your org |
abci5961 | 0:ae6c6727b545 | 47 | #define ID "" |
abci5961 | 0:ae6c6727b545 | 48 | #define AUTH_TOKEN "" |
abci5961 | 0:ae6c6727b545 | 49 | #define DEFAULT_TYPE_NAME "iotsample-mbed-Nucleo" |
abci5961 | 0:ae6c6727b545 | 50 | #define TOPIC "iot-2/evt/status/fmt/json" |
abci5961 | 0:ae6c6727b545 | 51 | #else // not def ORG_QUICKSTART |
abci5961 | 0:ae6c6727b545 | 52 | #define ORG "pvko17" // connect to play.internetofthings.ibmcloud.com/ For a registered connection, replace with your org |
abci5961 | 0:ae6c6727b545 | 53 | #define ID "testtype_112233445566" // For a registered connection, replace with your id |
abci5961 | 0:ae6c6727b545 | 54 | #define AUTH_TOKEN "testtype_112233445566" // For a registered connection, replace with your auth-token |
abci5961 | 0:ae6c6727b545 | 55 | #define DEFAULT_TYPE_NAME "TestType" |
abci5961 | 0:ae6c6727b545 | 56 | #define TOPIC "iot-2/type/TestType/id/testtype_112233445566/evt/status/fmt/json" |
abci5961 | 0:ae6c6727b545 | 57 | #endif |
abci5961 | 0:ae6c6727b545 | 58 | |
abci5961 | 0:ae6c6727b545 | 59 | // network credential |
abci5961 | 2:b72797c38464 | 60 | #define APN "web.omnitel.it" //VODAFONE apn definition's |
abci5961 | 2:b72797c38464 | 61 | //#define APN "internet.wind" //WIND apn definition's |
abci5961 | 0:ae6c6727b545 | 62 | #define PASSW "" |
abci5961 | 0:ae6c6727b545 | 63 | #define USNAME "" |
abci5961 | 0:ae6c6727b545 | 64 | |
abci5961 | 0:ae6c6727b545 | 65 | #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type |
abci5961 | 0:ae6c6727b545 | 66 | #define MQTT_PORT 1883 |
abci5961 | 0:ae6c6727b545 | 67 | #define MQTT_TLS_PORT 8883 |
abci5961 | 0:ae6c6727b545 | 68 | #define IBM_IOT_PORT MQTT_PORT |
abci5961 | 0:ae6c6727b545 | 69 | |
abci5961 | 0:ae6c6727b545 | 70 | char id[30] = ID; // mac without colons |
abci5961 | 0:ae6c6727b545 | 71 | char org[12] = ORG; |
abci5961 | 0:ae6c6727b545 | 72 | int connack_rc = 0; // MQTT connack return code |
abci5961 | 0:ae6c6727b545 | 73 | //const char* ip_addr = "11.12.13.14"; |
abci5961 | 0:ae6c6727b545 | 74 | //char* host_addr = "11.12.13.14"; |
abci5961 | 0:ae6c6727b545 | 75 | char sensor_id[50]; |
abci5961 | 0:ae6c6727b545 | 76 | char type[30] = TYPE; |
abci5961 | 0:ae6c6727b545 | 77 | char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode |
abci5961 | 0:ae6c6727b545 | 78 | bool netConnecting = false; |
abci5961 | 0:ae6c6727b545 | 79 | int connectTimeout = 1000; |
abci5961 | 0:ae6c6727b545 | 80 | bool mqttConnecting = false; |
abci5961 | 0:ae6c6727b545 | 81 | bool netConnected = false; |
abci5961 | 0:ae6c6727b545 | 82 | bool connected = false; |
abci5961 | 0:ae6c6727b545 | 83 | int retryAttempt = 0; |
abci5961 | 0:ae6c6727b545 | 84 | char subscription_url[MQTT_MAX_PAYLOAD_SIZE]; |
abci5961 | 0:ae6c6727b545 | 85 | |
abci5961 | 0:ae6c6727b545 | 86 | #define SENSOR_ENABLED 1 |
abci5961 | 0:ae6c6727b545 | 87 | #define SENSOR_MODEL 2 |
abci5961 | 0:ae6c6727b545 | 88 | |
abci5961 | 0:ae6c6727b545 | 89 | #define FW_REV "1.0a" |
abci5961 | 0:ae6c6727b545 | 90 | |
abci5961 | 0:ae6c6727b545 | 91 | PressureSensor *pressure_sensor; |
abci5961 | 0:ae6c6727b545 | 92 | HumiditySensor *humidity_sensor; |
abci5961 | 0:ae6c6727b545 | 93 | TempSensor *temp_sensor1; |
abci5961 | 0:ae6c6727b545 | 94 | |
abci5961 | 0:ae6c6727b545 | 95 | MQTT::Message message; |
abci5961 | 0:ae6c6727b545 | 96 | MQTTString TopicName={TOPIC}; |
abci5961 | 0:ae6c6727b545 | 97 | MQTT::MessageData MsgData(TopicName, message); |
abci5961 | 0:ae6c6727b545 | 98 | |
abci5961 | 1:a622606a49fd | 99 | /* Instantiate the expansion board */ |
abci5961 | 1:a622606a49fd | 100 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
abci5961 | 0:ae6c6727b545 | 101 | |
abci5961 | 1:a622606a49fd | 102 | /* Retrieve the composing elements of the expansion board */ |
abci5961 | 1:a622606a49fd | 103 | static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; |
abci5961 | 1:a622606a49fd | 104 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
abci5961 | 1:a622606a49fd | 105 | static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; |
abci5961 | 1:a622606a49fd | 106 | static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
abci5961 | 1:a622606a49fd | 107 | static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; |
abci5961 | 0:ae6c6727b545 | 108 | |
abci5961 | 0:ae6c6727b545 | 109 | void subscribe_cb(MQTT::MessageData & msgMQTT) { |
abci5961 | 0:ae6c6727b545 | 110 | char msg[MQTT_MAX_PAYLOAD_SIZE]; |
abci5961 | 0:ae6c6727b545 | 111 | msg[0]='\0'; |
abci5961 | 0:ae6c6727b545 | 112 | strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen); |
abci5961 | 0:ae6c6727b545 | 113 | printf ("--->>> subscribe_cb msg: %s\n\r", msg); |
abci5961 | 0:ae6c6727b545 | 114 | } |
abci5961 | 0:ae6c6727b545 | 115 | |
abci5961 | 0:ae6c6727b545 | 116 | int subscribe(MQTT::Client<MQTT_GSM, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_GSM* ipstack) |
abci5961 | 0:ae6c6727b545 | 117 | { |
abci5961 | 0:ae6c6727b545 | 118 | char* pubTopic = TOPIC; |
abci5961 | 0:ae6c6727b545 | 119 | return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb); |
abci5961 | 0:ae6c6727b545 | 120 | } |
abci5961 | 0:ae6c6727b545 | 121 | |
abci5961 | 0:ae6c6727b545 | 122 | int connect(MQTT::Client<MQTT_GSM, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_GSM* ipstack) |
abci5961 | 0:ae6c6727b545 | 123 | { |
abci5961 | 0:ae6c6727b545 | 124 | const char* iot_ibm = BROKER_URL; |
abci5961 | 0:ae6c6727b545 | 125 | |
abci5961 | 0:ae6c6727b545 | 126 | |
abci5961 | 0:ae6c6727b545 | 127 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
abci5961 | 0:ae6c6727b545 | 128 | sprintf(hostname, "%s%s", org, iot_ibm); |
abci5961 | 0:ae6c6727b545 | 129 | |
abci5961 | 0:ae6c6727b545 | 130 | |
abci5961 | 0:ae6c6727b545 | 131 | // Construct clientId - d:org:type:id |
abci5961 | 0:ae6c6727b545 | 132 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
abci5961 | 0:ae6c6727b545 | 133 | |
abci5961 | 0:ae6c6727b545 | 134 | #ifdef ORG_QUICKSTART |
abci5961 | 0:ae6c6727b545 | 135 | sprintf(clientId, "d:%s:%s:%s", org, type, id); //@@ |
abci5961 | 0:ae6c6727b545 | 136 | #else |
abci5961 | 0:ae6c6727b545 | 137 | sprintf(clientId, "g:%s:%s:%s", org, type, id); //@@ |
abci5961 | 0:ae6c6727b545 | 138 | #endif |
abci5961 | 0:ae6c6727b545 | 139 | |
abci5961 | 0:ae6c6727b545 | 140 | sprintf(subscription_url, "%s.%s/#/device/%s/sensor/", org, "internetofthings.ibmcloud.com",id); |
abci5961 | 0:ae6c6727b545 | 141 | |
abci5961 | 0:ae6c6727b545 | 142 | netConnecting = true; |
abci5961 | 0:ae6c6727b545 | 143 | ipstack->open(&ipstack->getGSM()); |
abci5961 | 0:ae6c6727b545 | 144 | int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout); |
abci5961 | 0:ae6c6727b545 | 145 | if (rc != 0) |
abci5961 | 0:ae6c6727b545 | 146 | { |
abci5961 | 0:ae6c6727b545 | 147 | //WARN("IP Stack connect returned: %d\n", rc); |
abci5961 | 0:ae6c6727b545 | 148 | return rc; |
abci5961 | 0:ae6c6727b545 | 149 | } |
abci5961 | 0:ae6c6727b545 | 150 | pc.printf ("--->TCP Connected\n\r"); |
abci5961 | 0:ae6c6727b545 | 151 | netConnected = true; |
abci5961 | 0:ae6c6727b545 | 152 | netConnecting = false; |
abci5961 | 0:ae6c6727b545 | 153 | |
abci5961 | 0:ae6c6727b545 | 154 | // MQTT Connect |
abci5961 | 0:ae6c6727b545 | 155 | mqttConnecting = true; |
abci5961 | 0:ae6c6727b545 | 156 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
abci5961 | 0:ae6c6727b545 | 157 | data.MQTTVersion = 3; |
abci5961 | 0:ae6c6727b545 | 158 | data.struct_version=0; |
abci5961 | 0:ae6c6727b545 | 159 | data.clientID.cstring = clientId; |
abci5961 | 0:ae6c6727b545 | 160 | |
abci5961 | 0:ae6c6727b545 | 161 | if (!quickstartMode) |
abci5961 | 0:ae6c6727b545 | 162 | { |
abci5961 | 0:ae6c6727b545 | 163 | data.username.cstring = "use-token-auth"; |
abci5961 | 0:ae6c6727b545 | 164 | data.password.cstring = auth_token; |
abci5961 | 0:ae6c6727b545 | 165 | } |
abci5961 | 0:ae6c6727b545 | 166 | if ((rc = client->connect(data)) == 0) |
abci5961 | 0:ae6c6727b545 | 167 | { |
abci5961 | 0:ae6c6727b545 | 168 | connected = true; |
abci5961 | 0:ae6c6727b545 | 169 | pc.printf ("--->MQTT Connected\n\r"); |
abci5961 | 0:ae6c6727b545 | 170 | #ifdef SUBSCRIBE |
abci5961 | 0:ae6c6727b545 | 171 | if (!subscribe(client, ipstack)) printf ("--->>>MQTT subscribed to: %s\n\r",TOPIC); |
abci5961 | 0:ae6c6727b545 | 172 | #endif |
abci5961 | 0:ae6c6727b545 | 173 | } |
abci5961 | 0:ae6c6727b545 | 174 | else { |
abci5961 | 0:ae6c6727b545 | 175 | //WARN("MQTT connect returned %d\n", rc); |
abci5961 | 0:ae6c6727b545 | 176 | } |
abci5961 | 0:ae6c6727b545 | 177 | if (rc >= 0) |
abci5961 | 0:ae6c6727b545 | 178 | connack_rc = rc; |
abci5961 | 0:ae6c6727b545 | 179 | mqttConnecting = false; |
abci5961 | 0:ae6c6727b545 | 180 | return rc; |
abci5961 | 0:ae6c6727b545 | 181 | } |
abci5961 | 0:ae6c6727b545 | 182 | |
abci5961 | 0:ae6c6727b545 | 183 | int getConnTimeout(int attemptNumber) |
abci5961 | 0:ae6c6727b545 | 184 | { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
abci5961 | 0:ae6c6727b545 | 185 | // after 20 attempts, retry every 10 minutes |
abci5961 | 0:ae6c6727b545 | 186 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
abci5961 | 0:ae6c6727b545 | 187 | } |
abci5961 | 0:ae6c6727b545 | 188 | |
abci5961 | 0:ae6c6727b545 | 189 | void attemptConnect(MQTT::Client<MQTT_GSM, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_GSM* ipstack) |
abci5961 | 0:ae6c6727b545 | 190 | { |
abci5961 | 0:ae6c6727b545 | 191 | connected = false; |
abci5961 | 0:ae6c6727b545 | 192 | |
abci5961 | 0:ae6c6727b545 | 193 | while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) |
abci5961 | 0:ae6c6727b545 | 194 | { |
abci5961 | 0:ae6c6727b545 | 195 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) { |
abci5961 | 0:ae6c6727b545 | 196 | printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc); |
abci5961 | 0:ae6c6727b545 | 197 | return; // don't reattempt to connect if credentials are wrong |
abci5961 | 0:ae6c6727b545 | 198 | } |
abci5961 | 0:ae6c6727b545 | 199 | int timeout = getConnTimeout(++retryAttempt); |
abci5961 | 0:ae6c6727b545 | 200 | //WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
abci5961 | 0:ae6c6727b545 | 201 | |
abci5961 | 0:ae6c6727b545 | 202 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
abci5961 | 0:ae6c6727b545 | 203 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
abci5961 | 0:ae6c6727b545 | 204 | // this works - reset the system when the retry count gets to a threshold |
abci5961 | 0:ae6c6727b545 | 205 | if (retryAttempt == 5){ |
abci5961 | 0:ae6c6727b545 | 206 | pc.printf ("\n\n\rFAIL!! system reset!!\n\n\r"); |
abci5961 | 0:ae6c6727b545 | 207 | NVIC_SystemReset(); |
abci5961 | 0:ae6c6727b545 | 208 | } |
abci5961 | 0:ae6c6727b545 | 209 | else |
abci5961 | 0:ae6c6727b545 | 210 | wait(timeout); |
abci5961 | 0:ae6c6727b545 | 211 | } |
abci5961 | 0:ae6c6727b545 | 212 | } |
abci5961 | 0:ae6c6727b545 | 213 | float hum_global = 50.0; |
abci5961 | 0:ae6c6727b545 | 214 | uint32_t n_msg = 0; |
abci5961 | 0:ae6c6727b545 | 215 | |
abci5961 | 0:ae6c6727b545 | 216 | int publish(MQTT::Client<MQTT_GSM, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_GSM* ipstack) |
abci5961 | 0:ae6c6727b545 | 217 | { |
abci5961 | 0:ae6c6727b545 | 218 | MQTT::Message message; |
abci5961 | 0:ae6c6727b545 | 219 | char* pubTopic = TOPIC; |
abci5961 | 0:ae6c6727b545 | 220 | |
abci5961 | 0:ae6c6727b545 | 221 | char buf[MQTT_MAX_PAYLOAD_SIZE]; |
abci5961 | 0:ae6c6727b545 | 222 | float temp, temp1, temp2, press, hum; |
abci5961 | 0:ae6c6727b545 | 223 | |
abci5961 | 0:ae6c6727b545 | 224 | #if SENSOR_ENABLED |
abci5961 | 1:a622606a49fd | 225 | pc.printf("A02 reading sensors..."); |
abci5961 | 1:a622606a49fd | 226 | |
abci5961 | 0:ae6c6727b545 | 227 | hum_temp->get_temperature(&temp1); |
abci5961 | 0:ae6c6727b545 | 228 | hum_temp->get_humidity(&hum); |
abci5961 | 0:ae6c6727b545 | 229 | |
abci5961 | 0:ae6c6727b545 | 230 | press_temp->get_temperature(&temp2); |
abci5961 | 0:ae6c6727b545 | 231 | press_temp->get_pressure(&press); |
abci5961 | 0:ae6c6727b545 | 232 | temp = (temp1+temp2)/2; |
abci5961 | 0:ae6c6727b545 | 233 | |
abci5961 | 0:ae6c6727b545 | 234 | pc.printf(" DONE\r\n"); |
abci5961 | 0:ae6c6727b545 | 235 | #else |
abci5961 | 0:ae6c6727b545 | 236 | temp=25.5; |
abci5961 | 0:ae6c6727b545 | 237 | hum_global +=0.1; |
abci5961 | 0:ae6c6727b545 | 238 | if (hum_global>99.0) |
abci5961 | 0:ae6c6727b545 | 239 | hum_global = 50.0; |
abci5961 | 0:ae6c6727b545 | 240 | hum=hum_global; |
abci5961 | 0:ae6c6727b545 | 241 | press=999; |
abci5961 | 0:ae6c6727b545 | 242 | #endif |
abci5961 | 0:ae6c6727b545 | 243 | |
abci5961 | 0:ae6c6727b545 | 244 | #ifdef ORG_QUICKSTART |
abci5961 | 0:ae6c6727b545 | 245 | sprintf(buf, |
abci5961 | 0:ae6c6727b545 | 246 | "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}", |
abci5961 | 0:ae6c6727b545 | 247 | temp, press, hum); |
abci5961 | 0:ae6c6727b545 | 248 | #else |
abci5961 | 0:ae6c6727b545 | 249 | sprintf (buf, |
abci5961 | 0:ae6c6727b545 | 250 | "{\"%s\": {\"temp\":%0.4f,\"humidity\":%0.4f,\"pressure\":%0.4f,\"ambient\":0,\"uv\":0,\"accel_X\":0,\"accel_Y\":0,\"accel_Z\":0}}", |
abci5961 | 0:ae6c6727b545 | 251 | sensor_id, temp, hum, press); |
abci5961 | 0:ae6c6727b545 | 252 | #endif |
abci5961 | 0:ae6c6727b545 | 253 | |
abci5961 | 0:ae6c6727b545 | 254 | message.qos = MQTT::QOS0; |
abci5961 | 0:ae6c6727b545 | 255 | message.retained = false; |
abci5961 | 0:ae6c6727b545 | 256 | message.dup = false; |
abci5961 | 0:ae6c6727b545 | 257 | message.payload = (void*)buf; |
abci5961 | 0:ae6c6727b545 | 258 | message.payloadlen = strlen(buf); |
abci5961 | 0:ae6c6727b545 | 259 | |
abci5961 | 0:ae6c6727b545 | 260 | //LOG("Publishing %s\n\r", buf); |
abci5961 | 0:ae6c6727b545 | 261 | n_msg++; |
abci5961 | 0:ae6c6727b545 | 262 | pc.printf("Publishing V%s #%d %s\n\r", FW_REV, n_msg, buf); |
abci5961 | 0:ae6c6727b545 | 263 | return client->publish(pubTopic, message); |
abci5961 | 0:ae6c6727b545 | 264 | } |
abci5961 | 0:ae6c6727b545 | 265 | |
abci5961 | 0:ae6c6727b545 | 266 | |
abci5961 | 0:ae6c6727b545 | 267 | int loop_count = 0; |
abci5961 | 0:ae6c6727b545 | 268 | void test_sens(void); |
abci5961 | 0:ae6c6727b545 | 269 | |
abci5961 | 0:ae6c6727b545 | 270 | int main() |
abci5961 | 0:ae6c6727b545 | 271 | { |
abci5961 | 0:ae6c6727b545 | 272 | const char * apn = APN; // Network must be visible otherwise it can't connect |
abci5961 | 0:ae6c6727b545 | 273 | const char * username = USNAME; |
abci5961 | 0:ae6c6727b545 | 274 | const char * password = PASSW; |
abci5961 | 0:ae6c6727b545 | 275 | BG96Interface bg96_if(D8, D2, false); |
abci5961 | 0:ae6c6727b545 | 276 | //sprintf(sensor_id,"%s",bg96_if.get_mac_address()); |
abci5961 | 0:ae6c6727b545 | 277 | //Timer tyeld; |
abci5961 | 0:ae6c6727b545 | 278 | |
abci5961 | 0:ae6c6727b545 | 279 | //change serial baud to 115200 |
abci5961 | 0:ae6c6727b545 | 280 | pc.baud(115200); |
abci5961 | 0:ae6c6727b545 | 281 | wait(0.1); |
abci5961 | 0:ae6c6727b545 | 282 | |
abci5961 | 0:ae6c6727b545 | 283 | myled=1; |
abci5961 | 0:ae6c6727b545 | 284 | //wait(0.5); |
abci5961 | 0:ae6c6727b545 | 285 | pc.printf("\r\n*************************************************"); |
abci5961 | 0:ae6c6727b545 | 286 | wait( 0.1 ); |
abci5961 | 1:a622606a49fd | 287 | pc.printf("\r\nAvnet Silica NbIotBG96 A02 mbed-os application\r\n"); |
abci5961 | 0:ae6c6727b545 | 288 | wait( 0.1 ); |
abci5961 | 0:ae6c6727b545 | 289 | pc.printf("MBED online version %s\r\n", FW_REV); |
abci5961 | 0:ae6c6727b545 | 290 | wait( 0.1 ); |
abci5961 | 0:ae6c6727b545 | 291 | //pc.printf("\r\nwait for APN ready ...\r\n"); |
abci5961 | 0:ae6c6727b545 | 292 | //wait( 0.1 ); |
abci5961 | 0:ae6c6727b545 | 293 | |
abci5961 | 0:ae6c6727b545 | 294 | #if SENSOR_ENABLED |
abci5961 | 1:a622606a49fd | 295 | /* Enable all sensors */ |
abci5961 | 1:a622606a49fd | 296 | hum_temp->enable(); |
abci5961 | 1:a622606a49fd | 297 | press_temp->enable(); |
abci5961 | 1:a622606a49fd | 298 | //magnetometer->enable(); |
abci5961 | 1:a622606a49fd | 299 | //accelerometer->enable(); |
abci5961 | 1:a622606a49fd | 300 | //acc_gyro->enable_x(); |
abci5961 | 1:a622606a49fd | 301 | //acc_gyro->enable_g(); |
abci5961 | 0:ae6c6727b545 | 302 | #endif |
abci5961 | 0:ae6c6727b545 | 303 | |
abci5961 | 0:ae6c6727b545 | 304 | |
abci5961 | 0:ae6c6727b545 | 305 | quickstartMode=false; |
abci5961 | 0:ae6c6727b545 | 306 | if (strcmp(org, "quickstart") == 0){quickstartMode = true;} |
abci5961 | 0:ae6c6727b545 | 307 | MQTT_GSM ipstack(bg96_if, apn, username, password); |
abci5961 | 0:ae6c6727b545 | 308 | MQTT::Client<MQTT_GSM, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
abci5961 | 0:ae6c6727b545 | 309 | if (quickstartMode){ |
abci5961 | 0:ae6c6727b545 | 310 | char mac[50]; // remove all : from mac |
abci5961 | 0:ae6c6727b545 | 311 | char *digit=NULL; |
abci5961 | 0:ae6c6727b545 | 312 | sprintf (id,"%s", ""); |
abci5961 | 0:ae6c6727b545 | 313 | sprintf (mac,"%s",ipstack.getGSM().get_mac_address()); |
abci5961 | 0:ae6c6727b545 | 314 | digit = strtok (mac,":"); |
abci5961 | 0:ae6c6727b545 | 315 | while (digit != NULL) |
abci5961 | 0:ae6c6727b545 | 316 | { |
abci5961 | 0:ae6c6727b545 | 317 | strcat (id, digit); |
abci5961 | 0:ae6c6727b545 | 318 | digit = strtok (NULL, ":"); |
abci5961 | 0:ae6c6727b545 | 319 | } |
abci5961 | 0:ae6c6727b545 | 320 | } |
abci5961 | 0:ae6c6727b545 | 321 | attemptConnect(&client, &ipstack); |
abci5961 | 0:ae6c6727b545 | 322 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
abci5961 | 0:ae6c6727b545 | 323 | { |
abci5961 | 0:ae6c6727b545 | 324 | while (true) |
abci5961 | 0:ae6c6727b545 | 325 | wait(1.0); // Permanent failures - don't retry |
abci5961 | 0:ae6c6727b545 | 326 | } |
abci5961 | 0:ae6c6727b545 | 327 | myled=0; |
abci5961 | 0:ae6c6727b545 | 328 | sprintf(sensor_id,"%s",bg96_if.get_mac_address()); |
abci5961 | 0:ae6c6727b545 | 329 | |
abci5961 | 0:ae6c6727b545 | 330 | while (true) |
abci5961 | 0:ae6c6727b545 | 331 | { |
abci5961 | 0:ae6c6727b545 | 332 | if (++loop_count == 60) |
abci5961 | 0:ae6c6727b545 | 333 | { |
abci5961 | 0:ae6c6727b545 | 334 | // Publish a message every 30 second |
abci5961 | 0:ae6c6727b545 | 335 | pc.printf("\n"); |
abci5961 | 0:ae6c6727b545 | 336 | myled=1; |
abci5961 | 0:ae6c6727b545 | 337 | if (publish(&client, &ipstack) != 0) { |
abci5961 | 0:ae6c6727b545 | 338 | myled=0; |
abci5961 | 0:ae6c6727b545 | 339 | attemptConnect(&client, &ipstack); // if we have lost the connection |
abci5961 | 0:ae6c6727b545 | 340 | } |
abci5961 | 0:ae6c6727b545 | 341 | //else |
abci5961 | 0:ae6c6727b545 | 342 | myled=0; |
abci5961 | 0:ae6c6727b545 | 343 | loop_count = 0; |
abci5961 | 0:ae6c6727b545 | 344 | } |
abci5961 | 0:ae6c6727b545 | 345 | // int start = tyeld.read_ms(); |
abci5961 | 0:ae6c6727b545 | 346 | client.yield(500); // allow the MQTT client to receive messages |
abci5961 | 0:ae6c6727b545 | 347 | pc.printf ("loop %d\r", (loop_count+1)); //: %d\n\r",tyeld.read_ms()-start); |
abci5961 | 0:ae6c6727b545 | 348 | |
abci5961 | 0:ae6c6727b545 | 349 | } |
abci5961 | 0:ae6c6727b545 | 350 | |
abci5961 | 0:ae6c6727b545 | 351 | } |
abci5961 | 0:ae6c6727b545 | 352 | |
abci5961 | 0:ae6c6727b545 | 353 | |
abci5961 | 0:ae6c6727b545 | 354 | |
abci5961 | 0:ae6c6727b545 | 355 | |
abci5961 | 0:ae6c6727b545 | 356 | /* Helper function for printing floats & doubles */ |
abci5961 | 0:ae6c6727b545 | 357 | static char *print_double(char* str, double v, int decimalDigits=2) |
abci5961 | 0:ae6c6727b545 | 358 | { |
abci5961 | 0:ae6c6727b545 | 359 | int i = 1; |
abci5961 | 0:ae6c6727b545 | 360 | int intPart, fractPart; |
abci5961 | 0:ae6c6727b545 | 361 | int len; |
abci5961 | 0:ae6c6727b545 | 362 | char *ptr; |
abci5961 | 0:ae6c6727b545 | 363 | |
abci5961 | 0:ae6c6727b545 | 364 | /* prepare decimal digits multiplicator */ |
abci5961 | 0:ae6c6727b545 | 365 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
abci5961 | 0:ae6c6727b545 | 366 | |
abci5961 | 0:ae6c6727b545 | 367 | /* calculate integer & fractinal parts */ |
abci5961 | 0:ae6c6727b545 | 368 | intPart = (int)v; |
abci5961 | 0:ae6c6727b545 | 369 | fractPart = (int)((v-(double)(int)v)*i); |
abci5961 | 0:ae6c6727b545 | 370 | |
abci5961 | 0:ae6c6727b545 | 371 | /* fill in integer part */ |
abci5961 | 0:ae6c6727b545 | 372 | sprintf(str, "%i.", intPart); |
abci5961 | 0:ae6c6727b545 | 373 | |
abci5961 | 0:ae6c6727b545 | 374 | /* prepare fill in of fractional part */ |
abci5961 | 0:ae6c6727b545 | 375 | len = strlen(str); |
abci5961 | 0:ae6c6727b545 | 376 | ptr = &str[len]; |
abci5961 | 0:ae6c6727b545 | 377 | |
abci5961 | 0:ae6c6727b545 | 378 | /* fill in leading fractional zeros */ |
abci5961 | 0:ae6c6727b545 | 379 | for (i/=10;i>1; i/=10, ptr++) { |
abci5961 | 0:ae6c6727b545 | 380 | if (fractPart >= i) { |
abci5961 | 0:ae6c6727b545 | 381 | break; |
abci5961 | 0:ae6c6727b545 | 382 | } |
abci5961 | 0:ae6c6727b545 | 383 | *ptr = '0'; |
abci5961 | 0:ae6c6727b545 | 384 | } |
abci5961 | 0:ae6c6727b545 | 385 | |
abci5961 | 0:ae6c6727b545 | 386 | /* fill in (rest of) fractional part */ |
abci5961 | 0:ae6c6727b545 | 387 | sprintf(ptr, "%i", fractPart); |
abci5961 | 0:ae6c6727b545 | 388 | |
abci5961 | 0:ae6c6727b545 | 389 | return str; |
abci5961 | 0:ae6c6727b545 | 390 | } |
abci5961 | 0:ae6c6727b545 | 391 | |
abci5961 | 0:ae6c6727b545 | 392 | |
abci5961 | 0:ae6c6727b545 | 393 | //for testing sensor board ... |
abci5961 | 0:ae6c6727b545 | 394 | void test_sens(void) |
abci5961 | 0:ae6c6727b545 | 395 | { |
abci5961 | 0:ae6c6727b545 | 396 | while(1) |
abci5961 | 0:ae6c6727b545 | 397 | { |
abci5961 | 0:ae6c6727b545 | 398 | |
abci5961 | 0:ae6c6727b545 | 399 | float value1, value2; |
abci5961 | 0:ae6c6727b545 | 400 | char buffer1[32], buffer2[32]; |
abci5961 | 0:ae6c6727b545 | 401 | printf("\r\n"); |
abci5961 | 0:ae6c6727b545 | 402 | |
abci5961 | 0:ae6c6727b545 | 403 | hum_temp->get_temperature(&value1); |
abci5961 | 0:ae6c6727b545 | 404 | hum_temp->get_humidity(&value2); |
abci5961 | 0:ae6c6727b545 | 405 | printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
abci5961 | 0:ae6c6727b545 | 406 | |
abci5961 | 0:ae6c6727b545 | 407 | press_temp->get_temperature(&value1); |
abci5961 | 0:ae6c6727b545 | 408 | press_temp->get_pressure(&value2); |
abci5961 | 0:ae6c6727b545 | 409 | printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
abci5961 | 0:ae6c6727b545 | 410 | |
abci5961 | 0:ae6c6727b545 | 411 | wait(2); |
abci5961 | 0:ae6c6727b545 | 412 | |
abci5961 | 0:ae6c6727b545 | 413 | } |
abci5961 | 0:ae6c6727b545 | 414 | } |
abci5961 | 0:ae6c6727b545 | 415 |