Hello,
I am using the wifi server tutorial at this link: https://blog.startingelectronics.com/basic-multi-file-arduino-web-server. I converted the Ethernet code to Wifi (from adafruit's example sketch), but the sram size used by variables is very large (1906 bytes). The arduino editor is giving me a warning that says that there may be stability problems. When I ran the sketch, it printed 2 characters, and hung there for a couple of hours. I'm using a chinese counterfeit CC3000 from keyes, so it doesn't work with the mega. The mega requires soldering jumpers, which my counterfeit doesn't have. My complete code is below:
#include <SdFat.h>
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "utility/socket.h"
#define Adafruit_CC3000_IRQ 3
#define Adafruit_CC3000_VBAT 5
#define Adafruit_CC3000_CS 10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(Adafruit_CC3000_CS, Adafruit_CC3000_IRQ, Adafruit_CC3000_VBAT, SPI_CLOCK_DIVIDER);
const char WLANSSID[] PROGMEM = "6T4MK";
const char WLANPASS[] PROGMEM = "YCX6HKPRKLZVNYSH";
#define WLANSECURITY WLAN_SEC_WPA2
#define LISTENPORT 80
Adafruit_CC3000_Server server(LISTENPORT);
#define FILENAMELEN 20
#define HTTPInvalid 0
#define HTTPGET 1
#define HTTPPOST 2
#define FTHTML 0
#define FTICON 1
#define FTCSS 2
#define FTJAVASCRIPT 3
#define FTJPG 4
#define FTPNG 5
#define FTGIF 6
#define FTTEXT 7
#define FTINVALID 8
#define PINETHSPI 10
SdFat SD;
void setup() {
Serial.begin(115200);
while (!Serial) {
;
}
Serial.println(F("Starting..."));
SD.begin(4);
if (!cc3000.begin()) {
Serial.println(F("Shield inserted improperly"));
while(1);
}
Serial.println(F("Stage 1 of 2 complete"));
if (!cc3000.connectToAP(WLANSSID, WLANPASS, WLANSECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Stage 2 of 2 complete"));
while (!cc3000.checkDHCP()) {
delay(100);
}
while (!displayConnectionDetails()) {
delay(1000);
}
server.begin();
Serial.println(F("Server ready"));
}
void loop() {
Adafruit_CC3000_ClientRef client = server.available();
if (client) {
while (client.connected()) {
if (ServiceClient(&client)) {
break;
}
}
delay(1);
client.stop();
}
}
bool ServiceClient(Adafruit_CC3000_ClientRef *client) {
static boolean currentLineIsBlank = true;
char clChar;
File webFile;
char fileName[FILENAMELEN + 1] = {0};
char httpReqType = 0;
char reqFileType = FTINVALID;
const char *fileTypes[] = {"text/html", "image/x-icon", "text/css", "application/javascript", "image/jpeg", "image/png", "image/gif", "text/plain"};
static char reqLine_1[40] = {0};
static unsigned char reqLineIndex = 0;
static bool gotLine_1 = false;
if (client->available()) {
clChar = client->read();
if ((reqLineIndex < 39) && (gotLine_1 == false)) {
if ((clChar != '\r') && (clChar != '\n')) {
reqLine_1[reqLineIndex] = clChar;
reqLineIndex++;
}
else {
gotLine_1 = true;
reqLine_1[39] = 0;
}
}
if ((clChar == '\n') && currentLineIsBlank) {
httpReqType = accessFile(reqLine_1, fileName, &reqFileType);
if (httpReqType == HTTPGET) {
if (reqFileType < FTINVALID) {
webFile = SD.open(fileName);
if (webFile) {
client->println(F("HTTP/1.1 200 OK"));
client->print(F("Content-Type: "));
client->println(fileTypes[reqFileType]);
client->println(F("Connection: close"));
client->println();
while(webFile.available()) {
int numBytesRead;
char byteBuffer[64];
numBytesRead = webFile.read(byteBuffer, 64);
client->write(byteBuffer, numBytesRead);
}
webFile.close();
}
else {
Serial.println(F("File cannot be opened"));
}
}
else {
// invalid file type
}
}
else if (httpReqType == HTTPPOST) {
// a POST HTTP request was received
}
else {
// unsupported HTTP request received
}
reqLine_1[0] = 0;
reqLineIndex = 0;
gotLine_1 = false;
return 1;
}
if (clChar == '\n') {
currentLineIsBlank = true;
}
else if (clChar != '\r') {
currentLineIsBlank = false;
}
} // if (client.available())
return 0;
}
char accessFile(char *reqLine, char *fileName, char *fileType) {
char requestType = HTTPInvalid; // 1 = GET, 2 = POST. 0 = invalid
char *strToken;
*fileType = FTINVALID;
strToken = strtok(reqLine, " ");
if (strcmp(strToken, "GET") == 0) {
requestType = HTTPGET;
strToken = strtok(NULL, " ");
if (strcmp(strToken, "/") == 0) {
strcpy(fileName, "index.html");
*fileType = FTHTML;
}
else if (strlen(strToken) <= FILENAMELEN) {
strcpy(fileName, strToken);
strToken = strtok(strToken, ".");
strToken = strtok(NULL, ".");
if (strcmp(strToken, "html") == 0) {*fileType = 0;}
else if (strcmp(strToken, "ico") == 0) {*fileType = 1;}
else if (strcmp(strToken, "css") == 0) {*fileType = 2;}
else if (strcmp(strToken, "js") == 0) {*fileType = 3;}
else if (strcmp(strToken, "jpg") == 0) {*fileType = 4;}
else if (strcmp(strToken, "png") == 0) {*fileType = 5;}
else if (strcmp(strToken, "gif") == 0) {*fileType = 6;}
else if (strcmp(strToken, "txt") == 0) {*fileType = 7;}
else {*fileType = 8;}
}
else {
}
}
else if (strcmp(strToken, "POST") == 0) {
requestType = HTTPPOST;
}
return requestType;
}
bool displayConnectionDetails() {
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) {
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else {
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.println();
return true;
}
}
Could anybody help me reduce program size? I already tried F() ing everything and using progmem.
Thanks!