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

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

Revision:
0:5107fce16490
Child:
2:abc89d2aede3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Apr 25 15:33:14 2017 +0000
@@ -0,0 +1,279 @@
+#include "mbed.h"
+
+#include "mtsas.h"
+
+#include "MQTTTimer.h"
+#include "CayenneMQTTClient.h"
+#include "MQTTNetwork.h"
+
+#include "x_nucleo_iks01a1.h"
+
+#include <string>
+#include <sstream>
+
+using std::string;
+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 = "CLIENT_ID";
+
+DigitalOut Led1Out(LED1);
+
+// Debug serial port
+//static Serial debug(USBTX, USBRX);
+Serial pc(USBTX, USBRX);
+
+// MTSSerialFlowControl - serial link between processor and radio
+static MTSSerialFlowControl* io;
+
+// Cellular - radio object for cellular operations (SMS, TCP, etc)
+Cellular* radio;
+
+/* Instantiate the expansion board */
+static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL);
+
+/* Retrieve the composing elements of the expansion board */
+static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
+static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
+static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
+static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
+static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
+static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
+static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
+
+
+CayenneMQTT::MessageData lastMessage;
+bool messageReady;
+
+/*
+* Initialize cellular radio.
+*/
+bool init_mtsas()
+{
+    io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
+    if (! io)
+        return false;
+
+    io->baud(115200);
+    radio = CellularFactory::create(io);
+    if (! radio)
+        return false;
+
+    Transport::setTransport(radio);
+    while (! radio->connect()) {
+        logError("failed to bring up PPP link");
+        wait(2);
+    }
+    
+    printf("Signal Strength: %d\n\r", radio->getSignalStrength()); 
+    return true;
+}
+
+/**
+* Print the message info.
+* @param[in] message The message received from the Cayenne server.
+*/
+void outputMessage(CayenneMQTT::MessageData& message)
+{
+    switch (message.topic)  {
+    case COMMAND_TOPIC:
+        printf("topic=Command");
+        break;
+    case CONFIG_TOPIC:
+        printf("topic=Config");
+        break;
+    default:
+        printf("topic=%d", message.topic);
+        break;
+    }
+    printf(" channel=%d", message.channel);
+    if (message.clientID) {
+        printf(" clientID=%s", message.clientID);
+    }
+    if (message.type) {
+        printf(" type=%s", message.type);
+    }
+    for (size_t i = 0; i < message.valueCount; ++i) {
+        if (message.getValue(i)) {
+            printf(" value=%s", message.getValue(i));
+        }
+        if (message.getUnit(i)) {
+            printf(" unit=%s", message.getUnit(i));
+        }
+    }
+    if (message.id) {
+        printf(" id=%s", message.id);
+    }
+    printf("\n");
+}
+
+/**
+* Handle messages received from the Cayenne server.
+* @param[in] message The message received from the Cayenne server.
+*/
+void messageArrived(CayenneMQTT::MessageData& message)
+{
+    int error = 0;
+    //note: if you change this example to use mbed-os you will need a mutex
+    lastMessage = message;
+    messageReady = true;
+
+}
+
+/**
+* Connect to the Cayenne server.
+* @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
+*/
+int connectClient(MQTTClient &mqttClient, MQTTNetwork<Cellular> &network)
+{
+    int error = 0;
+    // Connect to the server.
+    printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
+    while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
+        printf("TCP connect failed, error: %d\n", error);
+        wait(2);
+    }
+
+    if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
+        printf("MQTT connect failed, error: %d\n", error);
+        return error;
+    }
+    printf("Connected\n");
+
+    // Subscribe to required topics.
+    if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
+        printf("Subscription to Command topic failed, error: %d\n", error);
+    }
+    if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
+        printf("Subscription to Config topic failed, error:%d\n", error);
+    }
+
+    // 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.
+    mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
+    mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
+    //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
+    //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
+
+    return CAYENNE_SUCCESS;
+}
+
+/**
+* Main loop where MQTT code is run.
+*/
+void loop(MQTTClient &mqttClient, MQTTNetwork<Cellular> &network)
+{
+    // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
+    MQTTTimer timer(5000);
+    printf("Starting loop.\n");
+    while (true) {
+        // Yield to allow MQTT message processing.
+        mqttClient.yield(1000);
+        if(messageReady){
+            int error = 0;
+            messageReady = false;
+            // Add code to process the message. Here we just ouput the message data.
+            outputMessage(lastMessage);
+        
+            if (lastMessage.topic == COMMAND_TOPIC) {
+                switch(lastMessage.channel) {
+                case 0:
+                    // Set the onboard LED state
+                    Led1Out = atoi(lastMessage.getValue());
+                    // Publish the updated LED state
+                    if ((error = mqttClient.publishData(DATA_TOPIC, lastMessage.channel, NULL, NULL, lastMessage.getValue())) != CAYENNE_SUCCESS) {
+                        printf("Publish LED state failure, error: %d\n", error);
+                    }
+                    break;
+                }
+                
+                // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
+                // An error response should be sent if there are issues processing the message.
+                if ((error = mqttClient.publishResponse(lastMessage.id, NULL, lastMessage.clientID)) != CAYENNE_SUCCESS) {
+                    printf("Response failure, error: %d\n", error);
+                }
+            }
+        }
+
+        // Check that we are still connected, if not, reconnect.
+        if (!network.connected() || !mqttClient.connected()) {
+            network.disconnect();
+            mqttClient.disconnect();
+            printf("Reconnecting\n");
+            while (connectClient(mqttClient, network) != CAYENNE_SUCCESS) {
+                wait(2);
+                printf("Reconnect failed, retrying\n");
+            }
+        }
+
+        // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
+        if (timer.expired()) {
+            Led1Out = 1;
+            int error = 0;
+            float temp_data;
+            temp_sensor1->get_temperature(&temp_data);
+            printf("Temperature was: %f \n", temp_data);
+            if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, temp_data)) != CAYENNE_SUCCESS) {
+                printf("Publish temperature failed, error: %d\n", error);
+            }
+            humidity_sensor->get_humidity(&temp_data);
+            printf("Humidity was: %f \n", temp_data);
+            if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT, temp_data)) != CAYENNE_SUCCESS) {
+                printf("Publish luminosity failed, error: %d\n", error);
+            }
+            pressure_sensor->get_pressure(&temp_data);
+            printf("Pressure was: %f \n", temp_data);
+            if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, temp_data)) != CAYENNE_SUCCESS) {
+                printf("Publish barometric pressure failed, error: %d\n", error);
+            }
+            Led1Out = 0;
+            // 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 {
+            printf("Timer: %d", timer.left_ms());
+        }
+    }
+}
+
+int main()
+{
+    pc.baud(115200);
+    Led1Out = 0;
+    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
+    // init radio, setup Cayenne connection
+    if (!init_mtsas()) {
+        while (true) {
+            logError("failed to initialize cellular radio");
+            wait(1);
+        }
+    }
+    // Test with a ping
+    if(radio->ping("www.google.com")){
+        printf("Ping test succeeded!\n");
+    } else {
+        printf("Failed ping test!\n");
+    }
+    MQTTNetwork<Cellular> network(*radio);
+    messageReady = false;
+    MQTTClient mqttClient(network, username.c_str(), password.c_str(), clientID.c_str());
+
+    // Set the default function that receives Cayenne messages.
+    mqttClient.setDefaultMessageHandler(messageArrived);
+
+    // Connect to Cayenne.
+    if (connectClient(mqttClient, network) == CAYENNE_SUCCESS) {
+        // Run main loop.
+        loop(mqttClient, network);
+    }
+    else {
+        printf("Connection failed, exiting\n");
+    }
+
+    if (mqttClient.connected())
+        mqttClient.disconnect();
+    if (network.connected())
+        network.disconnect();
+    
+    return 0;
+}