STM32F103C8T6-Cayenne-WIZnet_SD1306_BMP280

Dependencies:   Cayenne-MQTT-mbed mbed Cayenne-WIZnet_Library WIZnet_Library BME280

Committer:
jburhenn
Date:
Wed Oct 12 20:04:14 2016 +0000
Revision:
0:8ce4fc106e50
Child:
2:472a2fd3359a
Example for connecting to Cayenne using the WIZnet_Library library.

Who changed what in which revision?

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