-->
Page 1 of 1

Doubt about the program structure

PostPosted: Mon Feb 25, 2019 4:06 am
by FRANCISCOGIMENO1000
Example:
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK  "your-password"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

ESP8266WebServer server(80);

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266!");
  digitalWrite(led, 0);
}

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);
}

void setup(void)
{
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}


void loop(void)
{
  server.handleClient();
  MDNS.update();
}



I have a question about how the program is structured with a difference to an arduino.

I mean that in arduino there are:
Includes, defines, start variables ... etc
Setup block----
Loop block------

But in the esp it seems that the setup block is the main and the loop block is the secondary one.

I mean that the pages that are going to be served and their capture of get or post data are defined in setup.

My question is those definitions and processes should not be in Loop{} instead of Setup{}?

A greeting
And thanks

Re: Doubt about the program structure

PostPosted: Mon Feb 25, 2019 9:13 am
by QuickFix
Both should be identical (it's the same language, only the generated assembler code is different): "setup(void)" is for initializing things (hence the name) and "loop(void)" is for the general program flow and runs in a... uhm... loop.
What you're referring to (and probably are probably confused by) are the event-handlers.

If you look closely the code does not actually run inside the setup, but are attached to an event (a pointer) to what has to be done when a specific event occurs (in this case: when a certain document, e.g. "/inline", is called).
In the first case "server.on("/", handleroot)" you attach an actual method ("handleroot") to the event ("/"), in the second case "server.on("/inline", [])" you attach an anonymous method to the event "/inline".

When someone will be asking for a document that has not been defined, the "server.onNotFound(handleNotFound)" line will make sure that the "handleNotFound"-method will be called.

I'm not absolutely sure about this, but to my knowledge the (normal) Arduino doesn't support event-handling out of the box, but the ESP-core does and this is used to serve (or call) code depending on a requested document; "server.on(Param1, Param2)" means: at the server object, on reception of request for document "Param1" do "Param2".
Since you have to set up things in setup, these event-handlers are also set in the setup-block (they code itself is not actually run there).

Because the ESP has a separate WiFi-stack using event-handlers is an effective way to program things.
It would be possible to make everything procedural (ie. handle everything in the loop: ask the server-object if a document was requested and if so what the actual request was and in turn send something back), but by using events you can keep code segregated and work with blocks of code shielded from the rest.

Re: Doubt about the program structure

PostPosted: Mon Feb 25, 2019 11:33 am
by rudy
Since you have to set up things in setup, these event-handlers are also set in the setup-block (they code itself is not actually run there).


From what I remember, this is no different than using interrupts on a Arduino Uno . You enable things in setup() but the code runs when the event occurs. That could be while still in setup() (if setup was lengthy) or while running loop().

Re: Doubt about the program structure

PostPosted: Mon Feb 25, 2019 5:51 pm
by FRANCISCOGIMENO1000
Hello first of all thank you very much for the help.
I was referring to what you mentioned.
I saw the code but I did not understand why they did it that way.
But I see how to focus and where to look.

Thanks again