gs fan / Mbed 2 deprecated GSwifiInterface_HelloMQTT

Dependencies:   C12832 GSwifiInterface MQTT mbed-rtos mbed

Fork of HelloMQTT by MQTT

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 /*
00027  * Supported for GsinSpan Wi-Fi Module.
00028  * Modified by gsfan
00029  *   src: http://developer.mbed.org/teams/mqtt/code/HelloMQTT/
00030  */
00031 
00032 #include "mbed.h"
00033 #include "GSwifiInterface.h"
00034 
00035 //#define MBED_APPLICATION_BOARD
00036 
00037 #define SEC  GSwifi::SEC_WPA_PSK
00038 #define SSID "SSID"
00039 #define PASS "PASSPHRASE"
00040 
00041 
00042 #ifdef MBED_APPLICATION_BOARD
00043 #include "C12832.h"
00044 C12832 lcd(p5, p7, p6, p8, p11);
00045 #else
00046 #define lcd pc
00047 #endif
00048 
00049 #include "MQTTGSwifi.h"
00050 #include "MQTTClient.h"
00051 
00052 int arrivedcount = 0;
00053 GSwifiInterface *gs;
00054 Serial pc(USBTX, USBRX);
00055 
00056 void messageArrived(MQTT::MessageData& md)
00057 {
00058     MQTT::Message &message = md.message;
00059 #ifdef MBED_APPLICATION_BOARD
00060     lcd.cls();
00061     lcd.locate(0,3);
00062 #endif
00063     printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
00064     printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
00065     ++arrivedcount;
00066     lcd.puts((char*)message.payload);
00067 }
00068 
00069 
00070 int main(int argc, char* argv[])
00071 {   
00072     pc.baud(115200);
00073 //    gs = new GSwifiInterface(p13, p14, p12, P0_22, p21, NC, 115200); //tx, rx, cts, rts, reset, alarm, baud
00074     gs = new GSwifiInterface(p9, p10, NC, NC, p30, NC, 9600); //tx, rx, cts, rts, reset, alarm, baud
00075     wait_ms(200);
00076     lcd.printf("Connecting...\n");
00077     gs->init(); //Use DHCP
00078     if (gs->connect(SEC, SSID, PASS)) return -1; // join the network
00079     lcd.printf("IP Address is %s\n", gs->getIPAddress());
00080 
00081     MQTTGSwifi ipstack = MQTTGSwifi(gs);
00082     float version = 0.47;
00083     char* topic = "mbed-sample";
00084     
00085     lcd.printf("Version is %f\n", version);
00086     printf("Version is %f\n", version);
00087               
00088     MQTT::Client<MQTTGSwifi, Countdown> client = MQTT::Client<MQTTGSwifi, Countdown>(ipstack);
00089     
00090     char* hostname = "m2m.eclipse.org";
00091     int port = 1883;
00092     lcd.printf("Connecting to %s:%d\n", hostname, port);
00093     int rc = ipstack.connect(hostname, port);
00094     if (rc != 0)
00095         lcd.printf("rc from TCP connect is %d\n", rc);
00096  
00097     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
00098     data.MQTTVersion = 3;
00099     data.clientID.cstring = "mbed-sample";
00100     data.username.cstring = "testuser";
00101     data.password.cstring = "testpassword";
00102     if ((rc = client.connect(&data)) != 0)
00103         lcd.printf("rc from MQTT connect is %d\n", rc);
00104     
00105     if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0)
00106         lcd.printf("rc from MQTT subscribe is %d\n", rc);
00107 
00108     MQTT::Message message;
00109 
00110     // QoS 0
00111     char buf[100];
00112     sprintf(buf, "Hello World!  QoS 0 message from app version %f\n", version);
00113     message.qos = MQTT::QOS0;
00114     message.retained = false;
00115     message.dup = false;
00116     message.payload = (void*)buf;
00117     message.payloadlen = strlen(buf)+1;
00118     rc = client.publish(topic, &message);
00119     while (arrivedcount < 1)
00120         client.yield(100);
00121         
00122     // QoS 1
00123     sprintf(buf, "Hello World!  QoS 1 message from app version %f\n", version);
00124     message.qos = MQTT::QOS1;
00125     message.payloadlen = strlen(buf)+1;
00126     rc = client.publish(topic, &message);
00127     while (arrivedcount < 2)
00128         client.yield(100);
00129         
00130     // QoS 2
00131     sprintf(buf, "Hello World!  QoS 2 message from app version %f\n", version);
00132     message.qos = MQTT::QOS2;
00133     message.payloadlen = strlen(buf)+1;
00134     rc = client.publish(topic, &message);
00135     while (arrivedcount < 3)
00136         client.yield(100);
00137         
00138     // n * QoS 2
00139     for (int i = 1; i <= 10; ++i)
00140     {
00141         sprintf(buf, "Hello World!  QoS 2 message number %d from app version %f\n", i, version);
00142         message.qos = MQTT::QOS2;
00143         message.payloadlen = strlen(buf)+1;
00144         rc = client.publish(topic, &message);
00145         while (arrivedcount < i + 3)
00146             client.yield(100);
00147     }
00148     
00149     if ((rc = client.unsubscribe(topic)) != 0)
00150         printf("rc from unsubscribe was %d\n", rc);
00151     
00152     if ((rc = client.disconnect()) != 0)
00153         printf("rc from disconnect was %d\n", rc);
00154     
00155     ipstack.disconnect();
00156     
00157 #ifdef MBED_APPLICATION_BOARD
00158     lcd.cls();
00159     lcd.locate(0,3);
00160 #endif
00161     lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
00162     printf("Finishing with %d messages received\n", arrivedcount);
00163     
00164     return 0;
00165 }