From fb5197a4faec8ea0b17b4d7e6aa53ecc8aea895e Mon Sep 17 00:00:00 2001 From: Tilman Date: Tue, 27 Dec 2022 19:17:20 +0100 Subject: [PATCH] energy_indicator, doorbell4-esp32, nightlight --- D1 mini/On_Air/On_Air.ino | 13 +- WS2812/WS2812.ino | 34 ++--- doorbell4-esp32/doorbell4-esp32.ino | 179 ++++++++++++++++++++++++++ energy_indicator/energy_indicator.ino | 135 +++++++++++++++++++ nightlight/nightlight.ino | 140 ++++++++++++++++++++ 5 files changed, 476 insertions(+), 25 deletions(-) create mode 100644 doorbell4-esp32/doorbell4-esp32.ino create mode 100644 energy_indicator/energy_indicator.ino create mode 100644 nightlight/nightlight.ino diff --git a/D1 mini/On_Air/On_Air.ino b/D1 mini/On_Air/On_Air.ino index 249a075..5f554ca 100644 --- a/D1 mini/On_Air/On_Air.ino +++ b/D1 mini/On_Air/On_Air.ino @@ -1,9 +1,6 @@ #include #include - -// FIXME BUILTIN_LED muss heißen LED_BUILTIN - https://desertbot.io/blog/d1-mini-blink - const int optocoupler = D2; const char* ssid = "LeWe"; @@ -17,8 +14,8 @@ int lightStatus = 0; // 0 off, 1 on, 2 pulse void setup() { Serial.begin(9600); - pinMode(BUILTIN_LED, OUTPUT); // initialize onboard LED as output - digitalWrite(BUILTIN_LED, HIGH); + pinMode(LED_BUILTIN, OUTPUT); // initialize onboard LED as output + digitalWrite(LED_BUILTIN, HIGH); pinMode(optocoupler, OUTPUT); digitalWrite(optocoupler, LOW); @@ -34,16 +31,16 @@ void setup() { while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(250); - digitalWrite(BUILTIN_LED, LOW); + digitalWrite(LED_BUILTIN, LOW); delay(250); - digitalWrite(BUILTIN_LED, HIGH); + digitalWrite(LED_BUILTIN, HIGH); } Serial.println(); Serial.print("IP address: "); Serial.println(WiFi.localIP()); WiFi.setAutoReconnect(true); WiFi.persistent(true); - digitalWrite(BUILTIN_LED, HIGH); + digitalWrite(LED_BUILTIN, HIGH); server.on("/click", handle_click); server.on("/light", handle_light); diff --git a/WS2812/WS2812.ino b/WS2812/WS2812.ino index 6f45f50..22912e6 100644 --- a/WS2812/WS2812.ino +++ b/WS2812/WS2812.ino @@ -16,24 +16,24 @@ void setup() void loop() { - pixels.setBrightness(20); //die Helligkeit setzen 0 dunkel -> 255 ganz hell - pixels.setPixelColor(0, pixels.Color(255, 255, 255)); - pixels.show(); - delay(1000); +// pixels.setBrightness(20); //die Helligkeit setzen 0 dunkel -> 255 ganz hell +// pixels.setPixelColor(0, pixels.Color(255, 255, 255)); +// pixels.show(); +// 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.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); + pixels.setBrightness(10); for (int hsv = 0; hsv < 65535; hsv++) { pixels.setPixelColor(0, pixels.gamma32(pixels.ColorHSV(hsv))); pixels.show(); diff --git a/doorbell4-esp32/doorbell4-esp32.ino b/doorbell4-esp32/doorbell4-esp32.ino new file mode 100644 index 0000000..1bb0ae4 --- /dev/null +++ b/doorbell4-esp32/doorbell4-esp32.ino @@ -0,0 +1,179 @@ +/** + * WiFi Doorbell for ESP32 + * by Tilman Liero + * www.tilman.de + **/ + +#include +#include +#include +#include + +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(""); + client.println(""); + client.println(""); + client.println("Doorbell"); + client.println(""); + + client.println("

Doorbell

"); + client.println("

Volume: " + String(volume) + "

"); + client.println("

"); + client.println(""); + + // 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(""); + } +} diff --git a/energy_indicator/energy_indicator.ino b/energy_indicator/energy_indicator.ino new file mode 100644 index 0000000..ba8e5ce --- /dev/null +++ b/energy_indicator/energy_indicator.ino @@ -0,0 +1,135 @@ +#include +#include +#include +#include + +#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; +} diff --git a/nightlight/nightlight.ino b/nightlight/nightlight.ino new file mode 100644 index 0000000..a0bb2e6 --- /dev/null +++ b/nightlight/nightlight.ino @@ -0,0 +1,140 @@ +#include + +#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); +}