|
| 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 ESPWIFI_STREAM_H |
| 18 | +#define ESPWIFI_STREAM_H |
| 19 | + |
| 20 | +#include <inttypes.h> |
| 21 | +#include <Stream.h> |
| 22 | +#include <ESP8266WiFi.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 | + return 0; |
| 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 | + return 0; |
| 145 | + } |
| 146 | + |
| 147 | + //WPA-encrypted networks |
| 148 | + inline int begin(char *ssid, const char *passphrase, uint16_t port) |
| 149 | + { |
| 150 | + if( !is_ready() ) return 0; |
| 151 | + |
| 152 | + _ssid = ssid; |
| 153 | + _port = port; |
| 154 | + _passphrase = passphrase; |
| 155 | + |
| 156 | + int result = WiFi.begin( ssid, passphrase); |
| 157 | + if( result == 0 ) return 0; |
| 158 | + |
| 159 | + _server = WiFiServer( port ); |
| 160 | + _server.begin(); |
| 161 | + return result; |
| 162 | + } |
| 163 | + |
| 164 | +/****************************************************************************** |
| 165 | + * Connection functions without DHCP |
| 166 | + ******************************************************************************/ |
| 167 | + |
| 168 | + //OPEN networks with static IP |
| 169 | + inline int begin(char *ssid, IPAddress local_ip, uint16_t port) |
| 170 | + { |
| 171 | + // if( !is_ready() ) return 0; |
| 172 | + |
| 173 | + // _ssid = ssid; |
| 174 | + // _port = port; |
| 175 | + // _local_ip = local_ip; |
| 176 | + |
| 177 | + // WiFi.config( local_ip ); |
| 178 | + // int result = WiFi.begin( ssid ); |
| 179 | + // if( result == 0 ) return 0; |
| 180 | + |
| 181 | + // _server = WiFiServer( port ); |
| 182 | + // _server.begin(); |
| 183 | + // return result; |
| 184 | + return 0; |
| 185 | + } |
| 186 | + |
| 187 | + //WEP-encrypted networks with static IP |
| 188 | + inline int begin(char *ssid, IPAddress local_ip, uint8_t key_idx, const char *key, uint16_t port) |
| 189 | + { |
| 190 | + // if( !is_ready() ) return 0; |
| 191 | + |
| 192 | + // _ssid = ssid; |
| 193 | + // _port = port; |
| 194 | + // _local_ip = local_ip; |
| 195 | + // _key_idx = key_idx; |
| 196 | + // _key = key; |
| 197 | + |
| 198 | + // WiFi.config( local_ip ); |
| 199 | + // int result = WiFi.begin( ssid, key_idx, key ); |
| 200 | + // if( result == 0 ) return 0; |
| 201 | + |
| 202 | + // _server = WiFiServer( port ); |
| 203 | + // _server.begin(); |
| 204 | + // return result; |
| 205 | + return 0; |
| 206 | + } |
| 207 | + |
| 208 | + //WPA-encrypted networks with static IP |
| 209 | + inline int begin(char *ssid, IPAddress local_ip, const char *passphrase, uint16_t port) |
| 210 | + { |
| 211 | + // if( !is_ready() ) return 0; |
| 212 | + |
| 213 | + // _ssid = ssid; |
| 214 | + // _port = port; |
| 215 | + // _local_ip = local_ip; |
| 216 | + // _passphrase = passphrase; |
| 217 | + |
| 218 | + // WiFi.config( local_ip ); |
| 219 | + // int result = WiFi.begin( ssid, passphrase); |
| 220 | + // if( result == 0 ) return 0; |
| 221 | + |
| 222 | + // _server = WiFiServer( port ); |
| 223 | + // _server.begin(); |
| 224 | + // return result; |
| 225 | + return 0; |
| 226 | + } |
| 227 | + |
| 228 | +/****************************************************************************** |
| 229 | + * Stream implementations |
| 230 | + ******************************************************************************/ |
| 231 | + |
| 232 | + inline int available() |
| 233 | + { |
| 234 | + return connect_client() ? _client.available() : 0; |
| 235 | + } |
| 236 | + |
| 237 | + inline void flush() |
| 238 | + { |
| 239 | + if( _client ) _client.flush(); |
| 240 | + } |
| 241 | + |
| 242 | + inline int peek() |
| 243 | + { |
| 244 | + return connect_client() ? _client.peek(): 0; |
| 245 | + } |
| 246 | + |
| 247 | + inline int read() |
| 248 | + { |
| 249 | + return connect_client() ? _client.read() : -1; |
| 250 | + } |
| 251 | + |
| 252 | + inline void stop() |
| 253 | + { |
| 254 | + _client.stop(); |
| 255 | + } |
| 256 | + |
| 257 | + inline size_t write(uint8_t byte) |
| 258 | + { |
| 259 | + if( connect_client() ) _client.write( byte ); |
| 260 | + } |
| 261 | +}; |
| 262 | + |
| 263 | +#endif //ESPWIFI_STREAM_H |
0 commit comments