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: C12832 EthernetInterface MQTT mbed-rtos mbed
Fork of HelloMQTT by
main.cpp
00001 /******************************************************************************* 00002 * Copyright (c) 2014, 2015 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 * Ian Craggs - make sure QoS2 processing works, and add device headers 00016 *******************************************************************************/ 00017 00018 /** 00019 This is a sample program to illustrate the use of the MQTT Client library 00020 on the mbed platform. The Client class requires two classes which mediate 00021 access to system interfaces for networking and timing. As long as these two 00022 classes provide the required public programming interfaces, it does not matter 00023 what facilities they use underneath. In this program, they use the mbed 00024 system libraries. 00025 00026 */ 00027 00028 // change this to 0 to output messages to serial instead of LCD 00029 #define USE_LCD 1 00030 00031 #if USE_LCD 00032 #include "C12832.h" 00033 00034 #if defined(TARGET_UBLOX_C027) 00035 #warning "Compiling for mbed C027" 00036 #include "C027.h" 00037 #elif defined(TARGET_LPC1768) 00038 #warning "Compiling for mbed LPC1768" 00039 #include "LPC1768.h" 00040 #elif defined(TARGET_K64F) 00041 #warning "Compiling for mbed K64F" 00042 #include "K64F.h" 00043 #endif 00044 00045 #define printf lcd.cls();lcd.printf 00046 00047 #endif 00048 00049 #define MQTTCLIENT_QOS2 1 00050 00051 #include "MQTTEthernet.h" 00052 #include "MQTTClient.h" 00053 00054 int arrivedcount = 0; 00055 00056 00057 void messageArrived(MQTT::MessageData& md) 00058 { 00059 MQTT::Message &message = md.message; 00060 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); 00061 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); 00062 ++arrivedcount; 00063 } 00064 00065 00066 int main(int argc, char* argv[]) 00067 { 00068 MQTTEthernet ipstack = MQTTEthernet(); 00069 float version = 0.5; 00070 char* topic = "mbed-sample"; 00071 00072 printf("HelloMQTT: version is %f\n", version); 00073 00074 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); 00075 00076 char* hostname = "m2m.eclipse.org"; 00077 int port = 1883; 00078 printf("Connecting to %s:%d\n", hostname, port); 00079 int rc = ipstack.connect(hostname, port); 00080 if (rc != 0) 00081 printf("rc from TCP connect is %d\n", rc); 00082 00083 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00084 data.MQTTVersion = 3; 00085 data.clientID.cstring = "mbed-sample"; 00086 data.username.cstring = "testuser"; 00087 data.password.cstring = "testpassword"; 00088 if ((rc = client.connect(data)) != 0) 00089 printf("rc from MQTT connect is %d\n", rc); 00090 00091 if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0) 00092 printf("rc from MQTT subscribe is %d\n", rc); 00093 00094 MQTT::Message message; 00095 00096 // QoS 0 00097 char buf[100]; 00098 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); 00099 message.qos = MQTT::QOS0; 00100 message.retained = false; 00101 message.dup = false; 00102 message.payload = (void*)buf; 00103 message.payloadlen = strlen(buf)+1; 00104 rc = client.publish(topic, message); 00105 while (arrivedcount < 1) 00106 client.yield(100); 00107 00108 // QoS 1 00109 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); 00110 message.qos = MQTT::QOS1; 00111 message.payloadlen = strlen(buf)+1; 00112 rc = client.publish(topic, message); 00113 while (arrivedcount < 2) 00114 client.yield(100); 00115 00116 // QoS 2 00117 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); 00118 message.qos = MQTT::QOS2; 00119 message.payloadlen = strlen(buf)+1; 00120 rc = client.publish(topic, message); 00121 while (arrivedcount < 3) 00122 client.yield(100); 00123 00124 if ((rc = client.unsubscribe(topic)) != 0) 00125 printf("rc from unsubscribe was %d\n", rc); 00126 00127 if ((rc = client.disconnect()) != 0) 00128 printf("rc from disconnect was %d\n", rc); 00129 00130 ipstack.disconnect(); 00131 00132 printf("Version %.2f: finish %d msgs\n", version, arrivedcount); 00133 00134 return 0; 00135 }
Generated on Mon Jul 18 2022 01:25:32 by
1.7.2
