Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832 LM75B MQ2 MQTT
main.cpp@17:0811bdbdd78a, 2015-07-27 (annotated)
- Committer:
- icraggs
- Date:
- Mon Jul 27 09:16:08 2015 +0000
- Revision:
- 17:0811bdbdd78a
- Parent:
- 16:28d062c5522b
- Child:
- 18:07a79d8f01c3
Make sure QoS 2 works
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| icraggs | 1:a1d5c7a6acbc | 1 | /******************************************************************************* | 
| icraggs | 17:0811bdbdd78a | 2 | * Copyright (c) 2014, 2015 IBM Corp. | 
| icraggs | 1:a1d5c7a6acbc | 3 | * | 
| icraggs | 1:a1d5c7a6acbc | 4 | * All rights reserved. This program and the accompanying materials | 
| icraggs | 1:a1d5c7a6acbc | 5 | * are made available under the terms of the Eclipse Public License v1.0 | 
| icraggs | 1:a1d5c7a6acbc | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. | 
| icraggs | 1:a1d5c7a6acbc | 7 | * | 
| icraggs | 1:a1d5c7a6acbc | 8 | * The Eclipse Public License is available at | 
| icraggs | 1:a1d5c7a6acbc | 9 | * http://www.eclipse.org/legal/epl-v10.html | 
| icraggs | 1:a1d5c7a6acbc | 10 | * and the Eclipse Distribution License is available at | 
| icraggs | 1:a1d5c7a6acbc | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. | 
| icraggs | 1:a1d5c7a6acbc | 12 | * | 
| icraggs | 1:a1d5c7a6acbc | 13 | * Contributors: | 
| icraggs | 1:a1d5c7a6acbc | 14 | * Ian Craggs - initial API and implementation and/or initial documentation | 
| icraggs | 17:0811bdbdd78a | 15 | * Ian Craggs - make sure QoS2 processing works, and add device headers | 
| icraggs | 1:a1d5c7a6acbc | 16 | *******************************************************************************/ | 
| icraggs | 2:638c854c0695 | 17 | |
| icraggs | 2:638c854c0695 | 18 | /** | 
| icraggs | 2:638c854c0695 | 19 | This is a sample program to illustrate the use of the MQTT Client library | 
| icraggs | 2:638c854c0695 | 20 | on the mbed platform. The Client class requires two classes which mediate | 
| icraggs | 2:638c854c0695 | 21 | access to system interfaces for networking and timing. As long as these two | 
| icraggs | 2:638c854c0695 | 22 | classes provide the required public programming interfaces, it does not matter | 
| icraggs | 2:638c854c0695 | 23 | what facilities they use underneath. In this program, they use the mbed | 
| icraggs | 2:638c854c0695 | 24 | system libraries. | 
| icraggs | 2:638c854c0695 | 25 | |
| icraggs | 2:638c854c0695 | 26 | */ | 
| icraggs | 1:a1d5c7a6acbc | 27 | |
| icraggs | 17:0811bdbdd78a | 28 | #include "C12832.h" | 
| icraggs | 2:638c854c0695 | 29 | |
| icraggs | 17:0811bdbdd78a | 30 | #if defined(TARGET_UBLOX_C027) | 
| icraggs | 17:0811bdbdd78a | 31 | #warning "Compiling for mbed C027" | 
| icraggs | 17:0811bdbdd78a | 32 | #include "C027.h" | 
| icraggs | 17:0811bdbdd78a | 33 | #elif defined(TARGET_LPC1768) | 
| icraggs | 17:0811bdbdd78a | 34 | #warning "Compiling for mbed LPC1768" | 
| icraggs | 17:0811bdbdd78a | 35 | #include "LPC1768.h" | 
| icraggs | 17:0811bdbdd78a | 36 | #elif defined(TARGET_K64F) | 
| icraggs | 17:0811bdbdd78a | 37 | #warning "Compiling for mbed K64F" | 
| icraggs | 17:0811bdbdd78a | 38 | #include "K64F.h" | 
| icraggs | 17:0811bdbdd78a | 39 | #endif | 
| icraggs | 17:0811bdbdd78a | 40 | |
| icraggs | 17:0811bdbdd78a | 41 | #define MQTTCLIENT_QOS2 1 | 
| icraggs | 0:0cae29831d01 | 42 | |
| icraggs | 8:a3e3113054a1 | 43 | #include "MQTTEthernet.h" | 
| icraggs | 2:638c854c0695 | 44 | #include "MQTTClient.h" | 
| icraggs | 2:638c854c0695 | 45 | |
| icraggs | 2:638c854c0695 | 46 | int arrivedcount = 0; | 
| icraggs | 2:638c854c0695 | 47 | |
| icraggs | 8:a3e3113054a1 | 48 | |
| icraggs | 9:5beb8609e9f7 | 49 | void messageArrived(MQTT::MessageData& md) | 
| icraggs | 2:638c854c0695 | 50 | { | 
| icraggs | 9:5beb8609e9f7 | 51 | MQTT::Message &message = md.message; | 
| icraggs | 9:5beb8609e9f7 | 52 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); | 
| icraggs | 9:5beb8609e9f7 | 53 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); | 
| icraggs | 2:638c854c0695 | 54 | ++arrivedcount; | 
| icraggs | 17:0811bdbdd78a | 55 | lcd.cls(); | 
| icraggs | 9:5beb8609e9f7 | 56 | lcd.puts((char*)message.payload); | 
| icraggs | 2:638c854c0695 | 57 | } | 
| icraggs | 0:0cae29831d01 | 58 | |
| icraggs | 2:638c854c0695 | 59 | |
| icraggs | 2:638c854c0695 | 60 | int main(int argc, char* argv[]) | 
| icraggs | 2:638c854c0695 | 61 | { | 
| icraggs | 8:a3e3113054a1 | 62 | MQTTEthernet ipstack = MQTTEthernet(); | 
| icraggs | 17:0811bdbdd78a | 63 | float version = 0.5; | 
| icraggs | 2:638c854c0695 | 64 | char* topic = "mbed-sample"; | 
| icraggs | 2:638c854c0695 | 65 | |
| icraggs | 17:0811bdbdd78a | 66 | lcd.cls(); | 
| icraggs | 17:0811bdbdd78a | 67 | lcd.printf("HelloMQTT: version is %f\n", version); | 
| icraggs | 2:638c854c0695 | 68 | |
| icraggs | 8:a3e3113054a1 | 69 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); | 
| icraggs | 3:7a6a899de7cc | 70 | |
| icraggs | 6:e4c690c45021 | 71 | char* hostname = "m2m.eclipse.org"; | 
| icraggs | 6:e4c690c45021 | 72 | int port = 1883; | 
| icraggs | 17:0811bdbdd78a | 73 | lcd.cls(); | 
| icraggs | 6:e4c690c45021 | 74 | lcd.printf("Connecting to %s:%d\n", hostname, port); | 
| icraggs | 6:e4c690c45021 | 75 | int rc = ipstack.connect(hostname, port); | 
| icraggs | 6:e4c690c45021 | 76 | if (rc != 0) | 
| icraggs | 17:0811bdbdd78a | 77 | { | 
| icraggs | 17:0811bdbdd78a | 78 | lcd.cls(); | 
| icraggs | 6:e4c690c45021 | 79 | lcd.printf("rc from TCP connect is %d\n", rc); | 
| icraggs | 17:0811bdbdd78a | 80 | } | 
| icraggs | 6:e4c690c45021 | 81 | |
| icraggs | 6:e4c690c45021 | 82 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; | 
| icraggs | 6:e4c690c45021 | 83 | data.MQTTVersion = 3; | 
| icraggs | 8:a3e3113054a1 | 84 | data.clientID.cstring = "mbed-sample"; | 
| icraggs | 12:086a9314e8a5 | 85 | data.username.cstring = "testuser"; | 
| icraggs | 12:086a9314e8a5 | 86 | data.password.cstring = "testpassword"; | 
| icraggs | 16:28d062c5522b | 87 | if ((rc = client.connect(data)) != 0) | 
| icraggs | 17:0811bdbdd78a | 88 | { | 
| icraggs | 17:0811bdbdd78a | 89 | lcd.cls(); | 
| icraggs | 6:e4c690c45021 | 90 | lcd.printf("rc from MQTT connect is %d\n", rc); | 
| icraggs | 17:0811bdbdd78a | 91 | } | 
| icraggs | 2:638c854c0695 | 92 | |
| icraggs | 8:a3e3113054a1 | 93 | if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) | 
| icraggs | 17:0811bdbdd78a | 94 | { | 
| icraggs | 17:0811bdbdd78a | 95 | lcd.cls(); | 
| icraggs | 8:a3e3113054a1 | 96 | lcd.printf("rc from MQTT subscribe is %d\n", rc); | 
| icraggs | 17:0811bdbdd78a | 97 | } | 
| icraggs | 2:638c854c0695 | 98 | |
| icraggs | 2:638c854c0695 | 99 | MQTT::Message message; | 
| icraggs | 0:0cae29831d01 | 100 | |
| icraggs | 2:638c854c0695 | 101 | // QoS 0 | 
| icraggs | 2:638c854c0695 | 102 | char buf[100]; | 
| icraggs | 2:638c854c0695 | 103 | sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); | 
| icraggs | 2:638c854c0695 | 104 | message.qos = MQTT::QOS0; | 
| icraggs | 2:638c854c0695 | 105 | message.retained = false; | 
| icraggs | 2:638c854c0695 | 106 | message.dup = false; | 
| icraggs | 2:638c854c0695 | 107 | message.payload = (void*)buf; | 
| icraggs | 2:638c854c0695 | 108 | message.payloadlen = strlen(buf)+1; | 
| icraggs | 16:28d062c5522b | 109 | rc = client.publish(topic, message); | 
| icraggs | 12:086a9314e8a5 | 110 | while (arrivedcount < 1) | 
| icraggs | 2:638c854c0695 | 111 | client.yield(100); | 
| icraggs | 2:638c854c0695 | 112 | |
| icraggs | 2:638c854c0695 | 113 | // QoS 1 | 
| icraggs | 2:638c854c0695 | 114 | sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); | 
| icraggs | 2:638c854c0695 | 115 | message.qos = MQTT::QOS1; | 
| icraggs | 2:638c854c0695 | 116 | message.payloadlen = strlen(buf)+1; | 
| icraggs | 16:28d062c5522b | 117 | rc = client.publish(topic, message); | 
| icraggs | 12:086a9314e8a5 | 118 | while (arrivedcount < 2) | 
| icraggs | 2:638c854c0695 | 119 | client.yield(100); | 
| icraggs | 2:638c854c0695 | 120 | |
| icraggs | 2:638c854c0695 | 121 | // QoS 2 | 
| icraggs | 2:638c854c0695 | 122 | sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); | 
| icraggs | 2:638c854c0695 | 123 | message.qos = MQTT::QOS2; | 
| icraggs | 2:638c854c0695 | 124 | message.payloadlen = strlen(buf)+1; | 
| icraggs | 16:28d062c5522b | 125 | rc = client.publish(topic, message); | 
| icraggs | 12:086a9314e8a5 | 126 | while (arrivedcount < 3) | 
| icraggs | 2:638c854c0695 | 127 | client.yield(100); | 
| icraggs | 17:0811bdbdd78a | 128 | |
| icraggs | 17:0811bdbdd78a | 129 | if ((rc = client.unsubscribe(topic)) != 0) | 
| icraggs | 12:086a9314e8a5 | 130 | { | 
| icraggs | 17:0811bdbdd78a | 131 | lcd.cls(); | 
| icraggs | 17:0811bdbdd78a | 132 | lcd.printf("rc from unsubscribe was %d\n", rc); | 
| icraggs | 12:086a9314e8a5 | 133 | } | 
| icraggs | 2:638c854c0695 | 134 | |
| icraggs | 8:a3e3113054a1 | 135 | if ((rc = client.disconnect()) != 0) | 
| icraggs | 17:0811bdbdd78a | 136 | { | 
| icraggs | 17:0811bdbdd78a | 137 | lcd.cls(); | 
| icraggs | 17:0811bdbdd78a | 138 | lcd.printf("rc from disconnect was %d\n", rc); | 
| icraggs | 17:0811bdbdd78a | 139 | } | 
| icraggs | 2:638c854c0695 | 140 | |
| icraggs | 2:638c854c0695 | 141 | ipstack.disconnect(); | 
| icraggs | 2:638c854c0695 | 142 | |
| sam_grove | 5:4a257f6ac09a | 143 | lcd.cls(); | 
| icraggs | 8:a3e3113054a1 | 144 | lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount); | 
| icraggs | 2:638c854c0695 | 145 | |
| icraggs | 0:0cae29831d01 | 146 | return 0; | 
| icraggs | 0:0cae29831d01 | 147 | } |