MQTT cellular example

Dependencies:   C027_Support C12832 MQTT mbed

Fork of Cellular_HelloMQTT by Michael Ammann

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016  
00017  /**
00018   This is a sample program to illustrate the use of the MQTT Client library
00019   on the mbed platform.  The Client class requires two classes which mediate
00020   access to system interfaces for networking and timing.  As long as these two
00021   classes provide the required public programming interfaces, it does not matter
00022   what facilities they use underneath. In this program, they use the mbed
00023   system libraries.
00024  
00025  */
00026 #include "mbed.h"
00027 
00028 //------------------------------------------------------------------------------------
00029 // You need to configure these cellular modem / SIM parameters.
00030 // These parameters are ignored for LISA-C200 variants and can be left NULL.
00031 //------------------------------------------------------------------------------------
00032 #include "MDM.h"
00033 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
00034 #define SIMPIN      NULL
00035 /*! The APN of your network operator SIM, sometimes it is "internet" check your 
00036     contract with the network operator. You can also try to look-up your settings in 
00037     google: https://www.google.de/search?q=APN+list */
00038 #define APN         NULL
00039 //! Set the user name for your APN, or NULL if not needed
00040 #define USERNAME    NULL
00041 //! Set the password for your APN, or NULL if not needed
00042 #define PASSWORD    NULL 
00043 //------------------------------------------------------------------------------------
00044 
00045 #include "C12832.h"
00046 #if defined(TARGET_LPC1768) && !defined(TARGET_UBLOX_C027)
00047 C12832 lcd(p5, p7, p6, p8, p11);    // The classic TARGET_MBED_LPC1768
00048 #else
00049 C12832 lcd(D11, D13, D12, D7, D10);
00050 #endif
00051 
00052 #include "MQTTSocket.h"
00053 #include "MQTTClient.h"
00054 
00055 int arrivedcount = 0;
00056  
00057 void messageArrived(MQTT::MessageData& md)
00058 {
00059     MQTT::Message &message = md.message;
00060     lcd.cls();
00061     lcd.locate(0,3);
00062     printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
00063     printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
00064     ++arrivedcount;
00065     lcd.puts((char*)message.payload);
00066 }
00067  
00068  
00069 int main(int argc, char* argv[])
00070 {   
00071     MDMSerial mdm;
00072     //mdm.setDebug(4); // enable this for debugging issues 
00073     if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
00074         return -1;
00075     
00076     MQTTSocket ipstack = MQTTSocket();
00077     float version = 0.47;
00078     char* topic = "mbed-sample";
00079     
00080     lcd.printf("Version is %f\n", version);
00081     printf("Version is %f\n", version);
00082               
00083     MQTT::Client<MQTTSocket, Countdown> client = MQTT::Client<MQTTSocket, Countdown>(ipstack);
00084     
00085     char* hostname = "m2m.eclipse.org";
00086     int port = 1883;
00087     lcd.printf("Connecting to %s:%d\n", hostname, port);
00088     int rc = ipstack.connect(hostname, port);
00089     if (rc != 0)
00090         lcd.printf("rc from TCP connect is %d\n", rc);
00091  
00092     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
00093     data.MQTTVersion = 3;
00094     data.clientID.cstring = "mbed-sample";
00095     if ((rc = client.connect(&data)) != 0)
00096         lcd.printf("rc from MQTT connect is %d\n", rc);
00097     
00098     if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0)
00099         lcd.printf("rc from MQTT subscribe is %d\n", rc);
00100  
00101     MQTT::Message message;
00102  
00103     // QoS 0
00104     char buf[100];
00105     sprintf(buf, "Hello World!  QoS 0 message from app version %f\n", version);
00106     message.qos = MQTT::QOS0;
00107     message.retained = false;
00108     message.dup = false;
00109     message.payload = (void*)buf;
00110     message.payloadlen = strlen(buf)+1;
00111     rc = client.publish(topic, &message);
00112     while (arrivedcount == 0)
00113         client.yield(100);
00114         
00115     // QoS 1
00116     sprintf(buf, "Hello World!  QoS 1 message from app version %f\n", version);
00117     message.qos = MQTT::QOS1;
00118     message.payloadlen = strlen(buf)+1;
00119     rc = client.publish(topic, &message);
00120     while (arrivedcount == 1)
00121         client.yield(100);
00122         
00123     // QoS 2
00124     sprintf(buf, "Hello World!  QoS 2 message from app version %f\n", version);
00125     message.qos = MQTT::QOS2;
00126     message.payloadlen = strlen(buf)+1;
00127     rc = client.publish(topic, &message);
00128     while (arrivedcount == 2)
00129         client.yield(100);
00130     
00131     if ((rc = client.unsubscribe(topic)) != 0)
00132         printf("rc from unsubscribe was %d\n", rc);
00133     
00134     if ((rc = client.disconnect()) != 0)
00135         printf("rc from disconnect was %d\n", rc);
00136     
00137     ipstack.disconnect();
00138     mdm.disconnect();
00139     mdm.powerOff();
00140     
00141     lcd.cls();
00142     lcd.locate(0,3);
00143     lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
00144     printf("Finishing with %d messages received\n", arrivedcount);
00145     
00146     return 0;
00147 }