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