10 years, 3 months ago.

HTTP_SD_Server_K64F & NTPClient

Has anybody managed to get both these working in the same program. I can get both to work independently, but not together.

  1. include "mbed.h"
  2. include "EthernetInterface.h"
  3. include "SDFileSystem.h"
  4. include <stdio.h>
  5. include <string.h>
  6. include "NTPClient.h"
  1. define HTTPD_SERVER_PORT 80
  2. define HTTPD_MAX_REQ_LENGTH 1023
  3. define HTTPD_MAX_HDR_LENGTH 255
  4. define HTTPD_MAX_FNAME_LENGTH 127
  5. define HTTPD_MAX_DNAME_LENGTH 127

Serial pc(USBTX, USBRX);

SDFileSystem sd(p5, p6, p7, p8, "sd"); LPC1768 MBD2PMD SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); Seeeduino Arch Pro SPI2SD SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); K64F

NTPClient ntp; EthernetInterface eth;

TCPSocketServer server; TCPSocketConnection client;

char buffer[HTTPD_MAX_REQ_LENGTH+1]; char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; char fileName[HTTPD_MAX_FNAME_LENGTH+1]; char dirName[HTTPD_MAX_DNAME_LENGTH+1]; char *uristr; char *eou; char *qrystr;

FILE *fp; int rdCnt;

void SetTimeNTP(void) { pc.printf("NTP update time...\r\n"); eth.init("192.168.0.11", "255.255.255.0", "192.168.0.1"); set blank to Use DHCP eth.init(); Use DHCP eth.connect(); if (ntp.setTime("0.pool.ntp.org") == 0) { pc.printf("Set time successfully\r\n"); time_t ctTime; ctTime = time(NULL); pc.printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); } else { pc.printf("Error\r\n"); }

eth.disconnect(); pc.printf("NTP eth closed"); while(1) { } } --- end of SetTimeNTP -----

void get_file(char* uri) { pc.printf("get_file %s\n", uri); char *lstchr = strrchr(uri, NULL) -1; if ('/' == *lstchr) { pc.printf("Open directory /sd%s\n", uri);

  • lstchr = 0; sprintf(fileName, "/sd%s", uri); DIR *d = opendir(fileName); if (d != NULL) { sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri); client.send(httpHeader,strlen(httpHeader)); struct dirent *p; while((p = readdir(d)) != NULL) { sprintf(dirName, "%s/%s", fileName, p->d_name); pc.printf("%s\n", dirName); DIR *subDir = opendir(dirName); if (subDir != NULL) { sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name); } else { sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name); } client.send(httpHeader,strlen(httpHeader)); } } closedir(d); pc.printf("Directory closed\n"); sprintf(httpHeader,"</ul></body></html>"); client.send(httpHeader,strlen(httpHeader)); } else { sprintf(fileName, "/sd%s", uri); fp = fopen(fileName, "r"); if (fp == NULL) { sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); client.send(uri,strlen(uri)); } else { sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) { client.send(buffer, rdCnt); } client.send(buffer, rdCnt); fclose(fp); } } }

int main (void) { Serial Interface pc.baud(115200); pc.printf("Initializing\n"); time_t ctTime; ctTime = time(NULL);

pc.printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));

Check File System pc.printf("Checking File System\n"); DIR *d = opendir("/sd/"); if (d != NULL) { pc.printf("SD Card Present\n"); } else { pc.printf("SD Card Root Directory Not Found\n"); }

EthernetInterface eth pc.printf("Initializing Ethernet\n"); pc.printf("Connecting\n");

SetTimeNTP(); Set NTP Time eth.init("192.168.0.11", "255.255.255.0", "192.168.0.1"); set blank to Use DHCP eth.connect();

pc.printf("MAC Address %s\n", eth.getMACAddress()); pc.printf("IPv4 Address %s\n", eth.getIPAddress()); pc.printf("Subnet Mask %s\n", eth.getNetworkMask()); pc.printf("Default Gateway %s\n", eth.getGateway());

TCPSocketServer server; server.bind(HTTPD_SERVER_PORT); server.listen(); pc.printf("Server Listening port : %d\n", HTTPD_SERVER_PORT);

while (true) { pc.printf("\nWait for new connection...\r\n"); server.accept(client); client.set_blocking(false, 1500); Timeout after (1.5)s

pc.printf("Connection from: %s\r\n", client.get_address()); while (true) { int n = client.receive(buffer, sizeof(buffer)); if (n <= 0) break; pc.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); if (n >= 1024) { sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); client.send(buffer,n); break; } else { buffer[n]=0; } if (!strncmp(buffer, "GET ", 4)) { uristr = buffer + 4; pc.printf("uristr %s\n",uristr); eou = strstr(uristr, " "); pc.printf("eou %s\n",eou); if (eou == NULL) { sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); client.send(buffer,n); } else {

  • eou = 0; get_file(uristr); } } }

client.close(); } }

Question relating to:

HTTP SD Card File Server for the FRDM-K64F board card, FRDM-K64F, SD, server, Web

Can you share more from your main program? Use code highlight "<<code>>" "<</code>>". Thanks.

posted by Martin Kojtal 22 Aug 2014

I think there are some issues combining the SDFileSystem and EthernetInterface libraries on the K64F. You can find the basic code for the K64F version of the web server here: http://mbed.org/teams/FRDM-K64F-Code-Share/code/HTTP_SD_Server_K64F/ The same code runs on the mbed1768 ( http://mbed.org/users/gsteiert/code/HTTP_SD_Server/ ) and Seeeduino Arch Pro ( http://mbed.org/users/gsteiert/code/HTTP_SD_Server_ArchPro/ ), but I cannot get it to successfully return a file from the SD card to the web browser with the K64F board. Tracking down the source of the problem is beyond my ability with the time I have for this right now.

posted by Greg Steiert 25 Aug 2014

1 Answer

10 years, 3 months ago.

George,

Please encapsulate your code with:

//your code

It makes it much, much easier to read.

...kevin

I eventually found the solution of using the NTP server and ethernet. Don't close the connection after the NTP and before the running the WEB server. The code now runs as a server. I connect the Ethernet port to the laptop ethernet port, and set up a Bridge connection via WiFi. I can now load files from the SD card via the WEB! I have even run some simple JS code. All I need to do is figure how the get "numbers" from code to the WEB pages, any ideas?

posted by george michie 02 Sep 2014