Skip to content

Commit 86a8273

Browse files
Merge pull request #286 from firmata/esp
ESP8266 support
2 parents e5582d0 + 51e3c7d commit 86a8273

File tree

12 files changed

+223
-321
lines changed

12 files changed

+223
-321
lines changed

Boards.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,28 @@ writePort(port, value, bitmask): Write an 8 bit port.
681681
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
682682
#define PIN_TO_SERVO(p) ((p) - 2)
683683

684+
// ESP8266
685+
// note: boot mode GPIOs 0, 2 and 15 can be used as outputs, GPIOs 6-11 are in use for flash IO
686+
#elif defined(ESP8266)
687+
#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
688+
#define TOTAL_PINS A0 + NUM_ANALOG_INPUTS
689+
#define PIN_SERIAL_RX 3
690+
#define PIN_SERIAL_TX 1
691+
#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) < A0))
692+
#define IS_PIN_ANALOG(p) ((p) >= A0 && (p) < A0 + NUM_ANALOG_INPUTS)
693+
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
694+
#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS)
695+
#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
696+
#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
697+
#define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT)
698+
#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL_RX || (p) == PIN_SERIAL_TX)
699+
#define PIN_TO_DIGITAL(p) (p)
700+
#define PIN_TO_ANALOG(p) ((p) - A0)
701+
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
702+
#define PIN_TO_SERVO(p) (p)
703+
#define DEFAULT_PWM_RESOLUTION 10
704+
705+
684706
// anything else
685707
#else
686708
#error "Please edit Boards.h with a hardware abstraction for this board"
@@ -695,6 +717,9 @@ writePort(port, value, bitmask): Write an 8 bit port.
695717
#define IS_PIN_SERIAL(p) 0
696718
#endif
697719

720+
#ifndef DEFAULT_PWM_RESOLUTION
721+
#define DEFAULT_PWM_RESOLUTION 8
722+
#endif
698723

699724
/*==============================================================================
700725
* readPort() - Read an 8 bit port

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
615615
}
616616
if (IS_PIN_PWM(pin)) {
617617
Firmata.write(PIN_MODE_PWM);
618-
Firmata.write(8); // 8 = 8-bit resolution
618+
Firmata.write(DEFAULT_PWM_RESOLUTION);
619619
}
620620
if (IS_PIN_DIGITAL(pin)) {
621621
Firmata.write(PIN_MODE_SERVO);

examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
616616
}
617617
if (IS_PIN_PWM(pin)) {
618618
Firmata.write(PIN_MODE_PWM);
619-
Firmata.write(8); // 8 = 8-bit resolution
619+
Firmata.write(DEFAULT_PWM_RESOLUTION);
620620
}
621621
if (IS_PIN_DIGITAL(pin)) {
622622
Firmata.write(PIN_MODE_SERVO);

examples/StandardFirmataEthernet/StandardFirmataEthernet.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
672672
}
673673
if (IS_PIN_PWM(pin)) {
674674
Firmata.write(PIN_MODE_PWM);
675-
Firmata.write(8); // 8 = 8-bit resolution
675+
Firmata.write(DEFAULT_PWM_RESOLUTION);
676676
}
677677
if (IS_PIN_DIGITAL(pin)) {
678678
Firmata.write(PIN_MODE_SERVO);

examples/StandardFirmataEthernetPlus/StandardFirmataEthernetPlus.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
680680
}
681681
if (IS_PIN_PWM(pin)) {
682682
Firmata.write(PIN_MODE_PWM);
683-
Firmata.write(8); // 8 = 8-bit resolution
683+
Firmata.write(DEFAULT_PWM_RESOLUTION);
684684
}
685685
if (IS_PIN_DIGITAL(pin)) {
686686
Firmata.write(PIN_MODE_SERVO);

examples/StandardFirmataPlus/StandardFirmataPlus.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
635635
}
636636
if (IS_PIN_PWM(pin)) {
637637
Firmata.write(PIN_MODE_PWM);
638-
Firmata.write(8); // 8 = 8-bit resolution
638+
Firmata.write(DEFAULT_PWM_RESOLUTION);
639639
}
640640
if (IS_PIN_DIGITAL(pin)) {
641641
Firmata.write(PIN_MODE_SERVO);

examples/StandardFirmataWiFi/StandardFirmataWiFi.ino

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Copyright (C) 2009 Shigeru Kobayashi. All rights reserved.
1414
Copyright (C) 2009-2016 Jeff Hoefs. All rights reserved.
1515
Copyright (C) 2015-2016 Jesse Frush. All rights reserved.
16+
Copyright (C) 2016 Jens B. All rights reserved.
1617
1718
This library is free software; you can redistribute it and/or
1819
modify it under the terms of the GNU Lesser General Public
@@ -21,7 +22,7 @@
2122
2223
See file LICENSE.txt for further informations on licensing terms.
2324
24-
Last updated by Jeff Hoefs: January 10th, 2016
25+
Last updated by Jeff Hoefs: April 10th, 2016
2526
*/
2627

2728
/*
@@ -36,7 +37,7 @@
3637
- Arduino WiFi Shield (or clone)
3738
- Arduino WiFi Shield 101
3839
- Arduino MKR1000 board (built-in WiFi 101)
39-
- Adafruit HUZZAH CC3000 WiFi Shield (support coming soon)
40+
- ESP8266 WiFi board compatible with ESP8266 Arduino core
4041
4142
Follow the instructions in the wifiConfig.h file (wifiConfig.h tab in Arduino IDE) to
4243
configure your particular hardware.
@@ -45,6 +46,8 @@
4546
- WiFi Shield 101 requires version 0.7.0 or higher of the WiFi101 library (available in Arduino
4647
1.6.8 or higher, or update the library via the Arduino Library Manager or clone from source:
4748
https://github.com/arduino-libraries/WiFi101)
49+
- ESP8266 requires the Arduino ESP8266 core which can be obtained here:
50+
https://github.com/esp8266/Arduino
4851
4952
In order to use the WiFi Shield 101 with Firmata you will need a board with at least
5053
35k of Flash memory. This means you cannot use the WiFi Shield 101 with an Arduino Uno
@@ -120,6 +123,12 @@ SerialFirmata serialFeature;
120123
#ifdef STATIC_IP_ADDRESS
121124
IPAddress local_ip(STATIC_IP_ADDRESS);
122125
#endif
126+
#ifdef SUBNET_MASK
127+
IPAddress subnet(SUBNET_MASK);
128+
#endif
129+
#ifdef GATEWAY_IP_ADDRESS
130+
IPAddress gateway(GATEWAY_IP_ADDRESS);
131+
#endif
123132

124133
int wifiConnectionAttemptCounter = 0;
125134
int wifiStatus = WL_IDLE_STATUS;
@@ -687,7 +696,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
687696
}
688697
if (IS_PIN_PWM(pin)) {
689698
Firmata.write(PIN_MODE_PWM);
690-
Firmata.write(8); // 8 = 8-bit resolution
699+
Firmata.write(DEFAULT_PWM_RESOLUTION);
691700
}
692701
if (IS_PIN_DIGITAL(pin)) {
693702
Firmata.write(PIN_MODE_SERVO);
@@ -817,37 +826,37 @@ void systemResetCallback()
817826
}
818827

819828
void printWifiStatus() {
820-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
829+
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
821830
if ( WiFi.status() != WL_CONNECTED )
822831
{
823832
DEBUG_PRINT( "WiFi connection failed. Status value: " );
824833
DEBUG_PRINTLN( WiFi.status() );
825834
}
826835
else
827-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
836+
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
828837
{
829838
// print the SSID of the network you're attached to:
830839
DEBUG_PRINT( "SSID: " );
831840

832-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
841+
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
833842
DEBUG_PRINTLN( WiFi.SSID() );
834-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
843+
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
835844

836845
// print your WiFi shield's IP address:
837846
DEBUG_PRINT( "IP Address: " );
838847

839-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
848+
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
840849
IPAddress ip = WiFi.localIP();
841850
DEBUG_PRINTLN( ip );
842-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
851+
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
843852

844853
// print the received signal strength:
845854
DEBUG_PRINT( "signal strength (RSSI): " );
846855

847-
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
856+
#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
848857
long rssi = WiFi.RSSI();
849858
DEBUG_PRINT( rssi );
850-
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101)
859+
#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI)
851860

852861
DEBUG_PRINTLN( " dBm" );
853862
}
@@ -868,6 +877,8 @@ void setup()
868877
DEBUG_PRINTLN( "using the WiFi 101 library." );
869878
#elif defined(ARDUINO_WIFI_SHIELD)
870879
DEBUG_PRINTLN( "using the legacy WiFi library." );
880+
#elif defined(ESP8266_WIFI)
881+
DEBUG_PRINTLN( "using the ESP8266 WiFi library." );
871882
#elif defined(HUZZAH_WIFI)
872883
DEBUG_PRINTLN( "using the HUZZAH WiFi library." );
873884
//else should never happen here as error-checking in wifiConfig.h will catch this
@@ -879,9 +890,13 @@ void setup()
879890
#ifdef STATIC_IP_ADDRESS
880891
DEBUG_PRINT( "Using static IP: " );
881892
DEBUG_PRINTLN( local_ip );
882-
//you can also provide a static IP in the begin() functions, but this simplifies
883-
//ifdef logic in this sketch due to support for all different encryption types.
893+
#ifdef ESP8266_WIFI
894+
stream.config( local_ip , gateway, subnet );
895+
#else
896+
// you can also provide a static IP in the begin() functions, but this simplifies
897+
// ifdef logic in this sketch due to support for all different encryption types.
884898
stream.config( local_ip );
899+
#endif
885900
#else
886901
DEBUG_PRINTLN( "IP will be requested from DHCP ..." );
887902
#endif

0 commit comments

Comments
 (0)