dd

Dependencies:   Cayenne-MQTT-mbed mbed ESP8266Interface

Committer:
jburhenn
Date:
Fri Nov 04 19:29:31 2016 +0000
Revision:
5:e4607ebed2f3
Parent:
4:8d67c9a3a66a
Child:
6:b90ebca0de01
Updated types and units.

Who changed what in which revision?

UserRevisionLine numberNew 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 3:508b266e010c 78 if ((error = mqttClient.publishResponse(message.channel, 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 0:803d1a77010e 132 MQTTTimer timer(5000);
jburhenn 0:803d1a77010e 133
jburhenn 0:803d1a77010e 134 while (true) {
jburhenn 0:803d1a77010e 135 // Yield to allow MQTT message processing.
jburhenn 0:803d1a77010e 136 mqttClient.yield(1000);
jburhenn 0:803d1a77010e 137
jburhenn 0:803d1a77010e 138 // Check that we are still connected, if not, reconnect.
jburhenn 0:803d1a77010e 139 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:803d1a77010e 140 network.disconnect();
jburhenn 0:803d1a77010e 141 mqttClient.disconnect();
jburhenn 0:803d1a77010e 142 printf("Reconnecting\n");
jburhenn 0:803d1a77010e 143 while (connectClient() != CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 144 wait(2);
jburhenn 0:803d1a77010e 145 printf("Reconnect failed, retrying\n");
jburhenn 0:803d1a77010e 146 }
jburhenn 0:803d1a77010e 147 }
jburhenn 0:803d1a77010e 148
jburhenn 0:803d1a77010e 149 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
jburhenn 0:803d1a77010e 150 if (timer.expired()) {
jburhenn 0:803d1a77010e 151 int error = 0;
jburhenn 5:e4607ebed2f3 152 if ((error = mqttClient.publishData(DATA_TOPIC, 0, TYPE_TEMPERATURE, UNIT_CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 153 printf("Publish temperature failed, error: %d\n", error);
jburhenn 0:803d1a77010e 154 }
jburhenn 5:e4607ebed2f3 155 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_LUMINOSITY, UNIT_LUX, 1000)) != CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 156 printf("Publish luminosity failed, error: %d\n", error);
jburhenn 0:803d1a77010e 157 }
jburhenn 5:e4607ebed2f3 158 if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 159 printf("Publish barometric pressure failed, error: %d\n", error);
jburhenn 0:803d1a77010e 160 }
jburhenn 0:803d1a77010e 161 timer.countdown_ms(5000);
jburhenn 0:803d1a77010e 162 }
jburhenn 0:803d1a77010e 163 }
jburhenn 0:803d1a77010e 164 }
jburhenn 0:803d1a77010e 165
jburhenn 0:803d1a77010e 166 /**
jburhenn 0:803d1a77010e 167 * Main function.
jburhenn 0:803d1a77010e 168 */
jburhenn 0:803d1a77010e 169 int main()
jburhenn 0:803d1a77010e 170 {
jburhenn 0:803d1a77010e 171 printf("Initialiizing interface\n");
jburhenn 0:803d1a77010e 172 interface.init();
jburhenn 0:803d1a77010e 173
jburhenn 0:803d1a77010e 174 // Set the default function that receives Cayenne messages.
jburhenn 0:803d1a77010e 175 mqttClient.setDefaultMessageHandler(messageArrived);
jburhenn 0:803d1a77010e 176
jburhenn 0:803d1a77010e 177 // Connect to Cayenne.
jburhenn 0:803d1a77010e 178 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 179 // Run main loop.
jburhenn 0:803d1a77010e 180 loop();
jburhenn 0:803d1a77010e 181 }
jburhenn 0:803d1a77010e 182 else {
jburhenn 0:803d1a77010e 183 printf("Connection failed, exiting\n");
jburhenn 0:803d1a77010e 184 }
jburhenn 0:803d1a77010e 185
jburhenn 0:803d1a77010e 186 if (mqttClient.connected())
jburhenn 0:803d1a77010e 187 mqttClient.disconnect();
jburhenn 0:803d1a77010e 188 if (network.connected())
jburhenn 0:803d1a77010e 189 network.disconnect();
jburhenn 0:803d1a77010e 190
jburhenn 0:803d1a77010e 191 return 0;
jburhenn 0:803d1a77010e 192 }
jburhenn 0:803d1a77010e 193