Test

Dependencies:   Cayenne-MQTT-mbed ESP8266Interface mbed

Fork of Cayenne-ESP8266Interface by myDevicesIoT

Committer:
jburhenn
Date:
Wed Oct 12 18:50:54 2016 +0000
Revision:
1:9043adf6ae53
Parent:
0:803d1a77010e
Child:
3:508b266e010c
Added app description.

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