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);
130130IPAddress 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 */
137137int 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
828834void 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 /*
0 commit comments