1 | /*
|
---|
2 | WiFiStream.h
|
---|
3 | An Arduino Stream that wraps an instance of a WiFi server. For use
|
---|
4 | with legacy Arduino WiFi shield and other boards and sheilds that
|
---|
5 | are compatible with the Arduino WiFi library.
|
---|
6 |
|
---|
7 | Copyright (C) 2015-2016 Jesse Frush. All rights reserved.
|
---|
8 |
|
---|
9 | This library is free software; you can redistribute it and/or
|
---|
10 | modify it under the terms of the GNU Lesser General Public
|
---|
11 | License as published by the Free Software Foundation; either
|
---|
12 | version 2.1 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | See file LICENSE.txt for further informations on licensing terms.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #ifndef WIFI_STREAM_H
|
---|
18 | #define WIFI_STREAM_H
|
---|
19 |
|
---|
20 | #include <inttypes.h>
|
---|
21 | #include <Stream.h>
|
---|
22 | #include <WiFi.h>
|
---|
23 |
|
---|
24 | class WiFiStream : public Stream
|
---|
25 | {
|
---|
26 | private:
|
---|
27 | WiFiServer _server = WiFiServer(23);
|
---|
28 | WiFiClient _client;
|
---|
29 |
|
---|
30 | //configuration members
|
---|
31 | IPAddress _local_ip;
|
---|
32 | uint16_t _port = 0;
|
---|
33 | uint8_t _key_idx = 0; //WEP
|
---|
34 | const char *_key = nullptr; //WEP
|
---|
35 | const char *_passphrase = nullptr; //WPA
|
---|
36 | char *_ssid = nullptr;
|
---|
37 |
|
---|
38 | inline int connect_client()
|
---|
39 | {
|
---|
40 | if( !( _client && _client.connected() ) )
|
---|
41 | {
|
---|
42 | WiFiClient newClient = _server.available();
|
---|
43 | if( !newClient )
|
---|
44 | {
|
---|
45 | return 0;
|
---|
46 | }
|
---|
47 |
|
---|
48 | _client = newClient;
|
---|
49 | }
|
---|
50 | return 1;
|
---|
51 | }
|
---|
52 |
|
---|
53 | inline bool is_ready()
|
---|
54 | {
|
---|
55 | uint8_t status = WiFi.status();
|
---|
56 | return !( status == WL_NO_SHIELD || status == WL_CONNECTED );
|
---|
57 | }
|
---|
58 |
|
---|
59 | public:
|
---|
60 | WiFiStream() {};
|
---|
61 |
|
---|
62 | // allows another way to configure a static IP before begin is called
|
---|
63 | inline void config(IPAddress local_ip)
|
---|
64 | {
|
---|
65 | _local_ip = local_ip;
|
---|
66 | WiFi.config( local_ip );
|
---|
67 | }
|
---|
68 |
|
---|
69 | // get DCHP IP
|
---|
70 | inline IPAddress localIP()
|
---|
71 | {
|
---|
72 | return WiFi.localIP();
|
---|
73 | }
|
---|
74 |
|
---|
75 | inline bool maintain()
|
---|
76 | {
|
---|
77 | if( connect_client() ) return true;
|
---|
78 |
|
---|
79 | stop();
|
---|
80 | int result = 0;
|
---|
81 | if( WiFi.status() != WL_CONNECTED )
|
---|
82 | {
|
---|
83 | if( _local_ip )
|
---|
84 | {
|
---|
85 | WiFi.config( _local_ip );
|
---|
86 | }
|
---|
87 |
|
---|
88 | if( _passphrase )
|
---|
89 | {
|
---|
90 | result = WiFi.begin( _ssid, _passphrase);
|
---|
91 | }
|
---|
92 | else if( _key_idx && _key )
|
---|
93 | {
|
---|
94 | result = WiFi.begin( _ssid, _key_idx, _key );
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | result = WiFi.begin( _ssid );
|
---|
99 | }
|
---|
100 | }
|
---|
101 | if( result == 0 ) return false;
|
---|
102 |
|
---|
103 | _server = WiFiServer( _port );
|
---|
104 | _server.begin();
|
---|
105 | return result;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /******************************************************************************
|
---|
109 | * Connection functions with DHCP
|
---|
110 | ******************************************************************************/
|
---|
111 |
|
---|
112 | //OPEN networks
|
---|
113 | inline int begin(char *ssid, uint16_t port)
|
---|
114 | {
|
---|
115 | if( !is_ready() ) return 0;
|
---|
116 |
|
---|
117 | _ssid = ssid;
|
---|
118 | _port = port;
|
---|
119 | int result = WiFi.begin( ssid );
|
---|
120 | if( result == 0 ) return 0;
|
---|
121 |
|
---|
122 | _server = WiFiServer( port );
|
---|
123 | _server.begin();
|
---|
124 | return result;
|
---|
125 | }
|
---|
126 |
|
---|
127 | //WEP-encrypted networks
|
---|
128 | inline int begin(char *ssid, uint8_t key_idx, const char *key, uint16_t port)
|
---|
129 | {
|
---|
130 | if( !is_ready() ) return 0;
|
---|
131 |
|
---|
132 | _ssid = ssid;
|
---|
133 | _port = port;
|
---|
134 | _key_idx = key_idx;
|
---|
135 | _key = key;
|
---|
136 |
|
---|
137 | int result = WiFi.begin( ssid, key_idx, key );
|
---|
138 | if( result == 0 ) return 0;
|
---|
139 |
|
---|
140 | _server = WiFiServer( port );
|
---|
141 | _server.begin();
|
---|
142 | return result;
|
---|
143 | }
|
---|
144 |
|
---|
145 | //WPA-encrypted networks
|
---|
146 | inline int begin(char *ssid, const char *passphrase, uint16_t port)
|
---|
147 | {
|
---|
148 | if( !is_ready() ) return 0;
|
---|
149 |
|
---|
150 | _ssid = ssid;
|
---|
151 | _port = port;
|
---|
152 | _passphrase = passphrase;
|
---|
153 |
|
---|
154 | int result = WiFi.begin( ssid, passphrase);
|
---|
155 | if( result == 0 ) return 0;
|
---|
156 |
|
---|
157 | _server = WiFiServer( port );
|
---|
158 | _server.begin();
|
---|
159 | return result;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /******************************************************************************
|
---|
163 | * Connection functions without DHCP
|
---|
164 | ******************************************************************************/
|
---|
165 |
|
---|
166 | //OPEN networks with static IP
|
---|
167 | inline int begin(char *ssid, IPAddress local_ip, uint16_t port)
|
---|
168 | {
|
---|
169 | if( !is_ready() ) return 0;
|
---|
170 |
|
---|
171 | _ssid = ssid;
|
---|
172 | _port = port;
|
---|
173 | _local_ip = local_ip;
|
---|
174 |
|
---|
175 | WiFi.config( local_ip );
|
---|
176 | int result = WiFi.begin( ssid );
|
---|
177 | if( result == 0 ) return 0;
|
---|
178 |
|
---|
179 | _server = WiFiServer( port );
|
---|
180 | _server.begin();
|
---|
181 | return result;
|
---|
182 | }
|
---|
183 |
|
---|
184 | //WEP-encrypted networks with static IP
|
---|
185 | inline int begin(char *ssid, IPAddress local_ip, uint8_t key_idx, const char *key, uint16_t port)
|
---|
186 | {
|
---|
187 | if( !is_ready() ) return 0;
|
---|
188 |
|
---|
189 | _ssid = ssid;
|
---|
190 | _port = port;
|
---|
191 | _local_ip = local_ip;
|
---|
192 | _key_idx = key_idx;
|
---|
193 | _key = key;
|
---|
194 |
|
---|
195 | WiFi.config( local_ip );
|
---|
196 | int result = WiFi.begin( ssid, key_idx, key );
|
---|
197 | if( result == 0 ) return 0;
|
---|
198 |
|
---|
199 | _server = WiFiServer( port );
|
---|
200 | _server.begin();
|
---|
201 | return result;
|
---|
202 | }
|
---|
203 |
|
---|
204 | //WPA-encrypted networks with static IP
|
---|
205 | inline int begin(char *ssid, IPAddress local_ip, const char *passphrase, uint16_t port)
|
---|
206 | {
|
---|
207 | if( !is_ready() ) return 0;
|
---|
208 |
|
---|
209 | _ssid = ssid;
|
---|
210 | _port = port;
|
---|
211 | _local_ip = local_ip;
|
---|
212 | _passphrase = passphrase;
|
---|
213 |
|
---|
214 | WiFi.config( local_ip );
|
---|
215 | int result = WiFi.begin( ssid, passphrase);
|
---|
216 | if( result == 0 ) return 0;
|
---|
217 |
|
---|
218 | _server = WiFiServer( port );
|
---|
219 | _server.begin();
|
---|
220 | return result;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /******************************************************************************
|
---|
224 | * Stream implementations
|
---|
225 | ******************************************************************************/
|
---|
226 |
|
---|
227 | inline int available()
|
---|
228 | {
|
---|
229 | return connect_client() ? _client.available() : 0;
|
---|
230 | }
|
---|
231 |
|
---|
232 | inline void flush()
|
---|
233 | {
|
---|
234 | if( _client ) _client.flush();
|
---|
235 | }
|
---|
236 |
|
---|
237 | inline int peek()
|
---|
238 | {
|
---|
239 | return connect_client() ? _client.peek(): 0;
|
---|
240 | }
|
---|
241 |
|
---|
242 | inline int read()
|
---|
243 | {
|
---|
244 | return connect_client() ? _client.read() : -1;
|
---|
245 | }
|
---|
246 |
|
---|
247 | inline void stop()
|
---|
248 | {
|
---|
249 | _client.stop();
|
---|
250 | }
|
---|
251 |
|
---|
252 | inline size_t write(uint8_t byte)
|
---|
253 | {
|
---|
254 | if( connect_client() ) _client.write( byte );
|
---|
255 | }
|
---|
256 | };
|
---|
257 |
|
---|
258 | #endif //WIFI_STREAM_H
|
---|