Skip to content

Commit

Permalink
bare minimum esp support
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobrosenthal committed Feb 18, 2016
1 parent b0ea648 commit 9586def
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 2 deletions.
18 changes: 18 additions & 0 deletions Boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,24 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
#define PIN_TO_SERVO(p) ((p) - 2)

// ESP8266 generic
#elif defined(ESP8266)
#define TOTAL_ANALOG_PINS 0
#define TOTAL_PINS 17
#define VERSION_BLINK_PIN 4
// #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
#define IS_PIN_DIGITAL(p) ((p) == 0 || (p) == 2 || (p) == 4 || (p) == 5 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15 || (p) == 16)
#define IS_PIN_ANALOG(p) (false)
#define IS_PIN_PWM(p) (false)
#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
#define IS_PIN_I2C(p) (false)
#define IS_PIN_SPI(p) (false)
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) ((p) - 17)
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
#define PIN_TO_SERVO(p) p


// anything else
#else
#error "Please edit Boards.h with a hardware abstraction for this board"
Expand Down
5 changes: 5 additions & 0 deletions examples/StandardFirmataWiFi/StandardFirmataWiFi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
#include <Wire.h>
#include <Firmata.h>

// I dont understand either
void disableI2CPins();
void enableI2CPins();
void reportAnalogCallback(byte analogPin, int value);

/*
* Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your
* connection that may help in the event of connection issues. If defined, some boards may not begin
Expand Down
18 changes: 16 additions & 2 deletions examples/StandardFirmataWiFi/wifiConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ WiFi101Stream stream;
//------------------------------
//#define HUZZAH_WIFI

/*
* OPTION D: Configure for ESP6288
*
* ESP6288 is supported through its Arduino Core at
* https://github.com/esp8266/Arduino/
*/

//#define ESP_WIFI

//do not modify these next 4 lines
#ifdef ESP_WIFI
#include "utility/ESPWiFiStream.h"
WiFiStream stream;
#endif

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

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

#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI))
#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI) || defined(ESP_WIFI))
#error "you must define a wifi device type in wifiConfig.h."
#endif

Expand Down
4 changes: 4 additions & 0 deletions utility/ESPWiFiStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
* Implementation is in WiFiStream.h to avoid linker issues. Legacy WiFi and modern WiFi101 both define WiFiClass which
* will cause linker errors whenever Firmata.h is included.
*/
263 changes: 263 additions & 0 deletions utility/ESPWiFiStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
/*
WiFiStream.h
An Arduino Stream that wraps an instance of a WiFi server. For use
with legacy Arduino WiFi shield and other boards and sheilds that
are compatible with the Arduino WiFi library.
Copyright (C) 2015-2016 Jesse Frush. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See file LICENSE.txt for further informations on licensing terms.
*/

#ifndef ESPWIFI_STREAM_H
#define ESPWIFI_STREAM_H

#include <inttypes.h>
#include <Stream.h>
#include <ESP8266WiFi.h>

class WiFiStream : public Stream
{
private:
WiFiServer _server = WiFiServer(23);
WiFiClient _client;

//configuration members
IPAddress _local_ip;
uint16_t _port = 0;
uint8_t _key_idx = 0; //WEP
const char *_key = nullptr; //WEP
const char *_passphrase = nullptr; //WPA
char *_ssid = nullptr;

inline int connect_client()
{
if( !( _client && _client.connected() ) )
{
WiFiClient newClient = _server.available();
if( !newClient )
{
return 0;
}

_client = newClient;
}
return 1;
}

inline bool is_ready()
{
uint8_t status = WiFi.status();
return !( status == WL_NO_SHIELD || status == WL_CONNECTED );
}

public:
WiFiStream() {};

// allows another way to configure a static IP before begin is called
inline void config(IPAddress local_ip)
{
// _local_ip = local_ip;
// WiFi.config( local_ip );
}

// get DCHP IP
inline IPAddress localIP()
{
return WiFi.localIP();
}

inline bool maintain()
{
if( connect_client() ) return true;

stop();
int result = 0;
if( WiFi.status() != WL_CONNECTED )
{
// if( _local_ip )
// {
// WiFi.config( _local_ip );
// }

if( _passphrase )
{
result = WiFi.begin( _ssid, _passphrase);
}
// else if( _key_idx && _key )
// {
// result = WiFi.begin( _ssid, _key_idx, _key );
// }
// else
// {
// result = WiFi.begin( _ssid );
// }
}
if( result == 0 ) return false;

_server = WiFiServer( _port );
_server.begin();
return result;
}

/******************************************************************************
* Connection functions with DHCP
******************************************************************************/

//OPEN networks
inline int begin(char *ssid, uint16_t port)
{
// if( !is_ready() ) return 0;

// _ssid = ssid;
// _port = port;
// int result = WiFi.begin( ssid );
// if( result == 0 ) return 0;

// _server = WiFiServer( port );
// _server.begin();
// return result;
return 0;
}

//WEP-encrypted networks
inline int begin(char *ssid, uint8_t key_idx, const char *key, uint16_t port)
{
// if( !is_ready() ) return 0;

// _ssid = ssid;
// _port = port;
// _key_idx = key_idx;
// _key = key;

// int result = WiFi.begin( ssid, key_idx, key );
// if( result == 0 ) return 0;

// _server = WiFiServer( port );
// _server.begin();
// return result;
return 0;
}

//WPA-encrypted networks
inline int begin(char *ssid, const char *passphrase, uint16_t port)
{
if( !is_ready() ) return 0;

_ssid = ssid;
_port = port;
_passphrase = passphrase;

int result = WiFi.begin( ssid, passphrase);
if( result == 0 ) return 0;

_server = WiFiServer( port );
_server.begin();
return result;
}

/******************************************************************************
* Connection functions without DHCP
******************************************************************************/

//OPEN networks with static IP
inline int begin(char *ssid, IPAddress local_ip, uint16_t port)
{
// if( !is_ready() ) return 0;

// _ssid = ssid;
// _port = port;
// _local_ip = local_ip;

// WiFi.config( local_ip );
// int result = WiFi.begin( ssid );
// if( result == 0 ) return 0;

// _server = WiFiServer( port );
// _server.begin();
// return result;
return 0;
}

//WEP-encrypted networks with static IP
inline int begin(char *ssid, IPAddress local_ip, uint8_t key_idx, const char *key, uint16_t port)
{
// if( !is_ready() ) return 0;

// _ssid = ssid;
// _port = port;
// _local_ip = local_ip;
// _key_idx = key_idx;
// _key = key;

// WiFi.config( local_ip );
// int result = WiFi.begin( ssid, key_idx, key );
// if( result == 0 ) return 0;

// _server = WiFiServer( port );
// _server.begin();
// return result;
return 0;
}

//WPA-encrypted networks with static IP
inline int begin(char *ssid, IPAddress local_ip, const char *passphrase, uint16_t port)
{
// if( !is_ready() ) return 0;

// _ssid = ssid;
// _port = port;
// _local_ip = local_ip;
// _passphrase = passphrase;

// WiFi.config( local_ip );
// int result = WiFi.begin( ssid, passphrase);
// if( result == 0 ) return 0;

// _server = WiFiServer( port );
// _server.begin();
// return result;
return 0;
}

/******************************************************************************
* Stream implementations
******************************************************************************/

inline int available()
{
return connect_client() ? _client.available() : 0;
}

inline void flush()
{
if( _client ) _client.flush();
}

inline int peek()
{
return connect_client() ? _client.peek(): 0;
}

inline int read()
{
return connect_client() ? _client.read() : -1;
}

inline void stop()
{
_client.stop();
}

inline size_t write(uint8_t byte)
{
if( connect_client() ) _client.write( byte );
}
};

#endif //ESPWIFI_STREAM_H

0 comments on commit 9586def

Please sign in to comment.