Skip to content

Commit 4799381

Browse files
committed
WiFiStream variant integration
1 parent 625cff9 commit 4799381

File tree

3 files changed

+94
-32
lines changed

3 files changed

+94
-32
lines changed

examples/StandardFirmataWiFi/StandardFirmataWiFi.ino

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ SerialFirmata serialFeature;
125125
#ifdef STATIC_IP_ADDRESS
126126
IPAddress local_ip(STATIC_IP_ADDRESS);
127127
#endif
128+
#ifdef SUBNET_MASK
129+
IPAddress subnet(SUBNET_MASK);
130+
#else
131+
IPAddress subnet(0,0,0,0);
132+
#endif
133+
#ifdef GATEWAY_IP_ADDRESS
134+
IPAddress gateway(GATEWAY_IP_ADDRESS);
135+
#else
136+
IPAddress gateway(0,0,0,0);
137+
#endif
128138

129139
int wifiConnectionAttemptCounter = 0;
130140
int wifiStatus = WL_IDLE_STATUS;
@@ -822,37 +832,37 @@ void systemResetCallback()
822832
}
823833

824834
void printWifiStatus() {
825-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
835+
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
826836
if ( WiFi.status() != WL_CONNECTED )
827837
{
828838
DEBUG_PRINT( "WiFi connection failed. Status value: " );
829839
DEBUG_PRINTLN( WiFi.status() );
830840
}
831841
else
832-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
842+
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
833843
{
834844
// print the SSID of the network you're attached to:
835845
DEBUG_PRINT( "SSID: " );
836846

837-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
847+
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
838848
DEBUG_PRINTLN( WiFi.SSID() );
839-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
849+
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
840850

841851
// print your WiFi shield's IP address:
842852
DEBUG_PRINT( "IP Address: " );
843853

844-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
854+
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
845855
IPAddress ip = WiFi.localIP();
846856
DEBUG_PRINTLN( ip );
847-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
857+
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
848858

849859
// print the received signal strength:
850860
DEBUG_PRINT( "signal strength (RSSI): " );
851861

852-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
862+
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
853863
long rssi = WiFi.RSSI();
854864
DEBUG_PRINT( rssi );
855-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
865+
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
856866

857867
DEBUG_PRINTLN( " dBm" );
858868
}
@@ -873,6 +883,8 @@ void setup()
873883
DEBUG_PRINTLN( "using the WiFi 101 library." );
874884
#elif defined(ARDUINO_WIFI_SHIELD)
875885
DEBUG_PRINTLN( "using the legacy WiFi library." );
886+
#elif defined(ESP8266_WIFI)
887+
DEBUG_PRINTLN( "using the ESP8266 WiFi library." );
876888
#elif defined(HUZZAH_WIFI)
877889
DEBUG_PRINTLN( "using the HUZZAH WiFi library." );
878890
//else should never happen here as error-checking in wifiConfig.h will catch this
@@ -886,7 +898,11 @@ void setup()
886898
DEBUG_PRINTLN( local_ip );
887899
//you can also provide a static IP in the begin() functions, but this simplifies
888900
//ifdef logic in this sketch due to support for all different encryption types.
901+
#ifdef ESP8266_WIFI
902+
stream.config( local_ip , gateway, subnet );
903+
#else
889904
stream.config( local_ip );
905+
#endif
890906
#else
891907
DEBUG_PRINTLN( "IP will be requested from DHCP ..." );
892908
#endif

examples/StandardFirmataWiFi/wifiConfig.h

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
*/
2424
#define ARDUINO_WIFI_SHIELD
2525

26-
//do not modify these next 4 lines
26+
//do not modify these next 5 lines
2727
#ifdef ARDUINO_WIFI_SHIELD
28+
#include <WiFi.h>
2829
#include "utility/WiFiStream.h"
2930
WiFiStream stream;
3031
#endif
@@ -44,46 +45,57 @@ WiFiStream stream;
4445
*/
4546
//#define WIFI_101
4647

47-
//do not modify these next 4 lines
48+
//do not modify these next 5 lines
4849
#ifdef WIFI_101
50+
#include <WiFi101.h>
4951
#include "utility/WiFi101Stream.h"
50-
WiFi101Stream stream;
52+
WiFiStream stream;
5153
#endif
5254

5355
/*
54-
* OPTION C: Configure for HUZZAH
56+
* OPTION C: Configure for ESP8266
57+
*
58+
* This will configure StandardFirmataWiFi to use the ESP8266WiFi library for boards
59+
* with an ESP8266 chip. It is compatible with 802.11 B/G/N networks.
60+
*
61+
* To enable, uncomment the #define ESP8266_WIFI below and verify the #define values under
62+
* options A and B are commented out.
5563
*
56-
* HUZZAH is not yet supported, this will be added in a later revision to StandardFirmataWiFi
64+
* IMPORTANT: You must have the esp8266 board support installed. To easily install this board, open the board manager via:
65+
* Arduino IDE Menus: Tools > Board > Manage Boards > filter search for "esp8266" > Select the result and click 'install'
5766
*/
67+
//#define ESP8266_WIFI
5868

59-
//------------------------------
60-
// TODO
61-
//------------------------------
62-
//#define HUZZAH_WIFI
69+
//do not modify these next 5 lines
70+
#ifdef ESP8266_WIFI
71+
#include <ESP8266WiFi.h>
72+
#include "utility/WiFiStream.h"
73+
WiFiStream stream;
74+
#endif
6375

6476
/*
65-
* OPTION D: Configure for ESP6288
77+
* OPTION D: Configure for HUZZAH
6678
*
67-
* ESP6288 is supported through its Arduino Core at
68-
* https://github.com/esp8266/Arduino/
79+
* HUZZAH with CC3000 is not yet supported, this will be added in a later revision to StandardFirmataWiFi.
80+
* For HUZZAH with ESP8266 use ESP8266_WIFI.
6981
*/
7082

71-
//#define ESP_WIFI
83+
//------------------------------
84+
// TODO
85+
//------------------------------
86+
//#define HUZZAH_WIFI
7287

73-
//do not modify these next 4 lines
74-
#ifdef ESP_WIFI
75-
#include "utility/ESPWiFiStream.h"
76-
WiFiStream stream;
77-
#endif
7888

7989
// STEP 2 [REQUIRED for all boards and shields]
8090
// replace this with your wireless network SSID
8191
char ssid[] = "your_network_name";
8292

8393
// STEP 3 [OPTIONAL for all boards and shields]
8494
// if you want to use a static IP (v4) address, uncomment the line below. You can also change the IP.
85-
// if this line is commented out, the WiFi shield will attempt to get an IP from the DHCP server
86-
// #define STATIC_IP_ADDRESS 192,168,1,113
95+
// if the first line is commented out, the WiFi shield will attempt to get an IP from the DHCP server
96+
#define STATIC_IP_ADDRESS 192,168,1,113
97+
#define SUBNET_MASK 255,255,255,0 // REQUIRED for ESP8266_WIFI, ignored for others
98+
#define GATEWAY_IP_ADDRESS 0,0,0,0 // REQUIRED for ESP8266_WIFI, ignored for others
8799

88100
// STEP 4 [REQUIRED for all boards and shields]
89101
// define your port number here, you will need this to open a TCP connection to your Arduino
@@ -107,6 +119,7 @@ char ssid[] = "your_network_name";
107119
char wpa_passphrase[] = "your_wpa_passphrase";
108120
#endif //WIFI_WPA_SECURITY
109121

122+
110123
/*
111124
* OPTION B: WEP
112125
*
@@ -140,11 +153,11 @@ char wep_key[] = "your_wep_key";
140153
* CONFIGURATION ERROR CHECK (don't change anything here)
141154
*============================================================================*/
142155

143-
#if ((defined(ARDUINO_WIFI_SHIELD) && (defined(WIFI_101) || defined(HUZZAH_WIFI))) || (defined(WIFI_101) && defined(HUZZAH_WIFI)) || (defined(WIFI_101) && defined(ESP_WIFI)) || (defined(ESP_WIFI) && defined(HUZZAH_WIFI)) || (defined(ESP_WIFI) && defined(ARDUINO_WIFI_SHIELD)))
156+
#if ((defined(ARDUINO_WIFI_SHIELD) && (defined(WIFI_101) || defined(HUZZAH_WIFI))) || (defined(WIFI_101) && defined(HUZZAH_WIFI)) || (defined(WIFI_101) && defined(ESP8266_WIFI)) || (defined(ESP8266_WIFI) && defined(HUZZAH_WIFI)) || (defined(ESP8266_WIFI) && defined(ARDUINO_WIFI_SHIELD)))
144157
#error "you may not define more than one wifi device type in wifiConfig.h."
145158
#endif //WIFI device type check
146159

147-
#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI) || defined(ESP_WIFI))
160+
#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI) || defined(ESP8266_WIFI))
148161
#error "you must define a wifi device type in wifiConfig.h."
149162
#endif
150163

@@ -156,6 +169,10 @@ char wep_key[] = "your_wep_key";
156169
#error "you must define a wifi security type in wifiConfig.h."
157170
#endif //WIFI_* security define check
158171

172+
#if (defined(ESP8266_WIFI) && !(defined(WIFI_NO_SECURITY) || (defined(WIFI_WPA_SECURITY))))
173+
#error "you must choose between WIFI_NO_SECURITY and WIFI_WPA_SECURITY"
174+
#endif
175+
159176
/*==============================================================================
160177
* PIN IGNORE MACROS (don't change anything here)
161178
*============================================================================*/

utility/WiFiStream.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
WiFiStream.h
33
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
4+
with legacy Arduino WiFi shield and other boards and shields that
55
are compatible with the Arduino WiFi library.
66
77
Copyright (C) 2015-2016 Jesse Frush. All rights reserved.
@@ -19,7 +19,6 @@
1919

2020
#include <inttypes.h>
2121
#include <Stream.h>
22-
#include <WiFi.h>
2322

2423
class WiFiStream : public Stream
2524
{
@@ -29,6 +28,8 @@ class WiFiStream : public Stream
2928

3029
//configuration members
3130
IPAddress _local_ip;
31+
IPAddress _gateway;
32+
IPAddress _subnet;
3233
uint16_t _port = 0;
3334
uint8_t _key_idx = 0; //WEP
3435
const char *_key = nullptr; //WEP
@@ -59,19 +60,37 @@ class WiFiStream : public Stream
5960
public:
6061
WiFiStream() {};
6162

63+
#ifndef ESP8266
6264
// allows another way to configure a static IP before begin is called
6365
inline void config(IPAddress local_ip)
6466
{
6567
_local_ip = local_ip;
6668
WiFi.config( local_ip );
6769
}
70+
#endif
71+
72+
// allows another way to configure a static IP before begin is called
73+
inline void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
74+
{
75+
_local_ip = local_ip;
76+
_gateway = gateway;
77+
_subnet = subnet;
78+
#ifdef ESP8266
79+
WiFi.config( local_ip, gateway, subnet );
80+
#else
81+
WiFi.config( local_ip, IPAddress(0, 0, 0, 0), gateway, subnet );
82+
#endif
83+
}
6884

6985
// get DCHP IP
7086
inline IPAddress localIP()
7187
{
7288
return WiFi.localIP();
7389
}
7490

91+
/**
92+
* @return true if connected
93+
*/
7594
inline bool maintain()
7695
{
7796
if( connect_client() ) return true;
@@ -82,17 +101,23 @@ class WiFiStream : public Stream
82101
{
83102
if( _local_ip )
84103
{
104+
#ifdef ESP8266
105+
WiFi.config( _local_ip, _gateway, _subnet );
106+
#else
85107
WiFi.config( _local_ip );
108+
#endif
86109
}
87110

88111
if( _passphrase )
89112
{
90113
result = WiFi.begin( _ssid, _passphrase);
91114
}
115+
#ifndef ESP8266
92116
else if( _key_idx && _key )
93117
{
94118
result = WiFi.begin( _ssid, _key_idx, _key );
95119
}
120+
#endif
96121
else
97122
{
98123
result = WiFi.begin( _ssid );
@@ -124,6 +149,7 @@ class WiFiStream : public Stream
124149
return result;
125150
}
126151

152+
#ifndef ESP8266
127153
//WEP-encrypted networks
128154
inline int begin(char *ssid, uint8_t key_idx, const char *key, uint16_t port)
129155
{
@@ -141,6 +167,7 @@ class WiFiStream : public Stream
141167
_server.begin();
142168
return result;
143169
}
170+
#endif
144171

145172
//WPA-encrypted networks
146173
inline int begin(char *ssid, const char *passphrase, uint16_t port)
@@ -163,6 +190,7 @@ class WiFiStream : public Stream
163190
* Connection functions without DHCP
164191
******************************************************************************/
165192

193+
#ifndef ESP8266
166194
//OPEN networks with static IP
167195
inline int begin(char *ssid, IPAddress local_ip, uint16_t port)
168196
{
@@ -219,6 +247,7 @@ class WiFiStream : public Stream
219247
_server.begin();
220248
return result;
221249
}
250+
#endif
222251

223252
/******************************************************************************
224253
* Stream implementations

0 commit comments

Comments
 (0)