MQTTClient

Dependents:   IoTGateway_Basic test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTClient.h Source File

MQTTClient.h

00001 /*
00002 MQTTClient.cpp
00003 Based on MQTTClient from http://ceit.uq.edu.au/content/mqttclient-mbed-version-20
00004 A simple MQTT client for mbed, version 2.0
00005 By Yilun FAN, @CEIT, @JAN 2011
00006 
00007 Bug fixes and additions by Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 */
00027 
00028 #ifndef MQTT_CLIENT_H
00029 #define MQTT_CLIENT_H
00030 
00031 #include "mbed.h"
00032 #include "TCPSocket.h"
00033 
00034 #define MQTTCONNECT 1<<4
00035 #define MQTTCONNACK 2<<4
00036 #define MQTTPUBLISH 3<<4
00037 #define MQTTSUBSCRIBE 8<<4
00038 
00039 #define MAX_PACKET_SIZE 128
00040 #define KEEPALIVE 15000
00041 
00042 /** MQTTClient 
00043  * 
00044  * Based on MQTTClient code from http://ceit.uq.edu.au/content/mqttclient-mbed-version-20
00045  * A simple MQTT client for mbed, version 2.0
00046  * By Yilun FAN, @CEIT, @JAN 2011
00047  *
00048  * Bug fixes and additions by Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
00049  */
00050 class MQTTClient 
00051 {
00052 public:
00053 
00054     MQTTClient();
00055     ~MQTTClient();
00056     MQTTClient(IpAddr server, int port, void (*callback)(char*, char*));
00057     void init(IpAddr *server, int port, void (*callback)(char*, char*));
00058     void init(IpAddr *server, int port, char *userName, char *password, void (*callback)(char*, char*));
00059     int connect(char *);
00060     void disconnect();
00061     int publish(char *, char *);
00062     int subscribe(char *);
00063     void live();
00064     
00065 private:
00066     int open_session(char* id);
00067     void read_open_session();
00068     int send_data(const char* msg, int size);
00069     void read_data();
00070     
00071     char* clientId;
00072     char* userName;
00073     char *password;
00074     Timer timer;
00075     IpAddr serverIp;
00076     int port;
00077     bool connected;
00078     bool sessionOpened;
00079     
00080     void onTCPSocketEvent(TCPSocketEvent e);
00081     TCPSocket* pTCPSocket;
00082     Host host;
00083     
00084     int lastActivity;
00085     void (*callback_server)(char*, char*);
00086 };
00087 
00088 #endif