garage_esp32: basic synchronous wifi
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
/*
|
||||
* Doorbell Speaker for WEMOS D1 mini with dfplayer
|
||||
* by Tilman Liero
|
||||
* www.tilman.de
|
||||
*
|
||||
* Sources:
|
||||
* dfplayer library: https://github.com/DFRobot/DFRobotDFPlayerMini via https://wolles-elektronikkiste.de/dfplayer-mini-ansteuerung-mit-dem-arduino
|
||||
* info on how to use dfplayer with WEMOS D1: https://forum.arduino.cc/t/dfplayer-mini-mit-wemos-d1-uno/677980/3
|
||||
* WEMOS D1 WiFi setup: https://averagemaker.com/2018/04/how-to-set-up-wifi-on-a-wemos.html
|
||||
@@ -42,7 +45,8 @@ void setup()
|
||||
dfPlayer.EQ(DFPLAYER_EQ_NORMAL);
|
||||
dfPlayer.outputDevice(DFPLAYER_DEVICE_SD);
|
||||
|
||||
Serial.println("Connect WiFi");
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
WiFi.hostname("Doorbell");
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
@@ -59,7 +63,7 @@ void setup()
|
||||
server.begin();
|
||||
delay(500);
|
||||
dfPlayer.play(2); // Play the second mp3
|
||||
|
||||
|
||||
Serial.println("Setup completed");
|
||||
}
|
||||
|
||||
@@ -67,7 +71,7 @@ void setup()
|
||||
void loop() {
|
||||
|
||||
WiFiClient client = server.available(); // Listen for incoming clients
|
||||
|
||||
|
||||
if (client) {
|
||||
String currentLine = ""; // make a String to hold incoming data from the client
|
||||
while (client.connected()) { // loop while the client's connected
|
||||
@@ -106,7 +110,7 @@ void loop() {
|
||||
dfPlayer.play(fileNo);
|
||||
}
|
||||
}
|
||||
else if (header.indexOf("GET /setvolume/") >= 0) {
|
||||
else if (header.indexOf("GET /setvolume/") >= 0) {
|
||||
String str1 = header;
|
||||
str1 = str1.substring(header.indexOf("GET /setvolume/") + 15);
|
||||
volume = str1.substring(0, str1.indexOf(" ")).toInt();
|
||||
@@ -122,7 +126,15 @@ void loop() {
|
||||
Serial.print("Volume set to ");
|
||||
Serial.println(volume);
|
||||
}
|
||||
|
||||
else if (header.indexOf("GET /restart") >= 0) {
|
||||
Serial.println("Restarting...");
|
||||
ESP.restart();
|
||||
}
|
||||
else if (header.indexOf("GET /dfreset") >= 0) {
|
||||
Serial.println("Reset dfplayer...");
|
||||
dfPlayer.reset();
|
||||
}
|
||||
|
||||
// Display the HTML web page
|
||||
client.println("<!DOCTYPE html><html>");
|
||||
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
#include <WiFi.h>
|
||||
#include <HTTPClient.h>
|
||||
|
||||
const char* ssid = "LeWe";
|
||||
const char* password = "5275274365464843";
|
||||
|
||||
WiFiServer server(80);
|
||||
String header;
|
||||
|
||||
WiFiClient webClient;
|
||||
String webhook = "http://www.google.com/";
|
||||
HTTPClient http;
|
||||
|
||||
|
||||
void setup() {
|
||||
// start serial output
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
btStop(); // turn off bluetooth
|
||||
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
WiFi.hostname("garage-esp32");
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("WiFi connected, IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
WiFi.setAutoReconnect(true);
|
||||
WiFi.persistent(true);
|
||||
|
||||
server.begin();
|
||||
Serial.println("Setup completed");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
WiFiClient client = server.available(); // Listen for incoming clients
|
||||
|
||||
if (client) {
|
||||
String currentLine = ""; // make a String to hold incoming data from the client
|
||||
while (client.connected()) { // loop while the client's connected
|
||||
if (client.available()) { // if there's bytes to read from the client,
|
||||
char c = client.read(); // read a byte, then
|
||||
//Serial.write(c); // print it out the serial monitor
|
||||
header += c;
|
||||
if (c == '\n') { // if the byte is a newline character
|
||||
// if the current line is blank, you got two newline characters in a row.
|
||||
// that's the end of the client HTTP request, so send a response:
|
||||
if (currentLine.length() == 0) {
|
||||
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
|
||||
// and a content-type so the client knows what's coming, then a blank line:
|
||||
client.println("HTTP/1.1 200 OK");
|
||||
client.println("Content-type: text/html; charset=utf-8");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
|
||||
// turns the GPIOs on and off
|
||||
if (header.indexOf("GET /bell/on") >= 0) {
|
||||
Serial.println("RING");
|
||||
}
|
||||
else if (header.indexOf("GET /play/") >= 0) { // yes, I know this is not RESTful
|
||||
String str1 = header;
|
||||
str1 = str1.substring(header.indexOf("GET /play/") + 10);
|
||||
int fileNo = str1.substring(0, str1.indexOf(" ")).toInt();
|
||||
|
||||
Serial.print("Play file ");
|
||||
Serial.println(fileNo);
|
||||
}
|
||||
else if (header.indexOf("GET /restart") >= 0) {
|
||||
Serial.println("Restarting...");
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
// 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("<title>Doorbell</title>");
|
||||
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>Doorbell</h1>");
|
||||
client.println("<p><a href=\"/bell/on\"><button class=\"button\">Ring</button></a></p>");
|
||||
client.println("</body></html>");
|
||||
|
||||
// The HTTP response ends with another blank line
|
||||
client.println();
|
||||
|
||||
// call webhook after ringing
|
||||
if (header.indexOf("GET /bell/on") >= 0) {
|
||||
|
||||
if (http.begin(webClient, webhook)) {
|
||||
int httpCode = http.GET();
|
||||
|
||||
if (httpCode > 0) {
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
String payload = http.getString();
|
||||
//Serial.println(payload);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str());
|
||||
}
|
||||
// close connection
|
||||
http.end();
|
||||
}
|
||||
else {
|
||||
Serial.println("Could not send HTTP request");
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
else { // if you got a newline, then clear currentLine
|
||||
currentLine = "";
|
||||
}
|
||||
}
|
||||
else if (c != '\r') { // if you got anything else but a carriage return character,
|
||||
currentLine += c; // add it to the end of the currentLine
|
||||
}
|
||||
}
|
||||
}
|
||||
// Clear the header variable
|
||||
header = "";
|
||||
// Close the connection
|
||||
client.stop();
|
||||
Serial.println("Client disconnected");
|
||||
Serial.println("");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user