Sample MQTT-SN program
Dependencies: C12832 MQTTSN mbed-rtos mbed
main.cpp@1:3e4e55b1e6dd, 2015-02-26 (annotated)
- Committer:
- icraggs
- Date:
- Thu Feb 26 23:53:26 2015 +0000
- Revision:
- 1:3e4e55b1e6dd
- Parent:
- 0:7bd27f2e3fb3
Sample MQTT-SN program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 0:7bd27f2e3fb3 | 1 | /******************************************************************************* |
icraggs | 0:7bd27f2e3fb3 | 2 | * Copyright (c) 2014, 2015 IBM Corp. |
icraggs | 0:7bd27f2e3fb3 | 3 | * |
icraggs | 0:7bd27f2e3fb3 | 4 | * All rights reserved. This program and the accompanying materials |
icraggs | 0:7bd27f2e3fb3 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
icraggs | 0:7bd27f2e3fb3 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
icraggs | 0:7bd27f2e3fb3 | 7 | * |
icraggs | 0:7bd27f2e3fb3 | 8 | * The Eclipse Public License is available at |
icraggs | 0:7bd27f2e3fb3 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
icraggs | 0:7bd27f2e3fb3 | 10 | * and the Eclipse Distribution License is available at |
icraggs | 0:7bd27f2e3fb3 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
icraggs | 0:7bd27f2e3fb3 | 12 | * |
icraggs | 0:7bd27f2e3fb3 | 13 | * Contributors: |
icraggs | 0:7bd27f2e3fb3 | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
icraggs | 0:7bd27f2e3fb3 | 15 | *******************************************************************************/ |
icraggs | 0:7bd27f2e3fb3 | 16 | |
icraggs | 0:7bd27f2e3fb3 | 17 | /** |
icraggs | 0:7bd27f2e3fb3 | 18 | This is a sample program to illustrate the use of the MQTTSN Client library |
icraggs | 0:7bd27f2e3fb3 | 19 | on the mbed platform. The Client class requires two classes which mediate |
icraggs | 0:7bd27f2e3fb3 | 20 | access to system interfaces for networking and timing. As long as these two |
icraggs | 0:7bd27f2e3fb3 | 21 | classes provide the required public programming interfaces, it does not matter |
icraggs | 0:7bd27f2e3fb3 | 22 | what facilities they use underneath. In this program, they use the mbed |
icraggs | 0:7bd27f2e3fb3 | 23 | system libraries. |
icraggs | 0:7bd27f2e3fb3 | 24 | |
icraggs | 0:7bd27f2e3fb3 | 25 | */ |
icraggs | 0:7bd27f2e3fb3 | 26 | |
icraggs | 0:7bd27f2e3fb3 | 27 | #define WARN printf |
icraggs | 0:7bd27f2e3fb3 | 28 | |
icraggs | 0:7bd27f2e3fb3 | 29 | #include "EthernetInterface.h" |
icraggs | 0:7bd27f2e3fb3 | 30 | #include "C12832.h" |
icraggs | 0:7bd27f2e3fb3 | 31 | |
icraggs | 0:7bd27f2e3fb3 | 32 | #if defined(TARGET_LPC1768) |
icraggs | 0:7bd27f2e3fb3 | 33 | #warning "Compiling for mbed LPC1768" |
icraggs | 0:7bd27f2e3fb3 | 34 | #include "LPC1768.h" |
icraggs | 0:7bd27f2e3fb3 | 35 | #elif defined(TARGET_K64F) |
icraggs | 0:7bd27f2e3fb3 | 36 | #warning "Compiling for mbed K64F" |
icraggs | 0:7bd27f2e3fb3 | 37 | #include "K64F.h" |
icraggs | 0:7bd27f2e3fb3 | 38 | #endif |
icraggs | 0:7bd27f2e3fb3 | 39 | |
icraggs | 0:7bd27f2e3fb3 | 40 | #define MQTTSNCLIENT_QOS2 1 |
icraggs | 0:7bd27f2e3fb3 | 41 | #include "MQTTSNUDP.h" |
icraggs | 0:7bd27f2e3fb3 | 42 | #include "MQTTSNClient.h" |
icraggs | 0:7bd27f2e3fb3 | 43 | |
icraggs | 0:7bd27f2e3fb3 | 44 | int arrivedcount = 0; |
icraggs | 0:7bd27f2e3fb3 | 45 | |
icraggs | 0:7bd27f2e3fb3 | 46 | |
icraggs | 0:7bd27f2e3fb3 | 47 | void off() |
icraggs | 0:7bd27f2e3fb3 | 48 | { |
icraggs | 0:7bd27f2e3fb3 | 49 | r = g = b = 1.0; // 1 is off, 0 is full brightness |
icraggs | 0:7bd27f2e3fb3 | 50 | } |
icraggs | 0:7bd27f2e3fb3 | 51 | |
icraggs | 0:7bd27f2e3fb3 | 52 | void red() |
icraggs | 0:7bd27f2e3fb3 | 53 | { |
icraggs | 0:7bd27f2e3fb3 | 54 | r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness |
icraggs | 0:7bd27f2e3fb3 | 55 | } |
icraggs | 0:7bd27f2e3fb3 | 56 | |
icraggs | 0:7bd27f2e3fb3 | 57 | void yellow() |
icraggs | 0:7bd27f2e3fb3 | 58 | { |
icraggs | 0:7bd27f2e3fb3 | 59 | r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness |
icraggs | 0:7bd27f2e3fb3 | 60 | } |
icraggs | 0:7bd27f2e3fb3 | 61 | |
icraggs | 0:7bd27f2e3fb3 | 62 | void green() |
icraggs | 0:7bd27f2e3fb3 | 63 | { |
icraggs | 0:7bd27f2e3fb3 | 64 | r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness |
icraggs | 0:7bd27f2e3fb3 | 65 | } |
icraggs | 0:7bd27f2e3fb3 | 66 | |
icraggs | 0:7bd27f2e3fb3 | 67 | |
icraggs | 0:7bd27f2e3fb3 | 68 | void messageArrived(MQTTSN::MessageData& md) |
icraggs | 0:7bd27f2e3fb3 | 69 | { |
icraggs | 0:7bd27f2e3fb3 | 70 | MQTTSN::Message &message = md.message; |
icraggs | 0:7bd27f2e3fb3 | 71 | lcd.cls(); |
icraggs | 0:7bd27f2e3fb3 | 72 | lcd.locate(0,3); |
icraggs | 0:7bd27f2e3fb3 | 73 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
icraggs | 0:7bd27f2e3fb3 | 74 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
icraggs | 0:7bd27f2e3fb3 | 75 | ++arrivedcount; |
icraggs | 0:7bd27f2e3fb3 | 76 | lcd.puts((char*)message.payload); |
icraggs | 0:7bd27f2e3fb3 | 77 | } |
icraggs | 0:7bd27f2e3fb3 | 78 | |
icraggs | 0:7bd27f2e3fb3 | 79 | |
icraggs | 0:7bd27f2e3fb3 | 80 | int main(int argc, char* argv[]) |
icraggs | 0:7bd27f2e3fb3 | 81 | { |
icraggs | 0:7bd27f2e3fb3 | 82 | MQTTSNUDP ipstack = MQTTSNUDP(); |
icraggs | 0:7bd27f2e3fb3 | 83 | float version = 0.47; |
icraggs | 0:7bd27f2e3fb3 | 84 | char* topic = "mbed-sample"; |
icraggs | 0:7bd27f2e3fb3 | 85 | |
icraggs | 0:7bd27f2e3fb3 | 86 | lcd.printf("Version is %f\n", version); |
icraggs | 0:7bd27f2e3fb3 | 87 | printf("Version is %f\n", version); |
icraggs | 0:7bd27f2e3fb3 | 88 | |
icraggs | 0:7bd27f2e3fb3 | 89 | MQTTSN::Client<MQTTSNUDP, Countdown> client = MQTTSN::Client<MQTTSNUDP, Countdown>(ipstack); |
icraggs | 0:7bd27f2e3fb3 | 90 | |
icraggs | 0:7bd27f2e3fb3 | 91 | EthernetInterface eth; |
icraggs | 0:7bd27f2e3fb3 | 92 | eth.init(); // Use DHCP |
icraggs | 0:7bd27f2e3fb3 | 93 | eth.connect(); |
icraggs | 0:7bd27f2e3fb3 | 94 | |
icraggs | 0:7bd27f2e3fb3 | 95 | char* hostname = "9.20.230.78"; |
icraggs | 0:7bd27f2e3fb3 | 96 | int port = 20000; |
icraggs | 0:7bd27f2e3fb3 | 97 | lcd.printf("Connecting to %s:%d\n", hostname, port); |
icraggs | 0:7bd27f2e3fb3 | 98 | int rc = ipstack.connect(hostname, port); |
icraggs | 0:7bd27f2e3fb3 | 99 | if (rc != 0) |
icraggs | 0:7bd27f2e3fb3 | 100 | lcd.printf("rc from TCP connect is %d\n", rc); |
icraggs | 0:7bd27f2e3fb3 | 101 | else |
icraggs | 0:7bd27f2e3fb3 | 102 | green(); |
icraggs | 0:7bd27f2e3fb3 | 103 | |
icraggs | 0:7bd27f2e3fb3 | 104 | MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer; |
icraggs | 0:7bd27f2e3fb3 | 105 | data.clientID.cstring = "mbed-sample"; |
icraggs | 0:7bd27f2e3fb3 | 106 | data.duration = 60; |
icraggs | 0:7bd27f2e3fb3 | 107 | if ((rc = client.connect(data)) != 0) |
icraggs | 0:7bd27f2e3fb3 | 108 | lcd.printf("rc from MQTT connect is %d\n", rc); |
icraggs | 0:7bd27f2e3fb3 | 109 | |
icraggs | 0:7bd27f2e3fb3 | 110 | MQTTSN_topicid topicid; |
icraggs | 0:7bd27f2e3fb3 | 111 | topicid.type = MQTTSN_TOPIC_TYPE_NORMAL; |
icraggs | 0:7bd27f2e3fb3 | 112 | topicid.data.long_.name = topic; |
icraggs | 0:7bd27f2e3fb3 | 113 | topicid.data.long_.len = strlen(topic); |
icraggs | 0:7bd27f2e3fb3 | 114 | MQTTSN::QoS grantedQoS; |
icraggs | 0:7bd27f2e3fb3 | 115 | if ((rc = client.subscribe(topicid, MQTTSN::QOS1, grantedQoS, messageArrived)) != 0) |
icraggs | 0:7bd27f2e3fb3 | 116 | lcd.printf("rc from MQTT subscribe is %d\n", rc); |
icraggs | 0:7bd27f2e3fb3 | 117 | |
icraggs | 0:7bd27f2e3fb3 | 118 | MQTTSN::Message message; |
icraggs | 0:7bd27f2e3fb3 | 119 | |
icraggs | 0:7bd27f2e3fb3 | 120 | // QoS 0 |
icraggs | 0:7bd27f2e3fb3 | 121 | char buf[100]; |
icraggs | 0:7bd27f2e3fb3 | 122 | sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); |
icraggs | 0:7bd27f2e3fb3 | 123 | message.qos = MQTTSN::QOS0; |
icraggs | 0:7bd27f2e3fb3 | 124 | message.retained = false; |
icraggs | 0:7bd27f2e3fb3 | 125 | message.dup = false; |
icraggs | 0:7bd27f2e3fb3 | 126 | message.payload = (void*)buf; |
icraggs | 0:7bd27f2e3fb3 | 127 | message.payloadlen = strlen(buf)+1; |
icraggs | 0:7bd27f2e3fb3 | 128 | rc = client.publish(topicid, message); |
icraggs | 0:7bd27f2e3fb3 | 129 | while (arrivedcount < 1) |
icraggs | 0:7bd27f2e3fb3 | 130 | client.yield(100); |
icraggs | 0:7bd27f2e3fb3 | 131 | |
icraggs | 0:7bd27f2e3fb3 | 132 | // QoS 1 |
icraggs | 0:7bd27f2e3fb3 | 133 | sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); |
icraggs | 0:7bd27f2e3fb3 | 134 | message.qos = MQTTSN::QOS1; |
icraggs | 0:7bd27f2e3fb3 | 135 | message.payloadlen = strlen(buf)+1; |
icraggs | 0:7bd27f2e3fb3 | 136 | rc = client.publish(topicid, message); |
icraggs | 0:7bd27f2e3fb3 | 137 | while (arrivedcount < 2) |
icraggs | 0:7bd27f2e3fb3 | 138 | client.yield(100); |
icraggs | 0:7bd27f2e3fb3 | 139 | |
icraggs | 0:7bd27f2e3fb3 | 140 | // QoS 2 |
icraggs | 0:7bd27f2e3fb3 | 141 | sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); |
icraggs | 0:7bd27f2e3fb3 | 142 | message.qos = MQTTSN::QOS2; |
icraggs | 0:7bd27f2e3fb3 | 143 | message.payloadlen = strlen(buf)+1; |
icraggs | 0:7bd27f2e3fb3 | 144 | rc = client.publish(topicid, message); |
icraggs | 0:7bd27f2e3fb3 | 145 | while (arrivedcount < 3) |
icraggs | 0:7bd27f2e3fb3 | 146 | client.yield(100); |
icraggs | 0:7bd27f2e3fb3 | 147 | |
icraggs | 0:7bd27f2e3fb3 | 148 | // n * QoS 2 |
icraggs | 0:7bd27f2e3fb3 | 149 | for (int i = 1; i <= 10; ++i) |
icraggs | 0:7bd27f2e3fb3 | 150 | { |
icraggs | 0:7bd27f2e3fb3 | 151 | sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version); |
icraggs | 0:7bd27f2e3fb3 | 152 | message.qos = MQTTSN::QOS2; |
icraggs | 0:7bd27f2e3fb3 | 153 | message.payloadlen = strlen(buf)+1; |
icraggs | 0:7bd27f2e3fb3 | 154 | rc = client.publish(topicid, message); |
icraggs | 0:7bd27f2e3fb3 | 155 | while (arrivedcount < i + 3) |
icraggs | 0:7bd27f2e3fb3 | 156 | client.yield(100); |
icraggs | 0:7bd27f2e3fb3 | 157 | } |
icraggs | 0:7bd27f2e3fb3 | 158 | |
icraggs | 0:7bd27f2e3fb3 | 159 | if ((rc = client.unsubscribe(topicid)) != 0) |
icraggs | 0:7bd27f2e3fb3 | 160 | printf("rc from unsubscribe was %d\n", rc); |
icraggs | 0:7bd27f2e3fb3 | 161 | |
icraggs | 0:7bd27f2e3fb3 | 162 | if ((rc = client.disconnect()) != 0) |
icraggs | 0:7bd27f2e3fb3 | 163 | printf("rc from disconnect was %d\n", rc); |
icraggs | 0:7bd27f2e3fb3 | 164 | |
icraggs | 0:7bd27f2e3fb3 | 165 | ipstack.disconnect(); |
icraggs | 0:7bd27f2e3fb3 | 166 | |
icraggs | 0:7bd27f2e3fb3 | 167 | lcd.cls(); |
icraggs | 0:7bd27f2e3fb3 | 168 | lcd.locate(0,3); |
icraggs | 0:7bd27f2e3fb3 | 169 | lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount); |
icraggs | 0:7bd27f2e3fb3 | 170 | printf("Finishing with %d messages received\n", arrivedcount); |
icraggs | 0:7bd27f2e3fb3 | 171 | |
icraggs | 0:7bd27f2e3fb3 | 172 | return 0; |
icraggs | 0:7bd27f2e3fb3 | 173 | } |