Example of sending MQTT data to MyDevices Cayenne using the MTSAS library

Dependencies:   Cayenne-MQTT-mbed-MTSAS X_NUCLEO_IKS01A1 mbed mtsas_lat3

Committer:
pferland
Date:
Tue Nov 21 20:24:38 2017 +0000
Revision:
7:12131978176d
Parent:
6:cd0be5cc1943
Added GPS support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pferland 0:5107fce16490 1 #include "mbed.h"
pferland 0:5107fce16490 2
pferland 0:5107fce16490 3 #include "mtsas.h"
pferland 0:5107fce16490 4
pferland 0:5107fce16490 5 #include "MQTTTimer.h"
pferland 0:5107fce16490 6 #include "CayenneMQTTClient.h"
pferland 0:5107fce16490 7 #include "MQTTNetwork.h"
pferland 0:5107fce16490 8
pferland 0:5107fce16490 9 #include "x_nucleo_iks01a1.h"
pferland 0:5107fce16490 10
pferland 0:5107fce16490 11 #include <string>
pferland 0:5107fce16490 12 #include <sstream>
pferland 0:5107fce16490 13
pferland 0:5107fce16490 14 using std::string;
pferland 0:5107fce16490 15 typedef CayenneMQTT::MQTTClient<MQTTNetwork<Cellular>, MQTTTimer> MQTTClient;
pferland 0:5107fce16490 16
pferland 0:5107fce16490 17 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
pferland 7:12131978176d 18 string username = "da497640-dcce-11e6-b089-9f6bfa78ab33"; //"MQTT-Username";
pferland 7:12131978176d 19 string password = "68e890972b6cc0fc47fcd152554db7e78ec9b29f"; //"MQTT-Password";
pferland 7:12131978176d 20 string clientID = "dc9c10d0-ced9-11e7-98e1-8369df76aa6d"; //"MQTT-ClientID";
pferland 0:5107fce16490 21
pferland 5:960d9d8974c8 22 DigitalOut Led1Out(D1);
pferland 5:960d9d8974c8 23 DigitalOut Led2Out(D0);
pferland 5:960d9d8974c8 24 DigitalOut Led3Out(D3);
pferland 5:960d9d8974c8 25 DigitalOut Led4Out(D6);
pferland 5:960d9d8974c8 26 DigitalOut Led5Out(D6);
pferland 5:960d9d8974c8 27 DigitalOut Led6Out(D8);
pferland 5:960d9d8974c8 28 DigitalOut Led7Out(D5);
pferland 5:960d9d8974c8 29 DigitalOut Led8Out(D4);
pferland 5:960d9d8974c8 30 DigitalOut Led9Out(D7);
pferland 5:960d9d8974c8 31 DigitalOut LedStatus(D2);
pferland 0:5107fce16490 32
pferland 0:5107fce16490 33 // Debug serial port
pferland 0:5107fce16490 34 //static Serial debug(USBTX, USBRX);
pferland 0:5107fce16490 35 Serial pc(USBTX, USBRX);
pferland 0:5107fce16490 36
pferland 0:5107fce16490 37 // MTSSerialFlowControl - serial link between processor and radio
pferland 0:5107fce16490 38 static MTSSerialFlowControl* io;
pferland 0:5107fce16490 39
pferland 0:5107fce16490 40 // Cellular - radio object for cellular operations (SMS, TCP, etc)
pferland 0:5107fce16490 41 Cellular* radio;
pferland 0:5107fce16490 42
pferland 0:5107fce16490 43 /* Instantiate the expansion board */
pferland 0:5107fce16490 44 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL);
pferland 0:5107fce16490 45
pferland 0:5107fce16490 46 /* Retrieve the composing elements of the expansion board */
pferland 0:5107fce16490 47 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
pferland 0:5107fce16490 48 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
pferland 0:5107fce16490 49 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
pferland 0:5107fce16490 50 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
pferland 0:5107fce16490 51 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
pferland 0:5107fce16490 52 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
pferland 0:5107fce16490 53 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
pferland 0:5107fce16490 54
pferland 7:12131978176d 55 static const std::string apn = "b2b.tmobile.com"; //"iot.aer.net";
pferland 0:5107fce16490 56
pferland 0:5107fce16490 57 CayenneMQTT::MessageData lastMessage;
pferland 0:5107fce16490 58 bool messageReady;
pferland 7:12131978176d 59 bool gpsAvailable;
pferland 0:5107fce16490 60
pferland 0:5107fce16490 61 /*
pferland 0:5107fce16490 62 * Initialize cellular radio.
pferland 0:5107fce16490 63 */
pferland 0:5107fce16490 64 bool init_mtsas()
pferland 0:5107fce16490 65 {
pferland 0:5107fce16490 66 io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
pferland 0:5107fce16490 67 if (! io)
pferland 0:5107fce16490 68 return false;
pferland 0:5107fce16490 69
pferland 0:5107fce16490 70 io->baud(115200);
pferland 0:5107fce16490 71 radio = CellularFactory::create(io);
pferland 0:5107fce16490 72 if (! radio)
pferland 0:5107fce16490 73 return false;
pferland 0:5107fce16490 74
pferland 5:960d9d8974c8 75 logInfo("setting APN");
pferland 5:960d9d8974c8 76 if (radio->setApn(apn) != MTS_SUCCESS)
pferland 7:12131978176d 77 logError("failed to set APN to \"%s\"", apn.c_str());
pferland 5:960d9d8974c8 78
pferland 0:5107fce16490 79 Transport::setTransport(radio);
pferland 0:5107fce16490 80 while (! radio->connect()) {
pferland 0:5107fce16490 81 logError("failed to bring up PPP link");
pferland 0:5107fce16490 82 wait(2);
pferland 0:5107fce16490 83 }
pferland 0:5107fce16490 84
pferland 0:5107fce16490 85 printf("Signal Strength: %d\n\r", radio->getSignalStrength());
pferland 0:5107fce16490 86 return true;
pferland 0:5107fce16490 87 }
pferland 0:5107fce16490 88
pferland 0:5107fce16490 89 /**
pferland 0:5107fce16490 90 * Print the message info.
pferland 0:5107fce16490 91 * @param[in] message The message received from the Cayenne server.
pferland 0:5107fce16490 92 */
pferland 0:5107fce16490 93 void outputMessage(CayenneMQTT::MessageData& message)
pferland 0:5107fce16490 94 {
pferland 0:5107fce16490 95 switch (message.topic) {
pferland 0:5107fce16490 96 case COMMAND_TOPIC:
pferland 0:5107fce16490 97 printf("topic=Command");
pferland 0:5107fce16490 98 break;
pferland 0:5107fce16490 99 case CONFIG_TOPIC:
pferland 0:5107fce16490 100 printf("topic=Config");
pferland 0:5107fce16490 101 break;
pferland 0:5107fce16490 102 default:
pferland 0:5107fce16490 103 printf("topic=%d", message.topic);
pferland 0:5107fce16490 104 break;
pferland 0:5107fce16490 105 }
pferland 0:5107fce16490 106 printf(" channel=%d", message.channel);
pferland 0:5107fce16490 107 if (message.clientID) {
pferland 0:5107fce16490 108 printf(" clientID=%s", message.clientID);
pferland 0:5107fce16490 109 }
pferland 0:5107fce16490 110 if (message.type) {
pferland 0:5107fce16490 111 printf(" type=%s", message.type);
pferland 0:5107fce16490 112 }
pferland 0:5107fce16490 113 for (size_t i = 0; i < message.valueCount; ++i) {
pferland 0:5107fce16490 114 if (message.getValue(i)) {
pferland 0:5107fce16490 115 printf(" value=%s", message.getValue(i));
pferland 0:5107fce16490 116 }
pferland 0:5107fce16490 117 if (message.getUnit(i)) {
pferland 0:5107fce16490 118 printf(" unit=%s", message.getUnit(i));
pferland 0:5107fce16490 119 }
pferland 0:5107fce16490 120 }
pferland 0:5107fce16490 121 if (message.id) {
pferland 0:5107fce16490 122 printf(" id=%s", message.id);
pferland 0:5107fce16490 123 }
pferland 2:abc89d2aede3 124 printf("\r\n");
pferland 0:5107fce16490 125 }
pferland 0:5107fce16490 126
pferland 0:5107fce16490 127 /**
pferland 5:960d9d8974c8 128 *
pferland 5:960d9d8974c8 129 *
pferland 5:960d9d8974c8 130 */
pferland 5:960d9d8974c8 131 void setLEDs(bool newState)
pferland 5:960d9d8974c8 132 {
pferland 5:960d9d8974c8 133 // note: false = lit LED
pferland 5:960d9d8974c8 134 Led1Out = !newState;
pferland 5:960d9d8974c8 135 Led2Out = !newState;
pferland 5:960d9d8974c8 136 Led3Out = !newState;
pferland 5:960d9d8974c8 137 Led4Out = !newState;
pferland 5:960d9d8974c8 138 Led5Out = !newState;
pferland 5:960d9d8974c8 139 Led6Out = !newState;
pferland 5:960d9d8974c8 140 Led7Out = !newState;
pferland 5:960d9d8974c8 141 Led8Out = !newState;
pferland 5:960d9d8974c8 142 Led9Out = !newState;
pferland 5:960d9d8974c8 143 }
pferland 5:960d9d8974c8 144
pferland 5:960d9d8974c8 145 /**
pferland 0:5107fce16490 146 * Handle messages received from the Cayenne server.
pferland 0:5107fce16490 147 * @param[in] message The message received from the Cayenne server.
pferland 0:5107fce16490 148 */
pferland 0:5107fce16490 149 void messageArrived(CayenneMQTT::MessageData& message)
pferland 0:5107fce16490 150 {
pferland 0:5107fce16490 151 int error = 0;
pferland 0:5107fce16490 152 //note: if you change this example to use mbed-os you will need a mutex
pferland 0:5107fce16490 153 lastMessage = message;
pferland 0:5107fce16490 154 messageReady = true;
pferland 0:5107fce16490 155
pferland 0:5107fce16490 156 }
pferland 0:5107fce16490 157
pferland 0:5107fce16490 158 /**
pferland 0:5107fce16490 159 * Connect to the Cayenne server.
pferland 0:5107fce16490 160 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
pferland 0:5107fce16490 161 */
pferland 0:5107fce16490 162 int connectClient(MQTTClient &mqttClient, MQTTNetwork<Cellular> &network)
pferland 0:5107fce16490 163 {
pferland 0:5107fce16490 164 int error = 0;
pferland 0:5107fce16490 165 // Connect to the server.
pferland 2:abc89d2aede3 166 printf("Connecting to %s:%d\r\n", CAYENNE_DOMAIN, CAYENNE_PORT);
pferland 0:5107fce16490 167 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
pferland 2:abc89d2aede3 168 printf("TCP connect failed, error: %d\r\n", error);
pferland 0:5107fce16490 169 wait(2);
pferland 0:5107fce16490 170 }
pferland 0:5107fce16490 171
pferland 0:5107fce16490 172 if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
pferland 2:abc89d2aede3 173 printf("MQTT connect failed, error: %d\r\n", error);
pferland 0:5107fce16490 174 return error;
pferland 0:5107fce16490 175 }
pferland 2:abc89d2aede3 176 printf("Connected\r\n");
pferland 0:5107fce16490 177
pferland 0:5107fce16490 178 // Subscribe to required topics.
pferland 0:5107fce16490 179 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
pferland 2:abc89d2aede3 180 printf("Subscription to Command topic failed, error: %d\r\n", error);
pferland 0:5107fce16490 181 }
pferland 0:5107fce16490 182 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
pferland 2:abc89d2aede3 183 printf("Subscription to Config topic failed, error:%d\r\n", error);
pferland 0:5107fce16490 184 }
pferland 0:5107fce16490 185
pferland 0:5107fce16490 186 // Send device info. Here we just send some example values for the system info. These should be changed to use actual system data, or removed if not needed.
pferland 0:5107fce16490 187 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
pferland 0:5107fce16490 188 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
pferland 0:5107fce16490 189 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
pferland 0:5107fce16490 190 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
pferland 0:5107fce16490 191
pferland 0:5107fce16490 192 return CAYENNE_SUCCESS;
pferland 0:5107fce16490 193 }
pferland 0:5107fce16490 194
pferland 0:5107fce16490 195 /**
pferland 0:5107fce16490 196 * Main loop where MQTT code is run.
pferland 0:5107fce16490 197 */
pferland 0:5107fce16490 198 void loop(MQTTClient &mqttClient, MQTTNetwork<Cellular> &network)
pferland 0:5107fce16490 199 {
pferland 0:5107fce16490 200 // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
pferland 2:abc89d2aede3 201 MQTTTimer timer(1000);
pferland 2:abc89d2aede3 202 printf("Starting loop.\r\n");
pferland 0:5107fce16490 203 while (true) {
pferland 0:5107fce16490 204 // Yield to allow MQTT message processing.
pferland 2:abc89d2aede3 205 mqttClient.yield(10);
pferland 0:5107fce16490 206 if(messageReady){
pferland 0:5107fce16490 207 int error = 0;
pferland 0:5107fce16490 208 messageReady = false;
pferland 0:5107fce16490 209 // Add code to process the message. Here we just ouput the message data.
pferland 0:5107fce16490 210 outputMessage(lastMessage);
pferland 0:5107fce16490 211
pferland 0:5107fce16490 212 if (lastMessage.topic == COMMAND_TOPIC) {
pferland 0:5107fce16490 213 switch(lastMessage.channel) {
pferland 0:5107fce16490 214 case 0:
pferland 0:5107fce16490 215 // Set the onboard LED state
pferland 5:960d9d8974c8 216 bool value = atoi(lastMessage.getValue());
pferland 5:960d9d8974c8 217 setLEDs(value);
pferland 5:960d9d8974c8 218
pferland 0:5107fce16490 219 // Publish the updated LED state
pferland 5:960d9d8974c8 220 if ((error = mqttClient.publishData(DATA_TOPIC, lastMessage.channel, NULL, NULL, Led1Out.read()==0?1:0)) != CAYENNE_SUCCESS) {
pferland 2:abc89d2aede3 221 printf("Publish LED state failure, error: %d\r\n", error);
pferland 0:5107fce16490 222 }
pferland 0:5107fce16490 223 break;
pferland 0:5107fce16490 224 }
pferland 0:5107fce16490 225
pferland 0:5107fce16490 226 // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
pferland 0:5107fce16490 227 // An error response should be sent if there are issues processing the message.
pferland 0:5107fce16490 228 if ((error = mqttClient.publishResponse(lastMessage.id, NULL, lastMessage.clientID)) != CAYENNE_SUCCESS) {
pferland 2:abc89d2aede3 229 printf("Response failure, error: %d\r\n", error);
pferland 0:5107fce16490 230 }
pferland 0:5107fce16490 231 }
pferland 0:5107fce16490 232 }
pferland 0:5107fce16490 233
pferland 0:5107fce16490 234 // Check that we are still connected, if not, reconnect.
pferland 0:5107fce16490 235 if (!network.connected() || !mqttClient.connected()) {
pferland 0:5107fce16490 236 network.disconnect();
pferland 0:5107fce16490 237 mqttClient.disconnect();
pferland 5:960d9d8974c8 238 LedStatus = true;
pferland 2:abc89d2aede3 239 printf("Reconnecting\r\n");
pferland 0:5107fce16490 240 while (connectClient(mqttClient, network) != CAYENNE_SUCCESS) {
pferland 0:5107fce16490 241 wait(2);
pferland 2:abc89d2aede3 242 printf("Reconnect failed, retrying\r\n");
pferland 0:5107fce16490 243 }
pferland 5:960d9d8974c8 244 LedStatus = false;
pferland 0:5107fce16490 245 }
pferland 0:5107fce16490 246
pferland 0:5107fce16490 247 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
pferland 0:5107fce16490 248 if (timer.expired()) {
pferland 7:12131978176d 249 printf("Sampling sensors\r\n");
pferland 0:5107fce16490 250 int error = 0;
pferland 0:5107fce16490 251 float temp_data;
pferland 0:5107fce16490 252 temp_sensor1->get_temperature(&temp_data);
pferland 2:abc89d2aede3 253 printf("Temperature was: %f \r\n", temp_data);
pferland 0:5107fce16490 254 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, temp_data)) != CAYENNE_SUCCESS) {
pferland 2:abc89d2aede3 255 printf("Publish temperature failed, error: %d\r\n", error);
pferland 0:5107fce16490 256 }
pferland 0:5107fce16490 257 humidity_sensor->get_humidity(&temp_data);
pferland 2:abc89d2aede3 258 printf("Humidity was: %f \r\n", temp_data);
pferland 0:5107fce16490 259 if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT, temp_data)) != CAYENNE_SUCCESS) {
pferland 2:abc89d2aede3 260 printf("Publish luminosity failed, error: %d\r\n", error);
pferland 0:5107fce16490 261 }
pferland 0:5107fce16490 262 pressure_sensor->get_pressure(&temp_data);
pferland 2:abc89d2aede3 263 printf("Pressure was: %f \r\n", temp_data);
pferland 0:5107fce16490 264 if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, temp_data)) != CAYENNE_SUCCESS) {
pferland 2:abc89d2aede3 265 printf("Publish barometric pressure failed, error: %d\r\n", error);
pferland 0:5107fce16490 266 }
pferland 5:960d9d8974c8 267 printf("Led is: %s\r\n", Led1Out.read() > 0 ? "off" : "on");
pferland 5:960d9d8974c8 268 if ((error = mqttClient.publishData(DATA_TOPIC, 0, "digital_actuator", UNIT_DIGITAL, Led1Out.read()==0?1:0)) != CAYENNE_SUCCESS) {
pferland 2:abc89d2aede3 269 printf("Publish LED status failed, error: %d\r\n", error);
pferland 2:abc89d2aede3 270 }
pferland 7:12131978176d 271
pferland 7:12131978176d 272 if(gpsAvailable){
pferland 7:12131978176d 273 // disconnect socket so we can query GPS
pferland 7:12131978176d 274 mqttClient.disconnect();
pferland 7:12131978176d 275 network.disconnect();
pferland 7:12131978176d 276
pferland 7:12131978176d 277 if( !radio->GPSenabled() ) {
pferland 7:12131978176d 278 printf("GPS: enabling");
pferland 7:12131978176d 279 radio->GPSenable();
pferland 7:12131978176d 280 while( !radio->GPSenabled() ) {
pferland 7:12131978176d 281 logInfo("...");
pferland 7:12131978176d 282 wait(5);
pferland 7:12131978176d 283 }
pferland 7:12131978176d 284 }
pferland 7:12131978176d 285 //collect gps data
pferland 7:12131978176d 286 Cellular::gpsData loc = radio->GPSgetPosition();
pferland 7:12131978176d 287
pferland 7:12131978176d 288 LedStatus = true;
pferland 7:12131978176d 289 printf("Reconnecting\r\n");
pferland 7:12131978176d 290 while (connectClient(mqttClient, network) != CAYENNE_SUCCESS) {
pferland 7:12131978176d 291 wait(2);
pferland 7:12131978176d 292 printf("Reconnect failed, retrying\r\n");
pferland 7:12131978176d 293 }
pferland 7:12131978176d 294
pferland 7:12131978176d 295 if(loc.success == false){
pferland 7:12131978176d 296 printf("GPSgetPosition failed\r\n");
pferland 7:12131978176d 297 } else {
pferland 7:12131978176d 298 switch(loc.fix){
pferland 7:12131978176d 299 case 0:
pferland 7:12131978176d 300 case 1:
pferland 7:12131978176d 301 printf("GPS - no Lock\r\n"); break;
pferland 7:12131978176d 302 case 2:
pferland 7:12131978176d 303 printf("GPS - 2d Lock\r\n"); break;
pferland 7:12131978176d 304 case 3:
pferland 7:12131978176d 305 printf("GPS - 3d Lock\r\n"); break;
pferland 7:12131978176d 306 }
pferland 7:12131978176d 307 }
pferland 7:12131978176d 308 if(loc.fix > 1){
pferland 7:12131978176d 309 std::string payload = loc.latitude + ",";
pferland 7:12131978176d 310 payload += loc.longitude;
pferland 7:12131978176d 311 payload += ",";
pferland 7:12131978176d 312 payload += loc.altitude;
pferland 7:12131978176d 313 error = mqttClient.publishData(DATA_TOPIC, 4, "gps", "m", payload.c_str());
pferland 7:12131978176d 314 if( error != CAYENNE_SUCCESS) {
pferland 7:12131978176d 315 printf("Publish GPS latitude status failed, error: %d\r\n", error);
pferland 7:12131978176d 316 }
pferland 7:12131978176d 317 // error = mqttClient.publishData(DATA_TOPIC, 5, "location_long", NULL, loc.longitude.c_str());
pferland 7:12131978176d 318 // if( error != CAYENNE_SUCCESS) {
pferland 7:12131978176d 319 // printf("Publish GPS longitude status failed, error: %d\r\n", error);
pferland 7:12131978176d 320 // }
pferland 7:12131978176d 321 }
pferland 7:12131978176d 322 }
pferland 0:5107fce16490 323 // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
pferland 0:5107fce16490 324 timer.countdown_ms(5000);
pferland 0:5107fce16490 325 } else {
pferland 2:abc89d2aede3 326 // debug
pferland 2:abc89d2aede3 327 // printf("Timer: %d", timer.left_ms());
pferland 0:5107fce16490 328 }
pferland 0:5107fce16490 329 }
pferland 0:5107fce16490 330 }
pferland 0:5107fce16490 331
pferland 0:5107fce16490 332 int main()
pferland 0:5107fce16490 333 {
pferland 0:5107fce16490 334 pc.baud(115200);
pferland 5:960d9d8974c8 335 setLEDs(false);
pferland 5:960d9d8974c8 336 LedStatus = true;
pferland 0:5107fce16490 337 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
pferland 0:5107fce16490 338 // init radio, setup Cayenne connection
pferland 0:5107fce16490 339 if (!init_mtsas()) {
pferland 0:5107fce16490 340 while (true) {
pferland 0:5107fce16490 341 logError("failed to initialize cellular radio");
pferland 0:5107fce16490 342 wait(1);
pferland 0:5107fce16490 343 }
pferland 0:5107fce16490 344 }
pferland 5:960d9d8974c8 345
pferland 0:5107fce16490 346 // Test with a ping
pferland 0:5107fce16490 347 if(radio->ping("www.google.com")){
pferland 2:abc89d2aede3 348 printf("Ping test succeeded!\r\n");
pferland 0:5107fce16490 349 } else {
pferland 2:abc89d2aede3 350 printf("Failed ping test!\r\n");
pferland 0:5107fce16490 351 }
pferland 7:12131978176d 352
pferland 7:12131978176d 353 gpsAvailable = radio
pferland 7:12131978176d 354 ->GPSenable();
pferland 0:5107fce16490 355 MQTTNetwork<Cellular> network(*radio);
pferland 0:5107fce16490 356 messageReady = false;
pferland 0:5107fce16490 357 MQTTClient mqttClient(network, username.c_str(), password.c_str(), clientID.c_str());
pferland 0:5107fce16490 358
pferland 0:5107fce16490 359 // Set the default function that receives Cayenne messages.
pferland 0:5107fce16490 360 mqttClient.setDefaultMessageHandler(messageArrived);
pferland 0:5107fce16490 361
pferland 0:5107fce16490 362 // Connect to Cayenne.
pferland 0:5107fce16490 363 if (connectClient(mqttClient, network) == CAYENNE_SUCCESS) {
pferland 0:5107fce16490 364 // Run main loop.
pferland 5:960d9d8974c8 365 LedStatus = false;
pferland 0:5107fce16490 366 loop(mqttClient, network);
pferland 0:5107fce16490 367 }
pferland 0:5107fce16490 368 else {
pferland 2:abc89d2aede3 369 printf("Connection failed, exiting\r\n");
pferland 0:5107fce16490 370 }
pferland 0:5107fce16490 371
pferland 0:5107fce16490 372 if (mqttClient.connected())
pferland 0:5107fce16490 373 mqttClient.disconnect();
pferland 0:5107fce16490 374 if (network.connected())
pferland 0:5107fce16490 375 network.disconnect();
pferland 0:5107fce16490 376
pferland 0:5107fce16490 377 return 0;
pferland 0:5107fce16490 378 }