Ethernet-Klingel
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
* dfPlayer auf Wemos D1: https://forum.arduino.cc/t/dfplayer-mini-mit-wemos-d1-uno/677980/3
|
||||
* Details dfPlayer: https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299#target_3
|
||||
* https://wolles-elektronikkiste.de/dfplayer-mini-ansteuerung-mit-dem-arduino
|
||||
* W5500 Ethernet Shield: https://www.arduino.cc/en/Tutorial/LibraryExamples/WebServer
|
||||
*/
|
||||
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
@@ -10,12 +13,15 @@
|
||||
|
||||
|
||||
DFRobotDFPlayerMini dfPlayer;
|
||||
int volume = 5;
|
||||
int volume = 22;
|
||||
|
||||
byte mac[] = { 0x02, 0x60, 0x0D, 0xF0, 0x0D, 0x04 };
|
||||
IPAddress ip(192, 168, 178, 60);
|
||||
EthernetServer server(80);
|
||||
|
||||
String header;
|
||||
String body;
|
||||
unsigned long lastring = 0;
|
||||
|
||||
void setup() {
|
||||
WiFi.mode(WIFI_OFF);
|
||||
@@ -23,20 +29,21 @@ void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
Serial.println("Starting server");
|
||||
|
||||
|
||||
Ethernet.init(5); // W5500 CS PIN auf D1
|
||||
Ethernet.begin(mac, ip);
|
||||
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||
Serial.println("Ethernet shield not found");
|
||||
while (true) {
|
||||
delay(1);
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
if (Ethernet.linkStatus() == LinkOFF) {
|
||||
Serial.println("Ethernet cable is not connected.");
|
||||
|
||||
while (Ethernet.linkStatus() == LinkOFF) {
|
||||
Serial.println("Ethernet cable is not connected");
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
|
||||
Serial1.begin(9600);// TX liegt auf Pin D4 RX geht nicht !!
|
||||
|
||||
@@ -56,7 +63,6 @@ void setup() {
|
||||
void loop() {
|
||||
EthernetClient client = server.available();
|
||||
if (client) {
|
||||
Serial.println("new client");
|
||||
// an http request ends with a blank line
|
||||
bool currentLineIsBlank = true;
|
||||
|
||||
@@ -86,32 +92,136 @@ void loop() {
|
||||
}
|
||||
|
||||
Serial.write(c);
|
||||
header += c;
|
||||
|
||||
// if you've gotten to the end of the line (received a newline
|
||||
// character) and the line is blank, the http request has ended,
|
||||
// so you can send a reply
|
||||
if (c == '\n' && currentLineIsBlank) {
|
||||
if (header.indexOf("GET /status") >= 0) {
|
||||
client.println("HTTP/1.1 200 OK");
|
||||
client.println("Content-type: application/json; charset=utf-8");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
client.println("{");
|
||||
client.println(" \"online\": true,");
|
||||
client.print(" \"volume\": ");
|
||||
client.print(volume);
|
||||
client.println(",");
|
||||
client.print(" \"lastring\": ");
|
||||
if (lastring <= 0) {
|
||||
client.println(-1);
|
||||
}
|
||||
else {
|
||||
client.println(millis() - lastring);
|
||||
}
|
||||
client.println("}");
|
||||
break;
|
||||
}
|
||||
else if (header.indexOf("PUT /volume") >= 0) {
|
||||
while (client.connected()) {
|
||||
char c = client.read();
|
||||
body += c;
|
||||
if (c == '}') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String str1 = body;
|
||||
str1 = str1.substring(str1.indexOf("value"));
|
||||
str1 = str1.substring(str1.indexOf(":") + 1);
|
||||
while (str1.length() > 0 && str1.startsWith(" ")) {
|
||||
str1 = str1.substring(1);
|
||||
}
|
||||
|
||||
int ix = 0;
|
||||
while (isDigit(str1.charAt(ix))) {
|
||||
ix++;
|
||||
}
|
||||
|
||||
if (ix > 0) {
|
||||
volume = str1.substring(0, ix).toInt();
|
||||
if (volume < 0) {
|
||||
volume = 0;
|
||||
}
|
||||
else if (volume > 30) {
|
||||
volume = 30;
|
||||
}
|
||||
|
||||
dfPlayer.volume(volume);
|
||||
Serial.print("Volume set to ");
|
||||
Serial.println(volume);
|
||||
|
||||
client.println("HTTP/1.1 200 OK");
|
||||
}
|
||||
else {
|
||||
client.println("HTTP/1.1 400 Bad Request");
|
||||
}
|
||||
|
||||
client.println("Connection: close");
|
||||
break;
|
||||
}
|
||||
|
||||
// send a standard http response header
|
||||
client.println("HTTP/1.1 200 OK");
|
||||
client.println("Content-Type: text/plain");
|
||||
client.println("Connection: close"); // the connection will be closed after completion of the response
|
||||
client.println("Content-type: text/html; charset=utf-8");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
client.println("bonjour");
|
||||
dfPlayer.play(1);
|
||||
|
||||
// turns the GPIOs on and off
|
||||
if (header.indexOf("GET /bell/on") >= 0) {
|
||||
dfPlayer.play(1); // play the first mp3
|
||||
lastring = millis();
|
||||
}
|
||||
else if (header.indexOf("GET /setvolume/") >= 0) { // yes, I know this is not RESTful
|
||||
String str1 = header;
|
||||
str1 = str1.substring(header.indexOf("GET /setvolume/") + 15);
|
||||
volume = str1.substring(0, str1.indexOf(" ")).toInt();
|
||||
|
||||
if (volume < 0) {
|
||||
volume = 0;
|
||||
}
|
||||
else if (volume > 30) {
|
||||
volume = 30;
|
||||
}
|
||||
|
||||
dfPlayer.volume(volume);
|
||||
Serial.print("Volume set to ");
|
||||
Serial.println(volume);
|
||||
}
|
||||
|
||||
// Display the HTML web page
|
||||
client.println("<!DOCTYPE html><html>");
|
||||
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
|
||||
client.println("<link rel=\"icon\" href=\"data:,\">");
|
||||
client.println("<style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; }");
|
||||
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
|
||||
client.println(".button2 { background-color: #4CAF50; border: none; color: white; padding: 4px 10px; text-decoration: none; margin: 1px; cursor: pointer;}</style></head>");
|
||||
|
||||
client.println("<body><h1>LeWe Türklingel</h1>");
|
||||
client.println("<p>Volume: <a href=\"/setvolume/" + String(volume-1) + "\"><button class=\"button2\">−</button></a> " + String(volume) + " <a href=\"/setvolume/" + String(volume+1) + "\"><button class=\"button2\">+</button></a></p>");
|
||||
client.println("<p><a href=\"/bell/on\"><button class=\"button\">Klingeln</button></a></p>");
|
||||
client.println("</body></html>");
|
||||
|
||||
// The HTTP response ends with another blank line
|
||||
client.println();
|
||||
|
||||
break;
|
||||
}
|
||||
if (c == '\n') {
|
||||
// you're starting a new line
|
||||
currentLineIsBlank = true;
|
||||
} else if (c != '\r') {
|
||||
// you've gotten a character on the current line
|
||||
currentLineIsBlank = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
header = "";
|
||||
body = "";
|
||||
|
||||
// give the web browser time to receive the data
|
||||
delay(10);
|
||||
// close the connection:
|
||||
client.stop();
|
||||
Serial.println("client disconnected");
|
||||
Serial.println("Client disconnected");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user