smkhk_bin
Dependencies: Cayenne-MQTT-mbed mbed Servo X_NUCLEO_IDW01M1v2 NetworkSocketAPI HCSR04
main.cpp@0:d20655cd1f3c, 2016-10-07 (annotated)
- Committer:
- jburhenn
- Date:
- Fri Oct 07 18:30:51 2016 +0000
- Revision:
- 0:d20655cd1f3c
- Child:
- 1:a2d8aec2bb92
Example for connecting to Cayenne using an X-NUCLEO-IDW01M1 WiFi expansion board.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jburhenn | 0:d20655cd1f3c | 1 | /** |
jburhenn | 0:d20655cd1f3c | 2 | * Example app for using the Cayenne MQTT C++ library to send and receive example data. This example uses |
jburhenn | 0:d20655cd1f3c | 3 | * the X-NUCLEO-IDW01M1 WiFi expansion board via the X_NUCLEO_IDW01M1v2 library. |
jburhenn | 0:d20655cd1f3c | 4 | */ |
jburhenn | 0:d20655cd1f3c | 5 | |
jburhenn | 0:d20655cd1f3c | 6 | #include "MQTTTimer.h" |
jburhenn | 0:d20655cd1f3c | 7 | #include "CayenneMQTTClient.h" |
jburhenn | 0:d20655cd1f3c | 8 | #include "MQTTNetworkIDW01M1.h" |
jburhenn | 0:d20655cd1f3c | 9 | #include "SpwfInterface.h" |
jburhenn | 0:d20655cd1f3c | 10 | |
jburhenn | 0:d20655cd1f3c | 11 | // WiFi network info. |
jburhenn | 0:d20655cd1f3c | 12 | char* ssid = "ssid"; |
jburhenn | 0:d20655cd1f3c | 13 | char* wifiPassword = "wifiPassword"; |
jburhenn | 0:d20655cd1f3c | 14 | |
jburhenn | 0:d20655cd1f3c | 15 | // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. |
jburhenn | 0:d20655cd1f3c | 16 | char* username = "MQTT_USERNAME"; |
jburhenn | 0:d20655cd1f3c | 17 | char* clientID = "CLIENT_ID"; |
jburhenn | 0:d20655cd1f3c | 18 | char* password = "MQTT_PASSWORD"; |
jburhenn | 0:d20655cd1f3c | 19 | |
jburhenn | 0:d20655cd1f3c | 20 | SpwfSAInterface interface(D8, D2); // TX, RX |
jburhenn | 0:d20655cd1f3c | 21 | MQTTNetwork<SpwfSAInterface> network(interface); |
jburhenn | 0:d20655cd1f3c | 22 | Cayenne::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network); |
jburhenn | 0:d20655cd1f3c | 23 | |
jburhenn | 0:d20655cd1f3c | 24 | /** |
jburhenn | 0:d20655cd1f3c | 25 | * Print the message info. |
jburhenn | 0:d20655cd1f3c | 26 | * @param[in] message The message received from the Cayenne server. |
jburhenn | 0:d20655cd1f3c | 27 | */ |
jburhenn | 0:d20655cd1f3c | 28 | void outputMessage(Cayenne::MessageData& message) |
jburhenn | 0:d20655cd1f3c | 29 | { |
jburhenn | 0:d20655cd1f3c | 30 | switch (message.topic) { |
jburhenn | 0:d20655cd1f3c | 31 | case COMMAND_TOPIC: |
jburhenn | 0:d20655cd1f3c | 32 | printf("topic=Command"); |
jburhenn | 0:d20655cd1f3c | 33 | break; |
jburhenn | 0:d20655cd1f3c | 34 | case CONFIG_TOPIC: |
jburhenn | 0:d20655cd1f3c | 35 | printf("topic=Config"); |
jburhenn | 0:d20655cd1f3c | 36 | break; |
jburhenn | 0:d20655cd1f3c | 37 | default: |
jburhenn | 0:d20655cd1f3c | 38 | printf("topic=%d", message.topic); |
jburhenn | 0:d20655cd1f3c | 39 | break; |
jburhenn | 0:d20655cd1f3c | 40 | } |
jburhenn | 0:d20655cd1f3c | 41 | printf(" channel=%d", message.channel); |
jburhenn | 0:d20655cd1f3c | 42 | if (message.clientID) { |
jburhenn | 0:d20655cd1f3c | 43 | printf(" clientID=%s", message.clientID); |
jburhenn | 0:d20655cd1f3c | 44 | } |
jburhenn | 0:d20655cd1f3c | 45 | if (message.type) { |
jburhenn | 0:d20655cd1f3c | 46 | printf(" type=%s", message.type); |
jburhenn | 0:d20655cd1f3c | 47 | } |
jburhenn | 0:d20655cd1f3c | 48 | for (size_t i = 0; i < message.valueCount; ++i) { |
jburhenn | 0:d20655cd1f3c | 49 | if (message.values[i].value) { |
jburhenn | 0:d20655cd1f3c | 50 | printf(" value=%s", message.values[i].value); |
jburhenn | 0:d20655cd1f3c | 51 | } |
jburhenn | 0:d20655cd1f3c | 52 | if (message.values[i].unit) { |
jburhenn | 0:d20655cd1f3c | 53 | printf(" unit=%s", message.values[i].unit); |
jburhenn | 0:d20655cd1f3c | 54 | } |
jburhenn | 0:d20655cd1f3c | 55 | } |
jburhenn | 0:d20655cd1f3c | 56 | if (message.id) { |
jburhenn | 0:d20655cd1f3c | 57 | printf(" id=%s", message.id); |
jburhenn | 0:d20655cd1f3c | 58 | } |
jburhenn | 0:d20655cd1f3c | 59 | printf("\n"); |
jburhenn | 0:d20655cd1f3c | 60 | } |
jburhenn | 0:d20655cd1f3c | 61 | |
jburhenn | 0:d20655cd1f3c | 62 | /** |
jburhenn | 0:d20655cd1f3c | 63 | * Handle messages received from the Cayenne server. |
jburhenn | 0:d20655cd1f3c | 64 | * @param[in] message The message received from the Cayenne server. |
jburhenn | 0:d20655cd1f3c | 65 | */ |
jburhenn | 0:d20655cd1f3c | 66 | void messageArrived(Cayenne::MessageData& message) |
jburhenn | 0:d20655cd1f3c | 67 | { |
jburhenn | 0:d20655cd1f3c | 68 | int error = 0; |
jburhenn | 0:d20655cd1f3c | 69 | // Add code to process the message. Here we just ouput the message data. |
jburhenn | 0:d20655cd1f3c | 70 | outputMessage(message); |
jburhenn | 0:d20655cd1f3c | 71 | |
jburhenn | 0:d20655cd1f3c | 72 | // If this is a command message we publish a response. Here we are just sending a default 'OK' response. |
jburhenn | 0:d20655cd1f3c | 73 | // An error response should be sent if there are issues processing the message. |
jburhenn | 0:d20655cd1f3c | 74 | if (message.topic == COMMAND_TOPIC && (error = mqttClient.publishResponse(message.channel, message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 75 | printf("Response failure, error: %d\n", error); |
jburhenn | 0:d20655cd1f3c | 76 | } |
jburhenn | 0:d20655cd1f3c | 77 | } |
jburhenn | 0:d20655cd1f3c | 78 | |
jburhenn | 0:d20655cd1f3c | 79 | /** |
jburhenn | 0:d20655cd1f3c | 80 | * Connect to the Cayenne server. |
jburhenn | 0:d20655cd1f3c | 81 | * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise. |
jburhenn | 0:d20655cd1f3c | 82 | */ |
jburhenn | 0:d20655cd1f3c | 83 | int connectClient(void) |
jburhenn | 0:d20655cd1f3c | 84 | { |
jburhenn | 0:d20655cd1f3c | 85 | int error = 0; |
jburhenn | 0:d20655cd1f3c | 86 | // Connect to the server. |
jburhenn | 0:d20655cd1f3c | 87 | printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT); |
jburhenn | 0:d20655cd1f3c | 88 | while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) { |
jburhenn | 0:d20655cd1f3c | 89 | printf("TCP connect failed, error: %d\n", error); |
jburhenn | 0:d20655cd1f3c | 90 | wait(2); |
jburhenn | 0:d20655cd1f3c | 91 | } |
jburhenn | 0:d20655cd1f3c | 92 | |
jburhenn | 0:d20655cd1f3c | 93 | if ((error = mqttClient.connect(username, clientID, password)) != MQTT::SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 94 | printf("MQTT connect failed, error: %d\n", error); |
jburhenn | 0:d20655cd1f3c | 95 | return error; |
jburhenn | 0:d20655cd1f3c | 96 | } |
jburhenn | 0:d20655cd1f3c | 97 | printf("Connected\n"); |
jburhenn | 0:d20655cd1f3c | 98 | |
jburhenn | 0:d20655cd1f3c | 99 | // Subscribe to required topics. |
jburhenn | 0:d20655cd1f3c | 100 | if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 101 | printf("Subscription to Command topic failed, error: %d\n", error); |
jburhenn | 0:d20655cd1f3c | 102 | } |
jburhenn | 0:d20655cd1f3c | 103 | if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 104 | printf("Subscription to Config topic failed, error:%d\n", error); |
jburhenn | 0:d20655cd1f3c | 105 | } |
jburhenn | 0:d20655cd1f3c | 106 | |
jburhenn | 0:d20655cd1f3c | 107 | // 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:d20655cd1f3c | 108 | mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION); |
jburhenn | 0:d20655cd1f3c | 109 | mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice"); |
jburhenn | 0:d20655cd1f3c | 110 | mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model"); |
jburhenn | 0:d20655cd1f3c | 111 | mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000"); |
jburhenn | 0:d20655cd1f3c | 112 | |
jburhenn | 0:d20655cd1f3c | 113 | return CAYENNE_SUCCESS; |
jburhenn | 0:d20655cd1f3c | 114 | } |
jburhenn | 0:d20655cd1f3c | 115 | |
jburhenn | 0:d20655cd1f3c | 116 | /** |
jburhenn | 0:d20655cd1f3c | 117 | * Main loop where MQTT code is run. |
jburhenn | 0:d20655cd1f3c | 118 | */ |
jburhenn | 0:d20655cd1f3c | 119 | void loop(void) |
jburhenn | 0:d20655cd1f3c | 120 | { |
jburhenn | 0:d20655cd1f3c | 121 | MQTTTimer timer(5000); |
jburhenn | 0:d20655cd1f3c | 122 | |
jburhenn | 0:d20655cd1f3c | 123 | while (true) { |
jburhenn | 0:d20655cd1f3c | 124 | // Yield to allow MQTT message processing. |
jburhenn | 0:d20655cd1f3c | 125 | mqttClient.yield(1000); |
jburhenn | 0:d20655cd1f3c | 126 | |
jburhenn | 0:d20655cd1f3c | 127 | // Check that we are still connected, if not, reconnect. |
jburhenn | 0:d20655cd1f3c | 128 | if (!network.connected() || !mqttClient.connected()) { |
jburhenn | 0:d20655cd1f3c | 129 | network.disconnect(); |
jburhenn | 0:d20655cd1f3c | 130 | mqttClient.disconnect(); |
jburhenn | 0:d20655cd1f3c | 131 | printf("Reconnecting\n"); |
jburhenn | 0:d20655cd1f3c | 132 | while (connectClient() != CAYENNE_SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 133 | wait(2); |
jburhenn | 0:d20655cd1f3c | 134 | printf("Reconnect failed, retrying\n"); |
jburhenn | 0:d20655cd1f3c | 135 | } |
jburhenn | 0:d20655cd1f3c | 136 | } |
jburhenn | 0:d20655cd1f3c | 137 | |
jburhenn | 0:d20655cd1f3c | 138 | // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne. |
jburhenn | 0:d20655cd1f3c | 139 | if (timer.expired()) { |
jburhenn | 0:d20655cd1f3c | 140 | int error = 0; |
jburhenn | 0:d20655cd1f3c | 141 | if ((error = mqttClient.publishData(DATA_TOPIC, 0, TEMPERATURE, CELSIUS, 30.5)) != CAYENNE_SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 142 | printf("Publish temperature failed, error: %d\n", error); |
jburhenn | 0:d20655cd1f3c | 143 | } |
jburhenn | 0:d20655cd1f3c | 144 | if ((error = mqttClient.publishData(DATA_TOPIC, 1, LUMINOSITY, LUX, 1000)) != CAYENNE_SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 145 | printf("Publish luminosity failed, error: %d\n", error); |
jburhenn | 0:d20655cd1f3c | 146 | } |
jburhenn | 0:d20655cd1f3c | 147 | if ((error = mqttClient.publishData(DATA_TOPIC, 2, BAROMETRIC_PRESSURE, HECTOPASCAL, 800)) != CAYENNE_SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 148 | printf("Publish barometric pressure failed, error: %d\n", error); |
jburhenn | 0:d20655cd1f3c | 149 | } |
jburhenn | 0:d20655cd1f3c | 150 | timer.countdown_ms(5000); |
jburhenn | 0:d20655cd1f3c | 151 | } |
jburhenn | 0:d20655cd1f3c | 152 | } |
jburhenn | 0:d20655cd1f3c | 153 | } |
jburhenn | 0:d20655cd1f3c | 154 | |
jburhenn | 0:d20655cd1f3c | 155 | /** |
jburhenn | 0:d20655cd1f3c | 156 | * Main function. |
jburhenn | 0:d20655cd1f3c | 157 | */ |
jburhenn | 0:d20655cd1f3c | 158 | int main() |
jburhenn | 0:d20655cd1f3c | 159 | { |
jburhenn | 0:d20655cd1f3c | 160 | // Initialize the network interface. |
jburhenn | 0:d20655cd1f3c | 161 | printf("Initializing interface\n"); |
jburhenn | 0:d20655cd1f3c | 162 | interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2); |
jburhenn | 0:d20655cd1f3c | 163 | |
jburhenn | 0:d20655cd1f3c | 164 | // Set the default function that receives Cayenne messages. |
jburhenn | 0:d20655cd1f3c | 165 | mqttClient.setDefaultMessageHandler(messageArrived); |
jburhenn | 0:d20655cd1f3c | 166 | |
jburhenn | 0:d20655cd1f3c | 167 | // Connect to Cayenne. |
jburhenn | 0:d20655cd1f3c | 168 | if (connectClient() == CAYENNE_SUCCESS) { |
jburhenn | 0:d20655cd1f3c | 169 | // Run main loop. |
jburhenn | 0:d20655cd1f3c | 170 | loop(); |
jburhenn | 0:d20655cd1f3c | 171 | } |
jburhenn | 0:d20655cd1f3c | 172 | else { |
jburhenn | 0:d20655cd1f3c | 173 | printf("Connection failed, exiting\n"); |
jburhenn | 0:d20655cd1f3c | 174 | } |
jburhenn | 0:d20655cd1f3c | 175 | |
jburhenn | 0:d20655cd1f3c | 176 | if (mqttClient.connected()) |
jburhenn | 0:d20655cd1f3c | 177 | mqttClient.disconnect(); |
jburhenn | 0:d20655cd1f3c | 178 | if (network.connected()) |
jburhenn | 0:d20655cd1f3c | 179 | network.disconnect(); |
jburhenn | 0:d20655cd1f3c | 180 | |
jburhenn | 0:d20655cd1f3c | 181 | return 0; |
jburhenn | 0:d20655cd1f3c | 182 | } |
jburhenn | 0:d20655cd1f3c | 183 |