Adds async webserver and mdns

This commit is contained in:
Lukas Bachschwell 2019-05-13 11:51:11 +02:00
parent 4201b53d88
commit 4097a93b8c
5 changed files with 109 additions and 5 deletions

5
.vscode/arduino.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"board": "esp8266:esp8266:d1",
"configuration": "xtal=80,vt=flash,exception=disabled,eesz=4M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=921600",
"port": "/dev/cu.wchusbserial230"
}

BIN
.vscode/ipch/e89e887f3099ca3f/main.ipch vendored Normal file

Binary file not shown.

Binary file not shown.

View File

@ -12,3 +12,9 @@
platform = espressif32
board = esp32-poe
framework = arduino
upload_port=/dev/tty.wchusbserial230
monitor_baud = 115200
lib_deps =
ESP Async WebServer

View File

@ -1,9 +1,102 @@
#include <Arduino.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESPmDNS.h>
void setup() {
// put your setup code here, to run once:
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 12
#include <ETH.h>
static bool eth_connected = false;
AsyncWebServer server(80);
const char *PARAM_MESSAGE = "message";
void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); }
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_ETH_START:
Serial.println("ETH Started");
// set eth hostname here
ETH.setHostname("esp32-ethernet");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex()) {
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
eth_connected = true;
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void setup() {
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
ETH.begin();
while (!eth_connected) {
delay(1000);
}
if (!MDNS.begin("a2clcontroller")) {
Serial.println("Error setting up MDNS responder!, system halt");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send(200, "text/plain", "Hello, world"); });
// Send a GET request to <IP>/get?message=<message>
server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request) {
String message;
if (request->hasParam(PARAM_MESSAGE)) {
message = request->getParam(PARAM_MESSAGE)->value();
} else {
message = "No message sent";
}
request->send(200, "text/plain", "Hello, GET: " + message);
});
// Send a POST request to <IP>/post with a form field message set to <message>
server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request) {
String message;
if (request->hasParam(PARAM_MESSAGE, true)) {
message = request->getParam(PARAM_MESSAGE, true)->value();
} else {
message = "No message sent";
}
request->send(200, "text/plain", "Hello, POST: " + message);
});
server.onNotFound(notFound);
server.begin();
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void loop() {}