Example of sending MQTT data to MyDevices Cayenne using the MTSAS library
Dependencies: Cayenne-MQTT-mbed-MTSAS X_NUCLEO_IKS01A1 mbed mtsas_lat3
Revision 7:12131978176d, committed 2017-11-21
- Comitter:
- pferland
- Date:
- Tue Nov 21 20:24:38 2017 +0000
- Parent:
- 6:cd0be5cc1943
- Commit message:
- Added GPS support
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r cd0be5cc1943 -r 12131978176d main.cpp --- a/main.cpp Mon Nov 20 21:25:46 2017 +0000 +++ b/main.cpp Tue Nov 21 20:24:38 2017 +0000 @@ -15,9 +15,9 @@ typedef CayenneMQTT::MQTTClient<MQTTNetwork<Cellular>, MQTTTimer> MQTTClient; // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. -string username = "MQTT-Username"; -string password = "MQTT-Password"; -string clientID = "MQTT-ClientID"; +string username = "da497640-dcce-11e6-b089-9f6bfa78ab33"; //"MQTT-Username"; +string password = "68e890972b6cc0fc47fcd152554db7e78ec9b29f"; //"MQTT-Password"; +string clientID = "dc9c10d0-ced9-11e7-98e1-8369df76aa6d"; //"MQTT-ClientID"; DigitalOut Led1Out(D1); DigitalOut Led2Out(D0); @@ -52,10 +52,11 @@ static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; -static const std::string apn = "iot.aer.net"; +static const std::string apn = "b2b.tmobile.com"; //"iot.aer.net"; CayenneMQTT::MessageData lastMessage; bool messageReady; +bool gpsAvailable; /* * Initialize cellular radio. @@ -73,7 +74,7 @@ logInfo("setting APN"); if (radio->setApn(apn) != MTS_SUCCESS) - logError("failed to set APN to \"%s\"", apn); + logError("failed to set APN to \"%s\"", apn.c_str()); Transport::setTransport(radio); while (! radio->connect()) { @@ -245,6 +246,7 @@ // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne. if (timer.expired()) { + printf("Sampling sensors\r\n"); int error = 0; float temp_data; temp_sensor1->get_temperature(&temp_data); @@ -266,6 +268,58 @@ if ((error = mqttClient.publishData(DATA_TOPIC, 0, "digital_actuator", UNIT_DIGITAL, Led1Out.read()==0?1:0)) != CAYENNE_SUCCESS) { printf("Publish LED status failed, error: %d\r\n", error); } + + if(gpsAvailable){ + // disconnect socket so we can query GPS + mqttClient.disconnect(); + network.disconnect(); + + if( !radio->GPSenabled() ) { + printf("GPS: enabling"); + radio->GPSenable(); + while( !radio->GPSenabled() ) { + logInfo("..."); + wait(5); + } + } + //collect gps data + Cellular::gpsData loc = radio->GPSgetPosition(); + + LedStatus = true; + printf("Reconnecting\r\n"); + while (connectClient(mqttClient, network) != CAYENNE_SUCCESS) { + wait(2); + printf("Reconnect failed, retrying\r\n"); + } + + if(loc.success == false){ + printf("GPSgetPosition failed\r\n"); + } else { + switch(loc.fix){ + case 0: + case 1: + printf("GPS - no Lock\r\n"); break; + case 2: + printf("GPS - 2d Lock\r\n"); break; + case 3: + printf("GPS - 3d Lock\r\n"); break; + } + } + if(loc.fix > 1){ + std::string payload = loc.latitude + ","; + payload += loc.longitude; + payload += ","; + payload += loc.altitude; + error = mqttClient.publishData(DATA_TOPIC, 4, "gps", "m", payload.c_str()); + if( error != CAYENNE_SUCCESS) { + printf("Publish GPS latitude status failed, error: %d\r\n", error); + } +// error = mqttClient.publishData(DATA_TOPIC, 5, "location_long", NULL, loc.longitude.c_str()); +// if( error != CAYENNE_SUCCESS) { +// printf("Publish GPS longitude status failed, error: %d\r\n", error); +// } + } + } // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. timer.countdown_ms(5000); } else { @@ -295,7 +349,9 @@ } else { printf("Failed ping test!\r\n"); } - + + gpsAvailable = radio + ->GPSenable(); MQTTNetwork<Cellular> network(*radio); messageReady = false; MQTTClient mqttClient(network, username.c_str(), password.c_str(), clientID.c_str());