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