Yield function call with timeout works, there was an issue with blockant socket.
Fork of HelloMQTT by
main.cpp@8:a3e3113054a1, 2014-05-20 (annotated)
- Committer:
- icraggs
- Date:
- Tue May 20 15:08:05 2014 +0000
- Revision:
- 8:a3e3113054a1
- Parent:
- 7:3de634f2d40c
- Child:
- 9:5beb8609e9f7
Refactoring headers
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 1:a1d5c7a6acbc | 1 | /******************************************************************************* |
icraggs | 1:a1d5c7a6acbc | 2 | * Copyright (c) 2014 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 | 1:a1d5c7a6acbc | 15 | *******************************************************************************/ |
icraggs | 2:638c854c0695 | 16 | |
icraggs | 2:638c854c0695 | 17 | /** |
icraggs | 2:638c854c0695 | 18 | This is a sample program to illustrate the use of the MQTT Client library |
icraggs | 2:638c854c0695 | 19 | on the mbed platform. The Client class requires two classes which mediate |
icraggs | 2:638c854c0695 | 20 | access to system interfaces for networking and timing. As long as these two |
icraggs | 2:638c854c0695 | 21 | classes provide the required public programming interfaces, it does not matter |
icraggs | 2:638c854c0695 | 22 | what facilities they use underneath. In this program, they use the mbed |
icraggs | 2:638c854c0695 | 23 | system libraries. |
icraggs | 2:638c854c0695 | 24 | |
icraggs | 2:638c854c0695 | 25 | */ |
icraggs | 1:a1d5c7a6acbc | 26 | |
icraggs | 2:638c854c0695 | 27 | |
sam_grove | 5:4a257f6ac09a | 28 | #include "C12832.h" |
sam_grove | 5:4a257f6ac09a | 29 | C12832 lcd(p5, p7, p6, p8, p11); |
icraggs | 0:0cae29831d01 | 30 | |
icraggs | 8:a3e3113054a1 | 31 | #include "MQTTEthernet.h" |
icraggs | 2:638c854c0695 | 32 | #include "MQTTClient.h" |
icraggs | 2:638c854c0695 | 33 | |
icraggs | 2:638c854c0695 | 34 | int arrivedcount = 0; |
icraggs | 2:638c854c0695 | 35 | |
icraggs | 8:a3e3113054a1 | 36 | |
icraggs | 2:638c854c0695 | 37 | void messageArrived(MQTT::Message* message) |
icraggs | 2:638c854c0695 | 38 | { |
sam_grove | 5:4a257f6ac09a | 39 | lcd.cls(); |
sam_grove | 5:4a257f6ac09a | 40 | lcd.locate(0,3); |
sam_grove | 5:4a257f6ac09a | 41 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message->qos, message->retained, message->dup, message->id); |
sam_grove | 5:4a257f6ac09a | 42 | printf("Payload %.*s\n", message->payloadlen, (char*)message->payload); |
icraggs | 2:638c854c0695 | 43 | ++arrivedcount; |
sam_grove | 5:4a257f6ac09a | 44 | lcd.puts((char*)message->payload); |
icraggs | 2:638c854c0695 | 45 | } |
icraggs | 0:0cae29831d01 | 46 | |
icraggs | 2:638c854c0695 | 47 | |
icraggs | 2:638c854c0695 | 48 | int main(int argc, char* argv[]) |
icraggs | 2:638c854c0695 | 49 | { |
icraggs | 8:a3e3113054a1 | 50 | MQTTEthernet ipstack = MQTTEthernet(); |
icraggs | 8:a3e3113054a1 | 51 | float version = 0.46; |
icraggs | 2:638c854c0695 | 52 | char* topic = "mbed-sample"; |
icraggs | 2:638c854c0695 | 53 | |
icraggs | 2:638c854c0695 | 54 | lcd.printf("Version is %f\n", version); |
sam_grove | 5:4a257f6ac09a | 55 | printf("Version is %f\n", version); |
icraggs | 2:638c854c0695 | 56 | |
icraggs | 8:a3e3113054a1 | 57 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
icraggs | 3:7a6a899de7cc | 58 | |
icraggs | 6:e4c690c45021 | 59 | char* hostname = "m2m.eclipse.org"; |
icraggs | 6:e4c690c45021 | 60 | int port = 1883; |
icraggs | 6:e4c690c45021 | 61 | lcd.printf("Connecting to %s:%d\n", hostname, port); |
icraggs | 6:e4c690c45021 | 62 | int rc = ipstack.connect(hostname, port); |
icraggs | 6:e4c690c45021 | 63 | if (rc != 0) |
icraggs | 6:e4c690c45021 | 64 | lcd.printf("rc from TCP connect is %d\n", rc); |
icraggs | 6:e4c690c45021 | 65 | |
icraggs | 6:e4c690c45021 | 66 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 67 | data.MQTTVersion = 3; |
icraggs | 8:a3e3113054a1 | 68 | data.clientID.cstring = "mbed-sample"; |
icraggs | 8:a3e3113054a1 | 69 | if ((rc = client.connect(&data)) != 0) |
icraggs | 6:e4c690c45021 | 70 | lcd.printf("rc from MQTT connect is %d\n", rc); |
icraggs | 2:638c854c0695 | 71 | |
icraggs | 8:a3e3113054a1 | 72 | if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) |
icraggs | 8:a3e3113054a1 | 73 | lcd.printf("rc from MQTT subscribe is %d\n", rc); |
icraggs | 2:638c854c0695 | 74 | |
icraggs | 2:638c854c0695 | 75 | MQTT::Message message; |
icraggs | 0:0cae29831d01 | 76 | |
icraggs | 2:638c854c0695 | 77 | // QoS 0 |
icraggs | 2:638c854c0695 | 78 | char buf[100]; |
icraggs | 2:638c854c0695 | 79 | sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); |
icraggs | 2:638c854c0695 | 80 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 81 | message.retained = false; |
icraggs | 2:638c854c0695 | 82 | message.dup = false; |
icraggs | 2:638c854c0695 | 83 | message.payload = (void*)buf; |
icraggs | 2:638c854c0695 | 84 | message.payloadlen = strlen(buf)+1; |
icraggs | 2:638c854c0695 | 85 | rc = client.publish(topic, &message); |
icraggs | 2:638c854c0695 | 86 | while (arrivedcount == 0) |
icraggs | 2:638c854c0695 | 87 | client.yield(100); |
icraggs | 2:638c854c0695 | 88 | |
icraggs | 2:638c854c0695 | 89 | // QoS 1 |
icraggs | 2:638c854c0695 | 90 | sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); |
icraggs | 2:638c854c0695 | 91 | message.qos = MQTT::QOS1; |
icraggs | 2:638c854c0695 | 92 | message.payloadlen = strlen(buf)+1; |
icraggs | 2:638c854c0695 | 93 | rc = client.publish(topic, &message); |
icraggs | 2:638c854c0695 | 94 | while (arrivedcount == 1) |
icraggs | 2:638c854c0695 | 95 | client.yield(100); |
icraggs | 2:638c854c0695 | 96 | |
icraggs | 2:638c854c0695 | 97 | // QoS 2 |
icraggs | 2:638c854c0695 | 98 | sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); |
icraggs | 2:638c854c0695 | 99 | message.qos = MQTT::QOS2; |
icraggs | 2:638c854c0695 | 100 | message.payloadlen = strlen(buf)+1; |
icraggs | 2:638c854c0695 | 101 | rc = client.publish(topic, &message); |
icraggs | 2:638c854c0695 | 102 | while (arrivedcount == 2) |
icraggs | 2:638c854c0695 | 103 | client.yield(100); |
icraggs | 2:638c854c0695 | 104 | |
icraggs | 8:a3e3113054a1 | 105 | if ((rc = client.unsubscribe(topic)) != 0) |
sam_grove | 5:4a257f6ac09a | 106 | printf("rc from unsubscribe was %d\n", rc); |
icraggs | 2:638c854c0695 | 107 | |
icraggs | 8:a3e3113054a1 | 108 | if ((rc = client.disconnect()) != 0) |
sam_grove | 5:4a257f6ac09a | 109 | printf("rc from disconnect was %d\n", rc); |
icraggs | 2:638c854c0695 | 110 | |
icraggs | 2:638c854c0695 | 111 | ipstack.disconnect(); |
icraggs | 2:638c854c0695 | 112 | |
sam_grove | 5:4a257f6ac09a | 113 | lcd.cls(); |
sam_grove | 5:4a257f6ac09a | 114 | lcd.locate(0,3); |
icraggs | 8:a3e3113054a1 | 115 | lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount); |
sam_grove | 5:4a257f6ac09a | 116 | printf("Finishing with %d messages received\n", arrivedcount); |
icraggs | 2:638c854c0695 | 117 | |
icraggs | 0:0cae29831d01 | 118 | return 0; |
icraggs | 0:0cae29831d01 | 119 | } |