MQTT cellular example

Dependencies:   C027_Support C12832 MQTT mbed

Fork of Cellular_HelloMQTT by Michael Ammann

Committer:
sam_grove
Date:
Thu Jul 03 17:28:22 2014 +0000
Revision:
16:c65e14e13728
Parent:
15:710f99b36ff2
Update dependency

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