Doubt about the program structure
Posted: Mon Feb 25, 2019 4:06 am
Example:
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
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