Sample MQTT program - simple send and receive
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 1 to output messages to LCD instead of serial 00029 #define USE_LCD 0 00030 00031 #if USE_LCD 00032 #include "C12832.h" 00033 00034 // the actual pins are defined in mbed_app.json and can be overridden per target 00035 C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS); 00036 00037 #define logMessage lcd.cls();lcd.printf 00038 00039 #else 00040 00041 #define logMessage printf 00042 00043 #endif 00044 00045 #define MQTTCLIENT_QOS2 1 00046 #define MQTT_DEBUG 1 00047 00048 #include "easy-connect.h" 00049 #include "MQTTNetwork.h" 00050 #include "MQTTmbed.h" 00051 #include "MQTTClient.h" 00052 00053 int arrivedcount = 0; 00054 00055 00056 void messageArrived(MQTT::MessageData& md) 00057 { 00058 MQTT::Message &message = md.message; 00059 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); 00060 logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); 00061 ++arrivedcount; 00062 } 00063 00064 00065 int main(int argc, char* argv[]) 00066 { 00067 float version = 0.6; 00068 char* topic = "mbed-sample"; 00069 00070 logMessage("HelloMQTT: version is %.2f\r\n", version); 00071 00072 NetworkInterface* network = easy_connect(true); 00073 if (!network) { 00074 return -1; 00075 } 00076 00077 MQTTNetwork mqttNetwork(network); 00078 00079 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); 00080 00081 const char* hostname = "iot.eclipse.org"; 00082 int port = 1883; 00083 logMessage("Connecting to %s:%d\r\n", hostname, port); 00084 int rc = mqttNetwork.connect(hostname, port); 00085 if (rc != 0) 00086 logMessage("rc from TCP connect is %d\r\n", rc); 00087 00088 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00089 char clientid[30]; 00090 sprintf(clientid, "mbed-sample-%d", rand()); 00091 logMessage("Client id is %s\r\n", clientid); 00092 data.clientID.cstring = clientid; 00093 data.username.cstring = "testuser"; 00094 data.password.cstring = "testpassword"; 00095 if ((rc = client.connect(data)) != 0) 00096 logMessage("rc from MQTT connect is %d\r\n", rc); 00097 00098 if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0) 00099 logMessage("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 < 1) 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 < 2) 00121 client.yield(100); 00122 00123 // QoS 2 00124 sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version); 00125 message.qos = MQTT::QOS2; 00126 message.payloadlen = strlen(buf)+1; 00127 rc = client.publish(topic, message); 00128 while (arrivedcount < 3) 00129 client.yield(100); 00130 00131 if ((rc = client.unsubscribe(topic)) != 0) 00132 logMessage("rc from unsubscribe was %d\r\n", rc); 00133 00134 if ((rc = client.disconnect()) != 0) 00135 logMessage("rc from disconnect was %d\r\n", rc); 00136 00137 mqttNetwork.disconnect(); 00138 00139 logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount); 00140 00141 return 0; 00142 }
Generated on Tue Jul 12 2022 17:58:21 by 1.7.2