NNN40 run a HTTP server with RPC using Soft AP mode
Dependencies: WIFI_API_32kRAM mbed
Fork of HTTP-Server by
The sample code will run as a WiFi Soft AP mode with given AP configuration setting including SSID name and password. IP address (fixed to 192.168.2.1 for the current version of WIFI_API)of AP router will be print out once Soft AP mode is operating.
User can open their web browser and go to http://192.168.2.1/. and have a try on switch on a led. Firstly, we need to create an object to control a led
Then, led can be switch on using RPC command
More information cab be found from the links below
https://developer.mbed.org/users/feb11/code/HTTP-Server/
https://developer.mbed.org/cookbook/Interfacing-Using-RPC
main.cpp@9:a9bf63017854, 2013-07-18 (annotated)
- Committer:
- feb11
- Date:
- Thu Jul 18 09:39:35 2013 +0000
- Revision:
- 9:a9bf63017854
- Parent:
- 3:fb0a778f2480
- Child:
- 11:f57e9de44f6f
Improved error message
Who changed what in which revision?
User | Revision | Line number | New 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 | |
feb11 | 0:9e4bcb10b3e3 | 10 | #define SERVER_PORT 80 |
feb11 | 0:9e4bcb10b3e3 | 11 | |
feb11 | 0:9e4bcb10b3e3 | 12 | HTTPServer create_simple_server() |
feb11 | 0:9e4bcb10b3e3 | 13 | { |
feb11 | 0:9e4bcb10b3e3 | 14 | HTTPServer srv; |
feb11 | 0:9e4bcb10b3e3 | 15 | srv.add_request_handler("DELETE", new DeleteRequestHandler()); |
feb11 | 0:9e4bcb10b3e3 | 16 | srv.add_request_handler("GET", new GetRequestHandler()); |
feb11 | 0:9e4bcb10b3e3 | 17 | srv.add_request_handler("PUT", new PutRequestHandler()); |
feb11 | 0:9e4bcb10b3e3 | 18 | return srv; |
feb11 | 0:9e4bcb10b3e3 | 19 | } |
feb11 | 0:9e4bcb10b3e3 | 20 | |
feb11 | 0:9e4bcb10b3e3 | 21 | HTTPServer create_interactive_server() |
feb11 | 0:9e4bcb10b3e3 | 22 | { |
feb11 | 0:9e4bcb10b3e3 | 23 | HTTPServer srv(new InteractiveHTMLFormatter()); |
feb11 | 0:9e4bcb10b3e3 | 24 | srv.add_request_handler("GET", new ComplexRequestHandler()); |
feb11 | 0:9e4bcb10b3e3 | 25 | return srv; |
feb11 | 0:9e4bcb10b3e3 | 26 | } |
feb11 | 0:9e4bcb10b3e3 | 27 | |
feb11 | 0:9e4bcb10b3e3 | 28 | int main(void) |
feb11 | 0:9e4bcb10b3e3 | 29 | { |
feb11 | 0:9e4bcb10b3e3 | 30 | RPCType::instance().register_types(); |
feb11 | 0:9e4bcb10b3e3 | 31 | |
feb11 | 0:9e4bcb10b3e3 | 32 | EthernetInterface eth; |
feb11 | 9:a9bf63017854 | 33 | if(eth.init()) |
feb11 | 9:a9bf63017854 | 34 | { |
feb11 | 9:a9bf63017854 | 35 | printf("Error while initializing the ethernet interface.\n"); |
feb11 | 9:a9bf63017854 | 36 | return -1; |
feb11 | 9:a9bf63017854 | 37 | } |
feb11 | 9:a9bf63017854 | 38 | if(eth.connect()) |
feb11 | 9:a9bf63017854 | 39 | { |
feb11 | 9:a9bf63017854 | 40 | printf("Error while starting the ethernet interface.\n"); |
feb11 | 9:a9bf63017854 | 41 | return -1; |
feb11 | 9:a9bf63017854 | 42 | } |
feb11 | 9:a9bf63017854 | 43 | |
feb11 | 0:9e4bcb10b3e3 | 44 | printf("IP Address is %s\n", eth.getIPAddress()); |
feb11 | 0:9e4bcb10b3e3 | 45 | |
feb11 | 3:fb0a778f2480 | 46 | HTTPServer srv = create_interactive_server(); |
feb11 | 0:9e4bcb10b3e3 | 47 | |
feb11 | 0:9e4bcb10b3e3 | 48 | if(!srv.init(SERVER_PORT)) |
feb11 | 0:9e4bcb10b3e3 | 49 | { |
feb11 | 0:9e4bcb10b3e3 | 50 | eth.disconnect(); |
feb11 | 0:9e4bcb10b3e3 | 51 | return -1; |
feb11 | 0:9e4bcb10b3e3 | 52 | } |
feb11 | 0:9e4bcb10b3e3 | 53 | |
feb11 | 0:9e4bcb10b3e3 | 54 | srv.run(); |
feb11 | 0:9e4bcb10b3e3 | 55 | |
feb11 | 0:9e4bcb10b3e3 | 56 | return 0; |
feb11 | 0:9e4bcb10b3e3 | 57 | } |
feb11 | 0:9e4bcb10b3e3 | 58 |