【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/
Dependents: mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn
MQTT/MQTTSocket.h@13:61e0cc093180, 2018-03-26 (annotated)
- Committer:
- jksoft
- Date:
- Mon Mar 26 04:49:20 2018 +0000
- Revision:
- 13:61e0cc093180
- Parent:
- 0:0a2f634d3324
???????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:0a2f634d3324 | 1 | #if !defined(MQTTSOCKET_H) |
jksoft | 0:0a2f634d3324 | 2 | #define MQTTSOCKET_H |
jksoft | 0:0a2f634d3324 | 3 | |
jksoft | 0:0a2f634d3324 | 4 | #include "MQTTmbed.h" |
jksoft | 0:0a2f634d3324 | 5 | #include "TCPSocket.h" |
jksoft | 0:0a2f634d3324 | 6 | |
jksoft | 0:0a2f634d3324 | 7 | class MQTTSocket |
jksoft | 0:0a2f634d3324 | 8 | { |
jksoft | 0:0a2f634d3324 | 9 | public: |
jksoft | 0:0a2f634d3324 | 10 | int open(NetworkInterface* nif) |
jksoft | 0:0a2f634d3324 | 11 | { |
jksoft | 0:0a2f634d3324 | 12 | return socket.open(nif); |
jksoft | 0:0a2f634d3324 | 13 | } |
jksoft | 0:0a2f634d3324 | 14 | |
jksoft | 0:0a2f634d3324 | 15 | int connect(char* hostname, int port, int timeout=1000) |
jksoft | 0:0a2f634d3324 | 16 | { |
jksoft | 0:0a2f634d3324 | 17 | socket.set_blocking (false); |
jksoft | 0:0a2f634d3324 | 18 | socket.set_timeout(timeout); |
jksoft | 0:0a2f634d3324 | 19 | _mutex.lock(); |
jksoft | 0:0a2f634d3324 | 20 | int ret = (int)socket.connect(hostname, port); |
jksoft | 0:0a2f634d3324 | 21 | _mutex.unlock(); |
jksoft | 0:0a2f634d3324 | 22 | |
jksoft | 0:0a2f634d3324 | 23 | return ret; |
jksoft | 0:0a2f634d3324 | 24 | } |
jksoft | 0:0a2f634d3324 | 25 | |
jksoft | 0:0a2f634d3324 | 26 | int read(unsigned char* buffer, int len, int timeout) |
jksoft | 0:0a2f634d3324 | 27 | { |
jksoft | 0:0a2f634d3324 | 28 | socket.set_blocking (false); |
jksoft | 0:0a2f634d3324 | 29 | socket.set_timeout(timeout); |
jksoft | 0:0a2f634d3324 | 30 | _mutex.lock(); |
jksoft | 0:0a2f634d3324 | 31 | int ret = (int)socket.recv((char*)buffer, len); |
jksoft | 0:0a2f634d3324 | 32 | _mutex.unlock(); |
jksoft | 0:0a2f634d3324 | 33 | |
jksoft | 0:0a2f634d3324 | 34 | return ret; |
jksoft | 0:0a2f634d3324 | 35 | } |
jksoft | 0:0a2f634d3324 | 36 | |
jksoft | 0:0a2f634d3324 | 37 | int write(unsigned char* buffer, int len, int timeout) |
jksoft | 0:0a2f634d3324 | 38 | { |
jksoft | 0:0a2f634d3324 | 39 | socket.set_blocking (false); |
jksoft | 0:0a2f634d3324 | 40 | socket.set_timeout(timeout); |
jksoft | 0:0a2f634d3324 | 41 | _mutex.lock(); |
jksoft | 0:0a2f634d3324 | 42 | int ret = (int)socket.send((char*)buffer, len); |
jksoft | 0:0a2f634d3324 | 43 | _mutex.unlock(); |
jksoft | 0:0a2f634d3324 | 44 | |
jksoft | 0:0a2f634d3324 | 45 | return ret; |
jksoft | 0:0a2f634d3324 | 46 | } |
jksoft | 0:0a2f634d3324 | 47 | |
jksoft | 0:0a2f634d3324 | 48 | int disconnect() |
jksoft | 0:0a2f634d3324 | 49 | { |
jksoft | 0:0a2f634d3324 | 50 | _mutex.lock(); |
jksoft | 0:0a2f634d3324 | 51 | int ret = (int)socket.close(); |
jksoft | 0:0a2f634d3324 | 52 | _mutex.unlock(); |
jksoft | 0:0a2f634d3324 | 53 | |
jksoft | 0:0a2f634d3324 | 54 | return ret; |
jksoft | 0:0a2f634d3324 | 55 | } |
jksoft | 0:0a2f634d3324 | 56 | |
jksoft | 0:0a2f634d3324 | 57 | private: |
jksoft | 0:0a2f634d3324 | 58 | Mutex _mutex; |
jksoft | 0:0a2f634d3324 | 59 | TCPSocket socket; |
jksoft | 0:0a2f634d3324 | 60 | |
jksoft | 0:0a2f634d3324 | 61 | }; |
jksoft | 0:0a2f634d3324 | 62 | |
jksoft | 0:0a2f634d3324 | 63 | |
jksoft | 0:0a2f634d3324 | 64 | |
jksoft | 0:0a2f634d3324 | 65 | #endif |