From 93e0abe9bce6378baf01b4e9f2e43919a33489a9 Mon Sep 17 00:00:00 2001 From: Jarda Date: Mon, 14 Aug 2023 23:59:03 +0200 Subject: [PATCH] Add code examples - esphome and webserver --- .../ESPlan_webserver_SD_read.ino | 244 ++++++++++++++++++ SW/ESPlan_webserver_SD_read/page.h | 41 +++ SW/esplan_esphome.yaml | 51 ++++ 3 files changed, 336 insertions(+) create mode 100644 SW/ESPlan_webserver_SD_read/ESPlan_webserver_SD_read.ino create mode 100644 SW/ESPlan_webserver_SD_read/page.h create mode 100644 SW/esplan_esphome.yaml diff --git a/SW/ESPlan_webserver_SD_read/ESPlan_webserver_SD_read.ino b/SW/ESPlan_webserver_SD_read/ESPlan_webserver_SD_read.ino new file mode 100644 index 0000000..14b98d3 --- /dev/null +++ b/SW/ESPlan_webserver_SD_read/ESPlan_webserver_SD_read.ino @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "page.h" + +const char *host = "esplan"; // Connect to http://ESPlan.local + +// SD card defines +#define SCK 14 +#define MISO 12 +#define MOSI 13 +#define CS 15 +SPIClass spi = SPIClass(HSPI); + +// Set web server port number to 80 +WebServer server(80); + +// LAN8720A parameters +#define ETH_POWER_PIN -1 +#define ETH_ADDR 0 +#define ETH_MDC_PIN 23 +#define ETH_MDIO_PIN 18 +#define ETH_NRST_PIN 5 + +static bool eth_connected = false; + +void WiFiEvent(WiFiEvent_t event) +{ + switch (event) { + case ARDUINO_EVENT_ETH_START: + Serial.println("ETH Started"); + //set eth hostname here + ETH.setHostname("esplan"); + break; + case ARDUINO_EVENT_ETH_CONNECTED: + Serial.println("ETH Connected"); + break; + case ARDUINO_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 ARDUINO_EVENT_ETH_DISCONNECTED: + Serial.println("ETH Disconnected"); + eth_connected = false; + break; + case ARDUINO_EVENT_ETH_STOP: + Serial.println("ETH Stopped"); + eth_connected = false; + break; + default: + break; + } +} + +void readFile(fs::FS &fs, const char *path) +{ + char text[100] = {0}; + char out[100] = {0}; + uint8_t i = 0; + Serial.printf("Reading file: %s\n", path); + + File file = fs.open(path); + 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); + sprintf(out, "

%s

", 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) +{ + Serial.printf("Writing file: %s\n", path); + + File file = fs.open(path, FILE_WRITE); + if (!file) + { + Serial.println("Failed to open file for writing"); + return; + } + if (file.print(message)) + { + Serial.println("File written"); + } + else + { + Serial.println("Write failed"); + } + file.close(); +} + +uint8_t appendFile(fs::FS &fs, const char *path, const char *message) +{ + Serial.printf("Appending to file: %s\n", path); + + File file = fs.open(path, FILE_APPEND); + if (!file) + { + Serial.println("Failed to open file for appending"); + return 1; + } + if (file.print(message)) + { + Serial.println("Message appended"); + file.close(); + return 0; + } + else + { + Serial.println("Append failed"); + file.close(); + return 1; + } +} + +void handleRoot() +{ + server.setContentLength(CONTENT_LENGTH_UNKNOWN); + server.send_P(200, "text/html", index_html); + spi.begin(SCK, MISO, MOSI, CS); + if (SD.begin(CS, spi)) + { + Serial.println("SD Card initialized."); + readFile(SD, "/output.txt"); + } + else + { + server.sendContent("

SD card not found!

"); + } + server.sendContent(""); + server.sendContent(""); // This ends sending + SD.end(); + spi.end(); +} + +void handleSubmit() +{ + char text[100] = {0}; + spi.begin(SCK, MISO, MOSI, CS); + 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); + } + } + SD.end(); + spi.end(); + handleRoot(); +} + +void handleNotFound() +{ + String message = "Error\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); +} + +void DNS_setup() +{ + if (MDNS.begin(host)) + { + MDNS.addService("http", "tcp", 80); + Serial.println("MDNS responder started"); + Serial.print("You can now connect to http://"); + Serial.print(host); + Serial.println(".local"); + } +} + +void setup() +{ + Serial.begin(115200); + // For reseting LAN8720A + pinMode(ETH_NRST_PIN, OUTPUT); + digitalWrite(ETH_NRST_PIN, LOW); + delay(500); + digitalWrite(ETH_NRST_PIN, HIGH); + + WiFi.onEvent(WiFiEvent); + ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_PHY_LAN8720, ETH_CLOCK_GPIO17_OUT); + + DNS_setup(); + + server.on("/", handleRoot); // Main page + + server.on("/get", handleSubmit); // Function done on GET request + + server.onNotFound(handleNotFound); // Function done when hangle is not found + + server.begin(); + Serial.println("HTTP server started"); +} + +void loop() +{ + if(eth_connected) + { + server.handleClient(); + } + delay(2); // allow the cpu to switch to other tasks +} \ No newline at end of file diff --git a/SW/ESPlan_webserver_SD_read/page.h b/SW/ESPlan_webserver_SD_read/page.h new file mode 100644 index 0000000..de0a7c5 --- /dev/null +++ b/SW/ESPlan_webserver_SD_read/page.h @@ -0,0 +1,41 @@ +#ifndef page_h +#define page_h + + +const char index_html[] PROGMEM = R"rawliteral( + + + + + ESPlan Webserver + + + + LaskaKit +

Text to save on SD card:

+
+ + +
+

Text on SD card:

+)rawliteral"; + +#endif \ No newline at end of file diff --git a/SW/esplan_esphome.yaml b/SW/esplan_esphome.yaml new file mode 100644 index 0000000..405a146 --- /dev/null +++ b/SW/esplan_esphome.yaml @@ -0,0 +1,51 @@ +esphome: + name: esplan + friendly_name: ESPlan + +esp32: + board: esp32dev + framework: + type: arduino + +# Enable logging +logger: + +# Enable Home Assistant API +api: + encryption: + key: "ADD_YOUR_KEY" + +ota: + +# Sensor example +i2c: + sda: 33 + scl: 32 + id: bus_a + +sensor: + - platform: sht4x + i2c_id: bus_a + address: 0x44 + temperature: + name: "Temperature" + id: temp + + humidity: + name: "Humidity" + id: humidity + +# Configuration +ethernet: + type: LAN8720 + mdc_pin: GPIO23 + mdio_pin: GPIO18 + clk_mode: GPIO17_OUT + phy_addr: 0 + +# # Optional manual IP +# manual_ip: +# static_ip: 192.168.1.123 +# gateway: 192.168.1.1 +# subnet: 255.255.255.0 + \ No newline at end of file