Add description
parent
93e0abe9bc
commit
7e23e437d6
|
|
@ -1,3 +1,9 @@
|
||||||
|
/* Webserver reads from SD text file test for LaskaKit ESPlan
|
||||||
|
* After boot connect to http://esplan.local or to IP adress of ESP32 (for example 192.168.0.98)
|
||||||
|
* Email:podpora@laskakit.cz
|
||||||
|
* Web:laskakit.cz
|
||||||
|
*/
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <ETH.h>
|
#include <ETH.h>
|
||||||
|
|
@ -8,7 +14,7 @@
|
||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
#include "page.h"
|
#include "page.h"
|
||||||
|
|
||||||
const char *host = "esplan"; // Connect to http://ESPlan.local
|
const char *host = "esplan"; // Connect to http://esplan.local
|
||||||
|
|
||||||
// SD card defines
|
// SD card defines
|
||||||
#define SCK 14
|
#define SCK 14
|
||||||
|
|
@ -21,224 +27,226 @@ SPIClass spi = SPIClass(HSPI);
|
||||||
WebServer server(80);
|
WebServer server(80);
|
||||||
|
|
||||||
// LAN8720A parameters
|
// LAN8720A parameters
|
||||||
#define ETH_POWER_PIN -1
|
#define ETH_POWER_PIN -1
|
||||||
#define ETH_ADDR 0
|
#define ETH_ADDR 0
|
||||||
#define ETH_MDC_PIN 23
|
#define ETH_MDC_PIN 23
|
||||||
#define ETH_MDIO_PIN 18
|
#define ETH_MDIO_PIN 18
|
||||||
#define ETH_NRST_PIN 5
|
#define ETH_NRST_PIN 5
|
||||||
|
|
||||||
static bool eth_connected = false;
|
static bool eth_connected = false;
|
||||||
|
|
||||||
void WiFiEvent(WiFiEvent_t event)
|
void WiFiEvent(WiFiEvent_t event)
|
||||||
{
|
{
|
||||||
switch (event) {
|
switch (event)
|
||||||
|
{
|
||||||
case ARDUINO_EVENT_ETH_START:
|
case ARDUINO_EVENT_ETH_START:
|
||||||
Serial.println("ETH Started");
|
Serial.println("ETH Started");
|
||||||
//set eth hostname here
|
// set eth hostname here
|
||||||
ETH.setHostname("esplan");
|
ETH.setHostname("esplan");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_EVENT_ETH_CONNECTED:
|
case ARDUINO_EVENT_ETH_CONNECTED:
|
||||||
Serial.println("ETH Connected");
|
Serial.println("ETH Connected");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_EVENT_ETH_GOT_IP:
|
case ARDUINO_EVENT_ETH_GOT_IP:
|
||||||
Serial.print("ETH MAC: ");
|
Serial.print("ETH MAC: ");
|
||||||
Serial.print(ETH.macAddress());
|
Serial.print(ETH.macAddress());
|
||||||
Serial.print(", IPv4: ");
|
Serial.print(", IPv4: ");
|
||||||
Serial.print(ETH.localIP());
|
Serial.print(ETH.localIP());
|
||||||
if (ETH.fullDuplex()) {
|
if (ETH.fullDuplex())
|
||||||
Serial.print(", FULL_DUPLEX");
|
{
|
||||||
}
|
Serial.print(", FULL_DUPLEX");
|
||||||
Serial.print(", ");
|
}
|
||||||
Serial.print(ETH.linkSpeed());
|
Serial.print(", ");
|
||||||
Serial.println("Mbps");
|
Serial.print(ETH.linkSpeed());
|
||||||
eth_connected = true;
|
Serial.println("Mbps");
|
||||||
break;
|
eth_connected = true;
|
||||||
|
break;
|
||||||
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
||||||
Serial.println("ETH Disconnected");
|
Serial.println("ETH Disconnected");
|
||||||
eth_connected = false;
|
eth_connected = false;
|
||||||
break;
|
break;
|
||||||
case ARDUINO_EVENT_ETH_STOP:
|
case ARDUINO_EVENT_ETH_STOP:
|
||||||
Serial.println("ETH Stopped");
|
Serial.println("ETH Stopped");
|
||||||
eth_connected = false;
|
eth_connected = false;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void readFile(fs::FS &fs, const char *path)
|
void readFile(fs::FS &fs, const char *path)
|
||||||
{
|
{
|
||||||
char text[100] = {0};
|
char text[100] = {0};
|
||||||
char out[100] = {0};
|
char out[100] = {0};
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
Serial.printf("Reading file: %s\n", path);
|
Serial.printf("Reading file: %s\n", path);
|
||||||
|
|
||||||
File file = fs.open(path);
|
File file = fs.open(path);
|
||||||
if (!file)
|
if (!file)
|
||||||
{
|
|
||||||
Serial.println("Failed to open file for reading");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.print("Read from file: ");
|
|
||||||
while (file.available())
|
|
||||||
{
|
|
||||||
text[i] = file.read();
|
|
||||||
if(text[i++] == '\n')
|
|
||||||
{
|
{
|
||||||
Serial.print(text);
|
Serial.println("Failed to open file for reading");
|
||||||
sprintf(out, "<p>%s</p>", text);
|
return;
|
||||||
server.sendContent(out);
|
|
||||||
memset(text, 0, 99*sizeof(*text));
|
|
||||||
memset(out, 0, 99*sizeof(*out));
|
|
||||||
i = 0;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
file.close();
|
Serial.print("Read from file: ");
|
||||||
|
while (file.available())
|
||||||
|
{
|
||||||
|
text[i] = file.read();
|
||||||
|
if (text[i++] == '\n')
|
||||||
|
{
|
||||||
|
Serial.print(text);
|
||||||
|
sprintf(out, "<p>%s</p>", text);
|
||||||
|
server.sendContent(out);
|
||||||
|
memset(text, 0, 99 * sizeof(*text));
|
||||||
|
memset(out, 0, 99 * sizeof(*out));
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFile(fs::FS &fs, const char *path, const char *message)
|
void writeFile(fs::FS &fs, const char *path, const char *message)
|
||||||
{
|
{
|
||||||
Serial.printf("Writing file: %s\n", path);
|
Serial.printf("Writing file: %s\n", path);
|
||||||
|
|
||||||
File file = fs.open(path, FILE_WRITE);
|
File file = fs.open(path, FILE_WRITE);
|
||||||
if (!file)
|
if (!file)
|
||||||
{
|
{
|
||||||
Serial.println("Failed to open file for writing");
|
Serial.println("Failed to open file for writing");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (file.print(message))
|
if (file.print(message))
|
||||||
{
|
{
|
||||||
Serial.println("File written");
|
Serial.println("File written");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Write failed");
|
Serial.println("Write failed");
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t appendFile(fs::FS &fs, const char *path, const char *message)
|
uint8_t appendFile(fs::FS &fs, const char *path, const char *message)
|
||||||
{
|
{
|
||||||
Serial.printf("Appending to file: %s\n", path);
|
Serial.printf("Appending to file: %s\n", path);
|
||||||
|
|
||||||
File file = fs.open(path, FILE_APPEND);
|
File file = fs.open(path, FILE_APPEND);
|
||||||
if (!file)
|
if (!file)
|
||||||
{
|
{
|
||||||
Serial.println("Failed to open file for appending");
|
Serial.println("Failed to open file for appending");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (file.print(message))
|
if (file.print(message))
|
||||||
{
|
{
|
||||||
Serial.println("Message appended");
|
Serial.println("Message appended");
|
||||||
file.close();
|
file.close();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Append failed");
|
Serial.println("Append failed");
|
||||||
file.close();
|
file.close();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleRoot()
|
void handleRoot()
|
||||||
{
|
{
|
||||||
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
||||||
server.send_P(200, "text/html", index_html);
|
server.send_P(200, "text/html", index_html);
|
||||||
spi.begin(SCK, MISO, MOSI, CS);
|
spi.begin(SCK, MISO, MOSI, CS);
|
||||||
if (SD.begin(CS, spi))
|
if (SD.begin(CS, spi))
|
||||||
{
|
{
|
||||||
Serial.println("SD Card initialized.");
|
Serial.println("SD Card initialized.");
|
||||||
readFile(SD, "/output.txt");
|
readFile(SD, "/output.txt");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
server.sendContent("<p style= \"color:rgb(255, 0, 0);\">SD card not found!</p>");
|
server.sendContent("<p style= \"color:rgb(255, 0, 0);\">SD card not found!</p>");
|
||||||
}
|
}
|
||||||
server.sendContent("</body>");
|
server.sendContent("</body>");
|
||||||
server.sendContent(""); // This ends sending
|
server.sendContent(""); // This ends sending
|
||||||
SD.end();
|
SD.end();
|
||||||
spi.end();
|
spi.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleSubmit()
|
void handleSubmit()
|
||||||
{
|
{
|
||||||
char text[100] = {0};
|
char text[100] = {0};
|
||||||
spi.begin(SCK, MISO, MOSI, CS);
|
spi.begin(SCK, MISO, MOSI, CS);
|
||||||
if (SD.begin(CS, spi))
|
if (SD.begin(CS, spi))
|
||||||
{
|
|
||||||
Serial.println("SD Card initialized.");
|
|
||||||
sprintf(text, "%s\n", server.arg(0).c_str());
|
|
||||||
Serial.println(server.arg(0));
|
|
||||||
if (appendFile(SD, "/output.txt", text))
|
|
||||||
{
|
{
|
||||||
writeFile(SD, "/output.txt", text);
|
Serial.println("SD Card initialized.");
|
||||||
|
sprintf(text, "%s\n", server.arg(0).c_str());
|
||||||
|
Serial.println(server.arg(0));
|
||||||
|
if (appendFile(SD, "/output.txt", text))
|
||||||
|
{
|
||||||
|
writeFile(SD, "/output.txt", text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
SD.end();
|
||||||
SD.end();
|
spi.end();
|
||||||
spi.end();
|
handleRoot();
|
||||||
handleRoot();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleNotFound()
|
void handleNotFound()
|
||||||
{
|
{
|
||||||
String message = "Error\n\n";
|
String message = "Error\n\n";
|
||||||
message += "URI: ";
|
message += "URI: ";
|
||||||
message += server.uri();
|
message += server.uri();
|
||||||
message += "\nMethod: ";
|
message += "\nMethod: ";
|
||||||
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||||
message += "\nArguments: ";
|
message += "\nArguments: ";
|
||||||
message += server.args();
|
message += server.args();
|
||||||
message += "\n";
|
message += "\n";
|
||||||
for (uint8_t i = 0; i < server.args(); i++)
|
for (uint8_t i = 0; i < server.args(); i++)
|
||||||
{
|
{
|
||||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||||
}
|
}
|
||||||
server.send(404, "text/plain", message);
|
server.send(404, "text/plain", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DNS_setup()
|
void DNS_setup()
|
||||||
{
|
{
|
||||||
if (MDNS.begin(host))
|
if (MDNS.begin(host))
|
||||||
{
|
{
|
||||||
MDNS.addService("http", "tcp", 80);
|
MDNS.addService("http", "tcp", 80);
|
||||||
Serial.println("MDNS responder started");
|
Serial.println("MDNS responder started");
|
||||||
Serial.print("You can now connect to http://");
|
Serial.print("You can now connect to http://");
|
||||||
Serial.print(host);
|
Serial.print(host);
|
||||||
Serial.println(".local");
|
Serial.println(".local");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
// For reseting LAN8720A
|
// For reseting LAN8720A
|
||||||
pinMode(ETH_NRST_PIN, OUTPUT);
|
pinMode(ETH_NRST_PIN, OUTPUT);
|
||||||
digitalWrite(ETH_NRST_PIN, LOW);
|
digitalWrite(ETH_NRST_PIN, LOW);
|
||||||
delay(500);
|
delay(500);
|
||||||
digitalWrite(ETH_NRST_PIN, HIGH);
|
digitalWrite(ETH_NRST_PIN, HIGH);
|
||||||
|
|
||||||
WiFi.onEvent(WiFiEvent);
|
WiFi.onEvent(WiFiEvent);
|
||||||
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_PHY_LAN8720, ETH_CLOCK_GPIO17_OUT);
|
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_PHY_LAN8720, ETH_CLOCK_GPIO17_OUT);
|
||||||
|
|
||||||
DNS_setup();
|
DNS_setup();
|
||||||
|
|
||||||
server.on("/", handleRoot); // Main page
|
server.on("/", handleRoot); // Main page
|
||||||
|
|
||||||
server.on("/get", handleSubmit); // Function done on GET request
|
server.on("/get", handleSubmit); // Function done on GET request
|
||||||
|
|
||||||
server.onNotFound(handleNotFound); // Function done when hangle is not found
|
server.onNotFound(handleNotFound); // Function done when hangle is not found
|
||||||
|
|
||||||
server.begin();
|
server.begin();
|
||||||
Serial.println("HTTP server started");
|
Serial.println("HTTP server started");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
if(eth_connected)
|
if (eth_connected)
|
||||||
{
|
{
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
}
|
}
|
||||||
delay(2); // allow the cpu to switch to other tasks
|
delay(2); // allow the cpu to switch to other tasks
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue