MQTT cellular example
Dependencies: C027_Support C12832 MQTT mbed
Fork of Cellular_HelloMQTT by
main.cpp@12:0ec1916059b5, 2014-05-20 (annotated)
- Committer:
- mazgch
- Date:
- Tue May 20 14:33:13 2014 +0000
- Revision:
- 12:0ec1916059b5
- Parent:
- 11:c074866fcad7
- Child:
- 14:d495a85ed55b
use restructured library
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 | 0:0cae29831d01 | 26 | #include "mbed.h" |
mazgch | 12:0ec1916059b5 | 27 | #include "MbedIPStack.h" |
mazgch | 8:b32c94be6522 | 28 | |
mazgch | 12:0ec1916059b5 | 29 | //------------------------------------------------------------------------------------ |
mazgch | 12:0ec1916059b5 | 30 | // You need to configure these cellular modem / SIM parameters. |
mazgch | 12:0ec1916059b5 | 31 | // These parameters are ignored for LISA-C200 variants and can be left NULL. |
mazgch | 12:0ec1916059b5 | 32 | //------------------------------------------------------------------------------------ |
mazgch | 12:0ec1916059b5 | 33 | #include "MDM.h" |
mazgch | 12:0ec1916059b5 | 34 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
mazgch | 12:0ec1916059b5 | 35 | #define SIMPIN NULL |
mazgch | 12:0ec1916059b5 | 36 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
mazgch | 12:0ec1916059b5 | 37 | contract with the network operator. You can also try to look-up your settings in |
mazgch | 12:0ec1916059b5 | 38 | google: https://www.google.de/search?q=APN+list */ |
mazgch | 8:b32c94be6522 | 39 | #define APN "gprs.swisscom.ch" |
mazgch | 12:0ec1916059b5 | 40 | //! Set the user name for your APN, or NULL if not needed |
mazgch | 8:b32c94be6522 | 41 | #define USERNAME NULL |
mazgch | 12:0ec1916059b5 | 42 | //! Set the password for your APN, or NULL if not needed |
mazgch | 8:b32c94be6522 | 43 | #define PASSWORD NULL |
mazgch | 12:0ec1916059b5 | 44 | //------------------------------------------------------------------------------------ |
icraggs | 2:638c854c0695 | 45 | |
sam_grove | 5:4a257f6ac09a | 46 | #include "C12832.h" |
mazgch | 8:b32c94be6522 | 47 | C12832 lcd(D11, D13, D12, D7, D10); |
icraggs | 0:0cae29831d01 | 48 | |
icraggs | 2:638c854c0695 | 49 | #include "FP.cpp" |
icraggs | 2:638c854c0695 | 50 | #include "MQTTClient.h" |
icraggs | 2:638c854c0695 | 51 | |
icraggs | 2:638c854c0695 | 52 | int arrivedcount = 0; |
icraggs | 2:638c854c0695 | 53 | |
icraggs | 2:638c854c0695 | 54 | void messageArrived(MQTT::Message* message) |
icraggs | 2:638c854c0695 | 55 | { |
sam_grove | 5:4a257f6ac09a | 56 | lcd.cls(); |
sam_grove | 5:4a257f6ac09a | 57 | lcd.locate(0,3); |
sam_grove | 5:4a257f6ac09a | 58 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message->qos, message->retained, message->dup, message->id); |
sam_grove | 5:4a257f6ac09a | 59 | printf("Payload %.*s\n", message->payloadlen, (char*)message->payload); |
icraggs | 2:638c854c0695 | 60 | ++arrivedcount; |
sam_grove | 5:4a257f6ac09a | 61 | lcd.puts((char*)message->payload); |
icraggs | 2:638c854c0695 | 62 | } |
icraggs | 0:0cae29831d01 | 63 | |
icraggs | 2:638c854c0695 | 64 | int main(int argc, char* argv[]) |
icraggs | 2:638c854c0695 | 65 | { |
mazgch | 8:b32c94be6522 | 66 | MDMSerial mdm; |
mazgch | 12:0ec1916059b5 | 67 | //mdm.setDebug(4); // enable this for debugging issues |
mazgch | 12:0ec1916059b5 | 68 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
mazgch | 10:6f1c3b718b9c | 69 | return -1; |
mazgch | 8:b32c94be6522 | 70 | |
mazgch | 10:6f1c3b718b9c | 71 | IPStack ipstack = IPStack(); |
mazgch | 10:6f1c3b718b9c | 72 | float version = 0.43; |
mazgch | 10:6f1c3b718b9c | 73 | char* topic = "mbed-sample"; |
mazgch | 10:6f1c3b718b9c | 74 | |
mazgch | 10:6f1c3b718b9c | 75 | lcd.printf("Version is %f\n", version); |
mazgch | 10:6f1c3b718b9c | 76 | printf("Version is %f\n", version); |
mazgch | 10:6f1c3b718b9c | 77 | |
mazgch | 10:6f1c3b718b9c | 78 | MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack); |
mazgch | 10:6f1c3b718b9c | 79 | |
mazgch | 10:6f1c3b718b9c | 80 | char* hostname = "m2m.eclipse.org"; |
mazgch | 10:6f1c3b718b9c | 81 | int port = 1883; |
mazgch | 10:6f1c3b718b9c | 82 | lcd.printf("Connecting to %s:%d\n", hostname, port); |
mazgch | 10:6f1c3b718b9c | 83 | int rc = ipstack.connect(hostname, port); |
mazgch | 10:6f1c3b718b9c | 84 | if (rc != 0) |
mazgch | 10:6f1c3b718b9c | 85 | lcd.printf("rc from TCP connect is %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 86 | |
mazgch | 10:6f1c3b718b9c | 87 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
mazgch | 10:6f1c3b718b9c | 88 | data.MQTTVersion = 3; |
mazgch | 10:6f1c3b718b9c | 89 | data.clientID.cstring = "mbed-icraggs"; |
mazgch | 10:6f1c3b718b9c | 90 | rc = client.connect(&data); |
mazgch | 10:6f1c3b718b9c | 91 | if (rc != 0) |
mazgch | 10:6f1c3b718b9c | 92 | lcd.printf("rc from MQTT connect is %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 93 | |
mazgch | 10:6f1c3b718b9c | 94 | rc = client.subscribe(topic, MQTT::QOS1, messageArrived); |
mazgch | 10:6f1c3b718b9c | 95 | if (rc != 0) { |
mazgch | 10:6f1c3b718b9c | 96 | printf("rc from MQTT subscribe is %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 97 | } |
mazgch | 10:6f1c3b718b9c | 98 | |
mazgch | 10:6f1c3b718b9c | 99 | MQTT::Message message; |
mazgch | 10:6f1c3b718b9c | 100 | |
mazgch | 10:6f1c3b718b9c | 101 | // QoS 0 |
mazgch | 10:6f1c3b718b9c | 102 | char buf[100]; |
mazgch | 10:6f1c3b718b9c | 103 | sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); |
mazgch | 10:6f1c3b718b9c | 104 | message.qos = MQTT::QOS0; |
mazgch | 10:6f1c3b718b9c | 105 | message.retained = false; |
mazgch | 10:6f1c3b718b9c | 106 | message.dup = false; |
mazgch | 10:6f1c3b718b9c | 107 | message.payload = (void*)buf; |
mazgch | 10:6f1c3b718b9c | 108 | message.payloadlen = strlen(buf)+1; |
mazgch | 10:6f1c3b718b9c | 109 | rc = client.publish(topic, &message); |
mazgch | 10:6f1c3b718b9c | 110 | while (arrivedcount == 0) |
mazgch | 10:6f1c3b718b9c | 111 | client.yield(100); |
mazgch | 8:b32c94be6522 | 112 | |
mazgch | 10:6f1c3b718b9c | 113 | // QoS 1 |
mazgch | 10:6f1c3b718b9c | 114 | sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); |
mazgch | 10:6f1c3b718b9c | 115 | message.qos = MQTT::QOS1; |
mazgch | 10:6f1c3b718b9c | 116 | message.payloadlen = strlen(buf)+1; |
mazgch | 10:6f1c3b718b9c | 117 | rc = client.publish(topic, &message); |
mazgch | 10:6f1c3b718b9c | 118 | while (arrivedcount == 1) |
mazgch | 10:6f1c3b718b9c | 119 | client.yield(100); |
mazgch | 10:6f1c3b718b9c | 120 | |
mazgch | 10:6f1c3b718b9c | 121 | // QoS 2 |
mazgch | 10:6f1c3b718b9c | 122 | sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); |
mazgch | 10:6f1c3b718b9c | 123 | message.qos = MQTT::QOS2; |
mazgch | 10:6f1c3b718b9c | 124 | message.payloadlen = strlen(buf)+1; |
mazgch | 10:6f1c3b718b9c | 125 | rc = client.publish(topic, &message); |
mazgch | 10:6f1c3b718b9c | 126 | while (arrivedcount == 2) |
mazgch | 10:6f1c3b718b9c | 127 | client.yield(100); |
mazgch | 10:6f1c3b718b9c | 128 | |
mazgch | 10:6f1c3b718b9c | 129 | rc = client.unsubscribe(topic); |
mazgch | 10:6f1c3b718b9c | 130 | if (rc != 0) { |
mazgch | 10:6f1c3b718b9c | 131 | printf("rc from unsubscribe was %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 132 | } |
mazgch | 10:6f1c3b718b9c | 133 | |
mazgch | 10:6f1c3b718b9c | 134 | rc = client.disconnect(); |
mazgch | 10:6f1c3b718b9c | 135 | if (rc != 0) { |
mazgch | 10:6f1c3b718b9c | 136 | printf("rc from disconnect was %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 137 | } |
icraggs | 0:0cae29831d01 | 138 | |
mazgch | 10:6f1c3b718b9c | 139 | mdm.disconnect(); |
mazgch | 8:b32c94be6522 | 140 | mdm.powerOff(); |
icraggs | 2:638c854c0695 | 141 | |
sam_grove | 5:4a257f6ac09a | 142 | lcd.cls(); |
sam_grove | 5:4a257f6ac09a | 143 | lcd.locate(0,3); |
sam_grove | 5:4a257f6ac09a | 144 | lcd.printf("Finish: %d msgs\n", arrivedcount); |
sam_grove | 5:4a257f6ac09a | 145 | printf("Finishing with %d messages received\n", arrivedcount); |
icraggs | 2:638c854c0695 | 146 | |
mazgch | 8:b32c94be6522 | 147 | while(true); |
icraggs | 0:0cae29831d01 | 148 | } |