Ultrasonic-Servo Combo

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

Committer:
stiotchallenge
Date:
Fri Apr 26 02:58:51 2019 +0000
Revision:
8:b19f86c36974
Parent:
7:78cefe0937ab
Child:
9:e3a211745f71
smkhk_mail

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