Skip to content

Commit 21fd184

Browse files
Merge pull request #289 from jnsbyr/wifi-client
WiFiClientStream added
2 parents 86a8273 + 57967d3 commit 21fd184

File tree

5 files changed

+355
-254
lines changed

5 files changed

+355
-254
lines changed

examples/StandardFirmataWiFi/StandardFirmataWiFi.ino

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
// the minimum interval for sampling analog input
111111
#define MINIMUM_SAMPLING_INTERVAL 1
112112

113-
#define WIFI_MAX_CONN_ATTEMPTS 3
113+
#define MAX_CONN_ATTEMPTS 20 // [500 ms] -> 10 s
114114

115115
/*==============================================================================
116116
* GLOBAL VARIABLES
@@ -130,8 +130,8 @@ IPAddress subnet(SUBNET_MASK);
130130
IPAddress gateway(GATEWAY_IP_ADDRESS);
131131
#endif
132132

133-
int wifiConnectionAttemptCounter = 0;
134-
int wifiStatus = WL_IDLE_STATUS;
133+
int connectionAttempts = 0;
134+
bool streamConnected = false;
135135

136136
/* analog inputs */
137137
int analogInputsToReport = 0; // bitwise array to store pin reporting
@@ -308,6 +308,12 @@ void checkDigitalInputs(void)
308308
if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false);
309309
}
310310

311+
// -----------------------------------------------------------------------------
312+
// function forward declarations
313+
void enableI2CPins();
314+
void disableI2CPins();
315+
void reportAnalogCallback(byte analogPin, int value);
316+
311317
// -----------------------------------------------------------------------------
312318
/* sets the pin mode to the correct state and sets the relevant bits in the
313319
* two bit-arrays that track Digital I/O and PWM status
@@ -826,38 +832,26 @@ void systemResetCallback()
826832
}
827833

828834
void printWifiStatus() {
829-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
830835
if ( WiFi.status() != WL_CONNECTED )
831836
{
832837
DEBUG_PRINT( "WiFi connection failed. Status value: " );
833838
DEBUG_PRINTLN( WiFi.status() );
834839
}
835840
else
836-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
837841
{
838842
// print the SSID of the network you're attached to:
839843
DEBUG_PRINT( "SSID: " );
840-
841-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
842844
DEBUG_PRINTLN( WiFi.SSID() );
843-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
844845

845846
// print your WiFi shield's IP address:
846847
DEBUG_PRINT( "IP Address: " );
847-
848-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
849848
IPAddress ip = WiFi.localIP();
850849
DEBUG_PRINTLN( ip );
851-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
852850

853851
// print the received signal strength:
854852
DEBUG_PRINT( "signal strength (RSSI): " );
855-
856-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
857853
long rssi = WiFi.RSSI();
858854
DEBUG_PRINT( rssi );
859-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
860-
861855
DEBUG_PRINTLN( " dBm" );
862856
}
863857
}
@@ -890,7 +884,7 @@ void setup()
890884
#ifdef STATIC_IP_ADDRESS
891885
DEBUG_PRINT( "Using static IP: " );
892886
DEBUG_PRINTLN( local_ip );
893-
#ifdef ESP8266_WIFI
887+
#if defined(ESP8266_WIFI) || (defined(SUBNET_MASK) && defined(GATEWAY_IP_ADDRESS))
894888
stream.config( local_ip , gateway, subnet );
895889
#else
896890
// you can also provide a static IP in the begin() functions, but this simplifies
@@ -902,37 +896,36 @@ void setup()
902896
#endif
903897

904898
/*
905-
* Configure WiFi security
899+
* Configure WiFi security and initiate WiFi connection
906900
*/
907901
#if defined(WIFI_WEP_SECURITY)
908-
while (wifiStatus != WL_CONNECTED) {
909902
DEBUG_PRINT( "Attempting to connect to WEP SSID: " );
910903
DEBUG_PRINTLN(ssid);
911-
wifiStatus = stream.begin( ssid, wep_index, wep_key, SERVER_PORT );
912-
delay(5000); // TODO - determine minimum delay
913-
if (++wifiConnectionAttemptCounter > WIFI_MAX_CONN_ATTEMPTS) break;
914-
}
915-
904+
stream.begin(ssid, wep_index, wep_key);
916905
#elif defined(WIFI_WPA_SECURITY)
917-
while (wifiStatus != WL_CONNECTED) {
918906
DEBUG_PRINT( "Attempting to connect to WPA SSID: " );
919907
DEBUG_PRINTLN(ssid);
920-
wifiStatus = stream.begin(ssid, wpa_passphrase, SERVER_PORT);
921-
delay(5000); // TODO - determine minimum delay
922-
if (++wifiConnectionAttemptCounter > WIFI_MAX_CONN_ATTEMPTS) break;
923-
}
924-
908+
stream.begin(ssid, wpa_passphrase);
925909
#else //OPEN network
926-
while (wifiStatus != WL_CONNECTED) {
927910
DEBUG_PRINTLN( "Attempting to connect to open SSID: " );
928911
DEBUG_PRINTLN(ssid);
929-
wifiStatus = stream.begin(ssid, SERVER_PORT);
930-
delay(5000); // TODO - determine minimum delay
931-
if (++wifiConnectionAttemptCounter > WIFI_MAX_CONN_ATTEMPTS) break;
932-
}
912+
stream.begin(ssid);
933913
#endif //defined(WIFI_WEP_SECURITY)
934-
935914
DEBUG_PRINTLN( "WiFi setup done" );
915+
916+
/*
917+
* Wait for TCP connection to be established
918+
*/
919+
while (!streamConnected && ++connectionAttempts <= MAX_CONN_ATTEMPTS) {
920+
delay(500);
921+
DEBUG_PRINT(".");
922+
streamConnected = stream.maintain();
923+
}
924+
if (streamConnected) {
925+
DEBUG_PRINTLN( "TCP connection established" );
926+
} else {
927+
DEBUG_PRINTLN( "failed to establish TCP connection" );
928+
}
936929
printWifiStatus();
937930

938931
/*

examples/StandardFirmataWiFi/wifiConfig.h

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
*/
3131
//#define WIFI_101
3232

33-
//do not modify the following 10 lines
33+
//do not modify the following 11 lines
3434
#if defined(ARDUINO_SAMD_MKR1000) && !defined(WIFI_101)
3535
// automatically include if compiling for MRK1000
3636
#define WIFI_101
3737
#endif
3838
#ifdef WIFI_101
3939
#include <WiFi101.h>
40-
#include "utility/WiFiStream.h"
41-
WiFiStream stream;
42-
#define WIFI_LIB_INCLUDED
40+
#include "utility/WiFiClientStream.h"
41+
#include "utility/WiFiServerStream.h"
42+
#define WIFI_LIB_INCLUDED
4343
#endif
4444

4545
/*
@@ -57,8 +57,8 @@ WiFiStream stream;
5757
//do not modify the following 10 lines
5858
#ifdef ARDUINO_WIFI_SHIELD
5959
#include <WiFi.h>
60-
#include "utility/WiFiStream.h"
61-
WiFiStream stream;
60+
#include "utility/WiFiClientStream.h"
61+
#include "utility/WiFiServerStream.h"
6262
#ifdef WIFI_LIB_INCLUDED
6363
#define MULTIPLE_WIFI_LIB_INCLUDES
6464
#else
@@ -85,8 +85,8 @@ WiFiStream stream;
8585
#endif
8686
#ifdef ESP8266_WIFI
8787
#include <ESP8266WiFi.h>
88-
#include "utility/WiFiStream.h"
89-
WiFiStream stream;
88+
#include "utility/WiFiClientStream.h"
89+
#include "utility/WiFiServerStream.h"
9090
#ifdef WIFI_LIB_INCLUDED
9191
#define MULTIPLE_WIFI_LIB_INCLUDES
9292
#else
@@ -122,12 +122,17 @@ char ssid[] = "your_network_name";
122122
//#define GATEWAY_IP_ADDRESS 0,0,0,0 // REQUIRED for ESP8266_WIFI, optional for others
123123

124124

125-
// STEP 4 [REQUIRED for all boards and shields]
125+
// STEP 4 [OPTIONAL for all boards and shields]
126+
// uncomment and replace with the IP address of your server if the Arduino is the TCP client
127+
//#define SERVER_IP 10, 0, 0, 15
128+
129+
130+
// STEP 5 [REQUIRED for all boards and shields]
126131
// define your port number here, you will need this to open a TCP connection to your Arduino
127132
#define SERVER_PORT 3030
128133

129134

130-
// STEP 5 [REQUIRED for all boards and shields]
135+
// STEP 6 [REQUIRED for all boards and shields]
131136
// determine your network security type (OPTION A, B, or C). Option A is the most common, and the
132137
// default.
133138

@@ -200,6 +205,16 @@ char wep_key[] = "your_wep_key";
200205
#error "you must choose between WIFI_NO_SECURITY and WIFI_WPA_SECURITY"
201206
#endif
202207

208+
/*==============================================================================
209+
* WIFI STREAM (don't change anything here)
210+
*============================================================================*/
211+
212+
#ifdef SERVER_IP
213+
WiFiClientStream stream(IPAddress(SERVER_IP), SERVER_PORT);
214+
#else
215+
WiFiServerStream stream(SERVER_PORT);
216+
#endif
217+
203218
/*==============================================================================
204219
* PIN IGNORE MACROS (don't change anything here)
205220
*============================================================================*/

utility/WiFiClientStream.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
WiFiClientStream.h
3+
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.
7+
8+
Copyright (C) 2016 Jens B. All rights reserved.
9+
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.
14+
15+
See file LICENSE.txt for further informations on licensing terms.
16+
17+
Parts of this class are based on
18+
19+
- EthernetClientStream - Copyright (C) 2013 Norbert Truchsess. All rights reserved.
20+
21+
published under the same license.
22+
23+
Last updated April 17th, 2016
24+
*/
25+
26+
#ifndef WIFI_CLIENT_STREAM_H
27+
#define WIFI_CLIENT_STREAM_H
28+
29+
#include "WiFiStream.h"
30+
31+
#define MILLIS_RECONNECT 5000
32+
33+
class WiFiClientStream : public WiFiStream
34+
{
35+
protected:
36+
uint32_t _time_connect = 0;
37+
38+
/**
39+
* check if TCP client is connected
40+
* @return true if connected
41+
*/
42+
virtual inline bool connect_client()
43+
{
44+
if( _client && _client.connected() ) return true;
45+
46+
if( _connected )
47+
{
48+
stop();
49+
}
50+
51+
// active TCP connect
52+
if( WiFi.status() == WL_CONNECTED )
53+
{
54+
// if the client is disconnected, try to reconnect every 5 seconds
55+
if( millis() - _time_connect >= MILLIS_RECONNECT )
56+
{
57+
_connected = _client.connect( _remote_ip, _port );
58+
if( !_connected )
59+
{
60+
_time_connect = millis();
61+
}
62+
else if ( _currentHostConnectionCallback )
63+
{
64+
(*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED);
65+
}
66+
}
67+
}
68+
69+
return _connected;
70+
}
71+
72+
public:
73+
/**
74+
* create a WiFi stream with a TCP client
75+
*/
76+
WiFiClientStream(IPAddress server_ip, uint16_t server_port) : WiFiStream(server_ip, server_port) {}
77+
78+
/**
79+
* maintain WiFi and TCP connection
80+
* @return true if WiFi and TCP connection are established
81+
*/
82+
virtual inline bool maintain()
83+
{
84+
return connect_client();
85+
}
86+
87+
/**
88+
* stop client connection
89+
*/
90+
virtual inline void stop()
91+
{
92+
if( _client)
93+
{
94+
_client.stop();
95+
if ( _currentHostConnectionCallback )
96+
{
97+
(*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED);
98+
}
99+
}
100+
_connected = false;
101+
_time_connect = millis();
102+
}
103+
104+
};
105+
106+
#endif //WIFI_CLIENT_STREAM_H

0 commit comments

Comments
 (0)