Test
Dependencies: Cayenne-MQTT-mbed ESP8266Interface mbed
Fork of Cayenne-ESP8266Interface by
Diff: main.cpp
- Revision:
- 3:508b266e010c
- Parent:
- 1:9043adf6ae53
- Child:
- 4:8d67c9a3a66a
diff -r ac118f9586a6 -r 508b266e010c main.cpp --- a/main.cpp Thu Oct 20 23:01:22 2016 +0000 +++ b/main.cpp Wed Oct 26 23:05:25 2016 +0000 @@ -14,18 +14,18 @@ // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. char* username = "MQTT_USERNAME"; +char* password = "MQTT_PASSWORD"; char* clientID = "CLIENT_ID"; -char* password = "MQTT_PASSWORD"; ESP8266Interface interface(D8, D2, D3, ssid, wifiPassword, 115200); // TX, RX, Reset, SSID, Password, Baud MQTTNetwork<ESP8266Interface> network(interface); -Cayenne::MQTTClient<MQTTNetwork<ESP8266Interface>, MQTTTimer> mqttClient(network); +CayenneMQTT::MQTTClient<MQTTNetwork<ESP8266Interface>, MQTTTimer> mqttClient(network, username, password, clientID); /** * Print the message info. * @param[in] message The message received from the Cayenne server. */ -void outputMessage(Cayenne::MessageData& message) +void outputMessage(CayenneMQTT::MessageData& message) { switch (message.topic) { case COMMAND_TOPIC: @@ -46,12 +46,12 @@ 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.getValue(i)) { + printf(" value=%s", message.getValue(i)); + } + if (message.getUnit(i)) { + printf(" unit=%s", message.getUnit(i)); + } } if (message.id) { printf(" id=%s", message.id); @@ -63,17 +63,25 @@ * Handle messages received from the Cayenne server. * @param[in] message The message received from the Cayenne server. */ -void messageArrived(Cayenne::MessageData& message) +void messageArrived(CayenneMQTT::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); - } + if (message.topic == COMMAND_TOPIC) { + // If this is a command message we publish a response to show we recieved it. Here we are just sending a default 'OK' response. + // An error response should be sent if there are issues processing the message. + if ((error = mqttClient.publishResponse(message.channel, message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) { + printf("Response failure, error: %d\n", error); + } + + // Send the updated state for the channel so it is reflected in the Cayenne dashboard. If a command is successfully processed + // the updated state will usually just be the value received in the command message. + if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) { + printf("Publish state failure, error: %d\n", error); + } + } } /** @@ -90,7 +98,7 @@ wait(2); } - if ((error = mqttClient.connect(username, clientID, password)) != MQTT::SUCCESS) { + if ((error = mqttClient.connect()) != MQTT::SUCCESS) { printf("MQTT connect failed, error: %d\n", error); return error; } @@ -107,8 +115,8 @@ // 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"); + //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; }