Sample MQTT program - simple send and receive (MQ Telemetry Transport) for GainSpan Wi-Fi module

Dependencies:   C12832 GSwifiInterface MQTT mbed-rtos mbed

Fork of HelloMQTT by MQTT

mbedのMQTTグループで保守されているMQTTライブラリを GainSpan Wi-Fi モジュールへ対応させたサンプルです。

MQTT (MQ Telemetry Transport) とは、IoTやM2M向け デバイス間通信のためのプロトコルです。

MQTTサーバーには http://iot.eclipse.org/ のサンドボックスを使います。 送信したメッセージをエコーバックしてくるだけの動作をします。


mbed Application Board を使う場合は次の行のコメントアウトを外すとLCDへ表示するようになる。

#define MBED_APPLICATION_BOARD

GainSpanモジュールの接続ピンについては次の行で指定する。

    gs = new GSwifiInterface(p9, p10, NC, NC, p30, NC, 9600); //tx, rx, cts, rts, reset, alarm, baud

Wi-Fiアクセスポイントは次の行で指定する。

#define SEC  GSwifi::SEC_WPA_PSK
#define SSID "SSID"
#define PASS "PASSPHRASE"
Committer:
gsfan
Date:
Thu Oct 02 03:26:56 2014 +0000
Revision:
16:fd155e824a80
Parent:
12:086a9314e8a5
Supported for GainSpan wi-fi module.; 1st build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:a1d5c7a6acbc 1 /*******************************************************************************
icraggs 1:a1d5c7a6acbc 2 * Copyright (c) 2014 IBM Corp.
icraggs 1:a1d5c7a6acbc 3 *
icraggs 1:a1d5c7a6acbc 4 * All rights reserved. This program and the accompanying materials
icraggs 1:a1d5c7a6acbc 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 1:a1d5c7a6acbc 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 1:a1d5c7a6acbc 7 *
icraggs 1:a1d5c7a6acbc 8 * The Eclipse Public License is available at
icraggs 1:a1d5c7a6acbc 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 1:a1d5c7a6acbc 10 * and the Eclipse Distribution License is available at
icraggs 1:a1d5c7a6acbc 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 1:a1d5c7a6acbc 12 *
icraggs 1:a1d5c7a6acbc 13 * Contributors:
icraggs 1:a1d5c7a6acbc 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 1:a1d5c7a6acbc 15 *******************************************************************************/
icraggs 2:638c854c0695 16
icraggs 2:638c854c0695 17 /**
icraggs 2:638c854c0695 18 This is a sample program to illustrate the use of the MQTT Client library
icraggs 2:638c854c0695 19 on the mbed platform. The Client class requires two classes which mediate
icraggs 2:638c854c0695 20 access to system interfaces for networking and timing. As long as these two
icraggs 2:638c854c0695 21 classes provide the required public programming interfaces, it does not matter
icraggs 2:638c854c0695 22 what facilities they use underneath. In this program, they use the mbed
icraggs 2:638c854c0695 23 system libraries.
icraggs 2:638c854c0695 24
icraggs 2:638c854c0695 25 */
gsfan 16:fd155e824a80 26 /*
gsfan 16:fd155e824a80 27 * Supported for GsinSpan Wi-Fi Module.
gsfan 16:fd155e824a80 28 * Modified by gsfan
gsfan 16:fd155e824a80 29 * src: http://developer.mbed.org/teams/mqtt/code/HelloMQTT/
gsfan 16:fd155e824a80 30 */
gsfan 16:fd155e824a80 31
gsfan 16:fd155e824a80 32 #include "mbed.h"
gsfan 16:fd155e824a80 33 #include "GSwifiInterface.h"
gsfan 16:fd155e824a80 34
gsfan 16:fd155e824a80 35 //#define MBED_APPLICATION_BOARD
gsfan 16:fd155e824a80 36
gsfan 16:fd155e824a80 37 #define SEC GSwifi::SEC_WPA_PSK
gsfan 16:fd155e824a80 38 #define SSID "SSID"
gsfan 16:fd155e824a80 39 #define PASS "PASSPHRASE"
icraggs 1:a1d5c7a6acbc 40
icraggs 2:638c854c0695 41
gsfan 16:fd155e824a80 42 #ifdef MBED_APPLICATION_BOARD
sam_grove 5:4a257f6ac09a 43 #include "C12832.h"
sam_grove 5:4a257f6ac09a 44 C12832 lcd(p5, p7, p6, p8, p11);
gsfan 16:fd155e824a80 45 #else
gsfan 16:fd155e824a80 46 #define lcd pc
gsfan 16:fd155e824a80 47 #endif
icraggs 0:0cae29831d01 48
gsfan 16:fd155e824a80 49 #include "MQTTGSwifi.h"
icraggs 2:638c854c0695 50 #include "MQTTClient.h"
icraggs 2:638c854c0695 51
icraggs 2:638c854c0695 52 int arrivedcount = 0;
gsfan 16:fd155e824a80 53 GSwifiInterface *gs;
gsfan 16:fd155e824a80 54 Serial pc(USBTX, USBRX);
icraggs 8:a3e3113054a1 55
icraggs 9:5beb8609e9f7 56 void messageArrived(MQTT::MessageData& md)
icraggs 2:638c854c0695 57 {
icraggs 9:5beb8609e9f7 58 MQTT::Message &message = md.message;
gsfan 16:fd155e824a80 59 #ifdef MBED_APPLICATION_BOARD
sam_grove 5:4a257f6ac09a 60 lcd.cls();
sam_grove 5:4a257f6ac09a 61 lcd.locate(0,3);
gsfan 16:fd155e824a80 62 #endif
icraggs 9:5beb8609e9f7 63 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
icraggs 9:5beb8609e9f7 64 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
icraggs 2:638c854c0695 65 ++arrivedcount;
icraggs 9:5beb8609e9f7 66 lcd.puts((char*)message.payload);
icraggs 2:638c854c0695 67 }
icraggs 0:0cae29831d01 68
icraggs 2:638c854c0695 69
icraggs 2:638c854c0695 70 int main(int argc, char* argv[])
icraggs 2:638c854c0695 71 {
gsfan 16:fd155e824a80 72 pc.baud(115200);
gsfan 16:fd155e824a80 73 // gs = new GSwifiInterface(p13, p14, p12, P0_22, p21, NC, 115200); //tx, rx, cts, rts, reset, alarm, baud
gsfan 16:fd155e824a80 74 gs = new GSwifiInterface(p9, p10, NC, NC, p30, NC, 9600); //tx, rx, cts, rts, reset, alarm, baud
gsfan 16:fd155e824a80 75 wait_ms(200);
gsfan 16:fd155e824a80 76 lcd.printf("Connecting...\n");
gsfan 16:fd155e824a80 77 gs->init(); //Use DHCP
gsfan 16:fd155e824a80 78 if (gs->connect(SEC, SSID, PASS)) return -1; // join the network
gsfan 16:fd155e824a80 79 lcd.printf("IP Address is %s\n", gs->getIPAddress());
gsfan 16:fd155e824a80 80
gsfan 16:fd155e824a80 81 MQTTGSwifi ipstack = MQTTGSwifi(gs);
icraggs 9:5beb8609e9f7 82 float version = 0.47;
icraggs 2:638c854c0695 83 char* topic = "mbed-sample";
icraggs 2:638c854c0695 84
icraggs 2:638c854c0695 85 lcd.printf("Version is %f\n", version);
sam_grove 5:4a257f6ac09a 86 printf("Version is %f\n", version);
icraggs 2:638c854c0695 87
gsfan 16:fd155e824a80 88 MQTT::Client<MQTTGSwifi, Countdown> client = MQTT::Client<MQTTGSwifi, Countdown>(ipstack);
icraggs 3:7a6a899de7cc 89
icraggs 6:e4c690c45021 90 char* hostname = "m2m.eclipse.org";
icraggs 6:e4c690c45021 91 int port = 1883;
icraggs 6:e4c690c45021 92 lcd.printf("Connecting to %s:%d\n", hostname, port);
icraggs 6:e4c690c45021 93 int rc = ipstack.connect(hostname, port);
icraggs 6:e4c690c45021 94 if (rc != 0)
icraggs 6:e4c690c45021 95 lcd.printf("rc from TCP connect is %d\n", rc);
icraggs 6:e4c690c45021 96
icraggs 6:e4c690c45021 97 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 6:e4c690c45021 98 data.MQTTVersion = 3;
icraggs 8:a3e3113054a1 99 data.clientID.cstring = "mbed-sample";
icraggs 12:086a9314e8a5 100 data.username.cstring = "testuser";
icraggs 12:086a9314e8a5 101 data.password.cstring = "testpassword";
icraggs 8:a3e3113054a1 102 if ((rc = client.connect(&data)) != 0)
icraggs 6:e4c690c45021 103 lcd.printf("rc from MQTT connect is %d\n", rc);
icraggs 2:638c854c0695 104
icraggs 8:a3e3113054a1 105 if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0)
icraggs 8:a3e3113054a1 106 lcd.printf("rc from MQTT subscribe is %d\n", rc);
icraggs 2:638c854c0695 107
icraggs 2:638c854c0695 108 MQTT::Message message;
icraggs 0:0cae29831d01 109
icraggs 2:638c854c0695 110 // QoS 0
icraggs 2:638c854c0695 111 char buf[100];
icraggs 2:638c854c0695 112 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version);
icraggs 2:638c854c0695 113 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 114 message.retained = false;
icraggs 2:638c854c0695 115 message.dup = false;
icraggs 2:638c854c0695 116 message.payload = (void*)buf;
icraggs 2:638c854c0695 117 message.payloadlen = strlen(buf)+1;
icraggs 2:638c854c0695 118 rc = client.publish(topic, &message);
icraggs 12:086a9314e8a5 119 while (arrivedcount < 1)
icraggs 2:638c854c0695 120 client.yield(100);
icraggs 2:638c854c0695 121
icraggs 2:638c854c0695 122 // QoS 1
icraggs 2:638c854c0695 123 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
icraggs 2:638c854c0695 124 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 125 message.payloadlen = strlen(buf)+1;
icraggs 2:638c854c0695 126 rc = client.publish(topic, &message);
icraggs 12:086a9314e8a5 127 while (arrivedcount < 2)
icraggs 2:638c854c0695 128 client.yield(100);
icraggs 2:638c854c0695 129
icraggs 2:638c854c0695 130 // QoS 2
icraggs 2:638c854c0695 131 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
icraggs 2:638c854c0695 132 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 133 message.payloadlen = strlen(buf)+1;
icraggs 2:638c854c0695 134 rc = client.publish(topic, &message);
icraggs 12:086a9314e8a5 135 while (arrivedcount < 3)
icraggs 2:638c854c0695 136 client.yield(100);
icraggs 12:086a9314e8a5 137
icraggs 12:086a9314e8a5 138 // n * QoS 2
icraggs 12:086a9314e8a5 139 for (int i = 1; i <= 10; ++i)
icraggs 12:086a9314e8a5 140 {
icraggs 12:086a9314e8a5 141 sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version);
icraggs 12:086a9314e8a5 142 message.qos = MQTT::QOS2;
icraggs 12:086a9314e8a5 143 message.payloadlen = strlen(buf)+1;
icraggs 12:086a9314e8a5 144 rc = client.publish(topic, &message);
icraggs 12:086a9314e8a5 145 while (arrivedcount < i + 3)
icraggs 12:086a9314e8a5 146 client.yield(100);
icraggs 12:086a9314e8a5 147 }
icraggs 2:638c854c0695 148
icraggs 8:a3e3113054a1 149 if ((rc = client.unsubscribe(topic)) != 0)
sam_grove 5:4a257f6ac09a 150 printf("rc from unsubscribe was %d\n", rc);
icraggs 2:638c854c0695 151
icraggs 8:a3e3113054a1 152 if ((rc = client.disconnect()) != 0)
sam_grove 5:4a257f6ac09a 153 printf("rc from disconnect was %d\n", rc);
icraggs 2:638c854c0695 154
icraggs 2:638c854c0695 155 ipstack.disconnect();
icraggs 2:638c854c0695 156
gsfan 16:fd155e824a80 157 #ifdef MBED_APPLICATION_BOARD
sam_grove 5:4a257f6ac09a 158 lcd.cls();
sam_grove 5:4a257f6ac09a 159 lcd.locate(0,3);
gsfan 16:fd155e824a80 160 #endif
icraggs 8:a3e3113054a1 161 lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
sam_grove 5:4a257f6ac09a 162 printf("Finishing with %d messages received\n", arrivedcount);
icraggs 2:638c854c0695 163
icraggs 0:0cae29831d01 164 return 0;
icraggs 0:0cae29831d01 165 }