dd

Dependencies:   Cayenne-MQTT-mbed mbed ESP8266Interface

Committer:
jburhenn
Date:
Wed Oct 12 18:20:48 2016 +0000
Revision:
0:803d1a77010e
Child:
1:9043adf6ae53
Example for connecting to Cayenne using the ESP8266Interface library.

Who changed what in which revision?

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