NNN40 change mode from AP to STA by HTTP server

Dependencies:   WIFI_API_32kRAM mbed

How to use Demo code

  • Step1: Prepare a router, and set the SSID and Password as follow
  1. SSID: "SSID"
  2. Password: "0123456789"
  • Step2: Burn demo code to NNN40 module.

You can drag and drop the sample code to NNN40 module.

  • Step3: Login module from Http server (192.168.2.1) by PC.

You can set SSID and password by browser. After clicking "confirm" button, the module will change mode from AP to STA, get IP from router

  • DEMO video:
Committer:
lester0507
Date:
Wed Oct 21 05:54:02 2015 +0000
Revision:
14:b007595028e2
Parent:
9:a9bf63017854
fix bug for ssid underline.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9e4bcb10b3e3 1 #include "HTTPServer.h"
feb11 0:9e4bcb10b3e3 2
feb11 0:9e4bcb10b3e3 3 #define INVALID_FORMATTER "No valid formatter specified"
feb11 0:9e4bcb10b3e3 4
feb11 0:9e4bcb10b3e3 5 bool cmp(char* a, char* b)
feb11 0:9e4bcb10b3e3 6 {
feb11 0:9e4bcb10b3e3 7 return strcmp(a,b) < 0;
feb11 0:9e4bcb10b3e3 8 }
feb11 4:624527ebc0fa 9
feb11 0:9e4bcb10b3e3 10 HTTPServer::HTTPServer(Formatter *f):
feb11 0:9e4bcb10b3e3 11 socket(),
feb11 0:9e4bcb10b3e3 12 handlers(&cmp),
feb11 0:9e4bcb10b3e3 13 formatter(f),
feb11 0:9e4bcb10b3e3 14 reply(),
feb11 0:9e4bcb10b3e3 15 command()
feb11 0:9e4bcb10b3e3 16 {
feb11 0:9e4bcb10b3e3 17 }
feb11 0:9e4bcb10b3e3 18
feb11 0:9e4bcb10b3e3 19 HTTPServer::~HTTPServer()
feb11 0:9e4bcb10b3e3 20 {
feb11 0:9e4bcb10b3e3 21 for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin();
feb11 0:9e4bcb10b3e3 22 itor != handlers.end();
feb11 0:9e4bcb10b3e3 23 ++itor)
feb11 0:9e4bcb10b3e3 24 delete itor->second;
feb11 0:9e4bcb10b3e3 25
feb11 0:9e4bcb10b3e3 26 if(formatter)
feb11 0:9e4bcb10b3e3 27 delete formatter;
feb11 0:9e4bcb10b3e3 28 }
feb11 0:9e4bcb10b3e3 29
feb11 0:9e4bcb10b3e3 30 bool HTTPServer::init(int port)
feb11 0:9e4bcb10b3e3 31 {
feb11 0:9e4bcb10b3e3 32 socket.set_blocking(true);
feb11 9:a9bf63017854 33 if(socket.bind(port))
feb11 9:a9bf63017854 34 {
feb11 9:a9bf63017854 35 printf("Could not bind on port %d.\n", port);
feb11 9:a9bf63017854 36 return false;
feb11 9:a9bf63017854 37 }
feb11 9:a9bf63017854 38
feb11 9:a9bf63017854 39 if(socket.listen())
feb11 9:a9bf63017854 40 {
feb11 9:a9bf63017854 41 printf("Could not listen %d\n", port);
feb11 9:a9bf63017854 42 return false;
feb11 9:a9bf63017854 43 }
feb11 9:a9bf63017854 44
feb11 9:a9bf63017854 45 return true;
feb11 0:9e4bcb10b3e3 46 }
feb11 0:9e4bcb10b3e3 47
feb11 0:9e4bcb10b3e3 48 void HTTPServer::run()
feb11 0:9e4bcb10b3e3 49 {
feb11 0:9e4bcb10b3e3 50 TCPSocketConnection c;
feb11 0:9e4bcb10b3e3 51 while(true)
feb11 0:9e4bcb10b3e3 52 {
feb11 0:9e4bcb10b3e3 53 while(socket.accept(c));
feb11 0:9e4bcb10b3e3 54 c.set_blocking(false, 1000);
feb11 0:9e4bcb10b3e3 55 while(c.is_connected())
feb11 0:9e4bcb10b3e3 56 {
feb11 0:9e4bcb10b3e3 57 char buffer[512];
feb11 0:9e4bcb10b3e3 58 int n = c.receive_all(buffer, sizeof(buffer)-1);
feb11 0:9e4bcb10b3e3 59 if(n == 0)
feb11 0:9e4bcb10b3e3 60 {
feb11 0:9e4bcb10b3e3 61 c.close();
feb11 0:9e4bcb10b3e3 62 break;
feb11 0:9e4bcb10b3e3 63 }
feb11 0:9e4bcb10b3e3 64 else if(n != -1)
feb11 0:9e4bcb10b3e3 65 {
feb11 7:838d7ea07e18 66 printf("Received data\n");
feb11 0:9e4bcb10b3e3 67 buffer[n] = '\0';
feb11 0:9e4bcb10b3e3 68 handle_request(buffer);
feb11 0:9e4bcb10b3e3 69 if(formatter != NULL)
feb11 0:9e4bcb10b3e3 70 {
feb11 7:838d7ea07e18 71 printf("Sending data...");
feb11 0:9e4bcb10b3e3 72 char *page = formatter->get_page(reply);
feb11 0:9e4bcb10b3e3 73 do
feb11 0:9e4bcb10b3e3 74 {
feb11 0:9e4bcb10b3e3 75 c.send(page, strlen(page)+1);
feb11 0:9e4bcb10b3e3 76 page = formatter->get_page(reply);
feb11 0:9e4bcb10b3e3 77 }while(strlen(page)>0);
feb11 7:838d7ea07e18 78 printf("done\n");
feb11 0:9e4bcb10b3e3 79 }
feb11 0:9e4bcb10b3e3 80 else
feb11 0:9e4bcb10b3e3 81 c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1);
feb11 0:9e4bcb10b3e3 82 }
feb11 7:838d7ea07e18 83 else
feb11 7:838d7ea07e18 84 printf("Error while receiving data\n");
feb11 0:9e4bcb10b3e3 85 }
feb11 0:9e4bcb10b3e3 86 }
feb11 0:9e4bcb10b3e3 87 }
feb11 0:9e4bcb10b3e3 88
feb11 0:9e4bcb10b3e3 89 void HTTPServer::handle_request(char *buffer)
feb11 0:9e4bcb10b3e3 90 {
feb11 0:9e4bcb10b3e3 91 char *request_type = strtok(buffer, " ");
feb11 0:9e4bcb10b3e3 92 char *request = strtok(NULL, " ");
feb11 0:9e4bcb10b3e3 93
feb11 0:9e4bcb10b3e3 94 reply[0] = '\0';
feb11 0:9e4bcb10b3e3 95 if(!strcmp(request, "/"))
feb11 0:9e4bcb10b3e3 96 return;
feb11 0:9e4bcb10b3e3 97
feb11 0:9e4bcb10b3e3 98 if(!command.decode(request))
feb11 0:9e4bcb10b3e3 99 {
feb11 0:9e4bcb10b3e3 100 strcat(reply, "Malformed request");
feb11 0:9e4bcb10b3e3 101 return;
feb11 0:9e4bcb10b3e3 102 }
feb11 0:9e4bcb10b3e3 103
feb11 0:9e4bcb10b3e3 104 std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type);
feb11 0:9e4bcb10b3e3 105 if(itor == handlers.end())
feb11 0:9e4bcb10b3e3 106 {
feb11 8:464abd184b7b 107 strcat(reply, "No request handler found for this type of request.");
feb11 0:9e4bcb10b3e3 108 return;
feb11 0:9e4bcb10b3e3 109 }
feb11 0:9e4bcb10b3e3 110 if(itor->second != NULL)
feb11 0:9e4bcb10b3e3 111 itor->second->handle(command, reply);
feb11 0:9e4bcb10b3e3 112 else
feb11 0:9e4bcb10b3e3 113 strcat(reply, "Invalid request handler");
feb11 0:9e4bcb10b3e3 114 }
feb11 0:9e4bcb10b3e3 115
feb11 0:9e4bcb10b3e3 116 void HTTPServer::add_request_handler(char *name, RequestHandler* handler)
feb11 0:9e4bcb10b3e3 117 {
feb11 0:9e4bcb10b3e3 118 handlers[name] = handler;
feb11 0:9e4bcb10b3e3 119 }
feb11 0:9e4bcb10b3e3 120