I am trying to use the Wifi101 library to set up a webserver to load a page from an SD card.
The problem is I can load the webpage once but when I try to reload it doesn't work.
Parts:
Adafruit ATWINC1500
Adafruit microSD breakout
Library:
a fork of the Wifi101 library that allows pin changes
Code:
Based off of WifiWebServer example
My hunch is it has something to do with the TCP connection. In wireshark I saw that the second TCP connection is setup and the browser sends the HTTP request and the server ACKs it. And then nothing.
If I just run the WifiWebServer it works but with the SD card it doesn't?
Any ideas?
/*
WiFi Web Server
A simple web server that shows the value of the analog input pins.
using a WiFi shield.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
* Analog inputs attached to pins A0 through A5 (optional)
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <SPI.h>
#include <Adafruit_WINC1500.h>
#include <SD.h>
// Define the WINC1500 board connections below.
// If you're following the Adafruit WINC1500 board
// guide you don't need to modify these:
#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
// #define WINC_EN 2 // or, tie EN to VCC and comment this out
// The SPI pins of the WINC1500 (SCK, MOSI, MISO) should be
// connected to the hardware SPI port of the Arduino.
// On an Uno or compatible these are SCK = #13, MISO = #12, MOSI = #11.
// On an Arduino Zero use the 6-pin ICSP header, see:
// https://www.arduino.cc/en/Reference/SPI
//SD chip select pin
#define SD_CS 10
// Setup the WINC1500 connection with the pins above and the default hardware SPI.
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
// Or just use hardware SPI (SCK/MOSI/MISO) and defaults, SS -> #10, INT -> #7, RST -> #5, EN -> 3-5V
//Adafruit_WINC1500 WiFi;
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "passs"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
Adafruit_WINC1500Server server(80);
//sd class instance
File myFile;
void setup() {
#ifdef WINC_EN
pinMode(WINC_EN, OUTPUT);
digitalWrite(WINC_EN, HIGH);
#endif
pinMode(WINC_CS, OUTPUT);
pinMode(SD_CS, OUTPUT);
digitalWrite(WINC_CS, HIGH);
digitalWrite(SD_CS, HIGH);
//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
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
// turn off wifi shield
//active LOW
digitalWrite(WINC_CS, HIGH);
Serial.print("Initializing SD card...");
//init of SD and spi
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
//SD is a global object
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
//unselect sd card
digitalWrite(SD_CS, HIGH);
// turn onwifi shield
// digitalWrite(WINC_CS, LOW);
}
void loop() {
// listen for incoming clients
Adafruit_WINC1500Client client = server.available();
//big loop
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
// Serial.println("c");
if (client.available()) {
// Serial.println("a");
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
// client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
//print the front webpage
webPageFromSD(client, "list.htm");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
//open web page from a file on the SD card
//param: the Adafruit_WINC1500Client
//param: the name of the file on the SD card
//return: void
void webPageFromSD(Adafruit_WINC1500Client client, String name)
{
// re-open the file for reading:
myFile = SD.open(name);
if (myFile) {
Serial.println(name);
// read from the file until there's nothing else in it:
while (myFile.available()) {
//do the access to SPI line separately, first read from SD card
// delay(1);
char j = myFile.read();
//then print it to client using Wifi on the SPI
// delay(1);
client.print(j);
}
// close the file:
// myFile.close();
}
else
{
// if the file didn't open, print an error:
Serial.println("sd error opening");
}
}
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 the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}