首页 > 解决方案 > 从网络服务器检索 URL/href 到 ESP8266(处理它)

问题描述

我制作了一个 HTML 表单,其中包含 2 个带有多项选择的下拉菜单和 4 个输入框,当我填写所有输入并按下提交时,所有数据都显示在 URL 中。问题是,我想通过使用 String.replace(); 处理 ESP8266(在 c++ 中)中 URL 的内容;然后最后将其发送回处理的网络服务器。

我只需要帮助获取 C++ 中字符串的 URL,因为我知道如何完成剩下的工作。

我环顾四周,对于这个简单的问题,我很惊讶我并没有真正找到那么多。但是,我找到了 Server.arg(),但我不知道如何使用它。


C++ page:


#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include "PageBuilder.h"
#include "newGame.h"

int x = 0; 
int y = 10;
String score="test";
String score2="test2";
String refresh = "5";


#if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer Server;
ESP8266WebServer  server;
#endif

String scoreSend(PageArgument& args) { 
  return score;
}
String scoreSend2(PageArgument& args) { 
  return score2;
}
const char html[] = "<div class=\"jumbotron\"><div align=\"center\"><h1 class=\"display-4\" style=\"font-family: zetafonts-cinematografica-extrabold; font-size: 30px;\" >{{SCORE}} {{SCORE2}} </h1></div></div>";


String rate(PageArgument& args) { 
  return refresh;
}

const char rfs[] = "<meta http-equiv=\"refresh\" content=\"{{REFRESH}}\">";

PageElement refresh_elm(rfs, { {"REFRESH", rate} });
PageElement body_elem(html, { {"SCORE", scoreSend} });
PageElement Score_2(html, { {"SCORE2", scoreSend2} });
PageElement CURRENT_GAME_ELEMENT(htmlPage2);
PageBuilder CURRENT_GAME("/current-game", { CURRENT_GAME_ELEMENT, body_elem, Score_2, refresh_elm});

PageElement NEW_GAME_ELEMENT(htmlPage3);
PageBuilder NEW_GAME("/new-game", {NEW_GAME_ELEMENT});

void setup() {
  Serial.begin(115200);
  pinMode(2, INPUT);
  WiFi.softAP("ssid", "pass");
  delay(100);       
  CURRENT_GAME.insert(Server);     
  NEW_GAME.insert(Server);        
  Server.begin();
}

void loop() {
Server.handleClient();
}


Html page:

const char htmlPage3[] PROGMEM = R"=====(

<form id="what-team">
            <div class="form-row">

            <div class="col" style="float: left; padding-left: 5%;">
            <label class="mr-sm-2 sr-only" for="inlineFormCustomSelect">Preference</label>
            <select name="team1-playing-as" form="what-team" class="custom-select mr-sm-2" id="inlineFormCustomSelect" style="width:700px;height:50px;">
            <option selected="">Choose Team</option>                    
            <option value="manchester-united">Manchester United</option>
            <option value="liverpool">Liverpool</option>
            <option value="fc-barcelona">FC-Barcelona</option>
            </select>
            <div class="form-group">
            <input type="Name" class="form-control" name="player1" value="" placeholder=" Player 1" style="width:700px;height:50px;">
            <input type="Name" class="form-control" name="player2" value="" placeholder=" Player 2" style="width:700px;height:50px;">
            </div>
            </div>

            <div class="col" style="float: right; padding-left: 5%;">
            <label class="mr-sm-2 sr-only" for="inlineFormCustomSelect">Preference</label> 
            <select name="team2-playing-as" form="what-team" class="custom-select mr-sm-2" id="inlineFormCustomSelect" style="width:700px;height:50px;">
            <option selected="">Choose Team</option>
            <option value="manchester-united">Manchester United</option>
            <option value="liverpool">Liverpool</option>
            <option value="fc-barcelona">FC-Barcelona</option>
            </select>
            <div class="form-group">
            <input type="Name" class="form-control" name="player1" value="" placeholder=" Player 1" style="width:700px;height:50px; text-align: right;">
            <input type="text" class="form-control" name="player2" value="" placeholder=" Player 2" style="width:700px;height:50px; text-align: right;">
            </div>
            </div>
            </div>

           <div style="padding-top: 50px; padding-left: 10%; padding-right: 10%; float: center;">
           <input type="submit" value="Start" class="btn btn-primary btn-lg btn-block">
           </div>

           </form>

)=====";

这是我填写表格并按下提交按钮时得到的 URL。 "192.168.0.1/new-game?team1-playing-as=manchester-united&player1=John&player2=Doe&team2-playing-as=liverpool&player1=Doe&player2=John "

简而言之,我想做的是我想把那个^ href 变成一个字符串

编辑/更新:

我找到了一些方法,但是第一个只返回与其比较的href,与server.hasarg()的功能相同,后者只返回元素的href,基本上,两者都只返回/new-game?。不幸的是,我想要 /new-game 之后的内容?

bool func(HTTPMethod method, String uri) {
  if (uri == "/new-game") {
  Serial.println(uri);
  return true;
  } else {
    Serial.println(uri);
    return false;
  }
}
const char* data = NEW_GAME.uri()

标签: htmlc++cssgetesp8266

解决方案


这就是我从表单中检索数据所做的:


int h = 1;
void handleForm() {

  String team1Team = Server.arg("team1-playing-as");
  String team1P1   = Server.arg("player1");
  String team1P2   = Server.arg("player2");
  String team2Team = Server.arg("team2-playing-as");
  String team2P3   = Server.arg("player3");
  String team2P4   = Server.arg("player4");

    if ((Server.args() == 6) && ( h == 1)) {
    Serial.println("Team 1: " + team1Team);
    Serial.println("Player 1: " + team1P1);
    Serial.println("player 2: " + team1P2);
    Serial.println("Team 2: " + team2Team);
    Serial.println("Player 3: " + team2P3);
    Serial.println("Player 4: " + team2P4);
    delay(1000);
    h = 0;
    //-------------------this is where I want to redirect from /new-game to /current-game
    }
    if ((Server.args() == 0) && ( h == 0)) {
      h = 1;
    }
}

我通过这样做来重定向网站:const char rDIR[] = "<meta http-equiv=\"Refresh\" content=\"0; url={{rDir}}\">";然后当这是真的时{{rDIR}}替换为:/current-gameif ((Server.args() == 6) && ( h == 1))

但是,我保存在字符串中的表单输入中的数据是正确的,当我尝试使用相同的方法(应该可以工作)接近时,字符串是空的。这真的很奇怪,因为它只有这样定义的字符串String team1P1 = Server.arg("player1");:不将其值返回给令牌。


推荐阅读