MQTT cellular example

Dependencies:   C027 C027_Support C12832 mbed-rtos mbed FP MQTTPacket

Fork of HelloMQTT by MQTT

Committer:
mazgch
Date:
Mon May 12 10:19:47 2014 +0000
Revision:
9:996c8c7aa1f9
Parent:
8:b32c94be6522
latest lib

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 0:0cae29831d01 27 #include "mbed.h"
sam_grove 5:4a257f6ac09a 28 #include "EthernetInterfaceIPStack.h"
mazgch 8:b32c94be6522 29 #include "C027.h"
mazgch 8:b32c94be6522 30 #include "MDM.h"
mazgch 8:b32c94be6522 31
mazgch 8:b32c94be6522 32 //----------------------------------------------------------------------
mazgch 8:b32c94be6522 33 // You may need to configure these parameters
mazgch 8:b32c94be6522 34
mazgch 8:b32c94be6522 35 /** Set your secret SIM pin here "1234"
mazgch 8:b32c94be6522 36 */
mazgch 8:b32c94be6522 37 #define SIMPIN NULL
mazgch 8:b32c94be6522 38
mazgch 8:b32c94be6522 39 /** The APN of your network operator, sometimes it is "internet"
mazgch 8:b32c94be6522 40 check your contract with the network operator
mazgch 8:b32c94be6522 41 */
mazgch 8:b32c94be6522 42 #define APN "gprs.swisscom.ch"
mazgch 8:b32c94be6522 43
mazgch 8:b32c94be6522 44 /** Set the user name for your APN, or NULL if not needed
mazgch 8:b32c94be6522 45 */
mazgch 8:b32c94be6522 46 #define USERNAME NULL
mazgch 8:b32c94be6522 47
mazgch 8:b32c94be6522 48 /** Set the password for your APN, or NULL if not needed
mazgch 8:b32c94be6522 49 */
mazgch 8:b32c94be6522 50 #define PASSWORD NULL
mazgch 8:b32c94be6522 51
mazgch 8:b32c94be6522 52 C027 c027;
icraggs 2:638c854c0695 53
sam_grove 5:4a257f6ac09a 54 #include "C12832.h"
mazgch 8:b32c94be6522 55 C12832 lcd(D11, D13, D12, D7, D10);
icraggs 0:0cae29831d01 56
icraggs 2:638c854c0695 57 #include "FP.cpp"
icraggs 2:638c854c0695 58 #include "MQTTClient.h"
icraggs 2:638c854c0695 59
icraggs 2:638c854c0695 60 int arrivedcount = 0;
icraggs 2:638c854c0695 61
icraggs 2:638c854c0695 62 void messageArrived(MQTT::Message* message)
icraggs 2:638c854c0695 63 {
sam_grove 5:4a257f6ac09a 64 lcd.cls();
sam_grove 5:4a257f6ac09a 65 lcd.locate(0,3);
sam_grove 5:4a257f6ac09a 66 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message->qos, message->retained, message->dup, message->id);
sam_grove 5:4a257f6ac09a 67 printf("Payload %.*s\n", message->payloadlen, (char*)message->payload);
icraggs 2:638c854c0695 68 ++arrivedcount;
sam_grove 5:4a257f6ac09a 69 lcd.puts((char*)message->payload);
icraggs 2:638c854c0695 70 }
icraggs 0:0cae29831d01 71
icraggs 2:638c854c0695 72 int main(int argc, char* argv[])
icraggs 2:638c854c0695 73 {
mazgch 8:b32c94be6522 74 // turn on the supplies of the Modem and the GPS
mazgch 8:b32c94be6522 75 c027.mdmPower(true);
mazgch 8:b32c94be6522 76 wait(2);
mazgch 8:b32c94be6522 77 // Create the modem object
mazgch 8:b32c94be6522 78 MDMSerial mdm;
icraggs 2:638c854c0695 79
mazgch 8:b32c94be6522 80 // initialize the modem
mazgch 8:b32c94be6522 81 printf("Modem Initialize\r\n");
mazgch 8:b32c94be6522 82 MDMParser::DevStatus devStatus;
mazgch 8:b32c94be6522 83 bool mdmOk = mdm.init(SIMPIN, &devStatus);
mazgch 8:b32c94be6522 84 if (mdmOk)
mazgch 8:b32c94be6522 85 {
mazgch 8:b32c94be6522 86 // wait until we are connected
mazgch 8:b32c94be6522 87 printf("Network Check\r\n");
mazgch 8:b32c94be6522 88 MDMParser::NetStatus netStatus;
mazgch 8:b32c94be6522 89 while (!mdm.checkNetStatus(&netStatus))
mazgch 8:b32c94be6522 90 wait_ms(1000);
icraggs 3:7a6a899de7cc 91
mazgch 8:b32c94be6522 92 printf("Network Join\r\n");
mazgch 8:b32c94be6522 93 // join the internet connection
mazgch 8:b32c94be6522 94 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
mazgch 8:b32c94be6522 95 if (ip != NOIP)
mazgch 8:b32c94be6522 96 {
mazgch 8:b32c94be6522 97 printf(" IP Address: " IPSTR "\r\n", IPNUM(ip));
mazgch 8:b32c94be6522 98
mazgch 8:b32c94be6522 99 IPStack ipstack = IPStack();
mazgch 8:b32c94be6522 100 float version = 0.43;
mazgch 8:b32c94be6522 101 char* topic = "mbed-sample";
mazgch 8:b32c94be6522 102
mazgch 8:b32c94be6522 103 lcd.printf("Version is %f\n", version);
mazgch 8:b32c94be6522 104 printf("Version is %f\n", version);
mazgch 8:b32c94be6522 105
mazgch 8:b32c94be6522 106 MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
mazgch 8:b32c94be6522 107
mazgch 8:b32c94be6522 108 char* hostname = "m2m.eclipse.org";
mazgch 8:b32c94be6522 109 int port = 1883;
mazgch 8:b32c94be6522 110 lcd.printf("Connecting to %s:%d\n", hostname, port);
mazgch 8:b32c94be6522 111 int rc = ipstack.connect(hostname, port);
mazgch 8:b32c94be6522 112 if (rc != 0)
mazgch 8:b32c94be6522 113 lcd.printf("rc from TCP connect is %d\n", rc);
mazgch 8:b32c94be6522 114
mazgch 8:b32c94be6522 115 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
mazgch 8:b32c94be6522 116 data.MQTTVersion = 3;
mazgch 8:b32c94be6522 117 data.clientID.cstring = "mbed-icraggs";
mazgch 8:b32c94be6522 118 rc = client.connect(&data);
mazgch 8:b32c94be6522 119 if (rc != 0)
mazgch 8:b32c94be6522 120 lcd.printf("rc from MQTT connect is %d\n", rc);
mazgch 8:b32c94be6522 121
mazgch 8:b32c94be6522 122 rc = client.subscribe(topic, MQTT::QOS1, messageArrived);
mazgch 8:b32c94be6522 123 if (rc != 0) {
mazgch 8:b32c94be6522 124 printf("rc from MQTT subscribe is %d\n", rc);
mazgch 8:b32c94be6522 125 }
mazgch 8:b32c94be6522 126
mazgch 8:b32c94be6522 127 MQTT::Message message;
mazgch 8:b32c94be6522 128
mazgch 8:b32c94be6522 129 // QoS 0
mazgch 8:b32c94be6522 130 char buf[100];
mazgch 8:b32c94be6522 131 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version);
mazgch 8:b32c94be6522 132 message.qos = MQTT::QOS0;
mazgch 8:b32c94be6522 133 message.retained = false;
mazgch 8:b32c94be6522 134 message.dup = false;
mazgch 8:b32c94be6522 135 message.payload = (void*)buf;
mazgch 8:b32c94be6522 136 message.payloadlen = strlen(buf)+1;
mazgch 8:b32c94be6522 137 rc = client.publish(topic, &message);
mazgch 8:b32c94be6522 138 while (arrivedcount == 0)
mazgch 8:b32c94be6522 139 client.yield(100);
mazgch 8:b32c94be6522 140
mazgch 8:b32c94be6522 141 // QoS 1
mazgch 8:b32c94be6522 142 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
mazgch 8:b32c94be6522 143 message.qos = MQTT::QOS1;
mazgch 8:b32c94be6522 144 message.payloadlen = strlen(buf)+1;
mazgch 8:b32c94be6522 145 rc = client.publish(topic, &message);
mazgch 8:b32c94be6522 146 while (arrivedcount == 1)
mazgch 8:b32c94be6522 147 client.yield(100);
mazgch 8:b32c94be6522 148
mazgch 8:b32c94be6522 149 // QoS 2
mazgch 8:b32c94be6522 150 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
mazgch 8:b32c94be6522 151 message.qos = MQTT::QOS2;
mazgch 8:b32c94be6522 152 message.payloadlen = strlen(buf)+1;
mazgch 8:b32c94be6522 153 rc = client.publish(topic, &message);
mazgch 8:b32c94be6522 154 while (arrivedcount == 2)
mazgch 8:b32c94be6522 155 client.yield(100);
mazgch 8:b32c94be6522 156
mazgch 8:b32c94be6522 157 rc = client.unsubscribe(topic);
mazgch 8:b32c94be6522 158 if (rc != 0) {
mazgch 8:b32c94be6522 159 printf("rc from unsubscribe was %d\n", rc);
mazgch 8:b32c94be6522 160 }
mazgch 8:b32c94be6522 161
mazgch 8:b32c94be6522 162 rc = client.disconnect();
mazgch 8:b32c94be6522 163 if (rc != 0) {
mazgch 8:b32c94be6522 164 printf("rc from disconnect was %d\n", rc);
mazgch 8:b32c94be6522 165 }
icraggs 0:0cae29831d01 166
mazgch 8:b32c94be6522 167 mdm.disconnect();
mazgch 8:b32c94be6522 168 }
sam_grove 5:4a257f6ac09a 169 }
mazgch 8:b32c94be6522 170 mdm.powerOff();
mazgch 8:b32c94be6522 171 c027.mdmPower(false);
icraggs 2:638c854c0695 172
sam_grove 5:4a257f6ac09a 173 lcd.cls();
sam_grove 5:4a257f6ac09a 174 lcd.locate(0,3);
sam_grove 5:4a257f6ac09a 175 lcd.printf("Finish: %d msgs\n", arrivedcount);
sam_grove 5:4a257f6ac09a 176 printf("Finishing with %d messages received\n", arrivedcount);
icraggs 2:638c854c0695 177
mazgch 8:b32c94be6522 178 while(true);
icraggs 0:0cae29831d01 179 }