4 An Arduino Stream that wraps an instance of a WiFiClient. For use
5 with legacy Arduino WiFi shield and other boards and shields that
6 are compatible with the Arduino WiFi library.
8 Copyright (C) 2016 Jens B. All rights reserved.
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
15 See file LICENSE.txt for further informations on licensing terms.
17 Parts of this class are based on
19 - EthernetClientStream - Copyright (C) 2013 Norbert Truchsess. All rights reserved.
21 published under the same license.
23 Last updated April 23rd, 2016
26 #ifndef WIFI_CLIENT_STREAM_H
27 #define WIFI_CLIENT_STREAM_H
29 #include "WiFiStream.h"
31 #define MILLIS_RECONNECT 5000
33 class WiFiClientStream : public WiFiStream
36 uint32_t _time_connect = 0;
39 * check if TCP client is connected
40 * @return true if connected
42 virtual inline bool connect_client()
46 if ( _client && _client.connected() ) return true;
51 if ( WiFi.status() == WL_CONNECTED )
53 // if the client is disconnected, try to reconnect every 5 seconds
54 if ( millis() - _time_connect >= MILLIS_RECONNECT )
56 _connected = _client.connect( _remote_ip, _port );
59 _time_connect = millis();
61 else if ( _currentHostConnectionCallback )
63 (*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED);
73 * create a WiFi stream with a TCP client
75 WiFiClientStream(IPAddress server_ip, uint16_t server_port) : WiFiStream(server_ip, server_port) {}
78 * maintain WiFi and TCP connection
79 * @return true if WiFi and TCP connection are established
81 virtual inline bool maintain()
83 return connect_client();
87 * stop client connection
89 virtual inline void stop()
94 if ( _currentHostConnectionCallback )
96 (*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED);
100 _time_connect = millis();
105 #endif //WIFI_CLIENT_STREAM_H