Demo program for UBLOX cell modem using Nucleo-L073RZ

Dependencies:   C027_Support MQTT mbed-dev

Fork of Cellular_HelloMQTT by u-blox

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         "epc.tmobile.com"
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 "MQTTSocket.h"
00046 #include "MQTTClient.h"
00047 DigitalOut myled(LED1);
00048 
00049 int arrivedcount = 0;
00050  
00051 void messageArrived(MQTT::MessageData& md)
00052 {
00053     MQTT::Message &message = md.message;
00054     printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
00055     printf("Payload %.*s\r", message.payloadlen, (char*)message.payload);
00056     ++arrivedcount;
00057 }
00058  
00059  
00060 int main(int argc, char* argv[])
00061 {   
00062     
00063     printf("start\r\n");
00064     MDMSerial mdm(PC_10,PC_11);
00065     //mdm.setDebug(4); // enable this for debugging issues 
00066     printf("attempting to connect to network\r\n");
00067     if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)){
00068         printf("no connect\r\n");
00069         while(1) {
00070             myled = 1;
00071             wait(0.25);
00072             myled = 0; 
00073             wait(0.25);
00074         }
00075     }
00076     
00077     MQTTSocket ipstack = MQTTSocket();
00078     float version = 0.47;
00079     char* topic = "HelloWorld";
00080     
00081     printf("Version is %f\r\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     printf("Connecting to %s:%d\r\n", hostname, port);
00088     int rc = ipstack.connect(hostname, port);
00089     if (rc != 0)
00090         printf("rc from TCP connect is %d\r\n", rc);
00091  
00092     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
00093     data.MQTTVersion = 4;
00094     data.clientID.cstring = "MQTT_Test";
00095     if ((rc = client.connect(data)) != 0)
00096         printf("rc from MQTT connect is %d\n", rc);
00097     
00098     if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0)
00099         printf("rc from MQTT subscribe is %d\r\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\r\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\r\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         //todo configure server for QoS2
00125     sprintf(buf, "Hello World!  QoS 2 message from app version %f\r\n", version);
00126     message.qos = MQTT::QOS2;
00127     message.payloadlen = strlen(buf)+1;
00128     rc = client.publish(topic, message);
00129     while (arrivedcount == 2)
00130         client.yield(100);
00131     */
00132     if ((rc = client.unsubscribe(topic)) != 0)
00133         printf("rc from unsubscribe was %d\r\n", rc);
00134     
00135     if ((rc = client.disconnect()) != 0)
00136         printf("rc from disconnect was %d\r\n", rc);
00137     
00138     ipstack.disconnect();
00139     mdm.disconnect();
00140     mdm.powerOff();
00141     
00142     printf("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
00143     printf("Finishing with %d messages received\r\n", arrivedcount);
00144     
00145     return 0;
00146 }
00147