Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027 C027_Support C12832 mbed-rtos mbed FP MQTTPacket
Fork of HelloMQTT by
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 00027 #include "mbed.h" 00028 #include "EthernetInterfaceIPStack.h" 00029 #include "C027.h" 00030 #include "MDM.h" 00031 00032 //---------------------------------------------------------------------- 00033 // You may need to configure these parameters 00034 00035 /** Set your secret SIM pin here "1234" 00036 */ 00037 #define SIMPIN NULL 00038 00039 /** The APN of your network operator, sometimes it is "internet" 00040 check your contract with the network operator 00041 */ 00042 #define APN "gprs.swisscom.ch" 00043 00044 /** Set the user name for your APN, or NULL if not needed 00045 */ 00046 #define USERNAME NULL 00047 00048 /** Set the password for your APN, or NULL if not needed 00049 */ 00050 #define PASSWORD NULL 00051 00052 C027 c027; 00053 00054 #include "C12832.h" 00055 C12832 lcd(D11, D13, D12, D7, D10); 00056 00057 #include "FP.cpp" 00058 #include "MQTTClient.h" 00059 00060 int arrivedcount = 0; 00061 00062 void messageArrived(MQTT::Message* message) 00063 { 00064 lcd.cls(); 00065 lcd.locate(0,3); 00066 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message->qos, message->retained, message->dup, message->id); 00067 printf("Payload %.*s\n", message->payloadlen, (char*)message->payload); 00068 ++arrivedcount; 00069 lcd.puts((char*)message->payload); 00070 } 00071 00072 int main(int argc, char* argv[]) 00073 { 00074 // turn on the supplies of the Modem and the GPS 00075 c027.mdmPower(true); 00076 wait(2); 00077 // Create the modem object 00078 MDMSerial mdm; 00079 00080 // initialize the modem 00081 printf("Modem Initialize\r\n"); 00082 MDMParser::DevStatus devStatus; 00083 bool mdmOk = mdm.init(SIMPIN, &devStatus); 00084 if (mdmOk) 00085 { 00086 // wait until we are connected 00087 printf("Network Check\r\n"); 00088 MDMParser::NetStatus netStatus; 00089 while (!mdm.checkNetStatus(&netStatus)) 00090 wait_ms(1000); 00091 00092 printf("Network Join\r\n"); 00093 // join the internet connection 00094 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); 00095 if (ip != NOIP) 00096 { 00097 printf(" IP Address: " IPSTR "\r\n", IPNUM(ip)); 00098 00099 IPStack ipstack = IPStack(); 00100 float version = 0.43; 00101 char* topic = "mbed-sample"; 00102 00103 lcd.printf("Version is %f\n", version); 00104 printf("Version is %f\n", version); 00105 00106 MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack); 00107 00108 char* hostname = "m2m.eclipse.org"; 00109 int port = 1883; 00110 lcd.printf("Connecting to %s:%d\n", hostname, port); 00111 int rc = ipstack.connect(hostname, port); 00112 if (rc != 0) 00113 lcd.printf("rc from TCP connect is %d\n", rc); 00114 00115 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00116 data.MQTTVersion = 3; 00117 data.clientID.cstring = "mbed-icraggs"; 00118 rc = client.connect(&data); 00119 if (rc != 0) 00120 lcd.printf("rc from MQTT connect is %d\n", rc); 00121 00122 rc = client.subscribe(topic, MQTT::QOS1, messageArrived); 00123 if (rc != 0) { 00124 printf("rc from MQTT subscribe is %d\n", rc); 00125 } 00126 00127 MQTT::Message message; 00128 00129 // QoS 0 00130 char buf[100]; 00131 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); 00132 message.qos = MQTT::QOS0; 00133 message.retained = false; 00134 message.dup = false; 00135 message.payload = (void*)buf; 00136 message.payloadlen = strlen(buf)+1; 00137 rc = client.publish(topic, &message); 00138 while (arrivedcount == 0) 00139 client.yield(100); 00140 00141 // QoS 1 00142 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); 00143 message.qos = MQTT::QOS1; 00144 message.payloadlen = strlen(buf)+1; 00145 rc = client.publish(topic, &message); 00146 while (arrivedcount == 1) 00147 client.yield(100); 00148 00149 // QoS 2 00150 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); 00151 message.qos = MQTT::QOS2; 00152 message.payloadlen = strlen(buf)+1; 00153 rc = client.publish(topic, &message); 00154 while (arrivedcount == 2) 00155 client.yield(100); 00156 00157 rc = client.unsubscribe(topic); 00158 if (rc != 0) { 00159 printf("rc from unsubscribe was %d\n", rc); 00160 } 00161 00162 rc = client.disconnect(); 00163 if (rc != 0) { 00164 printf("rc from disconnect was %d\n", rc); 00165 } 00166 00167 mdm.disconnect(); 00168 } 00169 } 00170 mdm.powerOff(); 00171 c027.mdmPower(false); 00172 00173 lcd.cls(); 00174 lcd.locate(0,3); 00175 lcd.printf("Finish: %d msgs\n", arrivedcount); 00176 printf("Finishing with %d messages received\n", arrivedcount); 00177 00178 while(true); 00179 }
Generated on Thu Jul 14 2022 01:31:03 by
1.7.2
