webserver

This commit is contained in:
Stefan Ostermann 2023-06-04 12:49:25 +02:00
parent 0835956179
commit 2b97967326
3 changed files with 102 additions and 72 deletions

View File

@ -16,4 +16,6 @@ lib_deps =
esphome/ESP32-audioI2S@^2.0.7 esphome/ESP32-audioI2S@^2.0.7
me-no-dev/AsyncTCP@^1.1.1 me-no-dev/AsyncTCP@^1.1.1
me-no-dev/ESP Async WebServer@^1.2.3 me-no-dev/ESP Async WebServer@^1.2.3
alanswx/ESPAsyncWiFiManager@^0.31
monitor_speed = 115200 monitor_speed = 115200
board_build.partitions = huge_app.csv

View File

@ -1,8 +1,8 @@
// HTML web page // HTML web page
const char index_html[] = R"rawliteral( const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html> <!DOCTYPE HTML><html>
<head> <head>
<title>Hanna Box</title> <title>HannaBox</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<style> <style>
body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;} body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;}
@ -34,11 +34,11 @@ const char index_html[] = R"rawliteral(
</style> </style>
</head> </head>
<body> <body>
<h1>Greenhousino Irrigation System</h1> <h1>HannaBox</h1>
Pump state: <span id="state"></span><br/><br/>
<button class="button" onmousedown="toggleCheckbox('on');" ontouchstart="toggleCheckbox('on');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">PUMP!</button><br/><br/> Playing %PLAYING%<br/><br/>
<button class="button" onmouseup="toggleCheckbox('on');" ontouchend="toggleCheckbox('on');">On!</button><br/><br/> <button class="button" onmouseup="toggleCheckbox('start');" ontouchend="toggleCheckbox('start');">On!</button><br/><br/>
<button class="button" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">Off!</button><br/><br/> <button class="button" onmouseup="toggleCheckbox('stop');" ontouchend="toggleCheckbox('stop');">Off!</button><br/><br/>
<script> <script>
setInterval(getState, 500); setInterval(getState, 500);
function toggleCheckbox(x) { function toggleCheckbox(x) {

View File

@ -1,14 +1,17 @@
// needed for library #if defined(ESP8266)
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#else
#include <WiFi.h> #include <WiFi.h>
#include "ESPAsyncWebServer.h" #endif
//#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ESPAsyncWebServer.h> //Local WebServer used to serve the configuration portal
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include "Audio.h" #include "Audio.h"
#define I2S_DOUT 26 // connect to DAC pin DIN #define I2S_DOUT 26 // connect to DAC pin DIN
#define I2S_BCLK 27 // connect to DAC pin BCK #define I2S_BCLK 27 // connect to DAC pin BCK
#define I2S_LRC 25 // connect to DAC pin LCK #define I2S_LRC 25 // connect to DAC pin LCK
#include <SPI.h> #include <SPI.h>
#include <SD.h> #include <SD.h>
@ -28,24 +31,30 @@ Audio audio;
// Set web server port number to 80 // Set web server port number to 80
AsyncWebServer server(80); AsyncWebServer server(80);
AsyncWebServer server2(81);
DNSServer dns;
File next;
// Variable to store the HTTP request // Variable to store the HTTP request
String header; String header;
void playNextMp3() { void playNextMp3()
File next = root.openNextFile(); {
next = root.openNextFile();
if (!next) {
if (!next)
{
root = SD.open("/"); root = SD.open("/");
} }
while (!String(next.name()).endsWith(".mp3")) { while (!String(next.name()).endsWith(".mp3"))
{
next = root.openNextFile(); next = root.openNextFile();
if (!next) { if (!next)
{
Serial.println("no more files found."); Serial.println("no more files found.");
return; return;
} }
} }
Serial.print("initialized"); Serial.print("initialized");
Serial.print(next.name()); Serial.print(next.name());
@ -54,12 +63,31 @@ void playNextMp3() {
audio.connecttoSD(next.name()); audio.connecttoSD(next.name());
} }
void audio_info(const char *info){ void audio_info(const char *info)
//Serial.print("info "); Serial.println(info); {
// Serial.print("info "); Serial.println(info);
} }
String processor(const String& var)
{
if(var == "PLAYING")
return F(next.name());
return String();
}
void stop() {
if (audio.isRunning()) {
audio.stopSong();
}
}
void start() {
if (!next) {
playNextMp3();
} else {
audio.connecttoSD(next.name());
}
}
void setup() void setup()
{ {
@ -68,67 +96,69 @@ void setup()
pinMode(D3, INPUT_PULLUP); pinMode(D3, INPUT_PULLUP);
// first parameter is name of access point, second is the password
AsyncWebServer server(80); AsyncWiFiManager wifiManager(&server, &dns);
//DNSServer dns;
//first parameter is name of access point, second is the password
//AsyncWiFiManager wifiManager(&server,&dns);
//fetches ssid and pass and tries to connect wifiManager.autoConnect("HannaBox");
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
/*
if (!wifiManager.autoConnect())
{
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
//ESP.restart();
//delay(1000);
} else {
// if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
*/
WiFi.begin("heim", "0ggnWLS.");
Serial.print("Initializing SD card..."); Serial.print("Initializing SD card...");
if (!SD.begin(D1)) { if (!SD.begin(D1))
{
Serial.println("SD initialization failed!"); Serial.println("SD initialization failed!");
while (1); while (1)
;
} }
Serial.println("SD initialization done."); Serial.println("SD initialization done.");
root = SD.open("/"); root = SD.open("/");
//printDirectory(root, 0); // printDirectory(root, 0);
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(12); // 0...21 audio.setVolume(12); // 0...21
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send_P(200, "text/html", index_html,processor); });
// Send web page with input fields to client server.on("/start", HTTP_GET, [] (AsyncWebServerRequest *request) {
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ start();
request->send_P(200, "text/html", index_html); request->send(200, "text/plain", "start");
});
server.on("/stop", HTTP_GET, [] (AsyncWebServerRequest *request) {
stop();
request->send(200, "text/plain", "stop");
}); });
server.begin();
} }
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) { void printDirectory(File dir, int numTabs)
{
while (true)
{
File entry = dir.openNextFile();
if (!entry)
{
// no more files // no more files
break; break;
} }
for (uint8_t i = 0; i < numTabs; i++) { for (uint8_t i = 0; i < numTabs; i++)
{
Serial.print('\t'); Serial.print('\t');
} }
Serial.print(entry.name()); Serial.print(entry.name());
if (entry.isDirectory()) { if (entry.isDirectory())
{
Serial.println("/"); Serial.println("/");
printDirectory(entry, numTabs + 1); printDirectory(entry, numTabs + 1);
} else { }
else
{
// files have sizes, directories do not // files have sizes, directories do not
Serial.print("\t\t"); Serial.print("\t\t");
Serial.println(entry.size(), DEC); Serial.println(entry.size(), DEC);
@ -137,22 +167,20 @@ void printDirectory(File dir, int numTabs) {
} }
} }
void loop() void loop()
{ {
audio.loop(); audio.loop();
//Serial.print(digitalRead(D2)); // Serial.print(digitalRead(D2));
if (digitalRead(D3)==LOW) {
unsigned long now = millis();
if (now-lastStart>startDelay) {
playNextMp3();
Serial.println("mp3 started.");
lastStart=now;
}
if (digitalRead(D3) == LOW)
{
unsigned long now = millis();
if (now - lastStart > startDelay)
{
playNextMp3();
Serial.println("mp3 started.");
lastStart = now;
}
} }
} }