esp32
This commit is contained in:
parent
e495cdbd78
commit
0835956179
|
|
@ -1,4 +1,7 @@
|
||||||
# HannaBox
|
# HannaBox
|
||||||
|
Manual install of wifi manager download zip in project dir using vs console:
|
||||||
|
|
||||||
|
pio lib install ~/Downloads/WiFiManager-2.0.16-rc.2.zip
|
||||||
|
|
||||||
|
|
||||||
## Pins
|
## Pins
|
||||||
|
|
@ -9,6 +12,8 @@ D4 -> LRC
|
||||||
5V -> Vin
|
5V -> Vin
|
||||||
GND -> GND
|
GND -> GND
|
||||||
|
|
||||||
|
DAC Channel ESP32 = PIN 25
|
||||||
|
|
||||||
SD Card:
|
SD Card:
|
||||||
CS -> D1
|
CS -> D1
|
||||||
MOSI -> D7
|
MOSI -> D7
|
||||||
|
|
|
||||||
BIN
data/ex.mp3
BIN
data/ex.mp3
Binary file not shown.
BIN
data/ex2.mp3
BIN
data/ex2.mp3
Binary file not shown.
|
|
@ -8,11 +8,12 @@
|
||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:d1_mini]
|
[env:d1_mini32]
|
||||||
platform = espressif8266
|
platform = espressif32
|
||||||
board = d1_mini
|
board = wemos_d1_mini32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
tzapu/WiFiManager@^0.16.0
|
esphome/ESP32-audioI2S@^2.0.7
|
||||||
earlephilhower/ESP8266Audio@^1.9.7
|
me-no-dev/AsyncTCP@^1.1.1
|
||||||
|
me-no-dev/ESP Async WebServer@^1.2.3
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
|
||||||
|
|
@ -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";
|
||||||
129
src/main.cpp
129
src/main.cpp
|
|
@ -1,45 +1,65 @@
|
||||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
|
||||||
|
|
||||||
// needed for library
|
// needed for library
|
||||||
#include <DNSServer.h>
|
#include <WiFi.h>
|
||||||
#include <ESP8266WebServer.h>
|
#include "ESPAsyncWebServer.h"
|
||||||
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
|
//#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 <SPI.h>
|
||||||
#include <SD.h>
|
#include <SD.h>
|
||||||
|
|
||||||
|
#include "WebContent.h"
|
||||||
|
|
||||||
File root;
|
File root;
|
||||||
File mp3File;
|
File mp3File;
|
||||||
|
|
||||||
void printDirectory(File dir, int numTabs);
|
void printDirectory(File dir, int numTabs);
|
||||||
|
|
||||||
AudioGeneratorMP3 *mp3;
|
|
||||||
AudioFileSourceSPIFFS *file;
|
|
||||||
AudioFileSourceSD *sdFile = NULL;
|
|
||||||
AudioOutputI2S *out;
|
|
||||||
|
|
||||||
unsigned long lastStart = 0;
|
unsigned long lastStart = 0;
|
||||||
|
|
||||||
const int startDelay = 250;
|
const int startDelay = 250;
|
||||||
|
|
||||||
void initMp3File() {
|
Audio audio;
|
||||||
file = new AudioFileSourceSPIFFS("/ex2.mp3");
|
|
||||||
|
|
||||||
|
// Set web server port number to 80
|
||||||
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
|
// Variable to store the HTTP request
|
||||||
|
String header;
|
||||||
|
|
||||||
|
void playNextMp3() {
|
||||||
File next = root.openNextFile();
|
File next = root.openNextFile();
|
||||||
|
|
||||||
if (!next) {
|
if (!next) {
|
||||||
root = SD.open("/");
|
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()
|
void setup()
|
||||||
{
|
{
|
||||||
|
|
@ -48,54 +68,49 @@ void setup()
|
||||||
|
|
||||||
pinMode(D3, INPUT_PULLUP);
|
pinMode(D3, INPUT_PULLUP);
|
||||||
|
|
||||||
// WiFiManager
|
|
||||||
// Local intialization. Once its business is done, there is no need to keep it around
|
|
||||||
|
|
||||||
|
AsyncWebServer server(80);
|
||||||
|
//DNSServer dns;
|
||||||
|
//first parameter is name of access point, second is the password
|
||||||
|
//AsyncWiFiManager wifiManager(&server,&dns);
|
||||||
|
|
||||||
//WiFiManager wifiManager;
|
//fetches ssid and pass and tries to connect
|
||||||
|
|
||||||
|
|
||||||
// reset saved settings
|
|
||||||
// wifiManager.resetSettings();
|
|
||||||
|
|
||||||
// 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
|
//if it does not connect it starts an access point with the specified name
|
||||||
//here "AutoConnectAP"
|
//here "AutoConnectAP"
|
||||||
//and goes into a blocking loop awaiting configuration
|
//and goes into a blocking loop awaiting configuration
|
||||||
//wifiManager.autoConnect("AutoConnectAP");
|
|
||||||
// or use this for auto generated name ESP + ChipID
|
|
||||||
|
|
||||||
|
|
||||||
// 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
|
// if you get here you have connected to the WiFi
|
||||||
Serial.println("connected...yeey :)");
|
Serial.println("connected...yeey :)");
|
||||||
audioLogger = &Serial;
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
WiFi.begin("heim", "0ggnWLS.");
|
||||||
|
|
||||||
Serial.print("Initializing SD card...");
|
Serial.print("Initializing SD card...");
|
||||||
|
|
||||||
if (!SD.begin(D1)) {
|
if (!SD.begin(D1)) {
|
||||||
Serial.println("initialization failed!");
|
Serial.println("SD initialization failed!");
|
||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
Serial.println("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.setVolume(12); // 0...21
|
||||||
|
|
||||||
Serial.println("done!");
|
// Send web page with input fields to client
|
||||||
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
SPIFFS.begin();
|
request->send_P(200, "text/html", index_html);
|
||||||
Serial.println("beginning mp3 setup...");
|
});
|
||||||
initMp3File();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void printDirectory(File dir, int numTabs) {
|
void printDirectory(File dir, int numTabs) {
|
||||||
|
|
@ -125,27 +140,19 @@ void printDirectory(File dir, int numTabs) {
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
audio.loop();
|
||||||
//Serial.print(digitalRead(D2));
|
//Serial.print(digitalRead(D2));
|
||||||
|
|
||||||
if (digitalRead(D3)==LOW) {
|
if (digitalRead(D3)==LOW) {
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
if (now-lastStart>startDelay) {
|
if (now-lastStart>startDelay) {
|
||||||
if (mp3->isRunning()) {
|
playNextMp3();
|
||||||
mp3->stop();
|
Serial.println("mp3 started.");
|
||||||
}
|
|
||||||
initMp3File();
|
|
||||||
mp3->begin(sdFile,out);
|
|
||||||
Serial.print("started.");
|
|
||||||
lastStart=now;
|
lastStart=now;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp3->isRunning())
|
|
||||||
{
|
|
||||||
if (!mp3->loop())
|
|
||||||
mp3->stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue