myDevicesIoT / Mbed 2 deprecated Cayenne-WIZnetInterface

Dependencies:   Cayenne-MQTT-mbed WIZnetInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 * Example app for using the Cayenne MQTT C++ library to send and receive example data. This example uses
00003 * the WIZnetInterface library to connect via Ethernet.
00004 */
00005 
00006 #include "MQTTTimer.h"
00007 #include "CayenneMQTTClient.h"
00008 #include "MQTTNetwork.h"
00009 #include "EthernetInterface.h"
00010     
00011 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
00012 char* username = "MQTT_USERNAME";
00013 char* password = "MQTT_PASSWORD";
00014 char* clientID = "CLIENT_ID";
00015 
00016 SPI spi(D11, D12, D13);
00017 EthernetInterface interface(&spi, D10, D5); // SPI, SEL, Reset
00018 MQTTNetwork<EthernetInterface> network(interface);
00019 CayenneMQTT::MQTTClient<MQTTNetwork<EthernetInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
00020 
00021 /**
00022 * Print the message info.
00023 * @param[in] message The message received from the Cayenne server.
00024 */
00025 void outputMessage(CayenneMQTT::MessageData& message)
00026 {
00027     switch (message.topic)  {
00028     case COMMAND_TOPIC:
00029         printf("topic=Command");
00030         break;
00031     case CONFIG_TOPIC:
00032         printf("topic=Config");
00033         break;
00034     default:
00035         printf("topic=%d", message.topic);
00036         break;
00037     }
00038     printf(" channel=%d", message.channel);
00039     if (message.clientID) {
00040         printf(" clientID=%s", message.clientID);
00041     }
00042     if (message.type) {
00043         printf(" type=%s", message.type);
00044     }
00045     for (size_t i = 0; i < message.valueCount; ++i) {
00046         if (message.getValue(i)) {
00047             printf(" value=%s", message.getValue(i));
00048         }
00049         if (message.getUnit(i)) {
00050             printf(" unit=%s", message.getUnit(i));
00051         }
00052     }
00053     if (message.id) {
00054         printf(" id=%s", message.id);
00055     }
00056     printf("\n");
00057 }
00058 
00059 /**
00060 * Handle messages received from the Cayenne server.
00061 * @param[in] message The message received from the Cayenne server.
00062 */
00063 void messageArrived(CayenneMQTT::MessageData& message)
00064 {
00065     int error = 0;
00066     // Add code to process the message. Here we just ouput the message data.
00067     outputMessage(message);
00068 
00069     if (message.topic == COMMAND_TOPIC) {
00070         // If this is a command message we publish a response to show we recieved it. Here we are just sending a default 'OK' response.
00071         // An error response should be sent if there are issues processing the message.
00072         if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
00073             printf("Response failure, error: %d\n", error);
00074         }
00075             
00076         // Send the updated state for the channel so it is reflected in the Cayenne dashboard. If a command is successfully processed
00077         // the updated state will usually just be the value received in the command message.
00078         if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
00079             printf("Publish state failure, error: %d\n", error);
00080         }
00081     }
00082 }
00083 
00084 /**
00085 * Connect to the Cayenne server.
00086 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
00087 */
00088 int connectClient(void)
00089 {
00090     int error = 0;
00091     // Connect to the server.
00092     printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
00093     while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
00094         printf("TCP connect failed, error: %d\n", error);
00095         wait(2);
00096     }
00097 
00098     if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
00099         printf("MQTT connect failed, error: %d\n", error);
00100         return error;
00101     }
00102     printf("Connected\n");
00103 
00104     // Subscribe to required topics.
00105     if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
00106         printf("Subscription to Command topic failed, error: %d\n", error);
00107     }
00108     if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
00109         printf("Subscription to Config topic failed, error:%d\n", error);
00110     }
00111 
00112     // 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.
00113     mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
00114     mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
00115     //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
00116     //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
00117 
00118     return CAYENNE_SUCCESS;
00119 }
00120 
00121 /**
00122 * Main loop where MQTT code is run.
00123 */
00124 void loop(void)
00125 {
00126     // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
00127     MQTTTimer timer(5000);
00128 
00129     while (true) {
00130         // Yield to allow MQTT message processing.
00131         mqttClient.yield(1000);
00132 
00133         // Check that we are still connected, if not, reconnect.
00134         if (!network.connected() || !mqttClient.connected()) {
00135             network.disconnect();
00136             mqttClient.disconnect();
00137             printf("Reconnecting\n");
00138             while (connectClient() != CAYENNE_SUCCESS) {
00139                 wait(2);
00140                 printf("Reconnect failed, retrying\n");
00141             }
00142         }
00143 
00144         // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
00145         if (timer.expired()) {
00146             int error = 0;
00147             if ((error = mqttClient.publishData(DATA_TOPIC, 0, TYPE_TEMPERATURE, UNIT_CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
00148                 printf("Publish temperature failed, error: %d\n", error);
00149             }
00150             if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_LUMINOSITY, UNIT_LUX, 1000)) != CAYENNE_SUCCESS) {
00151                 printf("Publish luminosity failed, error: %d\n", error);
00152             }
00153             if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
00154                 printf("Publish barometric pressure failed, error: %d\n", error);
00155             }
00156             // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
00157             timer.countdown_ms(5000);
00158         }
00159     }
00160 }
00161 
00162 /**
00163 * Main function.
00164 */
00165 int main()
00166 {
00167     printf("Initializing interface\n");
00168     // Set the correct SPI frequency for your shield, if necessary. For example, 42000000 for Arduino Ethernet Shield W5500 or 20000000 for Arduino Ethernet Shield W5100.
00169     spi.frequency(42000000);
00170     unsigned char MAC_Addr[6] = {0xFE,0x08,0xDC,0x12,0x34,0x56};
00171     mbed_mac_address((char *)MAC_Addr); // Use mbed mac address
00172     interface.init(MAC_Addr);   
00173 
00174     // Set the default function that receives Cayenne messages.
00175     mqttClient.setDefaultMessageHandler(messageArrived);
00176 
00177     // Connect to Cayenne.
00178     if (connectClient() == CAYENNE_SUCCESS) {
00179         // Run main loop.
00180         loop();
00181     }
00182     else {
00183         printf("Connection failed, exiting\n");
00184     }
00185 
00186     if (mqttClient.connected())
00187         mqttClient.disconnect();
00188     if (network.connected())
00189         network.disconnect();
00190 
00191     return 0;
00192 }
00193