Close

esp8266 + I2C

A project log for VoiceBox

Adding a Flask web interface (and other stuff) to the Google AIY Voice Kit.

tmTM 08/24/2018 at 23:250 Comments

To talk to our LED backpack we'll need to install a couple of Adafruit Arduino libraries:

<Sketch><Include Library><Manage Libraries>

Install  "Adafruit_LED_Backpack" (I used v1.1.6) and "Adafruit_GFX_Library" (I used 1.2.9).

Then take a few minutes to read drmpf's excellent tutorial on using ESP-01 pins. I decided to use his technique for driving I2C over the TX and RX pins, leaving GPIO0 and GPIO2 unused.

This gives me the option of maybe controlling a relay (or two) in addition to the I2C devices.

Because we'll be using TX (pin1) as SDA and RX (pin3) as SCL, rather than the esp8266 defaults of GPIO0 (pin0) and GPIO2(pin2), we'll need to make a slight modification to the Adafruit backpack library, to use the more general Wire.begin(sda, scl) from the esp8266 version of the Wire library, rather than the standard Arduino version - Wire.begin().

Find the source file for the library, on my Windows installation, it's at 

C:\Users\TM\Documents\Arduino\libraries\Adafruit_LED_Backpack_Library

We need to add a signature for the new generalized begin() to the file Adafruit_LED_Backpack_Library.h

  void begin(uint8_t _addr, int sda, int scl);

(Leave the old version without sda & scl for backwards compatibility.)

We then modify the definition of begin() in  Adafruit_LED_Backpack_Library.cpp.

void Adafruit_LEDBackpack::begin(uint8_t _addr = 0x70, int sda, int scl)
{
  i2c_addr = _addr;
#if defined(ESP8266)
  Wire.begin(sda, scl); // esp8266 specific generalized version
#else
  Wire.begin();         // default version
#endif  Wire.beginTransmission(i2c_addr);
  Wire.write(0x21);     // turn on oscillator
  Wire.endTransmission();
  blinkRate(HT16K33_BLINK_OFF);

  setBrightness(15);    // max brightness
}

void Adafruit_LEDBackpack::begin(uint8_t _addr = 0x70) {begin(_addr, SDA, SCL);}

So when existing code  calls begin() without I2C parameters, the defaults (SDA & SCL), will be used. If the target board isn't an esp8266 (ESP8266 is not defined), the standard, parameterless version of Wire.begin() is used (sda & scl arguments are ignored).

After all that, the actual sketch is a bit of an anti-climax (see "Wi8x8.ino" in the files section).

I haven't really decided what I'm going to do with Wi8x8 yet. You'll see from the code that I've added "ear" and "cloud" patterns, so maybe VoiceBox can display its state on the remote LED matrix - in addition to the flashing button and LCD display. Another possibility is is to have scrolling text messages or a stock price ticker, but the former would need more chained matrices, and the latter works better on a multi-color matrix.

Discussions