client
Dependencies: DISCO_L475VG_IOT01A_wifi mbed stm-spirit1-rf-driver
MQTT_wifi.h@4:40e6ed9dbfaf, 2017-09-25 (annotated)
- Committer:
- apate131
- Date:
- Mon Sep 25 16:19:27 2017 +0000
- Revision:
- 4:40e6ed9dbfaf
asdjnakjsd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apate131 | 4:40e6ed9dbfaf | 1 | #include <string> |
apate131 | 4:40e6ed9dbfaf | 2 | |
apate131 | 4:40e6ed9dbfaf | 3 | #include "wifi.h" |
apate131 | 4:40e6ed9dbfaf | 4 | |
apate131 | 4:40e6ed9dbfaf | 5 | |
apate131 | 4:40e6ed9dbfaf | 6 | class MQTT_wifi |
apate131 | 4:40e6ed9dbfaf | 7 | { |
apate131 | 4:40e6ed9dbfaf | 8 | public: |
apate131 | 4:40e6ed9dbfaf | 9 | MQTT_wifi() { |
apate131 | 4:40e6ed9dbfaf | 10 | } |
apate131 | 4:40e6ed9dbfaf | 11 | |
apate131 | 4:40e6ed9dbfaf | 12 | int connect(char* hostname, int port, int timeout=1000) { |
apate131 | 4:40e6ed9dbfaf | 13 | |
apate131 | 4:40e6ed9dbfaf | 14 | uint8_t addr[4]; |
apate131 | 4:40e6ed9dbfaf | 15 | if (WIFI_GetHostAddress(hostname, addr) != WIFI_STATUS_OK) { |
apate131 | 4:40e6ed9dbfaf | 16 | return -1; |
apate131 | 4:40e6ed9dbfaf | 17 | } |
apate131 | 4:40e6ed9dbfaf | 18 | |
apate131 | 4:40e6ed9dbfaf | 19 | if (WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", addr, port, 100) != WIFI_STATUS_OK) { |
apate131 | 4:40e6ed9dbfaf | 20 | return -1; |
apate131 | 4:40e6ed9dbfaf | 21 | } |
apate131 | 4:40e6ed9dbfaf | 22 | |
apate131 | 4:40e6ed9dbfaf | 23 | socket = 0; |
apate131 | 4:40e6ed9dbfaf | 24 | return socket; |
apate131 | 4:40e6ed9dbfaf | 25 | } |
apate131 | 4:40e6ed9dbfaf | 26 | |
apate131 | 4:40e6ed9dbfaf | 27 | int read(unsigned char* buffer, int len, int timeout) { |
apate131 | 4:40e6ed9dbfaf | 28 | uint16_t read_len = -1; |
apate131 | 4:40e6ed9dbfaf | 29 | if(WIFI_ReceiveData(socket, buffer, len, &read_len, timeout) == WIFI_STATUS_OK) { |
apate131 | 4:40e6ed9dbfaf | 30 | return read_len; |
apate131 | 4:40e6ed9dbfaf | 31 | } |
apate131 | 4:40e6ed9dbfaf | 32 | |
apate131 | 4:40e6ed9dbfaf | 33 | return -1; |
apate131 | 4:40e6ed9dbfaf | 34 | } |
apate131 | 4:40e6ed9dbfaf | 35 | |
apate131 | 4:40e6ed9dbfaf | 36 | int write(unsigned char* buffer, int len, int timeout) { |
apate131 | 4:40e6ed9dbfaf | 37 | uint16_t sent_len = -1; |
apate131 | 4:40e6ed9dbfaf | 38 | if(WIFI_SendData(socket, buffer, len, &sent_len, timeout) == WIFI_STATUS_OK) { |
apate131 | 4:40e6ed9dbfaf | 39 | return sent_len; |
apate131 | 4:40e6ed9dbfaf | 40 | } |
apate131 | 4:40e6ed9dbfaf | 41 | |
apate131 | 4:40e6ed9dbfaf | 42 | return -1; |
apate131 | 4:40e6ed9dbfaf | 43 | } |
apate131 | 4:40e6ed9dbfaf | 44 | |
apate131 | 4:40e6ed9dbfaf | 45 | |
apate131 | 4:40e6ed9dbfaf | 46 | |
apate131 | 4:40e6ed9dbfaf | 47 | private: |
apate131 | 4:40e6ed9dbfaf | 48 | int socket; |
apate131 | 4:40e6ed9dbfaf | 49 | |
apate131 | 4:40e6ed9dbfaf | 50 | }; |