energy_indicator, doorbell4-esp32, nightlight

This commit is contained in:
2022-12-27 19:17:20 +01:00
parent 9d3b77de0f
commit fb5197a4fa
5 changed files with 476 additions and 25 deletions
+5 -8
View File
@@ -1,9 +1,6 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
// FIXME BUILTIN_LED muss heißen LED_BUILTIN - https://desertbot.io/blog/d1-mini-blink
const int optocoupler = D2; const int optocoupler = D2;
const char* ssid = "LeWe"; const char* ssid = "LeWe";
@@ -17,8 +14,8 @@ int lightStatus = 0; // 0 off, 1 on, 2 pulse
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
pinMode(BUILTIN_LED, OUTPUT); // initialize onboard LED as output pinMode(LED_BUILTIN, OUTPUT); // initialize onboard LED as output
digitalWrite(BUILTIN_LED, HIGH); digitalWrite(LED_BUILTIN, HIGH);
pinMode(optocoupler, OUTPUT); pinMode(optocoupler, OUTPUT);
digitalWrite(optocoupler, LOW); digitalWrite(optocoupler, LOW);
@@ -34,16 +31,16 @@ void setup() {
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
Serial.print("."); Serial.print(".");
delay(250); delay(250);
digitalWrite(BUILTIN_LED, LOW); digitalWrite(LED_BUILTIN, LOW);
delay(250); delay(250);
digitalWrite(BUILTIN_LED, HIGH); digitalWrite(LED_BUILTIN, HIGH);
} }
Serial.println(); Serial.println();
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true); WiFi.setAutoReconnect(true);
WiFi.persistent(true); WiFi.persistent(true);
digitalWrite(BUILTIN_LED, HIGH); digitalWrite(LED_BUILTIN, HIGH);
server.on("/click", handle_click); server.on("/click", handle_click);
server.on("/light", handle_light); server.on("/light", handle_light);
+17 -17
View File
@@ -16,24 +16,24 @@ void setup()
void loop() void loop()
{ {
pixels.setBrightness(20); //die Helligkeit setzen 0 dunkel -> 255 ganz hell // pixels.setBrightness(20); //die Helligkeit setzen 0 dunkel -> 255 ganz hell
pixels.setPixelColor(0, pixels.Color(255, 255, 255)); // pixels.setPixelColor(0, pixels.Color(255, 255, 255));
pixels.show(); // pixels.show();
delay(1000); // delay(1000);
//
// pixels.setBrightness(80); //die Helligkeit setzen 0 dunkel -> 255 ganz hell
// pixels.setPixelColor(0, pixels.Color(255, 255, 255));
// pixels.show();
// delay(1000);
//
// pixels.setPixelColor(0, pixels.Color(226, 0, 200));
// for (int i = 1; i <= 250; i++) {
// pixels.setBrightness(i);
// pixels.show();
// delay(20);
// }
pixels.setBrightness(80); //die Helligkeit setzen 0 dunkel -> 255 ganz hell pixels.setBrightness(10);
pixels.setPixelColor(0, pixels.Color(255, 255, 255));
pixels.show();
delay(1000);
pixels.setPixelColor(0, pixels.Color(226, 0, 200));
for (int i = 1; i <= 250; i++) {
pixels.setBrightness(i);
pixels.show();
delay(20);
}
pixels.setBrightness(20);
for (int hsv = 0; hsv < 65535; hsv++) { for (int hsv = 0; hsv < 65535; hsv++) {
pixels.setPixelColor(0, pixels.gamma32(pixels.ColorHSV(hsv))); pixels.setPixelColor(0, pixels.gamma32(pixels.ColorHSV(hsv)));
pixels.show(); pixels.show();
+179
View File
@@ -0,0 +1,179 @@
/**
* WiFi Doorbell for ESP32
* by Tilman Liero
* www.tilman.de
**/
#include <DFRobotDFPlayerMini.h>
#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
DFRobotDFPlayerMini dfPlayer;
int volume = 22;
const char* ssid = "SSID";
const char* password = "PASSWORD";
WiFiServer server(80);
String header;
String webhook = "http://www.google.com/";
WiFiClient webClient;
HTTPClient http;
void setup()
{
// start serial output
Serial.begin(115200);
Serial.println();
Serial.println("Connect dfplayer");
Serial1.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX
dfPlayer.begin(Serial1);
delay(100);
dfPlayer.setTimeOut(500); // Set serial communication time out 500ms
dfPlayer.volume(volume); // Set volume value (0~30).
dfPlayer.EQ(DFPLAYER_EQ_NORMAL);
dfPlayer.outputDevice(DFPLAYER_DEVICE_SD);
Serial.println("Connect WiFi");
WiFi.hostname("Doorbell");
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();
delay(500);
dfPlayer.play(2); // Play the second mp3
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) {
dfPlayer.play(1); // play the first mp3
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);
if (fileNo <= 1) {
dfPlayer.play(1);
}
else {
dfPlayer.play(fileNo);
}
}
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();
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("<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>Volume: <a href=\"/setvolume/" + String(volume-1) + "\"><button class=\"button2\">&minus;</button></a> " + String(volume) + " <a href=\"/setvolume/" + String(volume+1) + "\"><button class=\"button2\">&plus;</button></a></p>");
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("");
}
}
+135
View File
@@ -0,0 +1,135 @@
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Arduino_JSON.h>
#define COLORSTEPS 24
#define PIN D2
#define NUMPIXELS 12
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
const char* ssid = "LeWe";
const char* password = "5275274365464843";
ESP8266WebServer server(80);
String lastcommand = "none";
void setup() {
Serial.begin(115200);
delay(150);
Serial.println("ENERGY INDICATOR");
pinMode(LED_BUILTIN, OUTPUT); // initialize onboard LED as output
digitalWrite(LED_BUILTIN, HIGH); // on D1 mini LED_BUILTIN = HIGH means off
pixels.begin(); // initialize NeoPixel library.
Serial.println("Connect WiFi");
WiFi.hostname("EnergyIndicator");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
server.on("/", handle_root);
server.on("/color", handle_color);
server.on("/rainbow", handle_rainbow);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handle_root() {
String message = "ENERGY INDICATOR\n[";
message += pixels.getPixelColor(0);
for (int i = 1; i < NUMPIXELS; i++) {
message += ", ";
message += pixels.getPixelColor(i);
}
message += "]\n \n";
message += "Last command: ";
message += lastcommand;
server.send(200, "text/plain", message);
}
void handle_color() {
if (server.method() == HTTP_GET and server.hasArg("rgb") and server.arg("rgb").length() == 6) {
int r = hexToDec(server.arg("rgb").substring(0,2));
int g = hexToDec(server.arg("rgb").substring(2,4));
int b = hexToDec(server.arg("rgb").substring(4));
Serial.println(server.arg("rgb"));
Serial.println(r);
Serial.println(g);
Serial.println(b);
pixels.fill(pixels.Color(r,g,b), 0, 0);
pixels.show();
server.send(200, "text/plain", "Done");
}
else if (server.method() == HTTP_POST and server.hasArg("plain")) {
Serial.println(server.arg("plain"));
JSONVar json = JSON.parse(server.arg("plain"));
pixels.fill(pixels.Color(json["r"],json["g"],json["b"]), 0, 0);
pixels.show();
server.send(202, "text/plain", "Done");
}
}
void handle_rainbow() {
pixels.rainbow(0);
pixels.setBrightness(80);
pixels.show();
server.send(200, "text/plain", "Rainbow");
}
void handle_NotFound(){
server.send(404, "text/plain", "Energy Indicator: Page not found");
}
// https://github.com/benrugg/Arduino-Hex-Decimal-Conversion/blob/master/hex_dec.ino
unsigned int hexToDec(String hexString) {
unsigned int decValue = 0;
int nextInt;
for (int i = 0; i < hexString.length(); i++) {
nextInt = int(hexString.charAt(i));
if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
nextInt = constrain(nextInt, 0, 15);
decValue = (decValue * 16) + nextInt;
}
return decValue;
}
+140
View File
@@ -0,0 +1,140 @@
#include <Adafruit_NeoPixel.h>
#define COLORSTEPS 24
#define PIN D2
#define NUMPIXELS 12
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int ledstate[NUMPIXELS];
void setup() {
Serial.begin(115200);
delay(150);
Serial.println("PIXELS");
pixels.begin(); // initialize NeoPixel library.
pixels.setBrightness(40);
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
pixels.rainbow(firstPixelHue);
pixels.show();
delay(10);
}
//rainbow();
}
void rainbow() {
pixels.setBrightness(10);
for (int i = 0; i < NUMPIXELS; i++) {
ledstate[i] = 0;
}
int increment = 65536 / COLORSTEPS;
while (ledstate[NUMPIXELS-1] < 65536) {
// find first inactive pixel, if any
int ix = NUMPIXELS-1;
while (ix > 0 and ledstate[ix-1] == 0) {
ix--;
}
for (int iy = ix; iy >= 0; iy--) {
ledstate[iy] = ledstate[iy] + increment;
}
for (int i = 0; i < NUMPIXELS; i++) {
if (ledstate[i] < 65536 and i <= ix) {
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(ledstate[i])));
}
else {
pixels.setPixelColor(i, 0, 0, 0);
}
}
pixels.show();
delay(50);
}
delay(1000);
pixels.setPixelColor(1, pixels.Color(1, 1, 1));
pixels.setPixelColor(2, pixels.Color(31, 31, 31));
pixels.setPixelColor(3, pixels.Color(63, 63, 63));
pixels.setPixelColor(4, pixels.Color(95, 95, 95));
pixels.setPixelColor(5, pixels.Color(127, 127, 127));
pixels.setPixelColor(6, pixels.Color(159, 159, 159));
pixels.setPixelColor(7, pixels.Color(191, 191, 191));
pixels.setPixelColor(8, pixels.Color(223, 223, 223));
pixels.setPixelColor(9, pixels.Color(255, 255, 255));
pixels.show();
delay(3000);
for (int i = 30; i < 256; i++) {
for (int j = 0; j < NUMPIXELS; j++) {
pixels.setPixelColor(j, pixels.Color(i, i, i));
}
pixels.show();
delay(5);
}
delay(3000);
pixels.fill(pixels.Color(217, 101, 0), 0, NUMPIXELS);
// for (int i = 0; i < NUMPIXELS; i++) {
// pixels.setPixelColor(i, pixels.Color(255, 234, 0));
// }
pixels.show();
delay(2000);
pixels.setBrightness(20);
pixels.show();
delay(2000);
pixels.setBrightness(10);
pixels.show();
delay(2000);
pixels.setBrightness(5);
pixels.show();
delay(2000);
pixels.setBrightness(1);
pixels.show();
}
void loop() {
// pixels.setBrightness(20);
//
// pixels.fill(pixels.Color(255,0,0), 0, 0);
// pixels.show();
// delay(3000);
//
// pixels.fill(pixels.Color(0,255,0), 0, 0);
// pixels.show();
// delay(3000);
//
// pixels.fill(pixels.Color(0,0,255), 0, 0);
// pixels.show();
// delay(3000);
//
// pixels.fill(pixels.Color(255,255,0), 0, 0);
// pixels.show();
// delay(3000);
//
// pixels.fill(pixels.Color(0,255,255), 0, 0);
// pixels.show();
// delay(3000);
//
// pixels.fill(pixels.Color(255,0,255), 0, 0);
// pixels.show();
// delay(3000);
//
// pixels.fill(pixels.Color(255,255,255), 0, 0);
// pixels.show();
// delay(3000);
}