An MQTT Client for the new etherNet Interface.
Dependencies: EthernetInterface mbed-rtos
Dependents: AV_MQTT niMQTT_example
niMQTT.h@4:afbc7b066cff, 2013-08-12 (annotated)
- Committer:
- Nim65s
- Date:
- Mon Aug 12 15:30:04 2013 +0000
- Revision:
- 4:afbc7b066cff
- Parent:
- 1:4faa96fa4350
- Child:
- 6:661892e00fe1
reconnect() when something went wrong
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nim65s | 0:d4dfed20c6ea | 1 | #ifndef NIMQTT_H |
Nim65s | 0:d4dfed20c6ea | 2 | #define NIMQTT_H |
Nim65s | 0:d4dfed20c6ea | 3 | |
Nim65s | 0:d4dfed20c6ea | 4 | #include "mbed.h" |
Nim65s | 0:d4dfed20c6ea | 5 | #include "rtos.h" |
Nim65s | 0:d4dfed20c6ea | 6 | #include "EthernetInterface.h" |
Nim65s | 0:d4dfed20c6ea | 7 | |
Nim65s | 0:d4dfed20c6ea | 8 | enum { // Message Type |
Nim65s | 0:d4dfed20c6ea | 9 | ZERO, |
Nim65s | 0:d4dfed20c6ea | 10 | CONNECT_NUM, |
Nim65s | 0:d4dfed20c6ea | 11 | CONNACK_NUM, |
Nim65s | 0:d4dfed20c6ea | 12 | PUBLISH_NUM, |
Nim65s | 0:d4dfed20c6ea | 13 | PUBACK_NUM, |
Nim65s | 0:d4dfed20c6ea | 14 | PUBREC_NUM, |
Nim65s | 0:d4dfed20c6ea | 15 | PUBREL_NUM, |
Nim65s | 0:d4dfed20c6ea | 16 | PUBCOMP_NUM, |
Nim65s | 0:d4dfed20c6ea | 17 | SUBSCRIBE_NUM, |
Nim65s | 0:d4dfed20c6ea | 18 | SUBACK_NUM, |
Nim65s | 0:d4dfed20c6ea | 19 | UNSUBSCRIBE_NUM, |
Nim65s | 0:d4dfed20c6ea | 20 | UNSUBACK_NUM, |
Nim65s | 0:d4dfed20c6ea | 21 | PINGREQ_NUM, |
Nim65s | 0:d4dfed20c6ea | 22 | PINGRESP_NUM, |
Nim65s | 0:d4dfed20c6ea | 23 | DISCONNECT_NUM |
Nim65s | 0:d4dfed20c6ea | 24 | }; |
Nim65s | 0:d4dfed20c6ea | 25 | |
Nim65s | 0:d4dfed20c6ea | 26 | #define CONNECT CONNECT_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 27 | #define CONNACK CONNACK_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 28 | #define PUBLISH PUBLISH_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 29 | #define PUBACK PUBACK_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 30 | #define PUBREC PUBREC_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 31 | #define PUBREL PUBREL_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 32 | #define PUBCOMP PUBCOMP_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 33 | #define SUBSCRIBE SUBSCRIBE_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 34 | #define SUBACK SUBACK_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 35 | #define UNSUBSCRIBE UNSUBSCRIBE_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 36 | #define UNSUBACK UNSUBACK_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 37 | #define PINGREQ PINGREQ_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 38 | #define PINGRESP PINGRESP_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 39 | #define DISCONNECT DISCONNECT_NUM << 4 |
Nim65s | 0:d4dfed20c6ea | 40 | |
Nim65s | 0:d4dfed20c6ea | 41 | enum { // QoS level |
Nim65s | 0:d4dfed20c6ea | 42 | MOST_ONCE_NUM, |
Nim65s | 0:d4dfed20c6ea | 43 | LEAST_ONCE_NUM, |
Nim65s | 0:d4dfed20c6ea | 44 | EXACTLY_ONCE_NUM |
Nim65s | 0:d4dfed20c6ea | 45 | }; |
Nim65s | 0:d4dfed20c6ea | 46 | |
Nim65s | 0:d4dfed20c6ea | 47 | #define MOST_ONCE MOST_ONCE_NUM << 1 |
Nim65s | 0:d4dfed20c6ea | 48 | #define LEAST_ONCE LEAST_ONCE_NUM << 1 |
Nim65s | 0:d4dfed20c6ea | 49 | #define EXACTLY_ONCE EXACTLY_ONCE_NUM << 1 |
Nim65s | 0:d4dfed20c6ea | 50 | |
Nim65s | 1:4faa96fa4350 | 51 | #define KEEP_ALIVE 100 // seconds |
Nim65s | 0:d4dfed20c6ea | 52 | #define TIMEOUT 1000 // ms |
Nim65s | 0:d4dfed20c6ea | 53 | |
Nim65s | 0:d4dfed20c6ea | 54 | #define START_THREAD 1 |
Nim65s | 0:d4dfed20c6ea | 55 | |
Nim65s | 0:d4dfed20c6ea | 56 | class niMQTT { |
Nim65s | 0:d4dfed20c6ea | 57 | public: |
Nim65s | 0:d4dfed20c6ea | 58 | /** Initialise and launch the MQTT Client |
Nim65s | 0:d4dfed20c6ea | 59 | * \param server the address of your server |
Nim65s | 1:4faa96fa4350 | 60 | * \param callback a callback to execute on receiving a PUBLISH |
Nim65s | 0:d4dfed20c6ea | 61 | * \param id the id of this client (should be unique) |
Nim65s | 1:4faa96fa4350 | 62 | * \param port the port of your server |
Nim65s | 0:d4dfed20c6ea | 63 | * \param username your username for the server |
Nim65s | 0:d4dfed20c6ea | 64 | * \param password your password for the server |
Nim65s | 0:d4dfed20c6ea | 65 | * \param debug get a more verbose output |
Nim65s | 0:d4dfed20c6ea | 66 | */ |
Nim65s | 1:4faa96fa4350 | 67 | niMQTT(char *server, void (*callback)(char *, char*), char *id="mbed", int port=1883, char *username="", char *password="", bool debug=false); |
Nim65s | 4:afbc7b066cff | 68 | void reconnect(); |
Nim65s | 0:d4dfed20c6ea | 69 | ~niMQTT(); |
Nim65s | 0:d4dfed20c6ea | 70 | |
Nim65s | 0:d4dfed20c6ea | 71 | /* Publish a message on a topic |
Nim65s | 0:d4dfed20c6ea | 72 | * \param topic the topic |
Nim65s | 0:d4dfed20c6ea | 73 | * \param message the message |
Nim65s | 0:d4dfed20c6ea | 74 | */ |
Nim65s | 0:d4dfed20c6ea | 75 | int pub(char *topic, char *message); |
Nim65s | 0:d4dfed20c6ea | 76 | |
Nim65s | 0:d4dfed20c6ea | 77 | /** Subscribe to a topic |
Nim65s | 0:d4dfed20c6ea | 78 | * \param topic the topic |
Nim65s | 0:d4dfed20c6ea | 79 | */ |
Nim65s | 0:d4dfed20c6ea | 80 | int sub(char *topic, bool unsub=false); |
Nim65s | 0:d4dfed20c6ea | 81 | |
Nim65s | 0:d4dfed20c6ea | 82 | private: |
Nim65s | 0:d4dfed20c6ea | 83 | char *server; |
Nim65s | 0:d4dfed20c6ea | 84 | int port; |
Nim65s | 0:d4dfed20c6ea | 85 | char *id; |
Nim65s | 0:d4dfed20c6ea | 86 | void (*callback)(char *, char*); |
Nim65s | 0:d4dfed20c6ea | 87 | char *username; |
Nim65s | 0:d4dfed20c6ea | 88 | char *password; |
Nim65s | 0:d4dfed20c6ea | 89 | |
Nim65s | 0:d4dfed20c6ea | 90 | bool debug; |
Nim65s | 0:d4dfed20c6ea | 91 | bool connected; |
Nim65s | 0:d4dfed20c6ea | 92 | int message_id; |
Nim65s | 0:d4dfed20c6ea | 93 | |
Nim65s | 0:d4dfed20c6ea | 94 | TCPSocketConnection *socket; |
Nim65s | 0:d4dfed20c6ea | 95 | |
Nim65s | 0:d4dfed20c6ea | 96 | int remaining_length_length(int remaining_length); |
Nim65s | 0:d4dfed20c6ea | 97 | void get_remaining_length(int remaining_length, char *packet); |
Nim65s | 0:d4dfed20c6ea | 98 | int send(char *packet, int size); |
Nim65s | 0:d4dfed20c6ea | 99 | |
Nim65s | 0:d4dfed20c6ea | 100 | int decode_remaining_length(); |
Nim65s | 0:d4dfed20c6ea | 101 | int recv(); |
Nim65s | 0:d4dfed20c6ea | 102 | |
Nim65s | 0:d4dfed20c6ea | 103 | int init(); |
Nim65s | 0:d4dfed20c6ea | 104 | |
Nim65s | 0:d4dfed20c6ea | 105 | int connect(); |
Nim65s | 0:d4dfed20c6ea | 106 | int connack(); |
Nim65s | 0:d4dfed20c6ea | 107 | int suback(bool unsub=false); |
Nim65s | 0:d4dfed20c6ea | 108 | int puback(); |
Nim65s | 0:d4dfed20c6ea | 109 | int puback_received(); |
Nim65s | 0:d4dfed20c6ea | 110 | int pingreq(); |
Nim65s | 0:d4dfed20c6ea | 111 | int pingresp(); |
Nim65s | 0:d4dfed20c6ea | 112 | int disconnect(); |
Nim65s | 0:d4dfed20c6ea | 113 | |
Nim65s | 0:d4dfed20c6ea | 114 | static void thread_starter(void const *p); |
Nim65s | 0:d4dfed20c6ea | 115 | void thread_worker(); |
Nim65s | 0:d4dfed20c6ea | 116 | Thread thread; |
Nim65s | 0:d4dfed20c6ea | 117 | |
Nim65s | 0:d4dfed20c6ea | 118 | void publish_received(); |
Nim65s | 0:d4dfed20c6ea | 119 | |
Nim65s | 0:d4dfed20c6ea | 120 | bool waiting_new_packet, packet_sent; |
Nim65s | 0:d4dfed20c6ea | 121 | int waiting_connack, waiting_suback, waiting_pingresp; |
Nim65s | 0:d4dfed20c6ea | 122 | }; |
Nim65s | 0:d4dfed20c6ea | 123 | |
Nim65s | 0:d4dfed20c6ea | 124 | #endif |