![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Example of sending MQTT data to MyDevices Cayenne using the MTSAS library
Dependencies: Cayenne-MQTT-mbed-MTSAS X_NUCLEO_IKS01A1 mbed mtsas_lat3
Diff: main.cpp
- Revision:
- 5:960d9d8974c8
- Parent:
- 4:2b412949efb9
- Child:
- 6:cd0be5cc1943
--- a/main.cpp Tue Apr 25 22:02:01 2017 +0000 +++ b/main.cpp Mon Nov 20 21:19:07 2017 +0000 @@ -15,11 +15,20 @@ 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"; +string password = "68e890972b6cc0fc47fcd152554db7e78ec9b29f"; +string clientID = "5fee1000-aeaa-11e7-98fe-97ee227ec16c"; -DigitalOut Led1Out(LED1); +DigitalOut Led1Out(D1); +DigitalOut Led2Out(D0); +DigitalOut Led3Out(D3); +DigitalOut Led4Out(D6); +DigitalOut Led5Out(D6); +DigitalOut Led6Out(D8); +DigitalOut Led7Out(D5); +DigitalOut Led8Out(D4); +DigitalOut Led9Out(D7); +DigitalOut LedStatus(D2); // Debug serial port //static Serial debug(USBTX, USBRX); @@ -43,6 +52,7 @@ 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"; CayenneMQTT::MessageData lastMessage; bool messageReady; @@ -61,6 +71,10 @@ if (! radio) return false; + logInfo("setting APN"); + if (radio->setApn(apn) != MTS_SUCCESS) + logError("failed to set APN to \"%s\"", apn); + Transport::setTransport(radio); while (! radio->connect()) { logError("failed to bring up PPP link"); @@ -110,6 +124,24 @@ } /** +* +* +*/ +void setLEDs(bool newState) +{ + // note: false = lit LED + Led1Out = !newState; + Led2Out = !newState; + Led3Out = !newState; + Led4Out = !newState; + Led5Out = !newState; + Led6Out = !newState; + Led7Out = !newState; + Led8Out = !newState; + Led9Out = !newState; +} + +/** * Handle messages received from the Cayenne server. * @param[in] message The message received from the Cayenne server. */ @@ -180,9 +212,11 @@ switch(lastMessage.channel) { case 0: // Set the onboard LED state - Led1Out = atoi(lastMessage.getValue()); + bool value = atoi(lastMessage.getValue()); + setLEDs(value); + // Publish the updated LED state - if ((error = mqttClient.publishData(DATA_TOPIC, lastMessage.channel, NULL, NULL, Led1Out.read())) != CAYENNE_SUCCESS) { + if ((error = mqttClient.publishData(DATA_TOPIC, lastMessage.channel, NULL, NULL, Led1Out.read()==0?1:0)) != CAYENNE_SUCCESS) { printf("Publish LED state failure, error: %d\r\n", error); } break; @@ -200,11 +234,13 @@ if (!network.connected() || !mqttClient.connected()) { network.disconnect(); mqttClient.disconnect(); + LedStatus = true; printf("Reconnecting\r\n"); while (connectClient(mqttClient, network) != CAYENNE_SUCCESS) { wait(2); printf("Reconnect failed, retrying\r\n"); } + LedStatus = false; } // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne. @@ -226,8 +262,8 @@ if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, temp_data)) != CAYENNE_SUCCESS) { printf("Publish barometric pressure failed, error: %d\r\n", error); } - printf("Led is: %s\r\n", Led1Out.read() ? "on" : "off"); - if ((error = mqttClient.publishData(DATA_TOPIC, 0, "digital_actuator", UNIT_DIGITAL, Led1Out.read())) != CAYENNE_SUCCESS) { + printf("Led is: %s\r\n", Led1Out.read() > 0 ? "off" : "on"); + 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); } // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. @@ -242,7 +278,8 @@ int main() { pc.baud(115200); - Led1Out = 0; + setLEDs(false); + LedStatus = true; mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); // init radio, setup Cayenne connection if (!init_mtsas()) { @@ -251,12 +288,14 @@ wait(1); } } + // Test with a ping if(radio->ping("www.google.com")){ printf("Ping test succeeded!\r\n"); } else { printf("Failed ping test!\r\n"); } + MQTTNetwork<Cellular> network(*radio); messageReady = false; MQTTClient mqttClient(network, username.c_str(), password.c_str(), clientID.c_str()); @@ -267,6 +306,7 @@ // Connect to Cayenne. if (connectClient(mqttClient, network) == CAYENNE_SUCCESS) { // Run main loop. + LedStatus = false; loop(mqttClient, network); } else {