Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: I2C, SPI, ESP webSockets controller for teensy

  1. #1
    Senior Member
    Join Date
    Dec 2016

    I2C, SPI, ESP webSockets controller for teensy

    Attached are the files for all 3 libraris/sketches

    SPI controller for multiple teensies: spi_controller.h
    Click image for larger version. 

Name:	teensyspi.jpg 
Views:	24 
Size:	101.9 KB 
ID:	12415

    I2C controller for multiple teensies: i2c_controller.h
    Click image for larger version. 

Name:	teensyi2c.jpg 
Views:	15 
Size:	84.1 KB 
ID:	12416

    ESP websockets controller for multiple teensies: teensywifi_controller.h
    Click image for larger version. 

Name:	teensyespwebsockets.jpg 
Views:	20 
Size:	96.3 KB 
ID:	12417

    For the SPI version, on the host, you may use any SPI0/1/2 of it, the slave must be using SPI0.

    here are a few class initializers for the SPI version:
    spi_controller teensyUART = spi_controller("Serial", 43, &SPI2);
    spi_controller teensyUART1 = spi_controller("Serial1", 43, &SPI2);
    spi_controller teensyUART2 = spi_controller("Serial2", 43, &SPI2);
    spi_controller teensyUART3 = spi_controller("Serial3", 43, &SPI2);
    spi_controller teensyGPIO = spi_controller("GPIO", 43, &SPI2);
    spi_controller TWire = spi_controller("Wire", 43, &SPI2);
    spi_controller TWire1 = spi_controller("Wire1", 43, &SPI2);
    spi_controller TWire2 = spi_controller("Wire2", 43, &SPI2);
    spi_controller TSPI1 = spi_controller("SPI1", 43, &SPI2);
    spi_controller TSPI2 = spi_controller("SPI2", 43, &SPI2);
    spi_controller teensyEP = spi_controller("EEPROM", 43, &SPI2);
    spi_controller SERVO = spi_controller("SERVO", 43, &SPI2);
    spi_controller FASTLED = spi_controller("FASTLED", 43, &SPI2);
    first const char dipicts the object to control, 2nd is the CS pin #, the library automatically sets the pinMode and deasserts the CS line from the class constructor, so thats already handled automatically, 3rd reference is the SPI port.
    The commands should be self-explanatory:
    You have access to the slave's eeprom, digital pins, analog pins, spi busses, wire busses, all uarts, control servos and fastleds
    The same can be done with the I2C version, here's a few initializers:

    i2c_controller S = i2c_controller("Serial", 0x04, &Wire);
    i2c_controller S1 = i2c_controller("Serial1", 0x04, &Wire);
    i2c_controller S2 = i2c_controller("Serial2", 0x04, &Wire);
    i2c_controller W2 = i2c_controller("Wire2", 0x04, &Wire);
    i2c_controller EE = i2c_controller("EEPROM", 0x04, &Wire);
    i2c_controller SV = i2c_controller("SERVO", 0x04, &Wire);
    i2c_controller FASTLED = i2c_controller("FASTLED", 0x04, &Wire);
    i2c_controller SPIport2 = i2c_controller("SPI2", 0x04, &Wire);
    const char object is the bus to control, the slave address of the slave (0x04), and the referenced bus it's attached to.

    the slave address is hardcoded on the MCU_SLAVE_CONTROLLER sketch, in the TEENSY_I2C_SETUP tab. --> Wire.begin(0x04);
    Changing it's value allows you to have multiple slaves on the bus.

    The SPI & I2C versions both are the same sketch code, so in the main sketch, choose which you want to use:
    const bool spi_slave = 1;
    const bool i2c_slave = 0;
    choose one, or the other, NOT both.
    pin10 and 13 in SPI mode are released for the user to use, since SCK and CS were moved to 14 and 15 respectively, so toggling the led is at your disposal via:
    For the Teensy & ESP8266 websockets version, it uses a single sketch for the ESP's and a single library for both teensies. It is designed as multi-master, and can even connect to each other and control each other's hardware. I also finished off adding user payload handler sending, where when it is modified (or not) at the other end, it will come back with the updated (or not) array in it's own handler. I also brought over some transparency for ESP control of both local and remote ESP's and even ESP standalone mode at the endpoint for control.

    teensywifi_controller S1 = teensywifi_controller("Serial1", &Serial1, "");
    teensywifi_controller S2 = teensywifi_controller("Serial2", &Serial1, "");
    teensywifi_controller S6 = teensywifi_controller("Serial6", &Serial1, "");
    teensywifi_controller EEP = teensywifi_controller("EEPROM", &Serial1, "");
    teensywifi_controller W1 = teensywifi_controller("Wire1", &Serial1, "");
    teensywifi_controller SRVO = teensywifi_controller("SERVO", &Serial1, "");
    teensywifi_controller FASTLED = teensywifi_controller("FASTLED", &Serial1, "");
    teensywifi_controller SPI2WIFI = teensywifi_controller("SPI2", &Serial1, "");
    teensywifi_controller TeensyNet = teensywifi_controller("WIFI", &Serial1, "");
    teensywifi_controller localesp = teensywifi_controller("LOCAL_ESP", &Serial1, "");
    teensywifi_controller useher = teensywifi_controller("USERDATA", &Serial1, "");
    teensywifi_controller eSerial1 = teensywifi_controller("ESP_Serial1", &Serial1, "");
    teensywifi_controller ESP = teensywifi_controller("ESP", &Serial1, "");
    teensywifi_controller eWire = teensywifi_controller("ESPWire", &Serial1, "");
    teensywifi_controller eSPI = teensywifi_controller("ESPSPI", &Serial1, "");
    teensywifi_controller eSerial = teensywifi_controller("ESP_Serial", &Serial1, "");
    teensywifi_controller eFL = teensywifi_controller("ESPFASTLED", &Serial1, "");
    teensywifi_controller Udp = teensywifi_controller("WiFiUdp", &Serial1);
    teensywifi_controller client = teensywifi_controller("WiFiClient", &Serial1);
    teensywifi_controller WiFiMulti = teensywifi_controller("WiFiMulti", &Serial1);
    teensywifi_controller WiFi = teensywifi_controller("WiFi", &Serial1);
    teensywifi_controller CONTROLLER = teensywifi_controller("ESP", &Serial1);
    the constructors with the IP given are the ones remotely to be controlled. the consts starting with ESP dipict ESP as standalone mode without a teensy attached, which frees up the comms port to use remotely
    the last few constructors without the IP are for controlling the local ESP
    You get the usual commands like:
    WiFi.config(ip, subnet, gateway);
    and the WifiClient class as well etc

    I tried to make them as transparent as possible, with most of the libs.
    I plan to work on the MQTT version as it seems it's a better idea than this was when I originally started, so I'll be busy on that subject
    heres the libraries (single folder) and the SLAVE sketch for the SPI/I2C teensy slave
    and here is the ESP sketch for the esp's

    SPI in action:
    I2C in action:
    ESP in action: Fastled streaming:
    ESP in action: Uart display streaming:
    ESP in action: teensy vs teensy, control each other's led toggling via !digitalread, digitalwrite:
    ESP in action: teensy running ESP's WiFiScan.ino demo with 3 line modification for library:

    wifiscan.ino demo below: ( red == modified/added )
     *  This sketch demonstrates how to scan WiFi networks. 
     *  The API is almost the same as with the WiFi Shield library, 
     *  the most obvious difference being the different file you need to include:
    #include <teensywifi_controller.h>
    teensywifi_controller WiFi = teensywifi_controller("WiFi", &Serial1);
    void setup() {
      // Set WiFi to station mode and disconnect from an AP if it was previously connected
      Serial.println("Setup done");
    void loop() {
      Serial.println("scan start");
      // WiFi.scanNetworks will return the number of networks found
      int n = WiFi.scanNetworks();
      Serial.println("scan done");
      if (n == 0)
        Serial.println("no networks found");
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i)
          // Print SSID and RSSI for each network found
          Serial.print(i + 1);
          Serial.print(": ");
          Serial.print(" (");
          Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
      // Wait a bit before scanning again


  2. #2
    Senior Member
    Join Date
    Dec 2016
    Dependancies: to use fastled or servos, you need the latest fastled version off github, and for the ESP you need the GIT version and not the stock one from the IDE.
    If your using the ESP version, be sure to set your Serial1.begin(1000000) so it can communicate with it.
    for Fastled, you setup your array buffer only, never addleds.

    #include <FastLED.h>
    #define NUM_LEDS    144
    CRGB leds[NUM_LEDS];
    after your done manipulating your CRGB array, you use:
    Code:, leds, NUM_LEDS);
    where pin23 for example is the teensie's pin to offload your fastled array onto.

    as for the servo controls, you simply do:
    SERVO.write(22, 25)
    Where 22 is the pin a servo is attached to, and 25 is the angle (values: 0-180)

    Note, watchdog on the slave runs in both i2c & spi versions, you must kick the dog to keep the slave happy, use at least one class object to kick the dog anywhere in your loop

    Last edited by tonton81; 12-28-2017 at 06:06 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts