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 Sep 23 03:08:49 2015 +0000
Revision:
12:eca9b56155c7
Parent:
11:f57e9de44f6f
Child:
14:b007595028e2
fork from NNN40_RPC_HTTP-Server project, and extend project that change AP mode to STA mode by HTTP server .

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9e4bcb10b3e3 1 #include "mbed.h"
feb11 0:9e4bcb10b3e3 2 #include "EthernetInterface.h"
feb11 0:9e4bcb10b3e3 3 #include "mbed_rpc.h"
feb11 0:9e4bcb10b3e3 4 #include "RPCCommand.h"
feb11 0:9e4bcb10b3e3 5 #include "HTTPServer.h"
feb11 0:9e4bcb10b3e3 6 #include "Formatter.h"
feb11 0:9e4bcb10b3e3 7 #include "RequestHandler.h"
feb11 0:9e4bcb10b3e3 8 #include "RPCType.h"
feb11 0:9e4bcb10b3e3 9
wgd8700 11:f57e9de44f6f 10 #include "WIFIDevice.h"
wgd8700 11:f57e9de44f6f 11
feb11 0:9e4bcb10b3e3 12 #define SERVER_PORT 80
feb11 0:9e4bcb10b3e3 13
lester0507 12:eca9b56155c7 14 #define AP_MODE_SSID "AP_MODE_SSID"
lester0507 12:eca9b56155c7 15 #define AP_MODE_PASSWORD "0123456789"
lester0507 12:eca9b56155c7 16
wgd8700 11:f57e9de44f6f 17 WIFIDevice wifi;
lester0507 12:eca9b56155c7 18 EthernetInterface eth;
lester0507 12:eca9b56155c7 19
lester0507 12:eca9b56155c7 20 void DeltaRequestHandler::handle(const RPCCommand& cmd, char *reply)
lester0507 12:eca9b56155c7 21 {
lester0507 12:eca9b56155c7 22 char *pch;
lester0507 12:eca9b56155c7 23
lester0507 12:eca9b56155c7 24 pch=strchr(cmd.get_func_name(),'_');
lester0507 12:eca9b56155c7 25 cmd.get_func_name()[pch-cmd.get_func_name()] = '\0';
lester0507 12:eca9b56155c7 26
lester0507 12:eca9b56155c7 27 //must call disconnect before switch between AP and STA mode
lester0507 12:eca9b56155c7 28 eth.disconnect();
lester0507 12:eca9b56155c7 29
lester0507 12:eca9b56155c7 30 wifi.setNetwork(cmd.get_func_name(),&(cmd.get_func_name()[pch-cmd.get_func_name()+1]),0);
lester0507 12:eca9b56155c7 31 eth.init();
lester0507 12:eca9b56155c7 32 printf("\n\rStart STA mode...\n");
lester0507 12:eca9b56155c7 33 wait(1);
lester0507 12:eca9b56155c7 34
lester0507 12:eca9b56155c7 35 if(eth.connect(40000) == 0)
lester0507 12:eca9b56155c7 36 printf("AP connect OK...\n\r");
lester0507 12:eca9b56155c7 37 else
lester0507 12:eca9b56155c7 38 printf("AP connect FAIL...\n\r");
lester0507 12:eca9b56155c7 39
lester0507 12:eca9b56155c7 40 printf("STA mode IP Address: %s\n\r",eth.getIPAddress());
lester0507 12:eca9b56155c7 41 printf("STA mode MAC Address: %s\n\r",eth.getMACAddress());
lester0507 12:eca9b56155c7 42 printf("STA mode Gateway Address: %s\n\r",eth.getGateway());
lester0507 12:eca9b56155c7 43 printf("STA mode Network Mask: %s\n\r",eth.getNetworkMask());
lester0507 12:eca9b56155c7 44
lester0507 12:eca9b56155c7 45 eth.disconnect();//must call disconnect before switch between AP and STA mod
lester0507 12:eca9b56155c7 46
lester0507 12:eca9b56155c7 47 switch(cmd.get_type()) {
lester0507 12:eca9b56155c7 48 case CREATE :
lester0507 12:eca9b56155c7 49 printf("CREATE\n");
lester0507 12:eca9b56155c7 50 putHandler.handle(cmd, reply);
lester0507 12:eca9b56155c7 51 break;
lester0507 12:eca9b56155c7 52 case DELETE :
lester0507 12:eca9b56155c7 53 printf("CREATE\n");
lester0507 12:eca9b56155c7 54 deleteHandler.handle(cmd, reply);
lester0507 12:eca9b56155c7 55 break;
lester0507 12:eca9b56155c7 56 case FUNCTION_CALL :
lester0507 12:eca9b56155c7 57 printf("FUNCTION_CALL\n");
lester0507 12:eca9b56155c7 58 getHandler.handle(cmd, reply);
lester0507 12:eca9b56155c7 59 break;
lester0507 12:eca9b56155c7 60 default :
lester0507 12:eca9b56155c7 61 // printf("default\n");
lester0507 12:eca9b56155c7 62 break;
lester0507 12:eca9b56155c7 63 }
lester0507 12:eca9b56155c7 64
lester0507 12:eca9b56155c7 65 }
wgd8700 11:f57e9de44f6f 66
feb11 0:9e4bcb10b3e3 67 HTTPServer create_simple_server()
lester0507 12:eca9b56155c7 68 {
feb11 0:9e4bcb10b3e3 69 HTTPServer srv;
feb11 0:9e4bcb10b3e3 70 srv.add_request_handler("DELETE", new DeleteRequestHandler());
feb11 0:9e4bcb10b3e3 71 srv.add_request_handler("GET", new GetRequestHandler());
feb11 0:9e4bcb10b3e3 72 srv.add_request_handler("PUT", new PutRequestHandler());
feb11 0:9e4bcb10b3e3 73 return srv;
feb11 0:9e4bcb10b3e3 74 }
feb11 0:9e4bcb10b3e3 75
feb11 0:9e4bcb10b3e3 76 HTTPServer create_interactive_server()
feb11 0:9e4bcb10b3e3 77 {
feb11 0:9e4bcb10b3e3 78 HTTPServer srv(new InteractiveHTMLFormatter());
feb11 0:9e4bcb10b3e3 79 srv.add_request_handler("GET", new ComplexRequestHandler());
feb11 0:9e4bcb10b3e3 80 return srv;
feb11 0:9e4bcb10b3e3 81 }
feb11 0:9e4bcb10b3e3 82
lester0507 12:eca9b56155c7 83 HTTPServer create_delta_server()
lester0507 12:eca9b56155c7 84 {
lester0507 12:eca9b56155c7 85 HTTPServer srv(new DeltaWifiSettingHTMLFormatter());
lester0507 12:eca9b56155c7 86 srv.add_request_handler("GET", new DeltaRequestHandler());
lester0507 12:eca9b56155c7 87 return srv;
lester0507 12:eca9b56155c7 88 }
lester0507 12:eca9b56155c7 89
feb11 0:9e4bcb10b3e3 90 int main(void)
feb11 0:9e4bcb10b3e3 91 {
lester0507 12:eca9b56155c7 92 RPCType::instance().register_types();
lester0507 12:eca9b56155c7 93 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
lester0507 12:eca9b56155c7 94 printf("\r*************** Program Start ***************\n\r");
feb11 0:9e4bcb10b3e3 95
lester0507 12:eca9b56155c7 96 wifi.setAccessPoint(AP_MODE_SSID, AP_MODE_PASSWORD, SECURITY_WPA2_TKIP_PSK, 2);
lester0507 12:eca9b56155c7 97 if(eth.init()) {
lester0507 12:eca9b56155c7 98 printf("Error while initializing the ethernet interface.\n\r");
feb11 9:a9bf63017854 99 return -1;
feb11 9:a9bf63017854 100 }
lester0507 12:eca9b56155c7 101 if(eth.connect(60000)) {
lester0507 12:eca9b56155c7 102 printf("Error while starting the ethernet interface.\n\r");
feb11 9:a9bf63017854 103 return -1;
feb11 9:a9bf63017854 104 }
lester0507 12:eca9b56155c7 105
lester0507 12:eca9b56155c7 106 printf("AP Mode IP Address is %s\n\r", eth.getIPAddress());
feb11 0:9e4bcb10b3e3 107
lester0507 12:eca9b56155c7 108 HTTPServer srv = create_delta_server();
lester0507 12:eca9b56155c7 109
lester0507 12:eca9b56155c7 110 if(!srv.init(SERVER_PORT)) {
feb11 0:9e4bcb10b3e3 111 eth.disconnect();
feb11 0:9e4bcb10b3e3 112 return -1;
feb11 0:9e4bcb10b3e3 113 }
feb11 0:9e4bcb10b3e3 114
feb11 0:9e4bcb10b3e3 115 srv.run();
feb11 0:9e4bcb10b3e3 116 return 0;
feb11 0:9e4bcb10b3e3 117 }