Need help with WiFi Access point on Wemos r2 Mini

Firstly I would like to say using the Arduino IDE has been a joy. I coded an App to drive a stepper motor via a Wemos R2 Mini and an A4988 chip (to handle the 12v and current load of the stepper). It took less than two days to get the App fully tested. I then decided to make use of the Wemos builtin WiFi to control fine tuning from my Android phone. I found an excellent example in the File>examples>Wemos... showing how to establish an access point and web server on the Mini. I amended the basic HTML web page to include a couple of input fields and a submit button. I compiled the App and downloaded it to the Wemos. On my android phone the Wemos WiFi network was visible so I connected to it and used Firefox to browse to 192.168.4.1 and up popped my web page. But now I am stuck - web and WiFi are not my forte. What do I have to code on the Mini to get the response from the browser? I tried to set up a client and handler but had no success at all; whatever I tried seemed to cause conflict between the server and the client.

If anyone can give me a few clues they would be most welcome.

I'm using an access point on the Wemos because I can't use a hotspot on my phone (No subnet mask). Also the combination of Android and Wemos will be used where there is no internet router or WiFi service within a bull's roar.

Dicky.

You have to set up an ESP8266WebServer object. This is the most convenient server library I've found so far, and does the job well for me. You can easily get the POST or GET arguments the page sends back to you through the args() function, and then do with it what you want.

Many thanks for the reply. I have set up an ESP8266WebServer object and it is running OK - I connect my phone to the Wemos WiFi access point OK and the web page I coded appears on my Android phone’s browser when I navigate to 192.168.4.1 and I can fill in my fields and press the ‘Submit’ button.

The problem is I can find no documentation for the ESP8266WebServer to describe how to get or handle the response. I have no idea what the syntax is for the server.args(); call. I looked at the ESP8266WebServer.h file but that didn’t help explain what to do. If you could give some guidance on the syntax and using the args function I’m sure I will be off and running in no time.

Please note my solution uses the access point on the Wemos R2 Mini because there is no WiFi network or router available for normal web server operation.

server.args() returns the number of available arguments.
server.arg(argName) gives you the value for a specific argument.
server.hasArg(argName) checks whether a specific argument is present.

Class reference herel. As usual with ESP8266 related topics the documentation is sparse.

This example shows how to get HTTP args.

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

Gdsports, many thanks for the very helpful link. I am getting closer to understanding what goes on.

It seems the call to server.handleClient(); in the looper responds to all client requests. It in turn passes them to a server.on routine, provided one is coded to handle the specific request.

server.onNotFound(handleNotFound); is a catch-all to deal with unexpected/unhandled requests.

There seem to be two forms of the server.on code - inline or via a procedure call. My code below uses both forms. My root html page has a button, two input fields in a form together with a submit button.

I have no idea what the does in the inline code form. The results seem to vary depending on the browser used. My code loops on the submit button press (it seems to stay ‘clicked’) when I use Firefox. Internet Explorer updates the navigation field to 1.192.164.4.1/submit… args… and this gets trapped by .onNotFound. Clearly I am still missing something. Both browsers return the arg data correctly.

// Handle requests

// Send the root HTML page
server.on("/", handleRoot);

// Handle specific button click on web page
server.on("/dreamon", {
Serial.println(“Pressed button to dreamon”);
server.send(80, “text”, “Didn’t get any data”);
delay(1000);
});

// Handle specific submit button click on web page
server.on("/Submit", {
Serial.println(“Action Submit”);
String gotarg = “”;
int argCount = server.args();
Serial.print("Count of args: ");
Serial.println(argCount);
for (int i = 0; i < argCount; i++) {
gotarg =server.arg(i);
Serial.println(gotarg);
}
delay(1000);
});

I must now learn how to respond without navigating away from the root page (unless of course I want to).

Every server.on() handles an HTTP request. So every request handler function (for example, handleRoot), must return an HTTP response using server.send(). The /Submit handler does not include a call to server.send so the browser/client is waiting for a response.

Add something like this after the for loop. The first parameter is the HTTP status code 200 = OK,success.

server.send(200, "text/plain", "submit received");

And remove the delay(1000).

I do not think 80 has a defined value so in the /dreamon handler, change 80 to 200. The HTTP protocol defines the codes so you cannot create new codes without also changing every web browser.

For now, just use 200 for OK except in the case of the notfound handler which should return 404.

HTTP status codes

Many thanks once again. All browsers are consistent now.

I will now try to send back a page with a message such as “Hello Jim” on it and I’m as good as home and hosed.

If you know of a way to send only an update to a page already displayed it would be most welcome, if not it is no big deal to send the whole page with amended fields.

Programming these Wemos and Arduino boards is so much easier than Android. I’m still waiting for a few bits for my star tracker for the final build but the Wemos stepper controller and phone interface are just about ready.

Thanks again,

Dicky.