Photon SoftAP Inputs

This would be one way to add two extra inputs to the index.html (just replace the original with this)

const char index_html[] = 
"<!DOCTYPE html>"
"<html>"
"<head>"
"  <meta name='viewport' content='width=device-width, initial-scale=1'>"
"  <title>Setup your device</title>"
"  <link rel='stylesheet' type='text/css' href='style.css'>"
"</head>"
"<body>"
"  <h1>Connect me to your WiFi!</h1>"
"  <h3>My device ID:</h3>"
"  <input type=text id='device-id' size='25' value='' disabled />"
"  <button type='button' class='input-helper' id='copy-button'>Copy</button>"
"  <div id='scan-div'>"
"    <h3>Scan for visible WiFi networks</h3>"
"    <button id='scan-button' type='button'>Scan</button>"
"  </div>"
"  <div id='networks-div'></div>"
"  <div id='connect-div' style='display: none'>"
"    <p>Don't see your network? Move me closer to your router, then re-scan.</p>"
"    <form id='connect-form'>"
"      <input type='password' id='password' size='25' placeholder='password' />"
"      <button type='button' class='input-helper' id='show-button'>Show</button>"
"      <button type='submit' id='connect-button'>Connect</button>"
"    </form>"
"  </div>"
"<!-- vvvv  THIS IS THE NEW PART  vvvv -->"
"  <div id='extra-div' >"
"    <p>Add your own data here</p>"
"    <form id='extras' >"
"      <table width='100%'>"
"        <tr><td><input type='text' id='str1' name='str1' size='35' /></td></tr>"
"        <tr><td><input type='text' id='str2' name='str2' size='35' /></td></tr>"
"        <tr><td><button type='submit' id='store'>Store Data</button></td></tr>"
"      </table>"
"    </form>"
"  </div>"
"<!-- ^^^^  THIS IS THE NEW PART  ^^^^ -->"
"  <script src='rsa-utils/jsbn_1.js'></script>"
"  <script src='rsa-utils/jsbn_2.js'></script>"
"  <script src='rsa-utils/prng4.js'></script>"
"  <script src='rsa-utils/rng.js'></script>"
"  <script src='rsa-utils/rsa.js'></script>"
"  <script src='script.js'></script>"
"</body>"
"</html>"
;

and thats the code to parse the data

void myPage(const char* url, ResponseCallback* cb, void* cbArg, Reader* body, Writer* result, void* reserved)
{
    char  _url[strlen(url)+1];
    char* query;
    char* value;
    
    Log.trace("handling page %s", url);

    strcpy(_url, url);
    if (strtok_r(_url, "?&", &query)) 
    { // is it the control page [0]?
      Log.trace("pre for() - URL: <%s>, *query: <%s>", _url, query);
      while(query) 
      { // and has it got a query string
        Log.trace("in  for() - URL: <%s>, *query: <%s>", _url, query);
        char *token = strtok_r(query, "=", &value);
        Log.trace("*query: <%s>, *value: <%s>, *token: <%s>", query, value, token);

        if(strcmp(token, "str1") == 0) 
        { // with a key str1
          char* val = strtok_r(NULL, "?&=", &value);
          Log.trace("String1: <%s>", val);
        }
        else if(strcmp(token, "str2") == 0) 
        { // with a key str2
          char* val = strtok_r(NULL, "?&=", &value);
          Log.trace("String2: <%s>", val);
        }

        query = value;  // continue with the rest
      }
    }

    if (strcmp(url,"/index")==0) {
        Log.trace("sending redirect");
        Header h("Location: /index.html\r\n");
        cb(cbArg, 0, 301, "text/plain", &h);
        return;
    }

    int8_t idx = 0;
    for (;;idx++) {
        Page& p = myPages[idx];
        if (!p.url) {
            idx = -1;
            break;
        }
        else if (strcmp(_url, p.url)==0) {
            break;
        }
    }

    Log.trace("refresh page%d <%s>", idx, myPages[idx].url);

    if (idx==-1) {
      cb(cbArg, 0, 404, nullptr, nullptr);
    }
    else {
      cb(cbArg, 0, 200, myPages[idx].mime_type, nullptr);
      result->write(myPages[idx].data);
    }
}
1 Like