Example for connecting to Cayenne using the WIZnet_Library library.

Dependencies:   Cayenne-MQTT-mbed WIZnet_Library mbed

Dependents:   STM32F103C8T6-Cayenne-WIZnet_SD1306_BMP280

Committer:
jburhenn
Date:
Fri Nov 11 18:26:34 2016 +0000
Revision:
8:be2ac38e5bb9
Parent:
5:f3de9c6ca8f5
Added comment to explain how to change the interval for publishing data.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:8ce4fc106e50 1 /**
jburhenn 0:8ce4fc106e50 2 * Example app for using the Cayenne MQTT C++ library to send and receive example data. This example uses
jburhenn 0:8ce4fc106e50 3 * the WIZnet_Library library to connect via Ethernet.
jburhenn 0:8ce4fc106e50 4 *
jburhenn 0:8ce4fc106e50 5 * NOTE: The WIZnet_Library defaults to using code for W5500. If you want to use a W5100 or W5200 code you need to
jburhenn 0:8ce4fc106e50 6 * set the appropriate define in wiznet.h.
jburhenn 0:8ce4fc106e50 7 */
jburhenn 0:8ce4fc106e50 8
jburhenn 0:8ce4fc106e50 9 #include "MQTTTimer.h"
jburhenn 0:8ce4fc106e50 10 #include "CayenneMQTTClient.h"
jburhenn 0:8ce4fc106e50 11 #include "MQTTNetwork.h"
jburhenn 0:8ce4fc106e50 12 #include "WIZnetInterface.h"
jburhenn 0:8ce4fc106e50 13
jburhenn 0:8ce4fc106e50 14 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
jburhenn 0:8ce4fc106e50 15 char* username = "MQTT_USERNAME";
jburhenn 3:9b9e0ca8956f 16 char* password = "MQTT_PASSWORD";
jburhenn 0:8ce4fc106e50 17 char* clientID = "CLIENT_ID";
jburhenn 0:8ce4fc106e50 18
jburhenn 0:8ce4fc106e50 19 SPI spi(D11, D12, D13);
jburhenn 0:8ce4fc106e50 20 WIZnetInterface interface(&spi, D10, D5); // SPI, SEL, Reset
jburhenn 0:8ce4fc106e50 21 MQTTNetwork<WIZnetInterface> network(interface);
jburhenn 2:472a2fd3359a 22 CayenneMQTT::MQTTClient<MQTTNetwork<WIZnetInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
jburhenn 0:8ce4fc106e50 23
jburhenn 0:8ce4fc106e50 24 /**
jburhenn 0:8ce4fc106e50 25 * Print the message info.
jburhenn 0:8ce4fc106e50 26 * @param[in] message The message received from the Cayenne server.
jburhenn 0:8ce4fc106e50 27 */
jburhenn 2:472a2fd3359a 28 void outputMessage(CayenneMQTT::MessageData& message)
jburhenn 0:8ce4fc106e50 29 {
jburhenn 0:8ce4fc106e50 30 switch (message.topic) {
jburhenn 0:8ce4fc106e50 31 case COMMAND_TOPIC:
jburhenn 0:8ce4fc106e50 32 printf("topic=Command");
jburhenn 0:8ce4fc106e50 33 break;
jburhenn 0:8ce4fc106e50 34 case CONFIG_TOPIC:
jburhenn 0:8ce4fc106e50 35 printf("topic=Config");
jburhenn 0:8ce4fc106e50 36 break;
jburhenn 0:8ce4fc106e50 37 default:
jburhenn 0:8ce4fc106e50 38 printf("topic=%d", message.topic);
jburhenn 0:8ce4fc106e50 39 break;
jburhenn 0:8ce4fc106e50 40 }
jburhenn 0:8ce4fc106e50 41 printf(" channel=%d", message.channel);
jburhenn 0:8ce4fc106e50 42 if (message.clientID) {
jburhenn 0:8ce4fc106e50 43 printf(" clientID=%s", message.clientID);
jburhenn 0:8ce4fc106e50 44 }
jburhenn 0:8ce4fc106e50 45 if (message.type) {
jburhenn 0:8ce4fc106e50 46 printf(" type=%s", message.type);
jburhenn 0:8ce4fc106e50 47 }
jburhenn 0:8ce4fc106e50 48 for (size_t i = 0; i < message.valueCount; ++i) {
jburhenn 2:472a2fd3359a 49 if (message.getValue(i)) {
jburhenn 2:472a2fd3359a 50 printf(" value=%s", message.getValue(i));
jburhenn 2:472a2fd3359a 51 }
jburhenn 2:472a2fd3359a 52 if (message.getUnit(i)) {
jburhenn 2:472a2fd3359a 53 printf(" unit=%s", message.getUnit(i));
jburhenn 2:472a2fd3359a 54 }
jburhenn 0:8ce4fc106e50 55 }
jburhenn 0:8ce4fc106e50 56 if (message.id) {
jburhenn 0:8ce4fc106e50 57 printf(" id=%s", message.id);
jburhenn 0:8ce4fc106e50 58 }
jburhenn 0:8ce4fc106e50 59 printf("\n");
jburhenn 0:8ce4fc106e50 60 }
jburhenn 0:8ce4fc106e50 61
jburhenn 0:8ce4fc106e50 62 /**
jburhenn 0:8ce4fc106e50 63 * Handle messages received from the Cayenne server.
jburhenn 0:8ce4fc106e50 64 * @param[in] message The message received from the Cayenne server.
jburhenn 0:8ce4fc106e50 65 */
jburhenn 2:472a2fd3359a 66 void messageArrived(CayenneMQTT::MessageData& message)
jburhenn 0:8ce4fc106e50 67 {
jburhenn 0:8ce4fc106e50 68 int error = 0;
jburhenn 0:8ce4fc106e50 69 // Add code to process the message. Here we just ouput the message data.
jburhenn 0:8ce4fc106e50 70 outputMessage(message);
jburhenn 0:8ce4fc106e50 71
jburhenn 2:472a2fd3359a 72 if (message.topic == COMMAND_TOPIC) {
jburhenn 2:472a2fd3359a 73 // If this is a command message we publish a response to show we recieved it. Here we are just sending a default 'OK' response.
jburhenn 2:472a2fd3359a 74 // An error response should be sent if there are issues processing the message.
jburhenn 5:f3de9c6ca8f5 75 if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
jburhenn 2:472a2fd3359a 76 printf("Response failure, error: %d\n", error);
jburhenn 2:472a2fd3359a 77 }
jburhenn 2:472a2fd3359a 78
jburhenn 2:472a2fd3359a 79 // Send the updated state for the channel so it is reflected in the Cayenne dashboard. If a command is successfully processed
jburhenn 2:472a2fd3359a 80 // the updated state will usually just be the value received in the command message.
jburhenn 2:472a2fd3359a 81 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
jburhenn 2:472a2fd3359a 82 printf("Publish state failure, error: %d\n", error);
jburhenn 2:472a2fd3359a 83 }
jburhenn 2:472a2fd3359a 84 }
jburhenn 0:8ce4fc106e50 85 }
jburhenn 0:8ce4fc106e50 86
jburhenn 0:8ce4fc106e50 87 /**
jburhenn 0:8ce4fc106e50 88 * Connect to the Cayenne server.
jburhenn 0:8ce4fc106e50 89 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
jburhenn 0:8ce4fc106e50 90 */
jburhenn 0:8ce4fc106e50 91 int connectClient(void)
jburhenn 0:8ce4fc106e50 92 {
jburhenn 0:8ce4fc106e50 93 int error = 0;
jburhenn 0:8ce4fc106e50 94 // Connect to the server.
jburhenn 0:8ce4fc106e50 95 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
jburhenn 0:8ce4fc106e50 96 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
jburhenn 0:8ce4fc106e50 97 printf("TCP connect failed, error: %d\n", error);
jburhenn 0:8ce4fc106e50 98 wait(2);
jburhenn 0:8ce4fc106e50 99 }
jburhenn 0:8ce4fc106e50 100
jburhenn 2:472a2fd3359a 101 if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
jburhenn 0:8ce4fc106e50 102 printf("MQTT connect failed, error: %d\n", error);
jburhenn 0:8ce4fc106e50 103 return error;
jburhenn 0:8ce4fc106e50 104 }
jburhenn 0:8ce4fc106e50 105 printf("Connected\n");
jburhenn 0:8ce4fc106e50 106
jburhenn 0:8ce4fc106e50 107 // Subscribe to required topics.
jburhenn 0:8ce4fc106e50 108 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:8ce4fc106e50 109 printf("Subscription to Command topic failed, error: %d\n", error);
jburhenn 0:8ce4fc106e50 110 }
jburhenn 0:8ce4fc106e50 111 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:8ce4fc106e50 112 printf("Subscription to Config topic failed, error:%d\n", error);
jburhenn 0:8ce4fc106e50 113 }
jburhenn 0:8ce4fc106e50 114
jburhenn 0:8ce4fc106e50 115 // 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.
jburhenn 0:8ce4fc106e50 116 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
jburhenn 0:8ce4fc106e50 117 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
jburhenn 2:472a2fd3359a 118 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
jburhenn 2:472a2fd3359a 119 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
jburhenn 0:8ce4fc106e50 120
jburhenn 0:8ce4fc106e50 121 return CAYENNE_SUCCESS;
jburhenn 0:8ce4fc106e50 122 }
jburhenn 0:8ce4fc106e50 123
jburhenn 0:8ce4fc106e50 124 /**
jburhenn 0:8ce4fc106e50 125 * Main loop where MQTT code is run.
jburhenn 0:8ce4fc106e50 126 */
jburhenn 0:8ce4fc106e50 127 void loop(void)
jburhenn 0:8ce4fc106e50 128 {
jburhenn 8:be2ac38e5bb9 129 // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
jburhenn 0:8ce4fc106e50 130 MQTTTimer timer(5000);
jburhenn 0:8ce4fc106e50 131
jburhenn 0:8ce4fc106e50 132 while (true) {
jburhenn 0:8ce4fc106e50 133 // Yield to allow MQTT message processing.
jburhenn 0:8ce4fc106e50 134 mqttClient.yield(1000);
jburhenn 0:8ce4fc106e50 135
jburhenn 0:8ce4fc106e50 136 // Check that we are still connected, if not, reconnect.
jburhenn 0:8ce4fc106e50 137 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:8ce4fc106e50 138 network.disconnect();
jburhenn 0:8ce4fc106e50 139 mqttClient.disconnect();
jburhenn 0:8ce4fc106e50 140 printf("Reconnecting\n");
jburhenn 0:8ce4fc106e50 141 while (connectClient() != CAYENNE_SUCCESS) {
jburhenn 0:8ce4fc106e50 142 wait(2);
jburhenn 0:8ce4fc106e50 143 printf("Reconnect failed, retrying\n");
jburhenn 0:8ce4fc106e50 144 }
jburhenn 0:8ce4fc106e50 145 }
jburhenn 0:8ce4fc106e50 146
jburhenn 0:8ce4fc106e50 147 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
jburhenn 0:8ce4fc106e50 148 if (timer.expired()) {
jburhenn 0:8ce4fc106e50 149 int error = 0;
jburhenn 4:58789e0a7b70 150 if ((error = mqttClient.publishData(DATA_TOPIC, 0, TYPE_TEMPERATURE, UNIT_CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
jburhenn 0:8ce4fc106e50 151 printf("Publish temperature failed, error: %d\n", error);
jburhenn 0:8ce4fc106e50 152 }
jburhenn 4:58789e0a7b70 153 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_LUMINOSITY, UNIT_LUX, 1000)) != CAYENNE_SUCCESS) {
jburhenn 0:8ce4fc106e50 154 printf("Publish luminosity failed, error: %d\n", error);
jburhenn 0:8ce4fc106e50 155 }
jburhenn 4:58789e0a7b70 156 if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
jburhenn 0:8ce4fc106e50 157 printf("Publish barometric pressure failed, error: %d\n", error);
jburhenn 0:8ce4fc106e50 158 }
jburhenn 8:be2ac38e5bb9 159 // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
jburhenn 0:8ce4fc106e50 160 timer.countdown_ms(5000);
jburhenn 0:8ce4fc106e50 161 }
jburhenn 0:8ce4fc106e50 162 }
jburhenn 0:8ce4fc106e50 163 }
jburhenn 0:8ce4fc106e50 164
jburhenn 0:8ce4fc106e50 165 /**
jburhenn 0:8ce4fc106e50 166 * Main function.
jburhenn 0:8ce4fc106e50 167 */
jburhenn 0:8ce4fc106e50 168 int main()
jburhenn 0:8ce4fc106e50 169 {
jburhenn 0:8ce4fc106e50 170 printf("Initializing interface\n");
jburhenn 0:8ce4fc106e50 171 // 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.
jburhenn 0:8ce4fc106e50 172 spi.frequency(42000000);
jburhenn 0:8ce4fc106e50 173 unsigned char MAC_Addr[6] = {0xFE,0x08,0xDC,0x12,0x34,0x56};
jburhenn 0:8ce4fc106e50 174 mbed_mac_address((char *)MAC_Addr); // Use mbed mac address
jburhenn 0:8ce4fc106e50 175 interface.init(MAC_Addr);
jburhenn 0:8ce4fc106e50 176
jburhenn 0:8ce4fc106e50 177 // Set the default function that receives Cayenne messages.
jburhenn 0:8ce4fc106e50 178 mqttClient.setDefaultMessageHandler(messageArrived);
jburhenn 0:8ce4fc106e50 179
jburhenn 0:8ce4fc106e50 180 // Connect to Cayenne.
jburhenn 0:8ce4fc106e50 181 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:8ce4fc106e50 182 // Run main loop.
jburhenn 0:8ce4fc106e50 183 loop();
jburhenn 0:8ce4fc106e50 184 }
jburhenn 0:8ce4fc106e50 185 else {
jburhenn 0:8ce4fc106e50 186 printf("Connection failed, exiting\n");
jburhenn 0:8ce4fc106e50 187 }
jburhenn 0:8ce4fc106e50 188
jburhenn 0:8ce4fc106e50 189 if (mqttClient.connected())
jburhenn 0:8ce4fc106e50 190 mqttClient.disconnect();
jburhenn 0:8ce4fc106e50 191 if (network.connected())
jburhenn 0:8ce4fc106e50 192 network.disconnect();
jburhenn 0:8ce4fc106e50 193
jburhenn 0:8ce4fc106e50 194 return 0;
jburhenn 0:8ce4fc106e50 195 }
jburhenn 0:8ce4fc106e50 196