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