Airlift Wifi simple web server problem

sjm-mcl

Active member
Hi, I'm using a Teensy4.1 with an ESP32-AirLift breakout board Arduino wired like this:

BUSY/RDY p9
GP0 p40
CS/SS p10
MISO p12
MOSI p11
SCLK p13 (note also built-in LED)

Adafruit's AP_SimpleWebServer example sketch works perfectly, once you move the LED to a differenet GPIO.
My attached sketch is just a modified version of the example, it serves a jpg file, from an SD-card, to a web client using very basic http over port 80.
Sadly the server is never able to send the whole image, the socket connection always fails at some random point, leaving a partial and/or corrupted image on the client screen.
The WiFi connection however remains solid, so it's easy to keep re-loading the image to see the randomness of the issue.
I've tried this with versions 1.2.2, 1.7.4 and 1.7.5 of the AirLift's firmware, with no difference.
I also tried decoupling the AirLift's power supplies with a 10uF tant cap,
upping the power-supply current limit to 1Amp (it never reads more than 250mA),
slowing down the SPI clock speed (to 4 and 2MHz),
trying different SPI_MODEs (only MODE0 works),
loading the SPI_CLK pin with a 47pF cap, or loading the MISO/MOSI pins with 47pF caps,
I also got hold of the latest WiFiNINA library (not the Adafruit one, the original - v1.8.14) and hacked it to work with my pin configuration,
I tried using different buffer sizes when writing data blocks to the client (64B & 8192B stopped it pretty much dead, other sizes in between had marginal effects on how the broken image looked).
NONE of the above had any significant effect on the problem - the whole image never makes it through, and the bits of image that do always look broken or corrupted, and always in different random ways.
I've posted on the Adafruit forum but as yet no one there is responding.
I'm hoping someone in the Teensy community has an Airlift they can try, principally just to duplicate the problem then hopefully provide some advice.
To run the attached sketch you'll need to load the Adafruit version of the WiFiNINA library from: https://github.com/adafruit/WiFiNINA/archive/master.zip
Include the library as a .ZIP to the sketch (do not update if offered!), and insert an SD card with a roughly 2MB JPG image at /test.jpg.
After booting attach your phone/device to SSID: "Teensy41" & pwd: "TeensyWeensy", and point your browser to http://192.128.4.1, you should see the image start to load then fail.
Thanks so much for reading this far.

Code:
// AirList_APwWebServer
// Modified version of WiFiNINA example AP_SimpleWebServer.ino for AirLift breakout board.
// Instead of serving just two lines of static text we attempt here to serve a ~2MB jpg file
// from an SD-Card at /test.jpg
// Requires Adafruit's modified WiFiNINA library from
//   https://github.com/adafruit/WiFiNINA/archive/master.zip
//   this version allows changing the pin assignments to suit Teensy & AirLift

#include <SPI.h>
#include <WiFiNINA.h>
#include <SD.h>

// Wiring rig:
#define SPIWIFI      SPI
//MISO p12
//MOSI p11
//SCLK p13 (note also built-in LED)
#define SPIWIFI_SS    10   // Chip select pin
#define SPIWIFI_ACK    9   // a.k.a BUSY or READY pin
#define ESP32_RESETN  41   // Reset pin
#define ESP32_GPIO0   40   // (Original example says -1 for not-connected)

uint8_t  buf[8192];
File     srcFile;

char ssid[] = "Teensy41";       // your network SSID (name)
char pass[] = "TeensyWeensy";   // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;               // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) ; // wait for serial port to connect. Needed for native USB port only
  Serial.println("Access Point Web Server");

  WiFi.setPins(SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE)
   {
    Serial.println("Communication with WiFi module failed!");
    while(1);
   }

  // by default the local IP address of will be 192.168.4.1
  // you can override it with the following:
  // WiFi.config(IPAddress(10, 0, 0, 1));

  // print the network name (SSID);
  Serial.print("Creating access point named: ");
  Serial.println(ssid);

  // Create open network. Change this line if you want to create an WEP network:
  status = WiFi.beginAP(ssid, pass);
  if (status != WL_AP_LISTENING)
   {
    Serial.println("Creating access point failed");
    while(1);
   }

  // wait 10 seconds for connection:
  delay(10000);

  // start the web server on port 80
  server.begin();

  // you're connected now, so print out the status
  printWiFiStatus();
}


void loop() {
  // compare the previous status to the current status
  if (status != WiFi.status()) {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {
      // a device has connected to the AP
      Serial.println("Device connected to AP");
    } else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }

  uint32_t rdlen = 1;
  uint32_t wrlen = 1;
  int n = 0;
  String currentLine;
  SD.begin(BUILTIN_SDCARD);
  
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    currentLine = "";                       // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0)
           {
            // Client has made its request, which we ignore, just serve the jpg file...
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:image/jpeg");
            client.println();
            // open the file for read. 
            srcFile = SD.open("/test.jpg");
            if (!srcFile) Serial.println("no /test.jpg");
            Serial.println("sending JPG...");
            while((rdlen > 0) && (wrlen > 0))
             {
              n++;
              rdlen = srcFile.read(buf, 2048);
              // delay(250); // the bigger the delay here the less bad the transmission gets
              if (rdlen > 0)
               {
                // while (client.availableForWrite() != 0); // always returns 0
                wrlen = client.write(buf, rdlen);
                // client.flush(); // no effect
               }
             }
            Serial.print("\r\n [ ");
            Serial.print(rdlen); Serial.print("  ");
            Serial.print(wrlen); Serial.print("  ");
            Serial.print(n); Serial.println(" ]");
            srcFile.close();
            break;
          }
          else currentLine = "";
        }
        else if (c != '\r') currentLine += c;
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);

}
 
Back
Top