4 An Arduino Stream extension for a WiFiClient or WiFiServer to be used
5 with legacy Arduino WiFi shield and other boards and shields that
6 are compatible with the Arduino WiFi library.
8 Copyright (C) 2015-2016 Jesse Frush. All rights reserved.
9 Copyright (C) 2016 Jens B. All rights reserved.
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 See file LICENSE.txt for further informations on licensing terms.
18 Last updated April 23rd, 2016
27 #define HOST_CONNECTION_DISCONNECTED 0
28 #define HOST_CONNECTION_CONNECTED 1
31 // callback function types
32 typedef void (*hostConnectionCallbackFunction)(byte);
35 class WiFiStream : public Stream
39 bool _connected = false;
40 hostConnectionCallbackFunction _currentHostConnectionCallback;
42 //configuration members
43 IPAddress _local_ip; // DHCP
48 uint8_t _key_idx; //WEP
49 const char *_key = nullptr; //WEP
50 const char *_passphrase = nullptr; //WPA
51 char *_ssid = nullptr;
54 * check if TCP client is connected
55 * @return true if connected
57 virtual bool connect_client() = 0;
60 /** constructor for TCP server */
61 WiFiStream(uint16_t server_port) : _port(server_port) {}
63 /** constructor for TCP client */
64 WiFiStream(IPAddress server_ip, uint16_t server_port) : _remote_ip(server_ip), _port(server_port) {}
66 inline void attach( hostConnectionCallbackFunction newFunction ) { _currentHostConnectionCallback = newFunction; }
68 /******************************************************************************
69 * network configuration
70 ******************************************************************************/
74 * configure a static local IP address without defining the local network
75 * DHCP will be used as long as local IP address is not defined
77 inline void config(IPAddress local_ip)
80 WiFi.config( local_ip );
85 * configure a static local IP address
86 * DHCP will be used as long as local IP address is not defined
88 inline void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
94 WiFi.config( local_ip, IPAddress(0, 0, 0, 0), gateway, subnet );
96 WiFi.config( local_ip, gateway, subnet );
101 * @return local IP address
103 inline IPAddress getLocalIP()
105 return WiFi.localIP();
108 /******************************************************************************
110 ******************************************************************************/
113 * maintain WiFi and TCP connection
114 * @return true if WiFi and TCP connection are established
116 virtual bool maintain() = 0;
120 * get status of TCP connection
121 * @return status of TCP connection
122 * CLOSED = 0 (typical)
123 * LISTEN = 1 (not used)
126 * ESTABLISHED = 4 (typical)
134 inline uint8_t status()
136 return _client.status();
141 * close TCP client connection
143 virtual void stop() = 0;
145 /******************************************************************************
147 ******************************************************************************/
150 * initialize WiFi without security (open) and initiate client connection
151 * if WiFi connection is already established
152 * @return WL_CONNECTED if WiFi connection is established
154 inline int begin(char *ssid)
159 int result = WiFi.status();
160 return WiFi.status();
165 * initialize WiFi with WEP security and initiate client connection
166 * if WiFi connection is already established
167 * @return WL_CONNECTED if WiFi connection is established
169 inline int begin(char *ssid, uint8_t key_idx, const char *key)
175 WiFi.begin( ssid, key_idx, key );
176 return WiFi.status();
181 * initialize WiFi with WPA-PSK security and initiate client connection
182 * if WiFi connection is already established
183 * @return WL_CONNECTED if WiFi connection is established
185 inline int begin(char *ssid, const char *passphrase)
188 _passphrase = passphrase;
190 WiFi.begin(ssid, passphrase);
191 return WiFi.status();
195 /******************************************************************************
197 ******************************************************************************/
199 inline int available()
201 return connect_client() ? _client.available() : 0;
206 if( _client ) _client.flush();
211 return connect_client() ? _client.peek(): 0;
216 return connect_client() ? _client.read() : -1;
219 inline size_t write(uint8_t byte)
221 return connect_client() ? _client.write( byte ) : 0;
226 #endif //WIFI_STREAM_H