Example for the [[https://os.mbed.com/users/hudakz/code/HTTPServer]] library.

Dependencies:   HTTPServer

Committer:
hudakz
Date:
Sun Oct 06 18:13:11 2019 +0000
Revision:
0:49fa55340890
Example for the [[https://os.mbed.com/users/hudakz/code/HTTPServer]] library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:49fa55340890 1 #include "mbed.h"
hudakz 0:49fa55340890 2 #include "EthernetInterface.h"
hudakz 0:49fa55340890 3 #include "FsHandler.h"
hudakz 0:49fa55340890 4 #include "HTTPServer.h"
hudakz 0:49fa55340890 5 #include "SDBlockDevice.h"
hudakz 0:49fa55340890 6 #include "FATFileSystem.h"
hudakz 0:49fa55340890 7
hudakz 0:49fa55340890 8 #define DHCP
hudakz 0:49fa55340890 9
hudakz 0:49fa55340890 10 #ifndef DHCP
hudakz 0:49fa55340890 11 #define IP "192.168.1.181"
hudakz 0:49fa55340890 12 #define GATEWAY "192.168.1.1"
hudakz 0:49fa55340890 13 #define NETMASK "255.255.255.0"
hudakz 0:49fa55340890 14 #endif
hudakz 0:49fa55340890 15
hudakz 0:49fa55340890 16 #define PORT 80
hudakz 0:49fa55340890 17
hudakz 0:49fa55340890 18 SDBlockDevice blockDevice(PC_3, PC_2, PB_10, PC_0); // mosi, miso, sck, cs
hudakz 0:49fa55340890 19 FATFileSystem fileSystem("local");
hudakz 0:49fa55340890 20 EthernetInterface* net;
hudakz 0:49fa55340890 21 HTTPServer svr;
hudakz 0:49fa55340890 22
hudakz 0:49fa55340890 23 int main()
hudakz 0:49fa55340890 24 {
hudakz 0:49fa55340890 25 printf("Starting.. \r\n\r\n");
hudakz 0:49fa55340890 26
hudakz 0:49fa55340890 27 net = new EthernetInterface();
hudakz 0:49fa55340890 28
hudakz 0:49fa55340890 29 if (!net) {
hudakz 0:49fa55340890 30 printf("Error! No network inteface found.\n");
hudakz 0:49fa55340890 31 return -1;
hudakz 0:49fa55340890 32 }
hudakz 0:49fa55340890 33
hudakz 0:49fa55340890 34 printf("Network interface found\r\n");
hudakz 0:49fa55340890 35
hudakz 0:49fa55340890 36 #ifndef DHCP
hudakz 0:49fa55340890 37 net->set_network (IP, NETMASK, GATEWAY); // to use static IP address
hudakz 0:49fa55340890 38 #endif
hudakz 0:49fa55340890 39
hudakz 0:49fa55340890 40 nsapi_size_or_error_t ret = net->connect();
hudakz 0:49fa55340890 41
hudakz 0:49fa55340890 42 if (ret != 0) {
hudakz 0:49fa55340890 43 printf("Error! net->connect() returned: %d\n", ret);
hudakz 0:49fa55340890 44 return ret;
hudakz 0:49fa55340890 45 }
hudakz 0:49fa55340890 46
hudakz 0:49fa55340890 47
hudakz 0:49fa55340890 48 printf("Mounting file system at /local\r\n");
hudakz 0:49fa55340890 49 int err = fileSystem.mount(&blockDevice);
hudakz 0:49fa55340890 50 printf("%s\r\n", (err ? "Fail :(" : "OK"));
hudakz 0:49fa55340890 51 if (err) {
hudakz 0:49fa55340890 52 printf("No filesystem found.\r\n");
hudakz 0:49fa55340890 53 error("error: %s (%d)\r\n", strerror(-err), err);
hudakz 0:49fa55340890 54 return err;
hudakz 0:49fa55340890 55 }
hudakz 0:49fa55340890 56
hudakz 0:49fa55340890 57 HTTPFsRequestHandler::mount("/local/", "/");
hudakz 0:49fa55340890 58 svr.addHandler<HTTPFsRequestHandler>("/");
hudakz 0:49fa55340890 59
hudakz 0:49fa55340890 60 if (!svr.start(80, net)) {
hudakz 0:49fa55340890 61 error("Server not starting !");
hudakz 0:49fa55340890 62 exit(0);
hudakz 0:49fa55340890 63 }
hudakz 0:49fa55340890 64
hudakz 0:49fa55340890 65 printf("Polling for client connections.\r\n");
hudakz 0:49fa55340890 66
hudakz 0:49fa55340890 67 while(1) {
hudakz 0:49fa55340890 68 svr.poll();
hudakz 0:49fa55340890 69 }
hudakz 0:49fa55340890 70
hudakz 0:49fa55340890 71 }