Post topics, source code that relate to the Arduino Platform

User avatar
By awwende
#27968 I'm working on a NTP Clock that will end up having log in information for my router that I don't want to be visible in the URL, I'm told the best way to do that is to just use method post, but I'm not seeing my message. Can anyone help me figure out what's going wrong here? Here's my code that I'm uploading to the ESP2866:

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

WiFiServer server(80);
String HTTP_req;

void setup()
{
  Serial.begin(9600);
  delay(10);
  WiFi.mode(WIFI_AP_STA);
  WiFi.softAP("ESP8266 Clock");
  server.begin();
}

void loop()
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  boolean currentLineIsBlank = true;
  while(client.connected())
  {
    yield();
    if (client.available())
    {
      char c = client.read();
      HTTP_req += c;
      if(c == '\n' && currentLineIsBlank)
      {
        // send a standard http response header
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: keep-alive");
        client.println();
        //HTTP request for web page
        client.println("<!DOCTYPE html>"
                       "<html>"
                       "<head>"
                             "<title>Clock Settings</title>"
                       "</head>"
                       "<body>"
                             "<form method=\"post\">"
                                   "<input type=\"submit\" name=\"clk_action\" value=\"RESET\">"
                             "</form>"
                       "</body>"
                       "</html>");
        Serial.println(HTTP_req);
        HTTP_req = "";            // finished with request, empty string
        break;
      }
      if (c == '\n')
      {
        currentLineIsBlank = true;
      }
      else if (c != '\r')
      {
        currentLineIsBlank = false;
      }
    }
  }
  delay(1);
  client.stop();
}


When I press the button, I do get a POST request, but I'm not seeing the data. Here's what I get on the serial window:
Code: Select allPOST / HTTP/1.1
Host: 192.168.4.1
Connection: keep-alive
Content-Length: 16
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,image/webp
Content-Type: application/x-www-form-urlencoded
Origin: http://192.168.4.1
Referer: http://192.168.4.1/
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) CriOS/45.0.2454.68 Mobile/12H143 Safari/600.1.4
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8


GET /favicon.ico HTTP/1.1
Host: 192.168.4.1
Connection: keep-alive
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) CriOS/45.0.2454.68 Mobile/12H143 Safari/600.1.4
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

User avatar
By martinayotte
#27976 It is not clear with the above code what you wish to accomplish.
Is your goal simply to display that "Clock Settings" when a browser query the ESP ?
First, you used a plain TCPServer, so you received all requests from the browser.
Second, about the GET vs POST, I'm not following you when you say "I don't want to be visible in the URL".
Do you means not seeing parameters when you press the "RESET" submit button ?
Maybe you should use ESP82WebServer class instead, the provide path parsing and form arguments on POST.
User avatar
By awwende
#27978 So this is a modified version of what I'm working on. But I'm just trying to view the post data.

In my current code I'm using the get method and reading the resulting URL to get the SSID and password for my router, along with the NTP server address and timezone. And because I'm sending the log in information for my router, I would like it to be a little more secure. So even though the code I posted doesn't show it, I'm getting 192.168.4.1/?ssid=myRouter&pass=myPass& server=128.4.15.30&GMToffset=4 I'm trying to get everything after the ? placed in the post http request.

I'm new the ESP and web programming, what's the difference between a plain TCPServer and the ESP82WebServer class?
User avatar
By martinayotte
#27980
awwende wrote:I'm new the ESP and web programming, what's the difference between a plain TCPServer and the ESP82WebServer class?

ESP82WebServer is based on top of a TCPServer, it does most of the URL/Arguments parsing for you, so you don't need to re-invent the wheel. Simply take a look at the examples provided.
About GET/POST, yes, understood, you don't want arguments to be seen, although packets still not encrypted yet, but we will have SSL soon (maybe it is already working in staging).
With ESP82WebServer, this quick untested example sketch will display arguments that you try to save :

Code: Select allESP8266WebServer webserver(80);

void handleRoot() {
  webserver.send(200, "text/plain", "Root page (place your form here with 'post' action path to '/save') ...");
}

void handleNotFound() {
  webserver.send(404, "text/plain", "Page not found ...");
}

void handleSave() {
  String = "Settings Saved ...\r\n";
  if (webserver.args() > 0 ) {
    for ( uint8_t i = 0; i < webserver.args(); i++ ) {
      str += webserver.argName(i) + " = " + webserver.arg(i) + "\r\n";
    }
  }
  webserver.send(200, "text/plain", str.c_str());
}

void setup() {
  webserver.on("/", handleRoot);
  webserver.on("/save", handleSave);
  webserver.onNotFound(handleNotFound);
  webserver.begin();
}

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