Example for connecting to Cayenne using the WIZnetInterface library.
Dependencies: Cayenne-MQTT-mbed WIZnetInterface mbed
Revision 0:1474d4e95b55, committed 2016-10-12
- Comitter:
- jburhenn
- Date:
- Wed Oct 12 18:56:25 2016 +0000
- Child:
- 1:f9c3eebc57ef
- Commit message:
- Example for connecting to Cayenne using the WIZnetInterface library.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Cayenne-MQTT-Embedded-C.lib Wed Oct 12 18:56:25 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/myDevicesIoT/code/Cayenne-MQTT-Embedded-C/#421366004b5d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnetInterface.lib Wed Oct 12 18:56:25 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Oct 12 18:56:25 2016 +0000
@@ -0,0 +1,183 @@
+/**
+* Example app for using the Cayenne MQTT C++ library to send and receive example data. This example uses
+* the WIZnetInterface library to connect via Ethernet.
+*/
+
+#include "MQTTTimer.h"
+#include "CayenneMQTTClient.h"
+#include "MQTTNetwork.h"
+#include "EthernetInterface.h"
+
+// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
+char* username = "MQTT_USERNAME";
+char* clientID = "CLIENT_ID";
+char* password = "MQTT_PASSWORD";
+
+SPI spi(D11, D12, D13);
+EthernetInterface interface(&spi, D10, D5); // MOSI, MISO, SCK, SEL, Reset
+MQTTNetwork<EthernetInterface> network(interface);
+Cayenne::MQTTClient<MQTTNetwork<EthernetInterface>, MQTTTimer> mqttClient(network);
+
+/**
+* Print the message info.
+* @param[in] message The message received from the Cayenne server.
+*/
+void outputMessage(Cayenne::MessageData& message)
+{
+ switch (message.topic) {
+ case COMMAND_TOPIC:
+ printf("topic=Command");
+ break;
+ case CONFIG_TOPIC:
+ printf("topic=Config");
+ break;
+ default:
+ printf("topic=%d", message.topic);
+ break;
+ }
+ printf(" channel=%d", message.channel);
+ if (message.clientID) {
+ printf(" clientID=%s", message.clientID);
+ }
+ if (message.type) {
+ printf(" type=%s", message.type);
+ }
+ for (size_t i = 0; i < message.valueCount; ++i) {
+ if (message.values[i].value) {
+ printf(" value=%s", message.values[i].value);
+ }
+ if (message.values[i].unit) {
+ printf(" unit=%s", message.values[i].unit);
+ }
+ }
+ if (message.id) {
+ printf(" id=%s", message.id);
+ }
+ printf("\n");
+}
+
+/**
+* Handle messages received from the Cayenne server.
+* @param[in] message The message received from the Cayenne server.
+*/
+void messageArrived(Cayenne::MessageData& message)
+{
+ int error = 0;
+ // Add code to process the message. Here we just ouput the message data.
+ outputMessage(message);
+
+ // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
+ // An error response should be sent if there are issues processing the message.
+ if (message.topic == COMMAND_TOPIC && (error = mqttClient.publishResponse(message.channel, message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
+ printf("Response failure, error: %d\n", error);
+ }
+}
+
+/**
+* Connect to the Cayenne server.
+* @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
+*/
+int connectClient(void)
+{
+ int error = 0;
+ // Connect to the server.
+ printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
+ while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
+ printf("TCP connect failed, error: %d\n", error);
+ wait(2);
+ }
+
+ if ((error = mqttClient.connect(username, clientID, password)) != MQTT::SUCCESS) {
+ printf("MQTT connect failed, error: %d\n", error);
+ return error;
+ }
+ printf("Connected\n");
+
+ // Subscribe to required topics.
+ if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
+ printf("Subscription to Command topic failed, error: %d\n", error);
+ }
+ if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
+ printf("Subscription to Config topic failed, error:%d\n", error);
+ }
+
+ // 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.
+ mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
+ mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
+ mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
+ mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
+
+ return CAYENNE_SUCCESS;
+}
+
+/**
+* Main loop where MQTT code is run.
+*/
+void loop(void)
+{
+ MQTTTimer timer(5000);
+
+ while (true) {
+ // Yield to allow MQTT message processing.
+ mqttClient.yield(1000);
+
+ // Check that we are still connected, if not, reconnect.
+ if (!network.connected() || !mqttClient.connected()) {
+ network.disconnect();
+ mqttClient.disconnect();
+ printf("Reconnecting\n");
+ while (connectClient() != CAYENNE_SUCCESS) {
+ wait(2);
+ printf("Reconnect failed, retrying\n");
+ }
+ }
+
+ // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
+ if (timer.expired()) {
+ int error = 0;
+ if ((error = mqttClient.publishData(DATA_TOPIC, 0, TEMPERATURE, CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
+ printf("Publish temperature failed, error: %d\n", error);
+ }
+ if ((error = mqttClient.publishData(DATA_TOPIC, 1, LUMINOSITY, LUX, 1000)) != CAYENNE_SUCCESS) {
+ printf("Publish luminosity failed, error: %d\n", error);
+ }
+ if ((error = mqttClient.publishData(DATA_TOPIC, 2, BAROMETRIC_PRESSURE, HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
+ printf("Publish barometric pressure failed, error: %d\n", error);
+ }
+ timer.countdown_ms(5000);
+ }
+ }
+}
+
+/**
+* Main function.
+*/
+int main()
+{
+ printf("Initializing interface\n");
+ // 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.
+ spi.frequency(42000000);
+ unsigned char MAC_Addr[6] = {0xFE,0x08,0xDC,0x12,0x34,0x56};
+ mbed_mac_address((char *)MAC_Addr); // Use mbed mac address
+ interface.init(MAC_Addr);
+
+ // Set the default function that receives Cayenne messages.
+ mqttClient.setDefaultMessageHandler(messageArrived);
+
+ // Connect to Cayenne.
+ if (connectClient() == CAYENNE_SUCCESS) {
+ // Run main loop.
+ loop();
+ }
+ else {
+ printf("Connection failed, exiting\n");
+ }
+
+ if (mqttClient.connected())
+ mqttClient.disconnect();
+ if (network.connected())
+ network.disconnect();
+
+ return 0;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Oct 12 18:56:25 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/25aea2a3f4e3 \ No newline at end of file
myDevicesIoT