Chat freely about anything...

User avatar
By xl97
#48533 Hey gang-

I'm new to the ESP8266 module.. and have some questions, I'm hoping can clear up some things for me.

I believe I need the ESP module to be both an AP and a server? As I want to connect DIRECTLY to it... but also need to host/serve up a 'webpage' that has some links that when clicked will send query param back to the ESP.... and parse out the data...?

Goal:
Connect -directly- to ESP module using (cell phone, tablet,..whatever). I do not want to connect to local wi-fi using ssid/pass, then navigate to some dynamically provided IP for the ESP. I want to connect -directly- to it...

*** I believe this means I need to configure it as a an access point? (correct?)

Reading this:
https://www.hackster.io/rayburne/esp826 ... 6&offset=2

*** This seems to be VERY close to what I want.


Once connected directly to the ESP.. I want to serve up a hosted webpage...

*** Again.. this seems to also be included in the tutorial link above.


Here is what I can NOT grasp yet. I want this webpage to have some links/buttons on it.. that when clicked (form submitted,...whatever) that is posts (get?) back to itself and parses out a query param (ie: ?command=1 ...etc)

This parsed query param.. will then be sent to a connected Arduino via serial.


At this point.. we can forget the connected Arduino portion.



Questions:

What needs to be done in the tutorial above to have some links on the page (I'm not asking for the HTML portion) :).. that went clicked posts this data/query/url data back to the ESP to be parsed out?

I guess I am little confused on many of the terms being throw around..

AP (Access Point) vs Server vs Client.

A client would reach out/connect to some IP to get some data returned... correct?

So what is the difference between an AP configuration and SERVER configuration? (outside of the directly being able to connect to an AP state without local wi-fi/network connection)..

I'm not clear if some sort of 'web server' stuff needs to be added/configured so the page can post back to itself and grab some info from the query string?


Another question, what is meant when the term "softAP" is used?

thanks!
-xl
User avatar
By borelg
#48550 Good Morning xl97,

I'm not an expert, but I would like to try to answer some of your question anyway.
First of all "SoftAP" is "Software Access Point" I think that it is so colled because is driven by a piece of software, in fact it has some limitations, such as the maximum number of conneccted stations, which is 4.

You need an Acces Point because you want to create a WiFi network in order to connect different devices.

You need a server, especially a WebServer, becuase, from what I read, you want to send some http-requests to this server and get some answers from it. When you browse a page with your browser you mostly send http requests to some servers and they get back to you with an http page.
Here the part in which I am not very competent, your server has to send a page in which you put some objects useful to generate other http request. For instance: you click a button, and your browser sends another http request maybe attacching some parameters from text fields inside the page. Your ESP8266 configured as webserver receives this request and then you can parse the parameters simply working with the string that has been received by the ESP8266 as a request.

I can share with you this code:
Code: Select all#include <ESP8266WiFi.h>

//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPSK[] = "TestSoftAP";

/////////////////////
// Pin Definitions //
/////////////////////
const int LED_PIN = 5; // Thing's onboard, green LED
const int ANALOG_PIN = A0; // The only analog pin on the Thing
const int DIGITAL_PIN = 12; // Digital pin to be read

WiFiServer server(80);

void setup()
{
  initHardware();
  setupWiFi();
  server.begin();
}

void loop()
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();

  // Match the request
  int val = -1; // We'll use 'val' to keep track of both the
                // request type (read/set) and value if set.
  if (req.indexOf("/led/0") != -1)
    val = 0; // Will write LED low
  else if (req.indexOf("/led/1") != -1)
    val = 1; // Will write LED high
  else if (req.indexOf("/read") != -1)
    val = -2; // Will print pin reads
  // Otherwise request will be invalid. We'll say as much in HTML

  // Set GPIO5 according to the request
  if (val >= 0)
    digitalWrite(LED_PIN, val);

  client.flush();

  // Prepare the response. Start with the common header:
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  // If we're setting the LED, print out a message saying we did
  if (val >= 0)
  {
    s += "LED is now ";
    s += (val)?"on":"off";
  }
  else if (val == -2)
  { // If we're reading pins, print out those values:
    s += "Analog Pin = ";
    s += String(analogRead(ANALOG_PIN));
    s += "<br>"; // Go to the next line.
    s += "Digital Pin 12 = ";
    s += String(digitalRead(DIGITAL_PIN));
  }
  else
  {
    s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";
  }
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected
  // when the function returns and 'client' object is detroyed
}

void setupWiFi()
{
  WiFi.mode(WIFI_AP);

  // Do a little work to get a unique-ish name. Append the
  // last two bytes of the MAC (HEX'd) to "Thing-":
  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  String AP_NameString = "ESP8266 Thing " + macID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i=0; i<AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);
}

void initHardware()
{
  Serial.begin(115200);
  pinMode(DIGITAL_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  // Don't need to set ANALOG_PIN as input,
  // that's all it can be.
}


I found it on the web, but I can't remeber where so I can't share the link too.

Hope this could be useful.

ps. Yesterday I tried to write a new topic too, but I think that moderators didn't accepted it. Is there anyway I can contact them?
Thank you in advance.