Ultrasonic-Servo Combo

Dependencies:   Cayenne-MQTT-mbed mbed Servo X_NUCLEO_IDW01M1v2 NetworkSocketAPI HCSR04

Committer:
jburhenn
Date:
Fri Nov 04 19:39:28 2016 +0000
Revision:
3:4056f1cd4c47
Parent:
2:445f09de09bd
Child:
4:939f308d004a
Updated types and units.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:d20655cd1f3c 1 /**
jburhenn 0:d20655cd1f3c 2 * Example app for using the Cayenne MQTT C++ library to send and receive example data. This example uses
jburhenn 0:d20655cd1f3c 3 * the X-NUCLEO-IDW01M1 WiFi expansion board via the X_NUCLEO_IDW01M1v2 library.
jburhenn 0:d20655cd1f3c 4 */
jburhenn 0:d20655cd1f3c 5
jburhenn 0:d20655cd1f3c 6 #include "MQTTTimer.h"
jburhenn 0:d20655cd1f3c 7 #include "CayenneMQTTClient.h"
jburhenn 0:d20655cd1f3c 8 #include "MQTTNetworkIDW01M1.h"
jburhenn 0:d20655cd1f3c 9 #include "SpwfInterface.h"
jburhenn 0:d20655cd1f3c 10
jburhenn 0:d20655cd1f3c 11 // WiFi network info.
jburhenn 0:d20655cd1f3c 12 char* ssid = "ssid";
jburhenn 0:d20655cd1f3c 13 char* wifiPassword = "wifiPassword";
jburhenn 0:d20655cd1f3c 14
jburhenn 0:d20655cd1f3c 15 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
jburhenn 0:d20655cd1f3c 16 char* username = "MQTT_USERNAME";
jburhenn 2:445f09de09bd 17 char* password = "MQTT_PASSWORD";
jburhenn 0:d20655cd1f3c 18 char* clientID = "CLIENT_ID";
jburhenn 0:d20655cd1f3c 19
jburhenn 0:d20655cd1f3c 20 SpwfSAInterface interface(D8, D2); // TX, RX
jburhenn 0:d20655cd1f3c 21 MQTTNetwork<SpwfSAInterface> network(interface);
jburhenn 2:445f09de09bd 22 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
jburhenn 0:d20655cd1f3c 23
jburhenn 1:a2d8aec2bb92 24 DigitalOut led1(LED1);
jburhenn 1:a2d8aec2bb92 25
jburhenn 0:d20655cd1f3c 26 /**
jburhenn 0:d20655cd1f3c 27 * Print the message info.
jburhenn 0:d20655cd1f3c 28 * @param[in] message The message received from the Cayenne server.
jburhenn 0:d20655cd1f3c 29 */
jburhenn 2:445f09de09bd 30 void outputMessage(CayenneMQTT::MessageData& message)
jburhenn 0:d20655cd1f3c 31 {
jburhenn 0:d20655cd1f3c 32 switch (message.topic) {
jburhenn 0:d20655cd1f3c 33 case COMMAND_TOPIC:
jburhenn 0:d20655cd1f3c 34 printf("topic=Command");
jburhenn 0:d20655cd1f3c 35 break;
jburhenn 0:d20655cd1f3c 36 case CONFIG_TOPIC:
jburhenn 0:d20655cd1f3c 37 printf("topic=Config");
jburhenn 0:d20655cd1f3c 38 break;
jburhenn 0:d20655cd1f3c 39 default:
jburhenn 0:d20655cd1f3c 40 printf("topic=%d", message.topic);
jburhenn 0:d20655cd1f3c 41 break;
jburhenn 0:d20655cd1f3c 42 }
jburhenn 0:d20655cd1f3c 43 printf(" channel=%d", message.channel);
jburhenn 0:d20655cd1f3c 44 if (message.clientID) {
jburhenn 0:d20655cd1f3c 45 printf(" clientID=%s", message.clientID);
jburhenn 0:d20655cd1f3c 46 }
jburhenn 0:d20655cd1f3c 47 if (message.type) {
jburhenn 0:d20655cd1f3c 48 printf(" type=%s", message.type);
jburhenn 0:d20655cd1f3c 49 }
jburhenn 0:d20655cd1f3c 50 for (size_t i = 0; i < message.valueCount; ++i) {
jburhenn 2:445f09de09bd 51 if (message.getValue(i)) {
jburhenn 2:445f09de09bd 52 printf(" value=%s", message.getValue(i));
jburhenn 0:d20655cd1f3c 53 }
jburhenn 2:445f09de09bd 54 if (message.getUnit(i)) {
jburhenn 2:445f09de09bd 55 printf(" unit=%s", message.getUnit(i));
jburhenn 0:d20655cd1f3c 56 }
jburhenn 0:d20655cd1f3c 57 }
jburhenn 0:d20655cd1f3c 58 if (message.id) {
jburhenn 0:d20655cd1f3c 59 printf(" id=%s", message.id);
jburhenn 0:d20655cd1f3c 60 }
jburhenn 0:d20655cd1f3c 61 printf("\n");
jburhenn 0:d20655cd1f3c 62 }
jburhenn 0:d20655cd1f3c 63
jburhenn 0:d20655cd1f3c 64 /**
jburhenn 0:d20655cd1f3c 65 * Handle messages received from the Cayenne server.
jburhenn 0:d20655cd1f3c 66 * @param[in] message The message received from the Cayenne server.
jburhenn 0:d20655cd1f3c 67 */
jburhenn 2:445f09de09bd 68 void messageArrived(CayenneMQTT::MessageData& message)
jburhenn 0:d20655cd1f3c 69 {
jburhenn 0:d20655cd1f3c 70 int error = 0;
jburhenn 0:d20655cd1f3c 71 // Add code to process the message. Here we just ouput the message data.
jburhenn 0:d20655cd1f3c 72 outputMessage(message);
jburhenn 0:d20655cd1f3c 73
jburhenn 1:a2d8aec2bb92 74 if (message.topic == COMMAND_TOPIC) {
jburhenn 1:a2d8aec2bb92 75 switch(message.channel) {
jburhenn 1:a2d8aec2bb92 76 case 0:
jburhenn 1:a2d8aec2bb92 77 // Set the onboard LED state
jburhenn 2:445f09de09bd 78 led1 = atoi(message.getValue());
jburhenn 1:a2d8aec2bb92 79 // Publish the updated LED state
jburhenn 2:445f09de09bd 80 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
jburhenn 1:a2d8aec2bb92 81 printf("Publish LED state failure, error: %d\n", error);
jburhenn 1:a2d8aec2bb92 82 }
jburhenn 1:a2d8aec2bb92 83 break;
jburhenn 1:a2d8aec2bb92 84 }
jburhenn 1:a2d8aec2bb92 85
jburhenn 1:a2d8aec2bb92 86 // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
jburhenn 1:a2d8aec2bb92 87 // An error response should be sent if there are issues processing the message.
jburhenn 1:a2d8aec2bb92 88 if ((error = mqttClient.publishResponse(message.channel, message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
jburhenn 1:a2d8aec2bb92 89 printf("Response failure, error: %d\n", error);
jburhenn 1:a2d8aec2bb92 90 }
jburhenn 0:d20655cd1f3c 91 }
jburhenn 0:d20655cd1f3c 92 }
jburhenn 0:d20655cd1f3c 93
jburhenn 0:d20655cd1f3c 94 /**
jburhenn 0:d20655cd1f3c 95 * Connect to the Cayenne server.
jburhenn 0:d20655cd1f3c 96 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
jburhenn 0:d20655cd1f3c 97 */
jburhenn 0:d20655cd1f3c 98 int connectClient(void)
jburhenn 0:d20655cd1f3c 99 {
jburhenn 0:d20655cd1f3c 100 int error = 0;
jburhenn 0:d20655cd1f3c 101 // Connect to the server.
jburhenn 0:d20655cd1f3c 102 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
jburhenn 0:d20655cd1f3c 103 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
jburhenn 0:d20655cd1f3c 104 printf("TCP connect failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 105 wait(2);
jburhenn 0:d20655cd1f3c 106 }
jburhenn 0:d20655cd1f3c 107
jburhenn 2:445f09de09bd 108 if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
jburhenn 0:d20655cd1f3c 109 printf("MQTT connect failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 110 return error;
jburhenn 0:d20655cd1f3c 111 }
jburhenn 0:d20655cd1f3c 112 printf("Connected\n");
jburhenn 0:d20655cd1f3c 113
jburhenn 0:d20655cd1f3c 114 // Subscribe to required topics.
jburhenn 0:d20655cd1f3c 115 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 116 printf("Subscription to Command topic failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 117 }
jburhenn 0:d20655cd1f3c 118 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 119 printf("Subscription to Config topic failed, error:%d\n", error);
jburhenn 0:d20655cd1f3c 120 }
jburhenn 0:d20655cd1f3c 121
jburhenn 0:d20655cd1f3c 122 // 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:d20655cd1f3c 123 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
jburhenn 0:d20655cd1f3c 124 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
jburhenn 2:445f09de09bd 125 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
jburhenn 2:445f09de09bd 126 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
jburhenn 0:d20655cd1f3c 127
jburhenn 0:d20655cd1f3c 128 return CAYENNE_SUCCESS;
jburhenn 0:d20655cd1f3c 129 }
jburhenn 0:d20655cd1f3c 130
jburhenn 0:d20655cd1f3c 131 /**
jburhenn 0:d20655cd1f3c 132 * Main loop where MQTT code is run.
jburhenn 0:d20655cd1f3c 133 */
jburhenn 0:d20655cd1f3c 134 void loop(void)
jburhenn 0:d20655cd1f3c 135 {
jburhenn 0:d20655cd1f3c 136 MQTTTimer timer(5000);
jburhenn 0:d20655cd1f3c 137
jburhenn 0:d20655cd1f3c 138 while (true) {
jburhenn 0:d20655cd1f3c 139 // Yield to allow MQTT message processing.
jburhenn 0:d20655cd1f3c 140 mqttClient.yield(1000);
jburhenn 0:d20655cd1f3c 141
jburhenn 0:d20655cd1f3c 142 // Check that we are still connected, if not, reconnect.
jburhenn 0:d20655cd1f3c 143 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:d20655cd1f3c 144 network.disconnect();
jburhenn 0:d20655cd1f3c 145 mqttClient.disconnect();
jburhenn 0:d20655cd1f3c 146 printf("Reconnecting\n");
jburhenn 0:d20655cd1f3c 147 while (connectClient() != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 148 wait(2);
jburhenn 0:d20655cd1f3c 149 printf("Reconnect failed, retrying\n");
jburhenn 0:d20655cd1f3c 150 }
jburhenn 0:d20655cd1f3c 151 }
jburhenn 0:d20655cd1f3c 152
jburhenn 0:d20655cd1f3c 153 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
jburhenn 0:d20655cd1f3c 154 if (timer.expired()) {
jburhenn 0:d20655cd1f3c 155 int error = 0;
jburhenn 3:4056f1cd4c47 156 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 157 printf("Publish temperature failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 158 }
jburhenn 3:4056f1cd4c47 159 if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_LUMINOSITY, UNIT_LUX, 1000)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 160 printf("Publish luminosity failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 161 }
jburhenn 3:4056f1cd4c47 162 if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 163 printf("Publish barometric pressure failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 164 }
jburhenn 0:d20655cd1f3c 165 timer.countdown_ms(5000);
jburhenn 0:d20655cd1f3c 166 }
jburhenn 0:d20655cd1f3c 167 }
jburhenn 0:d20655cd1f3c 168 }
jburhenn 0:d20655cd1f3c 169
jburhenn 0:d20655cd1f3c 170 /**
jburhenn 0:d20655cd1f3c 171 * Main function.
jburhenn 0:d20655cd1f3c 172 */
jburhenn 0:d20655cd1f3c 173 int main()
jburhenn 0:d20655cd1f3c 174 {
jburhenn 0:d20655cd1f3c 175 // Initialize the network interface.
jburhenn 0:d20655cd1f3c 176 printf("Initializing interface\n");
jburhenn 0:d20655cd1f3c 177 interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
jburhenn 0:d20655cd1f3c 178
jburhenn 0:d20655cd1f3c 179 // Set the default function that receives Cayenne messages.
jburhenn 0:d20655cd1f3c 180 mqttClient.setDefaultMessageHandler(messageArrived);
jburhenn 0:d20655cd1f3c 181
jburhenn 0:d20655cd1f3c 182 // Connect to Cayenne.
jburhenn 0:d20655cd1f3c 183 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 184 // Run main loop.
jburhenn 0:d20655cd1f3c 185 loop();
jburhenn 0:d20655cd1f3c 186 }
jburhenn 0:d20655cd1f3c 187 else {
jburhenn 0:d20655cd1f3c 188 printf("Connection failed, exiting\n");
jburhenn 0:d20655cd1f3c 189 }
jburhenn 0:d20655cd1f3c 190
jburhenn 0:d20655cd1f3c 191 if (mqttClient.connected())
jburhenn 0:d20655cd1f3c 192 mqttClient.disconnect();
jburhenn 0:d20655cd1f3c 193 if (network.connected())
jburhenn 0:d20655cd1f3c 194 network.disconnect();
jburhenn 0:d20655cd1f3c 195
jburhenn 0:d20655cd1f3c 196 return 0;
jburhenn 0:d20655cd1f3c 197 }
jburhenn 0:d20655cd1f3c 198