Add "TARGET_RZ_A1H"as target of using Ethernet IF.
Fork of Milkcocoa_EthernetIF by
MClient.h@2:a174c8a8d53e, 2015-12-18 (annotated)
- Committer:
- jksoft
- Date:
- Fri Dec 18 04:38:59 2015 +0000
- Revision:
- 2:a174c8a8d53e
- Parent:
- 1:4a634c06c5dc
- Child:
- 3:8dc743628d54
EthernetIF?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 1:4a634c06c5dc | 1 | #ifndef _MCLIENT_H_ |
jksoft | 1:4a634c06c5dc | 2 | #define _MCLIENT_H_ |
jksoft | 1:4a634c06c5dc | 3 | |
jksoft | 2:a174c8a8d53e | 4 | //#define USE_ESP8266 |
jksoft | 2:a174c8a8d53e | 5 | #define USE_ETHERNET |
jksoft | 1:4a634c06c5dc | 6 | |
jksoft | 1:4a634c06c5dc | 7 | #include "mbed.h" |
jksoft | 1:4a634c06c5dc | 8 | #ifdef USE_ESP8266 |
jksoft | 1:4a634c06c5dc | 9 | #include "MQTTESP8266.h" |
jksoft | 1:4a634c06c5dc | 10 | #else |
jksoft | 1:4a634c06c5dc | 11 | #include "MQTTEthernet.h" |
jksoft | 1:4a634c06c5dc | 12 | #endif |
jksoft | 1:4a634c06c5dc | 13 | #include "MQTTClient.h" |
jksoft | 1:4a634c06c5dc | 14 | |
jksoft | 1:4a634c06c5dc | 15 | class MClient |
jksoft | 1:4a634c06c5dc | 16 | { |
jksoft | 1:4a634c06c5dc | 17 | public: |
jksoft | 1:4a634c06c5dc | 18 | typedef void (*messageHandler)(MQTT::MessageData&); |
jksoft | 1:4a634c06c5dc | 19 | #ifdef USE_ESP8266 |
jksoft | 1:4a634c06c5dc | 20 | MClient(MQTTESP8266 *ipstack) : client(MQTT::Client<MQTTESP8266, Countdown>(*ipstack)) |
jksoft | 1:4a634c06c5dc | 21 | #else |
jksoft | 1:4a634c06c5dc | 22 | MClient(MQTTEthernet *ipstack) : client(MQTT::Client<MQTTEthernet, Countdown>(*ipstack)) |
jksoft | 1:4a634c06c5dc | 23 | #endif |
jksoft | 1:4a634c06c5dc | 24 | { |
jksoft | 1:4a634c06c5dc | 25 | _ipstack = ipstack; |
jksoft | 1:4a634c06c5dc | 26 | } |
jksoft | 1:4a634c06c5dc | 27 | void setDefaultMessageHandler(messageHandler mh) |
jksoft | 1:4a634c06c5dc | 28 | { |
jksoft | 1:4a634c06c5dc | 29 | client.setDefaultMessageHandler(mh); |
jksoft | 1:4a634c06c5dc | 30 | } |
jksoft | 1:4a634c06c5dc | 31 | int connect(char* host,int port) |
jksoft | 1:4a634c06c5dc | 32 | { |
jksoft | 1:4a634c06c5dc | 33 | return _ipstack->connect(host, port); |
jksoft | 1:4a634c06c5dc | 34 | } |
jksoft | 1:4a634c06c5dc | 35 | int connect() |
jksoft | 1:4a634c06c5dc | 36 | { |
jksoft | 1:4a634c06c5dc | 37 | return client.connect(); |
jksoft | 1:4a634c06c5dc | 38 | } |
jksoft | 1:4a634c06c5dc | 39 | int connect(MQTTPacket_connectData& options) |
jksoft | 1:4a634c06c5dc | 40 | { |
jksoft | 1:4a634c06c5dc | 41 | return client.connect(options); |
jksoft | 1:4a634c06c5dc | 42 | } |
jksoft | 1:4a634c06c5dc | 43 | int publish(const char* topicName, MQTT::Message& message) |
jksoft | 1:4a634c06c5dc | 44 | { |
jksoft | 1:4a634c06c5dc | 45 | return client.publish(topicName,message); |
jksoft | 1:4a634c06c5dc | 46 | } |
jksoft | 1:4a634c06c5dc | 47 | int publish(const char* topicName, void* payload, size_t payloadlen, enum MQTT::QoS qos = MQTT::QOS0, bool retained = false) |
jksoft | 1:4a634c06c5dc | 48 | { |
jksoft | 1:4a634c06c5dc | 49 | return client.publish(topicName,payload,payloadlen,qos,retained); |
jksoft | 1:4a634c06c5dc | 50 | } |
jksoft | 1:4a634c06c5dc | 51 | int publish(const char* topicName, void* payload, size_t payloadlen, unsigned short& id, enum MQTT::QoS qos = MQTT::QOS1, bool retained = false) |
jksoft | 1:4a634c06c5dc | 52 | { |
jksoft | 1:4a634c06c5dc | 53 | return client.publish(topicName,payload,payloadlen,id,qos,retained); |
jksoft | 1:4a634c06c5dc | 54 | } |
jksoft | 1:4a634c06c5dc | 55 | int subscribe(const char* topicFilter, enum MQTT::QoS qos, messageHandler mh) |
jksoft | 1:4a634c06c5dc | 56 | { |
jksoft | 1:4a634c06c5dc | 57 | return client.subscribe(topicFilter,qos,mh); |
jksoft | 1:4a634c06c5dc | 58 | } |
jksoft | 1:4a634c06c5dc | 59 | int unsubscribe(const char* topicFilter) |
jksoft | 1:4a634c06c5dc | 60 | { |
jksoft | 1:4a634c06c5dc | 61 | return client.unsubscribe(topicFilter); |
jksoft | 1:4a634c06c5dc | 62 | } |
jksoft | 1:4a634c06c5dc | 63 | int disconnect() |
jksoft | 1:4a634c06c5dc | 64 | { |
jksoft | 1:4a634c06c5dc | 65 | return client.disconnect(); |
jksoft | 1:4a634c06c5dc | 66 | } |
jksoft | 1:4a634c06c5dc | 67 | int yield(unsigned long timeout_ms = 1000L) |
jksoft | 1:4a634c06c5dc | 68 | { |
jksoft | 1:4a634c06c5dc | 69 | return client.yield(timeout_ms); |
jksoft | 1:4a634c06c5dc | 70 | } |
jksoft | 1:4a634c06c5dc | 71 | bool isConnected() |
jksoft | 1:4a634c06c5dc | 72 | { |
jksoft | 1:4a634c06c5dc | 73 | return client.isConnected(); |
jksoft | 1:4a634c06c5dc | 74 | } |
jksoft | 1:4a634c06c5dc | 75 | private: |
jksoft | 1:4a634c06c5dc | 76 | #ifdef USE_ESP8266 |
jksoft | 1:4a634c06c5dc | 77 | MQTT::Client<MQTTESP8266, Countdown> client; |
jksoft | 1:4a634c06c5dc | 78 | MQTTESP8266 *_ipstack; |
jksoft | 1:4a634c06c5dc | 79 | #else |
jksoft | 1:4a634c06c5dc | 80 | MQTT::Client<MQTTEthernet, Countdown> client; |
jksoft | 1:4a634c06c5dc | 81 | MQTTEthernet *_ipstack; |
jksoft | 1:4a634c06c5dc | 82 | #endif |
jksoft | 1:4a634c06c5dc | 83 | }; |
jksoft | 1:4a634c06c5dc | 84 | |
jksoft | 1:4a634c06c5dc | 85 | #endif |