Skip to content

Commit 9586def

Browse files
bare minimum esp support
1 parent b0ea648 commit 9586def

File tree

5 files changed

+306
-2
lines changed

5 files changed

+306
-2
lines changed

Boards.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,24 @@ writePort(port, value, bitmask): Write an 8 bit port.
665665
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
666666
#define PIN_TO_SERVO(p) ((p) - 2)
667667

668+
// ESP8266 generic
669+
#elif defined(ESP8266)
670+
#define TOTAL_ANALOG_PINS 0
671+
#define TOTAL_PINS 17
672+
#define VERSION_BLINK_PIN 4
673+
// #define IS_PIN_DIGITAL(p) ((p) == 0 || (p) == 1 || (p) == 2 || (p) == 3 || (p) == 4 || (p) == 5 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15 || (p) == 16) //for wifi dont protect serial pins because these things only have 2 pins otherwise
674+
#define IS_PIN_DIGITAL(p) ((p) == 0 || (p) == 2 || (p) == 4 || (p) == 5 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15 || (p) == 16)
675+
#define IS_PIN_ANALOG(p) (false)
676+
#define IS_PIN_PWM(p) (false)
677+
#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
678+
#define IS_PIN_I2C(p) (false)
679+
#define IS_PIN_SPI(p) (false)
680+
#define PIN_TO_DIGITAL(p) (p)
681+
#define PIN_TO_ANALOG(p) ((p) - 17)
682+
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
683+
#define PIN_TO_SERVO(p) p
684+
685+
668686
// anything else
669687
#else
670688
#error "Please edit Boards.h with a hardware abstraction for this board"

examples/StandardFirmataWiFi/StandardFirmataWiFi.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
#include <Wire.h>
7575
#include <Firmata.h>
7676

77+
// I dont understand either
78+
void disableI2CPins();
79+
void enableI2CPins();
80+
void reportAnalogCallback(byte analogPin, int value);
81+
7782
/*
7883
* Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your
7984
* connection that may help in the event of connection issues. If defined, some boards may not begin

examples/StandardFirmataWiFi/wifiConfig.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ WiFi101Stream stream;
6161
//------------------------------
6262
//#define HUZZAH_WIFI
6363

64+
/*
65+
* OPTION D: Configure for ESP6288
66+
*
67+
* ESP6288 is supported through its Arduino Core at
68+
* https://github.com/esp8266/Arduino/
69+
*/
70+
71+
//#define ESP_WIFI
72+
73+
//do not modify these next 4 lines
74+
#ifdef ESP_WIFI
75+
#include "utility/ESPWiFiStream.h"
76+
WiFiStream stream;
77+
#endif
6478

6579
// STEP 2 [REQUIRED for all boards and shields]
6680
// replace this with your wireless network SSID
@@ -126,11 +140,11 @@ char wep_key[] = "your_wep_key";
126140
* CONFIGURATION ERROR CHECK (don't change anything here)
127141
*============================================================================*/
128142

129-
#if ((defined(ARDUINO_WIFI_SHIELD) && (defined(WIFI_101) || defined(HUZZAH_WIFI))) || (defined(WIFI_101) && defined(HUZZAH_WIFI)))
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) ))
130144
#error "you may not define more than one wifi device type in wifiConfig.h."
131145
#endif //WIFI device type check
132146

133-
#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI))
147+
#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI) || defined(ESP_WIFI))
134148
#error "you must define a wifi device type in wifiConfig.h."
135149
#endif
136150

utility/ESPWiFiStream.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
* Implementation is in WiFiStream.h to avoid linker issues. Legacy WiFi and modern WiFi101 both define WiFiClass which
3+
* will cause linker errors whenever Firmata.h is included.
4+
*/

utility/ESPWiFiStream.h

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
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

Comments
 (0)