MQTT cellular example
Dependencies: C027 C027_Support C12832 mbed-rtos mbed FP MQTTPacket
Fork of HelloMQTT by
MQTT/IPStack/EthernetInterfaceIPStack.h@8:b32c94be6522, 2014-05-12 (annotated)
- Committer:
- mazgch
- Date:
- Mon May 12 09:59:50 2014 +0000
- Revision:
- 8:b32c94be6522
MQTT example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 8:b32c94be6522 | 1 | #include "TCPSocketConnection.h" |
mazgch | 8:b32c94be6522 | 2 | |
mazgch | 8:b32c94be6522 | 3 | #ifndef ETHERNETINTERFACEIPSTACK_H |
mazgch | 8:b32c94be6522 | 4 | #define ETHERNETINTERFACEIPSTACK_H |
mazgch | 8:b32c94be6522 | 5 | |
mazgch | 8:b32c94be6522 | 6 | class IPStack |
mazgch | 8:b32c94be6522 | 7 | { |
mazgch | 8:b32c94be6522 | 8 | public: |
mazgch | 8:b32c94be6522 | 9 | IPStack() |
mazgch | 8:b32c94be6522 | 10 | { |
mazgch | 8:b32c94be6522 | 11 | mysock.set_blocking(false, 1000); // 1 second Timeout |
mazgch | 8:b32c94be6522 | 12 | } |
mazgch | 8:b32c94be6522 | 13 | |
mazgch | 8:b32c94be6522 | 14 | int connect(char* hostname, int port) |
mazgch | 8:b32c94be6522 | 15 | { |
mazgch | 8:b32c94be6522 | 16 | return mysock.connect(hostname, port); |
mazgch | 8:b32c94be6522 | 17 | } |
mazgch | 8:b32c94be6522 | 18 | |
mazgch | 8:b32c94be6522 | 19 | int read(char* buffer, int len, int timeout) |
mazgch | 8:b32c94be6522 | 20 | { |
mazgch | 8:b32c94be6522 | 21 | mysock.set_blocking(false, timeout); |
mazgch | 8:b32c94be6522 | 22 | return mysock.receive(buffer, len); |
mazgch | 8:b32c94be6522 | 23 | } |
mazgch | 8:b32c94be6522 | 24 | |
mazgch | 8:b32c94be6522 | 25 | int write(char* buffer, int len, int timeout) |
mazgch | 8:b32c94be6522 | 26 | { |
mazgch | 8:b32c94be6522 | 27 | mysock.set_blocking(false, timeout); |
mazgch | 8:b32c94be6522 | 28 | return mysock.send(buffer, len); |
mazgch | 8:b32c94be6522 | 29 | } |
mazgch | 8:b32c94be6522 | 30 | |
mazgch | 8:b32c94be6522 | 31 | int disconnect() |
mazgch | 8:b32c94be6522 | 32 | { |
mazgch | 8:b32c94be6522 | 33 | return mysock.close(); |
mazgch | 8:b32c94be6522 | 34 | } |
mazgch | 8:b32c94be6522 | 35 | |
mazgch | 8:b32c94be6522 | 36 | private: |
mazgch | 8:b32c94be6522 | 37 | |
mazgch | 8:b32c94be6522 | 38 | TCPSocketConnection mysock; |
mazgch | 8:b32c94be6522 | 39 | |
mazgch | 8:b32c94be6522 | 40 | }; |
mazgch | 8:b32c94be6522 | 41 | |
mazgch | 8:b32c94be6522 | 42 | |
mazgch | 8:b32c94be6522 | 43 | class Countdown |
mazgch | 8:b32c94be6522 | 44 | { |
mazgch | 8:b32c94be6522 | 45 | public: |
mazgch | 8:b32c94be6522 | 46 | Countdown() |
mazgch | 8:b32c94be6522 | 47 | { |
mazgch | 8:b32c94be6522 | 48 | t = Timer(); |
mazgch | 8:b32c94be6522 | 49 | } |
mazgch | 8:b32c94be6522 | 50 | |
mazgch | 8:b32c94be6522 | 51 | Countdown(int ms) |
mazgch | 8:b32c94be6522 | 52 | { |
mazgch | 8:b32c94be6522 | 53 | t = Timer(); |
mazgch | 8:b32c94be6522 | 54 | countdown_ms(ms); |
mazgch | 8:b32c94be6522 | 55 | } |
mazgch | 8:b32c94be6522 | 56 | |
mazgch | 8:b32c94be6522 | 57 | |
mazgch | 8:b32c94be6522 | 58 | bool expired() |
mazgch | 8:b32c94be6522 | 59 | { |
mazgch | 8:b32c94be6522 | 60 | return t.read_ms() >= interval_end_ms; |
mazgch | 8:b32c94be6522 | 61 | } |
mazgch | 8:b32c94be6522 | 62 | |
mazgch | 8:b32c94be6522 | 63 | void countdown_ms(int ms) |
mazgch | 8:b32c94be6522 | 64 | { |
mazgch | 8:b32c94be6522 | 65 | t.stop(); |
mazgch | 8:b32c94be6522 | 66 | interval_end_ms = ms; |
mazgch | 8:b32c94be6522 | 67 | t.reset(); |
mazgch | 8:b32c94be6522 | 68 | t.start(); |
mazgch | 8:b32c94be6522 | 69 | } |
mazgch | 8:b32c94be6522 | 70 | |
mazgch | 8:b32c94be6522 | 71 | void countdown(int seconds) |
mazgch | 8:b32c94be6522 | 72 | { |
mazgch | 8:b32c94be6522 | 73 | countdown_ms(seconds * 1000); |
mazgch | 8:b32c94be6522 | 74 | } |
mazgch | 8:b32c94be6522 | 75 | |
mazgch | 8:b32c94be6522 | 76 | int left_ms() |
mazgch | 8:b32c94be6522 | 77 | { |
mazgch | 8:b32c94be6522 | 78 | return interval_end_ms - t.read_ms(); |
mazgch | 8:b32c94be6522 | 79 | } |
mazgch | 8:b32c94be6522 | 80 | |
mazgch | 8:b32c94be6522 | 81 | private: |
mazgch | 8:b32c94be6522 | 82 | Timer t; |
mazgch | 8:b32c94be6522 | 83 | int interval_end_ms; |
mazgch | 8:b32c94be6522 | 84 | }; |
mazgch | 8:b32c94be6522 | 85 | |
mazgch | 8:b32c94be6522 | 86 | #endif |