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