HelloMQTT porting to FRDM-K64F+AppShield

Dependencies:   C12832 EthernetInterface MQTT mbed-rtos mbed

Fork of HelloMQTT by MQTT

Tested with RabbitMQ 3.4.4 on OSX.

modification are below

- skip QOS2 test since RabbitMQ is not supported QOS2 message type

Committer:
hogejun
Date:
Mon Apr 13 09:41:48 2015 +0000
Revision:
17:e3aa8f5ee6ed
Parent:
16:28d062c5522b
initial revision

Who changed what in which revision?

UserRevisionLine numberNew 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"
hogejun 17:e3aa8f5ee6ed 29 //C12832 lcd(p5, p7, p6, p8, p11);
hogejun 17:e3aa8f5ee6ed 30 // Using Arduino pin notation
hogejun 17:e3aa8f5ee6ed 31 C12832 lcd(D11, D13, D12, D7, D10);
icraggs 0:0cae29831d01 32
icraggs 8:a3e3113054a1 33 #include "MQTTEthernet.h"
icraggs 2:638c854c0695 34 #include "MQTTClient.h"
icraggs 2:638c854c0695 35
icraggs 2:638c854c0695 36 int arrivedcount = 0;
icraggs 2:638c854c0695 37
icraggs 8:a3e3113054a1 38
icraggs 9:5beb8609e9f7 39 void messageArrived(MQTT::MessageData& md)
icraggs 2:638c854c0695 40 {
icraggs 9:5beb8609e9f7 41 MQTT::Message &message = md.message;
sam_grove 5:4a257f6ac09a 42 lcd.cls();
sam_grove 5:4a257f6ac09a 43 lcd.locate(0,3);
icraggs 9:5beb8609e9f7 44 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
icraggs 9:5beb8609e9f7 45 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
icraggs 2:638c854c0695 46 ++arrivedcount;
icraggs 9:5beb8609e9f7 47 lcd.puts((char*)message.payload);
icraggs 2:638c854c0695 48 }
icraggs 0:0cae29831d01 49
icraggs 2:638c854c0695 50
icraggs 2:638c854c0695 51 int main(int argc, char* argv[])
icraggs 2:638c854c0695 52 {
icraggs 8:a3e3113054a1 53 MQTTEthernet ipstack = MQTTEthernet();
icraggs 9:5beb8609e9f7 54 float version = 0.47;
icraggs 2:638c854c0695 55 char* topic = "mbed-sample";
icraggs 2:638c854c0695 56
icraggs 2:638c854c0695 57 lcd.printf("Version is %f\n", version);
sam_grove 5:4a257f6ac09a 58 printf("Version is %f\n", version);
icraggs 2:638c854c0695 59
icraggs 8:a3e3113054a1 60 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
icraggs 3:7a6a899de7cc 61
icraggs 6:e4c690c45021 62 char* hostname = "m2m.eclipse.org";
icraggs 6:e4c690c45021 63 int port = 1883;
icraggs 6:e4c690c45021 64 lcd.printf("Connecting to %s:%d\n", hostname, port);
icraggs 6:e4c690c45021 65 int rc = ipstack.connect(hostname, port);
icraggs 6:e4c690c45021 66 if (rc != 0)
icraggs 6:e4c690c45021 67 lcd.printf("rc from TCP connect is %d\n", rc);
icraggs 6:e4c690c45021 68
icraggs 6:e4c690c45021 69 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 6:e4c690c45021 70 data.MQTTVersion = 3;
icraggs 8:a3e3113054a1 71 data.clientID.cstring = "mbed-sample";
icraggs 12:086a9314e8a5 72 data.username.cstring = "testuser";
icraggs 12:086a9314e8a5 73 data.password.cstring = "testpassword";
icraggs 16:28d062c5522b 74 if ((rc = client.connect(data)) != 0)
icraggs 6:e4c690c45021 75 lcd.printf("rc from MQTT connect is %d\n", rc);
icraggs 2:638c854c0695 76
icraggs 8:a3e3113054a1 77 if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0)
icraggs 8:a3e3113054a1 78 lcd.printf("rc from MQTT subscribe is %d\n", rc);
icraggs 2:638c854c0695 79
icraggs 2:638c854c0695 80 MQTT::Message message;
icraggs 0:0cae29831d01 81
icraggs 2:638c854c0695 82 // QoS 0
icraggs 2:638c854c0695 83 char buf[100];
icraggs 2:638c854c0695 84 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version);
icraggs 2:638c854c0695 85 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 86 message.retained = false;
icraggs 2:638c854c0695 87 message.dup = false;
icraggs 2:638c854c0695 88 message.payload = (void*)buf;
icraggs 2:638c854c0695 89 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 90 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 91 while (arrivedcount < 1)
icraggs 2:638c854c0695 92 client.yield(100);
icraggs 2:638c854c0695 93
icraggs 2:638c854c0695 94 // QoS 1
icraggs 2:638c854c0695 95 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
icraggs 2:638c854c0695 96 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 97 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 98 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 99 while (arrivedcount < 2)
icraggs 2:638c854c0695 100 client.yield(100);
icraggs 2:638c854c0695 101
icraggs 2:638c854c0695 102 // QoS 2
icraggs 2:638c854c0695 103 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
icraggs 2:638c854c0695 104 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 105 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 106 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 107 while (arrivedcount < 3)
icraggs 2:638c854c0695 108 client.yield(100);
icraggs 12:086a9314e8a5 109
icraggs 12:086a9314e8a5 110 // n * QoS 2
icraggs 12:086a9314e8a5 111 for (int i = 1; i <= 10; ++i)
icraggs 12:086a9314e8a5 112 {
icraggs 12:086a9314e8a5 113 sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version);
icraggs 12:086a9314e8a5 114 message.qos = MQTT::QOS2;
icraggs 12:086a9314e8a5 115 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 116 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 117 while (arrivedcount < i + 3)
icraggs 12:086a9314e8a5 118 client.yield(100);
icraggs 12:086a9314e8a5 119 }
icraggs 2:638c854c0695 120
icraggs 8:a3e3113054a1 121 if ((rc = client.unsubscribe(topic)) != 0)
sam_grove 5:4a257f6ac09a 122 printf("rc from unsubscribe was %d\n", rc);
icraggs 2:638c854c0695 123
icraggs 8:a3e3113054a1 124 if ((rc = client.disconnect()) != 0)
sam_grove 5:4a257f6ac09a 125 printf("rc from disconnect was %d\n", rc);
icraggs 2:638c854c0695 126
icraggs 2:638c854c0695 127 ipstack.disconnect();
icraggs 2:638c854c0695 128
sam_grove 5:4a257f6ac09a 129 lcd.cls();
sam_grove 5:4a257f6ac09a 130 lcd.locate(0,3);
icraggs 8:a3e3113054a1 131 lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
sam_grove 5:4a257f6ac09a 132 printf("Finishing with %d messages received\n", arrivedcount);
icraggs 2:638c854c0695 133
icraggs 0:0cae29831d01 134 return 0;
icraggs 0:0cae29831d01 135 }