I'm trying to get a simple web server woking on a FRDM-K64F board.
I'm using the HTTPServer_echoback program as a starting point. I have replaced mbed with mbed-src and updated all the libraries. The HTTPServer_echoback program works as expected.
I then modified the code to read a file from an SD-card (using SDFileSystem) and return that instead of echoing the header. My code for reading the SD-card works in a separate program, but in the modified HTTP server program it does not work.
With my extra code it will not get an IP trough DHCP, it will not answer ping and the LED indicating that the server is listening for HTTP requests does not blink. Note that it does not work normally until it gets to my code and then fails. If my code is there it will not work at all.
Anyone know what's going on?
#include "mbed.h"
#include "EthernetInterface.h"
#include <stdio.h>
#include <string.h>
#include <SDFileSystem.h>
#define PORT 80
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //MOSI, MISO, SCLK, SSEL. Tested on K64F, correct pins.
EthernetInterface eth;
TCPSocketServer svr;
bool serverIsListened = false;
TCPSocketConnection client;
bool clientIsConnected = false;
DigitalOut led1(LED1); //server listning status
DigitalOut led2(LED2); //socket connecting status
Ticker ledTick;
int i = 0;
char c;
char fileContent[1024] = {};
void ledTickfunc()
{
if(serverIsListened) {
led1 = !led1;
} else {
led1 = false;
}
}
int main (void)
{
ledTick.attach(&ledTickfunc,0.5);
//setup ethernet interface
eth.init(); //Use DHCP
eth.connect();
printf("IP Address is %s\n\r", eth.getIPAddress());
//setup tcp socket
if(svr.bind(PORT)< 0) {
printf("tcp server bind failed.\n\r");
return -1;
} else {
printf("tcp server bind successed.\n\r");
serverIsListened = true;
}
if(svr.listen(1) < 0) {
printf("tcp server listen failed.\n\r");
return -1;
} else {
printf("tcp server is listening...\n\r");
}
//listening for http GET request
while (serverIsListened) {
//blocking mode(never timeout)
if(svr.accept(client)<0) {
printf("failed to accept connection.\n\r");
} else {
printf("connection success!\n\rIP: %s\n\r",client.get_address());
clientIsConnected = true;
led2 = true;
while(clientIsConnected) {
char buffer[1024] = {};
switch(client.receive(buffer, 1023)) {
case 0:
printf("recieved buffer is empty.\n\r");
clientIsConnected = false;
break;
case -1:
printf("failed to read data from client.\n\r");
clientIsConnected = false;
break;
default:
printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) {
printf("GET request incomming.\n\r");
char echoHeader[256] = {};
if (buffer[5] == ' ') { //No file name specified, let's return index.html
//Uncoment the next block to make the program stop working.
//The program will then fail to get an IP trough DHCP, it will not answer ping and the LED will not blink.
/*
i = 0;
FILE *fp = fopen("/sd/index.html", "r");
while (!feof(fp)){
c = fgetc(fp);
fileContent[i] = c;
i++;
}
fclose(fp); /
fileContent[i] = NULL;
*/
sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(fileContent));
client.send(echoHeader,strlen(echoHeader));
client.send(fileContent,strlen(fileContent));
} else {
//Just echos the request if it's something else.
sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
client.send(echoHeader,strlen(echoHeader));
client.send(buffer,strlen(buffer));
clientIsConnected = false;
printf("echo back done.\n\r");
}
}
break;
}
}
printf("close connection.\n\rtcp server is listening...\n\r");
client.close();
led2 = false;
}
}
}
I'm trying to get a simple web server woking on a FRDM-K64F board.
I'm using the HTTPServer_echoback program as a starting point. I have replaced mbed with mbed-src and updated all the libraries. The HTTPServer_echoback program works as expected.
I then modified the code to read a file from an SD-card (using SDFileSystem) and return that instead of echoing the header. My code for reading the SD-card works in a separate program, but in the modified HTTP server program it does not work.
With my extra code it will not get an IP trough DHCP, it will not answer ping and the LED indicating that the server is listening for HTTP requests does not blink. Note that it does not work normally until it gets to my code and then fails. If my code is there it will not work at all.
Anyone know what's going on?