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:

main.cpp

Committer:
lester0507
Date:
2015-09-23
Revision:
12:eca9b56155c7
Parent:
11:f57e9de44f6f
Child:
14:b007595028e2

File content as of revision 12:eca9b56155c7:

#include "mbed.h"
#include "EthernetInterface.h"
#include "mbed_rpc.h"
#include "RPCCommand.h"
#include "HTTPServer.h"
#include "Formatter.h"
#include "RequestHandler.h"
#include "RPCType.h"

#include "WIFIDevice.h"

#define SERVER_PORT 80

#define AP_MODE_SSID "AP_MODE_SSID"
#define AP_MODE_PASSWORD "0123456789"

WIFIDevice wifi;
EthernetInterface eth;

void DeltaRequestHandler::handle(const RPCCommand& cmd, char *reply)
{
    char *pch;

    pch=strchr(cmd.get_func_name(),'_');
    cmd.get_func_name()[pch-cmd.get_func_name()] = '\0';
    
    //must call disconnect before switch between AP and STA mode
    eth.disconnect();
    
    wifi.setNetwork(cmd.get_func_name(),&(cmd.get_func_name()[pch-cmd.get_func_name()+1]),0);
    eth.init();
    printf("\n\rStart STA mode...\n");
    wait(1);

    if(eth.connect(40000) == 0)
        printf("AP connect OK...\n\r");
    else
        printf("AP connect FAIL...\n\r");

    printf("STA mode IP Address: %s\n\r",eth.getIPAddress());
    printf("STA mode MAC Address: %s\n\r",eth.getMACAddress());
    printf("STA mode Gateway Address: %s\n\r",eth.getGateway());
    printf("STA mode Network Mask: %s\n\r",eth.getNetworkMask());

    eth.disconnect();//must call disconnect before switch between AP and STA mod

    switch(cmd.get_type()) {
        case CREATE :
            printf("CREATE\n");
            putHandler.handle(cmd, reply);
            break;
        case DELETE :
            printf("CREATE\n");
            deleteHandler.handle(cmd, reply);
            break;
        case FUNCTION_CALL :
            printf("FUNCTION_CALL\n");
            getHandler.handle(cmd, reply);
            break;
        default :
           // printf("default\n");
            break;
    }

}

HTTPServer create_simple_server()
{
    HTTPServer srv;
    srv.add_request_handler("DELETE", new DeleteRequestHandler());
    srv.add_request_handler("GET", new GetRequestHandler());
    srv.add_request_handler("PUT", new PutRequestHandler());
    return srv;
}

HTTPServer create_interactive_server()
{
    HTTPServer srv(new InteractiveHTMLFormatter());
    srv.add_request_handler("GET", new ComplexRequestHandler());
    return srv;
}

HTTPServer create_delta_server()
{
    HTTPServer srv(new DeltaWifiSettingHTMLFormatter());
    srv.add_request_handler("GET", new DeltaRequestHandler());
    return srv;
}

int main(void)
{
    RPCType::instance().register_types();
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    printf("\r*************** Program Start ***************\n\r");

    wifi.setAccessPoint(AP_MODE_SSID, AP_MODE_PASSWORD, SECURITY_WPA2_TKIP_PSK, 2);
    if(eth.init()) {
        printf("Error while initializing the ethernet interface.\n\r");
        return -1;
    }
    if(eth.connect(60000)) {
        printf("Error while starting the ethernet interface.\n\r");
        return -1;
    }

    printf("AP Mode IP Address is %s\n\r", eth.getIPAddress());

    HTTPServer srv = create_delta_server();

    if(!srv.init(SERVER_PORT)) {
        eth.disconnect();
        return -1;
    }

    srv.run();
    return 0;
}