Test

Dependencies:   Cayenne-MQTT-mbed ESP8266Interface mbed

Fork of Cayenne-ESP8266Interface by myDevicesIoT

Committer:
stema
Date:
Wed Oct 11 14:01:48 2017 +0000
Revision:
10:4f0cd8d9fbd5
Parent:
9:4302de820980
Test;

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.
stema 10:4f0cd8d9fbd5 12 char* wifi_ssid = "FERRARELLE";
stema 10:4f0cd8d9fbd5 13 char* wifi_password = "effervescentenaturale2017";
jburhenn 0:803d1a77010e 14
jburhenn 0:803d1a77010e 15 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
stema 10:4f0cd8d9fbd5 16 char* username = "4c713520-d0e3-11e6-ae8c-f90e83be502a";
stema 10:4f0cd8d9fbd5 17 char* password = "93c1cdfaafe5a296a36d6e5247eb73c2b1ab01d4";
stema 10:4f0cd8d9fbd5 18 char* clientID = "3070fd00-ae74-11e7-8c02-137ff2c4ffef";
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.
stema 10:4f0cd8d9fbd5 21 #define CAYENNE_IP "34.226.39.40"
jburhenn 4:8d67c9a3a66a 22
stema 10:4f0cd8d9fbd5 23 ESP8266Interface interface(D8, D2, D3, wifi_ssid, wifi_password, 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
stema 10:4f0cd8d9fbd5 135 static float temperature = 0;
stema 10:4f0cd8d9fbd5 136 static float luminosity = 10;
stema 10:4f0cd8d9fbd5 137 static float pressure = -10;
stema 10:4f0cd8d9fbd5 138
jburhenn 0:803d1a77010e 139 while (true) {
jburhenn 0:803d1a77010e 140 // Yield to allow MQTT message processing.
jburhenn 0:803d1a77010e 141 mqttClient.yield(1000);
jburhenn 0:803d1a77010e 142
jburhenn 0:803d1a77010e 143 // Check that we are still connected, if not, reconnect.
jburhenn 0:803d1a77010e 144 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:803d1a77010e 145 network.disconnect();
jburhenn 0:803d1a77010e 146 mqttClient.disconnect();
jburhenn 0:803d1a77010e 147 printf("Reconnecting\n");
jburhenn 0:803d1a77010e 148 while (connectClient() != CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 149 wait(2);
jburhenn 0:803d1a77010e 150 printf("Reconnect failed, retrying\n");
jburhenn 0:803d1a77010e 151 }
jburhenn 0:803d1a77010e 152 }
jburhenn 0:803d1a77010e 153
jburhenn 0:803d1a77010e 154 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
jburhenn 0:803d1a77010e 155 if (timer.expired()) {
jburhenn 0:803d1a77010e 156 int error = 0;
stema 10:4f0cd8d9fbd5 157 if ((error = mqttClient.publishData(DATA_TOPIC, 0, TYPE_TEMPERATURE, UNIT_CELSIUS, temperature++)) != CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 158 printf("Publish temperature failed, error: %d\n", error);
jburhenn 0:803d1a77010e 159 }
stema 10:4f0cd8d9fbd5 160 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_LUMINOSITY, UNIT_LUX, luminosity++)) != CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 161 printf("Publish luminosity failed, error: %d\n", error);
jburhenn 0:803d1a77010e 162 }
stema 10:4f0cd8d9fbd5 163 if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, pressure++)) != CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 164 printf("Publish barometric pressure failed, error: %d\n", error);
jburhenn 0:803d1a77010e 165 }
jburhenn 9:4302de820980 166 // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
jburhenn 0:803d1a77010e 167 timer.countdown_ms(5000);
jburhenn 0:803d1a77010e 168 }
jburhenn 0:803d1a77010e 169 }
jburhenn 0:803d1a77010e 170 }
jburhenn 0:803d1a77010e 171
jburhenn 0:803d1a77010e 172 /**
jburhenn 0:803d1a77010e 173 * Main function.
jburhenn 0:803d1a77010e 174 */
jburhenn 0:803d1a77010e 175 int main()
jburhenn 0:803d1a77010e 176 {
jburhenn 0:803d1a77010e 177 printf("Initialiizing interface\n");
jburhenn 0:803d1a77010e 178 interface.init();
jburhenn 0:803d1a77010e 179
jburhenn 0:803d1a77010e 180 // Set the default function that receives Cayenne messages.
jburhenn 0:803d1a77010e 181 mqttClient.setDefaultMessageHandler(messageArrived);
jburhenn 0:803d1a77010e 182
jburhenn 0:803d1a77010e 183 // Connect to Cayenne.
jburhenn 0:803d1a77010e 184 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:803d1a77010e 185 // Run main loop.
jburhenn 0:803d1a77010e 186 loop();
jburhenn 0:803d1a77010e 187 }
jburhenn 0:803d1a77010e 188 else {
jburhenn 0:803d1a77010e 189 printf("Connection failed, exiting\n");
jburhenn 0:803d1a77010e 190 }
jburhenn 0:803d1a77010e 191
jburhenn 0:803d1a77010e 192 if (mqttClient.connected())
jburhenn 0:803d1a77010e 193 mqttClient.disconnect();
jburhenn 0:803d1a77010e 194 if (network.connected())
jburhenn 0:803d1a77010e 195 network.disconnect();
jburhenn 0:803d1a77010e 196
jburhenn 0:803d1a77010e 197 return 0;
jburhenn 0:803d1a77010e 198 }
jburhenn 0:803d1a77010e 199