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:
13:f3d681e5b6c6
fix bug for ssid underline.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9e4bcb10b3e3 1 #include "Formatter.h"
feb11 0:9e4bcb10b3e3 2 #include "mbed.h"
feb11 0:9e4bcb10b3e3 3 #include "RPCObjectManager.h"
feb11 0:9e4bcb10b3e3 4 #include "EthernetInterface.h"
feb11 0:9e4bcb10b3e3 5
feb11 0:9e4bcb10b3e3 6 const char *SIMPLE_HTML_CODE = "\
feb11 0:9e4bcb10b3e3 7 <!DOCTYPE html>\
feb11 0:9e4bcb10b3e3 8 <html>\
feb11 4:624527ebc0fa 9 <head>\
feb11 4:624527ebc0fa 10 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
feb11 4:624527ebc0fa 11 <title>TCP Server</title>\
feb11 4:624527ebc0fa 12 </head>\
feb11 4:624527ebc0fa 13 <body>";
feb11 0:9e4bcb10b3e3 14
feb11 0:9e4bcb10b3e3 15
feb11 0:9e4bcb10b3e3 16 const char* INTERACTIVE_HTML_CODE_1 = "\
feb11 4:624527ebc0fa 17 <!DOCTYPE html> \
feb11 0:9e4bcb10b3e3 18 <html>\
feb11 4:624527ebc0fa 19 <head>\
feb11 4:624527ebc0fa 20 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
feb11 4:624527ebc0fa 21 <title>TCP Server</title>\
feb11 4:624527ebc0fa 22 <script type=\"text/javascript\">\
feb11 4:624527ebc0fa 23 var ip = \"%s\";\
feb11 4:624527ebc0fa 24 function submitCreateForm()\
feb11 4:624527ebc0fa 25 {\
feb11 4:624527ebc0fa 26 var list = document.getElementById(\"type\");\
feb11 4:624527ebc0fa 27 var type = list.options[list.selectedIndex].value;\
feb11 4:624527ebc0fa 28 var name = document.getElementById(\"name\").value;\
feb11 10:8b4c3d605bf0 29 if(name === \"\") \
feb11 10:8b4c3d605bf0 30 return;\
feb11 4:624527ebc0fa 31 var arg = document.getElementById(\"arg\").value;\
feb11 4:624527ebc0fa 32 var url;\
feb11 6:d03e189ebbfe 33 if(arg === \"\") url = \"http://\" + ip + type + \"new?name=\" + name;\
feb11 6:d03e189ebbfe 34 else url = \"http://\" + ip + type + \"new?arg=\" + arg + \"&name=\" + name;\
feb11 4:624527ebc0fa 35 location.href= url;\
feb11 4:624527ebc0fa 36 }\
feb11 4:624527ebc0fa 37 function submitCallFuncForm()\
feb11 4:624527ebc0fa 38 {\
feb11 4:624527ebc0fa 39 var command = document.getElementById(\"command\").value;\
feb11 10:8b4c3d605bf0 40 if(command === \"\") \
feb11 10:8b4c3d605bf0 41 return; \
feb11 4:624527ebc0fa 42 var tmp = command.split(\' \');\
feb11 4:624527ebc0fa 43 var url = tmp[0];\
feb11 4:624527ebc0fa 44 if(tmp.length > 1)\
feb11 4:624527ebc0fa 45 url += \"?\";\
feb11 4:624527ebc0fa 46 for(var i = 1; i < tmp.length; ++i)\
feb11 4:624527ebc0fa 47 {\
feb11 4:624527ebc0fa 48 url += \"arg\" + i + \"=\" + tmp[i];\
feb11 4:624527ebc0fa 49 if(i+1 < tmp.length)\
feb11 4:624527ebc0fa 50 url += \"&\";\
feb11 4:624527ebc0fa 51 }\
feb11 4:624527ebc0fa 52 location.href = url;\
feb11 4:624527ebc0fa 53 }\
feb11 4:624527ebc0fa 54 </script>\
feb11 4:624527ebc0fa 55 </head> \
feb11 0:9e4bcb10b3e3 56 <body>";
lester0507 12:eca9b56155c7 57
feb11 0:9e4bcb10b3e3 58 const char* INTERACTIVE_HTML_CODE_2 = "<h3>Create Object :</h3>\
feb11 9:a9bf63017854 59 <form>\
feb11 4:624527ebc0fa 60 Type: <select id=\"type\">\
feb11 4:624527ebc0fa 61 <option value=\"/DigitalOut/\">DigitalOut</option>\
feb11 4:624527ebc0fa 62 <option value=\"/DigitalIn/\">DigitalIn</option>\
feb11 4:624527ebc0fa 63 <option value=\"/DigitalInOut/\">DigitalInOut</option>\
feb11 4:624527ebc0fa 64 <option value=\"/PwmOut/\">PwmOut</option>\
feb11 4:624527ebc0fa 65 <option value=\"/Timer/\">Timer</option>\
feb11 4:624527ebc0fa 66 </select><br>\
feb11 4:624527ebc0fa 67 name: <input type=\"text\" id=\"name\"><br>\
feb11 4:624527ebc0fa 68 arg(optional): <input type=\"text\" id=\"arg\">\
feb11 4:624527ebc0fa 69 <p><input type=\"button\" value=\"Create\" onclick=\"javascript:submitCreateForm();\"></p>\
feb11 4:624527ebc0fa 70 </form> \
feb11 4:624527ebc0fa 71 \
feb11 4:624527ebc0fa 72 <h3>Call a function :</h3>\
feb11 4:624527ebc0fa 73 <p>Enter an RPC command.</p>\
feb11 9:a9bf63017854 74 <form>\
feb11 10:8b4c3d605bf0 75 Command: <input type= \"text\" id=\"command\" maxlength=127><br>\
feb11 9:a9bf63017854 76 <p><input type=\"button\" value=\"Send\" onclick=\"javascript:submitCallFuncForm();\"></p><br>\
feb11 4:624527ebc0fa 77 </form>\
feb11 4:624527ebc0fa 78 </body> \
feb11 0:9e4bcb10b3e3 79 </html>";
feb11 0:9e4bcb10b3e3 80
lester0507 12:eca9b56155c7 81 const char* DELRA_WIFI_SETTING_HTML_CODE_0 = "<!DOCTYPE html> \
lester0507 12:eca9b56155c7 82 <html>\
lester0507 12:eca9b56155c7 83 <head>\
lester0507 12:eca9b56155c7 84 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\
lester0507 12:eca9b56155c7 85 <title>NNN40 Wifi Module Setting</title>\
lester0507 12:eca9b56155c7 86 <style>\
lester0507 12:eca9b56155c7 87 #background {\
lester0507 12:eca9b56155c7 88 background: -moz-linear-gradient(top,#ffffff,#0099FF);\
lester0507 12:eca9b56155c7 89 border-width: 3px;\
lester0507 12:eca9b56155c7 90 border-style: ridge;\
lester0507 12:eca9b56155c7 91 width: 500px;\
lester0507 12:eca9b56155c7 92 height: 700px;\
lester0507 12:eca9b56155c7 93 border-color: #000055;\
lester0507 12:eca9b56155c7 94 padding: 20px\
lester0507 12:eca9b56155c7 95 }#title {color: #444444;}#input {margin: 10px;}\
lester0507 12:eca9b56155c7 96 </style><script type=\"application/javascript\">\
lester0507 12:eca9b56155c7 97 function reset() {\
lester0507 12:eca9b56155c7 98 document.getElementById(\"defaultchs\").checked = true;\
lester0507 12:eca9b56155c7 99 document.getElementById(\"ssid\").value = \"\";\
lester0507 12:eca9b56155c7 100 document.getElementById(\"password\").value = \"\";\
lester0507 12:eca9b56155c7 101 }function send() {var ip = \"192.168.2.1/WebSetting/\";\
lester0507 12:eca9b56155c7 102 var ssid = document.getElementById(\"ssid\").value;\
lester0507 12:eca9b56155c7 103 var pwd = document.getElementById(\"password\").value;\
lester0507 14:b007595028e2 104 location.href = \"http://\"+ip+ssid +\"/\"+pwd;}\
lester0507 12:eca9b56155c7 105 </script></head><body>";
lester0507 12:eca9b56155c7 106
lester0507 12:eca9b56155c7 107 const char* DELRA_WIFI_SETTING_HTML_CODE_1 = "<div id=\"background\">\
lester0507 12:eca9b56155c7 108 <div align=\"center\">\
lester0507 12:eca9b56155c7 109 <img src=\"https://upload.wikimedia.org/wikipedia/commons/8/8c/DELTA_Electronics_Logo.png\" width=\"354\" height=\"109\";\>\
lester0507 12:eca9b56155c7 110 </div><h1 id=\"title\"align=\"center\">WIFI SETTING</h1>\
lester0507 12:eca9b56155c7 111 <HR align=\"center\" width=\"90%\"><form id=\"input\" method=\"get\">\
lester0507 12:eca9b56155c7 112 <font size=\"6\" class=\"font\">Security:</font>\
lester0507 12:eca9b56155c7 113 <br><input id=\"defaultchs\" type=\"radio\" name=\"security\" value=\"NONE\" checked>\
lester0507 12:eca9b56155c7 114 NONE<input type=\"radio\" name=\"security\" value=\"WEP\">\
lester0507 12:eca9b56155c7 115 WEP<input type=\"radio\" name=\"security\" value=\"WPA\">\
lester0507 12:eca9b56155c7 116 WPA<input type=\"radio\" name=\"security\" value=\"WPA2\">\
lester0507 12:eca9b56155c7 117 WPA2<br> <br><font size=\"6\" class=\"font\">SSID:</font>\
lester0507 12:eca9b56155c7 118 <br><input id=\"ssid\" size=\"20\" type=\"text\" name=\"ssid\">\
lester0507 12:eca9b56155c7 119 <br><br><font size=\"6\" class=\"font\">Password:</font>\
lester0507 12:eca9b56155c7 120 <br><input type=\"text\" id=\"password\" name=\"pwd\">\
lester0507 12:eca9b56155c7 121 <div align=\"center\"><input class=\"font\" type=\"button\" style=\"width: 160px; height: 80px; margin: 50px 40px 0px 0px; font-size: 25px\" value=\"Confirm\" onclick=\"send()\" />\
lester0507 12:eca9b56155c7 122 <input class=\"font\" type=\"button\" style=\"width: 160px; height: 80px; margin: 50px 40px 0px 0px; font-size: 25px\" value=\"Reset\" onclick=\"reset()\" />\
lester0507 13:f3d681e5b6c6 123 </div></form></div></body></html>" ;
lester0507 12:eca9b56155c7 124
feb11 0:9e4bcb10b3e3 125 static char chunk[1024];
lester0507 12:eca9b56155c7 126
feb11 0:9e4bcb10b3e3 127 Formatter::Formatter(int nb):
lester0507 12:eca9b56155c7 128 currentChunk(0),
lester0507 12:eca9b56155c7 129 nbChunk(nb)
feb11 0:9e4bcb10b3e3 130 {
lester0507 12:eca9b56155c7 131 }
feb11 0:9e4bcb10b3e3 132
feb11 0:9e4bcb10b3e3 133 char* Formatter::get_page(char *reply)
feb11 0:9e4bcb10b3e3 134 {
feb11 0:9e4bcb10b3e3 135 chunk[0] = '\0';
feb11 0:9e4bcb10b3e3 136
lester0507 12:eca9b56155c7 137 if(currentChunk < nbChunk) {
feb11 0:9e4bcb10b3e3 138 get_chunk(currentChunk, reply);
feb11 0:9e4bcb10b3e3 139 currentChunk++;
lester0507 12:eca9b56155c7 140 } else
feb11 0:9e4bcb10b3e3 141 currentChunk = 0;
lester0507 12:eca9b56155c7 142
feb11 0:9e4bcb10b3e3 143 return chunk;
lester0507 12:eca9b56155c7 144 }
feb11 0:9e4bcb10b3e3 145
feb11 0:9e4bcb10b3e3 146 void Formatter::get_chunk(const int c, char *reply)
feb11 0:9e4bcb10b3e3 147 {
feb11 0:9e4bcb10b3e3 148 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 149 }
feb11 0:9e4bcb10b3e3 150
feb11 0:9e4bcb10b3e3 151 SimpleHTMLFormatter::SimpleHTMLFormatter():
lester0507 12:eca9b56155c7 152 Formatter()
feb11 0:9e4bcb10b3e3 153 {
feb11 0:9e4bcb10b3e3 154 }
feb11 0:9e4bcb10b3e3 155
feb11 0:9e4bcb10b3e3 156 void SimpleHTMLFormatter::get_chunk(const int c, char* reply)
feb11 0:9e4bcb10b3e3 157 {
feb11 0:9e4bcb10b3e3 158 strcat(chunk, SIMPLE_HTML_CODE);
lester0507 12:eca9b56155c7 159
lester0507 12:eca9b56155c7 160 if(reply != NULL && strlen(reply) != 0) {
feb11 0:9e4bcb10b3e3 161 strcat(chunk, "RPC reply : ");
feb11 0:9e4bcb10b3e3 162 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 163 }
lester0507 12:eca9b56155c7 164
lester0507 12:eca9b56155c7 165 if(!RPCObjectManager::instance().is_empty()) {
feb11 0:9e4bcb10b3e3 166 strcat(chunk, "<ul>");
feb11 0:9e4bcb10b3e3 167 for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
lester0507 12:eca9b56155c7 168 itor != RPCObjectManager::instance().end();
lester0507 12:eca9b56155c7 169 ++itor) {
feb11 0:9e4bcb10b3e3 170 strcat(chunk, "<li>");
feb11 0:9e4bcb10b3e3 171 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 172 strcat(chunk, "</li>");
feb11 0:9e4bcb10b3e3 173 }
feb11 0:9e4bcb10b3e3 174 strcat(chunk, "</ul>");
feb11 0:9e4bcb10b3e3 175 }
feb11 0:9e4bcb10b3e3 176 strcat(chunk, "</body></html>");
feb11 0:9e4bcb10b3e3 177 }
feb11 0:9e4bcb10b3e3 178
feb11 0:9e4bcb10b3e3 179 InteractiveHTMLFormatter::InteractiveHTMLFormatter():
lester0507 12:eca9b56155c7 180 Formatter(3)
feb11 0:9e4bcb10b3e3 181 {
feb11 0:9e4bcb10b3e3 182 }
feb11 0:9e4bcb10b3e3 183
feb11 0:9e4bcb10b3e3 184 void InteractiveHTMLFormatter::get_chunk(const int c, char *reply)
feb11 0:9e4bcb10b3e3 185 {
feb11 0:9e4bcb10b3e3 186 if(c == 0)
feb11 0:9e4bcb10b3e3 187 sprintf(chunk, INTERACTIVE_HTML_CODE_1, EthernetInterface::getIPAddress());
feb11 4:624527ebc0fa 188
lester0507 12:eca9b56155c7 189 else if(c == 1) {
lester0507 12:eca9b56155c7 190 if(reply != NULL && strlen(reply) != 0) {
feb11 0:9e4bcb10b3e3 191 strcat(chunk, "RPC reply : ");
feb11 0:9e4bcb10b3e3 192 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 193 }
lester0507 12:eca9b56155c7 194 if(!RPCObjectManager::instance().is_empty()) {
feb11 0:9e4bcb10b3e3 195 strcat(chunk, "<p>Objects created :</p>");
feb11 0:9e4bcb10b3e3 196
feb11 0:9e4bcb10b3e3 197 strcat(chunk, "<ul>");
feb11 0:9e4bcb10b3e3 198 for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
lester0507 12:eca9b56155c7 199 itor != RPCObjectManager::instance().end();
lester0507 12:eca9b56155c7 200 ++itor) {
feb11 0:9e4bcb10b3e3 201 strcat(chunk, "<li>");
feb11 0:9e4bcb10b3e3 202 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 203 strcat(chunk, " (<a href=\"http://");
feb11 0:9e4bcb10b3e3 204 strcat(chunk, EthernetInterface::getIPAddress());
feb11 0:9e4bcb10b3e3 205 strcat(chunk, "/");
feb11 0:9e4bcb10b3e3 206 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 207 strcat(chunk, "/delete\">delete</a>)");
feb11 0:9e4bcb10b3e3 208 strcat(chunk, "</li>");
feb11 0:9e4bcb10b3e3 209 }
feb11 0:9e4bcb10b3e3 210 strcat(chunk, "</ul>");
feb11 0:9e4bcb10b3e3 211 }
feb11 0:9e4bcb10b3e3 212 strcat(chunk, " ");
lester0507 12:eca9b56155c7 213 } else if(c == 2)
feb11 0:9e4bcb10b3e3 214 strcat(chunk, INTERACTIVE_HTML_CODE_2);
feb11 0:9e4bcb10b3e3 215 }
feb11 0:9e4bcb10b3e3 216
lester0507 12:eca9b56155c7 217 DeltaWifiSettingHTMLFormatter::DeltaWifiSettingHTMLFormatter():
lester0507 12:eca9b56155c7 218 Formatter(2)
lester0507 12:eca9b56155c7 219 {
lester0507 12:eca9b56155c7 220 }
lester0507 12:eca9b56155c7 221 void DeltaWifiSettingHTMLFormatter::get_chunk(const int c, char *reply)
lester0507 12:eca9b56155c7 222 {
lester0507 12:eca9b56155c7 223 if(c == 0)
lester0507 12:eca9b56155c7 224 strcat(chunk, DELRA_WIFI_SETTING_HTML_CODE_0);
feb11 0:9e4bcb10b3e3 225
lester0507 12:eca9b56155c7 226
lester0507 12:eca9b56155c7 227 else if(c == 1) {
lester0507 12:eca9b56155c7 228 strcat(chunk,DELRA_WIFI_SETTING_HTML_CODE_1);
lester0507 12:eca9b56155c7 229
lester0507 12:eca9b56155c7 230 }
lester0507 12:eca9b56155c7 231 }