Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By sumotoy
#35142 I have recently added ESP8266 support to my gpio_MCP23S17 SPI library. It uses standard SPI library and uses SPI Transaction where applicable, it support all the features of the chip included HAEN that allow you connect 8 of this chip using the same SPI lines (included CS), giving you 16x8=128 extra I/O with 3(4 with input) wires but leaving 2(or 3 if you use MISO) wires still usable for other stuff.
https://github.com/sumotoy/gpio_MCP23S17
This is an extract of my other library on github that is able to drive at list 25 other GPIO expander from different brands, I2C or SPI and I will adapt soon for use with ESP8266.
The library can be easily included in other libraries and has many examples.
User avatar
By Parsec
#35353 Dude, Sumotoy, you are awesome for doing this!! I look forward to using your library. I blew through a whole day of research, testing, trial & error trying to retrofit MetalPhreak's native SPI driver and MCP23S17 driver for the ESP8266-Arduino platform with no luck here http://www.eevblog.com/forum/microcontrollers/esp8266-native-spi-hardware-driver/. I almost went down the road of rolling my own driver lib until I just saw your post. I'll post results using your lib as I get them. Thanks again for your generous contribution to the community! :D
User avatar
By sumotoy
#35378 I've just tested 5 x MCP23s17 (5*16 ) together with my other library for ILI9163C (128x128 color tft), WiFiWebServer sketch at 160Mhz.
I was able to control 80 output and have server running and worked pretty well, not bad for a tiny chip.
Here's a quick & disrty example:

Code: Select all/*
 *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *  It uses my TFT_ILI9163C https://github.com/sumotoy/TFT_ILI9163C/tree/Pre-Release-1.0r
 *  And 5x MCP23s17 drived by gpio_MCP23S17 https://github.com/sumotoy/gpio_MCP23S17
 *  With this configuration you have 80 outputs and a color TFT with wifi.
 *    http://server_ip/gpio/0/0 will set the mcp23s17 port 0 low
 *    http://server_ip/gpio/0/1 will set the mcp23s17 port 0 high
 *    .....
 *    http://server_ip/gpio/4/0 will set the mcp23s17 port 0 low
 *  server_ip is the IP address of the ESP8266 module, will be
 *  printed to Serial when the module is connected.
 *  This is just a quick and dirty example of many SPI devices connected to esp8266
 */

#include <ESP8266WiFi.h>
#include <SPI.h>
#include <TFT_ILI9163C.h>
#include <gpio_MCP23S17.h>

const char* ssid = "yourSid";
const char* password = "yourPass";
#define __CS_TFT        16  //(D0)
#define __DC_TFT        2   //(D1)
#define __CS_GPIO       4   //(D2)
#define __GPIO_ADRS1    0x20
#define __GPIO_ADRS2    0x21
#define __GPIO_ADRS3    0x22
#define __GPIO_ADRS4    0x23
#define __GPIO_ADRS5    0x24

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
TFT_ILI9163C tft = TFT_ILI9163C(__CS_TFT, __DC_TFT);
gpio_MCP23S17 gpio1(__CS_GPIO, __GPIO_ADRS1);
gpio_MCP23S17 gpio2(__CS_GPIO, __GPIO_ADRS2);
gpio_MCP23S17 gpio3(__CS_GPIO, __GPIO_ADRS3);
gpio_MCP23S17 gpio4(__CS_GPIO, __GPIO_ADRS4);
gpio_MCP23S17 gpio5(__CS_GPIO, __GPIO_ADRS5);

void setup() {
  Serial.begin(115200);
  delay(10);

  tft.begin();
  gpio1.begin(true);//avoid SPI initialization, already done by tft
  gpio2.begin(true);
  gpio3.begin(true);
  gpio4.begin(true);
  gpio5.begin(true);
  gpio1.gpioPinMode(OUTPUT);
  gpio2.gpioPinMode(OUTPUT);
  gpio3.gpioPinMode(OUTPUT);
  gpio4.gpioPinMode(OUTPUT);
  gpio5.gpioPinMode(OUTPUT);
  gpio1.gpioPort(0x0000);
  gpio2.gpioPort(0x0000);
  gpio3.gpioPort(0x0000);
  gpio4.gpioPort(0x0000);
  gpio5.gpioPort(0x0000);


  // Connect to WiFi network
  tft.setCursor(0, 0);
  tft.print("Link to ");
  tft.setTextColor(CYAN);
  tft.println(ssid);
  tft.setTextColor(WHITE);

  WiFi.begin(ssid, password);
  tft.setCursor(0, 10);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    tft.print(".");
  }
  tft.setCursor(0, 0);
  tft.setTextColor(WHITE, BLACK);
  //tft.println("WiFi connected");
  tft.clearScreen();
  // Start the server
  server.begin();
  tft.println("Server started at:");
  tft.setTextColor(YELLOW, BLACK);
  // Print the IP address
  tft.println(WiFi.localIP());
  tft.setTextColor(WHITE, BLACK);
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  tft.setCursor(0, 20);
  tft.println("new client");
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  tft.setCursor(0, 30);
  tft.println(req);
  client.flush();

  // Match the request
  int val;
  int port;
  int gp;
  if (req.indexOf("/gpio/0/0") != -1) {
    gp = 0;
    port = 0;
    val = 0;
  } else if (req.indexOf("/gpio/0/1") != -1) {
    gp = 0;
    port = 0;
    val = 1;
  } else if (req.indexOf("/gpio/1/0") != -1) {
    gp = 0;
    port = 1;
    val = 0;
  } else if (req.indexOf("/gpio/1/1") != -1) {
    gp = 0;
    port = 1;
    val = 1;
  } else if (req.indexOf("/gpio/2/0") != -1) {
    gp = 0;
    port = 2;
    val = 0;
  } else if (req.indexOf("/gpio/2/1") != -1) {
    gp = 0;
    port = 2;
    val = 1;
  } else if (req.indexOf("/gpio/3/0") != -1) {
    gp = 0;
    port = 3;
    val = 0;
  } else if (req.indexOf("/gpio/3/1") != -1) {
    gp = 0;
    port = 3;
    val = 1;
  } else if (req.indexOf("/gpio/4/0") != -1) {
    gp = 0;
    port = 4;
    val = 0;
  } else if (req.indexOf("/gpio/4/1") != -1) {
    gp = 0;
    port = 4;
    val = 1;
  } else if (req.indexOf("/gpio/5/0") != -1) {
    gp = 0;
    port = 5;
    val = 0;
  } else if (req.indexOf("/gpio/5/1") != -1) {
    gp = 0;
    port = 5;
    val = 1;
  } else if (req.indexOf("/gpio/5/0") != -1) {
    gp = 0;
    port = 5;
    val = 0;
  } else if (req.indexOf("/gpio/5/1") != -1) {
    gp = 0;
    port = 5;
    val = 1;
  } else if (req.indexOf("/gpio/6/0") != -1) {
    gp = 0;
    port = 6;
    val = 0;
  } else if (req.indexOf("/gpio/6/1") != -1) {
    gp = 0;
    port = 6;
    val = 1;
  } else if (req.indexOf("/gpio/7/0") != -1) {
    gp = 0;
    port = 7;
    val = 0;
  } else if (req.indexOf("/gpio/7/1") != -1) {
    gp = 0;
    port = 7;
    val = 1;
  } else if (req.indexOf("/gpio/8/0") != -1) {
    gp = 0;
    port = 8;
    val = 0;
  } else if (req.indexOf("/gpio/8/1") != -1) {
    gp = 0;
    port = 8;
    val = 1;
    //.....etc, etc, just an example!
  } else {
    tft.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  switch(gp){
    case 0:
      gpio1.gpioDigitalWrite(port, val);
    break;
    case 1:
      gpio2.gpioDigitalWrite(port, val);
    break;
    case 2:
      gpio3.gpioDigitalWrite(port, val);
    break;
    case 3:
      gpio4.gpioDigitalWrite(port, val);
    break;
    case 4:
      gpio5.gpioDigitalWrite(port, val);
    break;
  }
 

  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO";
  s += gp;
  s += "_port";
  s += port;
  s += " is now ";
  s += (val) ? "high" : "low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  //tft.println("Client disonnected");

  // The client will actually be disconnected
  // when the function returns and 'client' object is detroyed
}

User avatar
By Ecoli-557
#53781 Sumotoy-
I came across this post (old) but I think it could help me figure out how to get a MCP23S17 and a ILI9341 to work together on the same bus.
I have tried it MMISCOOLs basic interpreter but it does not seem to work so now I am trying your libs.

Do you by any chance have an example of 1 23S17 and 1 ILI9341 working on the same bus? It would greatly help my learning....

Regards.