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