esp32
This commit is contained in:
69
src/WebContent.h
Normal file
69
src/WebContent.h
Normal file
@@ -0,0 +1,69 @@
|
||||
// HTML web page
|
||||
const char index_html[] = R"rawliteral(
|
||||
<!DOCTYPE HTML><html>
|
||||
<head>
|
||||
<title>Hanna Box</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style>
|
||||
body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;}
|
||||
.button {
|
||||
padding: 10px 20px;
|
||||
font-size: 24px;
|
||||
text-align: center;
|
||||
outline: none;
|
||||
color: #fff;
|
||||
background-color: #2f4468;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
box-shadow: 0 6px #999;
|
||||
cursor: pointer;
|
||||
-webkit-touch-callout: none;
|
||||
-webkit-user-select: none;
|
||||
-khtml-user-select: none;
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
user-select: none;
|
||||
-webkit-tap-highlight-color: rgba(0,0,0,0);
|
||||
}
|
||||
.button:hover {background-color: #1f2e45}
|
||||
.button:active {
|
||||
background-color: #1f2e45;
|
||||
box-shadow: 0 4px #666;
|
||||
transform: translateY(2px);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Greenhousino Irrigation System</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/>
|
||||
<button class="button" onmouseup="toggleCheckbox('on');" ontouchend="toggleCheckbox('on');">On!</button><br/><br/>
|
||||
<button class="button" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">Off!</button><br/><br/>
|
||||
<script>
|
||||
setInterval(getState, 500);
|
||||
function toggleCheckbox(x) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "/" + x, true);
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function getState() {
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
displayState(xhr.response);
|
||||
}
|
||||
}
|
||||
|
||||
xhr.open("GET","/state", true);
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function displayState(state) {
|
||||
document.getElementById("state").innerHTML = state;
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>)rawliteral";
|
||||
137
src/main.cpp
137
src/main.cpp
@@ -1,46 +1,66 @@
|
||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||
|
||||
// needed for library
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||
#include <WiFi.h>
|
||||
#include "ESPAsyncWebServer.h"
|
||||
//#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||
|
||||
#include "Audio.h"
|
||||
|
||||
#define I2S_DOUT 26 // connect to DAC pin DIN
|
||||
#define I2S_BCLK 27 // connect to DAC pin BCK
|
||||
#define I2S_LRC 25 // connect to DAC pin LCK
|
||||
|
||||
#include "AudioFileSourceSPIFFS.h"
|
||||
#include "AudioGeneratorMP3.h"
|
||||
#include "AudioOutputI2SNoDAC.h"
|
||||
#include "AudioFileSourceSD.h"
|
||||
|
||||
#include <SPI.h>
|
||||
#include <SD.h>
|
||||
|
||||
#include "WebContent.h"
|
||||
|
||||
File root;
|
||||
File mp3File;
|
||||
|
||||
void printDirectory(File dir, int numTabs);
|
||||
|
||||
AudioGeneratorMP3 *mp3;
|
||||
AudioFileSourceSPIFFS *file;
|
||||
AudioFileSourceSD *sdFile = NULL;
|
||||
AudioOutputI2S *out;
|
||||
|
||||
unsigned long lastStart = 0;
|
||||
|
||||
const int startDelay = 250;
|
||||
|
||||
void initMp3File() {
|
||||
file = new AudioFileSourceSPIFFS("/ex2.mp3");
|
||||
Audio audio;
|
||||
|
||||
// Set web server port number to 80
|
||||
AsyncWebServer server(80);
|
||||
|
||||
// Variable to store the HTTP request
|
||||
String header;
|
||||
|
||||
void playNextMp3() {
|
||||
File next = root.openNextFile();
|
||||
|
||||
if (!next) {
|
||||
root = SD.open("/");
|
||||
}
|
||||
if (String(next.name()).endsWith(".mp3")) {
|
||||
sdFile = new AudioFileSourceSD(next.name());
|
||||
|
||||
while (!String(next.name()).endsWith(".mp3")) {
|
||||
next = root.openNextFile();
|
||||
if (!next) {
|
||||
Serial.println("no more files found.");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
out = new AudioOutputI2S();
|
||||
mp3 = new AudioGeneratorMP3();
|
||||
Serial.print("initialized");
|
||||
Serial.print(next.name());
|
||||
Serial.print("\n");
|
||||
audio.stopSong();
|
||||
audio.connecttoSD(next.name());
|
||||
}
|
||||
|
||||
void audio_info(const char *info){
|
||||
//Serial.print("info "); Serial.println(info);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
// put your setup code here, to run once:
|
||||
@@ -48,54 +68,49 @@ void setup()
|
||||
|
||||
pinMode(D3, INPUT_PULLUP);
|
||||
|
||||
// WiFiManager
|
||||
// Local intialization. Once its business is done, there is no need to keep it around
|
||||
|
||||
|
||||
//WiFiManager wifiManager;
|
||||
|
||||
|
||||
// reset saved settings
|
||||
// wifiManager.resetSettings();
|
||||
AsyncWebServer server(80);
|
||||
//DNSServer dns;
|
||||
//first parameter is name of access point, second is the password
|
||||
//AsyncWiFiManager wifiManager(&server,&dns);
|
||||
|
||||
// set custom ip for portal
|
||||
// wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
||||
|
||||
// fetches ssid and pass from eeprom and tries to connect
|
||||
// if it does not connect it starts an access point with the specified name
|
||||
// here "AutoConnectAP"
|
||||
// and goes into a blocking loop awaiting configuration
|
||||
//wifiManager.autoConnect("AutoConnectAP");
|
||||
// or use this for auto generated name ESP + ChipID
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
|
||||
|
||||
// wifiManager.autoConnect();
|
||||
|
||||
/*
|
||||
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 :)");
|
||||
audioLogger = &Serial;
|
||||
}
|
||||
*/
|
||||
|
||||
WiFi.begin("heim", "0ggnWLS.");
|
||||
|
||||
Serial.print("Initializing SD card...");
|
||||
Serial.print("Initializing SD card...");
|
||||
|
||||
if (!SD.begin(D1)) {
|
||||
Serial.println("initialization failed!");
|
||||
Serial.println("SD initialization failed!");
|
||||
while (1);
|
||||
}
|
||||
Serial.println("initialization done.");
|
||||
Serial.println("SD initialization done.");
|
||||
|
||||
root = SD.open("/");
|
||||
|
||||
|
||||
//printDirectory(root, 0);
|
||||
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
|
||||
audio.setVolume(12); // 0...21
|
||||
|
||||
Serial.println("done!");
|
||||
|
||||
SPIFFS.begin();
|
||||
Serial.println("beginning mp3 setup...");
|
||||
initMp3File();
|
||||
|
||||
|
||||
// Send web page with input fields to client
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
request->send_P(200, "text/html", index_html);
|
||||
});
|
||||
}
|
||||
|
||||
void printDirectory(File dir, int numTabs) {
|
||||
@@ -125,27 +140,19 @@ void printDirectory(File dir, int numTabs) {
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
audio.loop();
|
||||
//Serial.print(digitalRead(D2));
|
||||
|
||||
if (digitalRead(D3)==LOW) {
|
||||
unsigned long now = millis();
|
||||
if (now-lastStart>startDelay) {
|
||||
if (mp3->isRunning()) {
|
||||
mp3->stop();
|
||||
}
|
||||
initMp3File();
|
||||
mp3->begin(sdFile,out);
|
||||
Serial.print("started.");
|
||||
playNextMp3();
|
||||
Serial.println("mp3 started.");
|
||||
lastStart=now;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (mp3->isRunning())
|
||||
{
|
||||
if (!mp3->loop())
|
||||
mp3->stop();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user