/********************************************************************************
Transistor used for piezo buzzer:
https://media.digikey.com/pdf/Data%20Sheets/ON%20Semiconductor%20PDFs/PN2222.pdf
********************************************************************************/
/*******************************************
Level shifter used for LCD display:
https:www.ti.com/lit/ds/symlink/txs0108e.pdf
********************************************/
/*****************************
Time API
https://www.worldtimeapi.org/
*****************************/
/***********************
Weather API
https://open-meteo.com/
***********************/
#include "Clock.h"
#include "Weather.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
//#include <Keypad.h>
/*Touchpad
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include <touch_pad.h">
*/
//Audio-Streams herstellen
#include <ESPAsyncWebServer.h>
//#include <ESP32Audio.h>
/*Definition der Klassen*/
//LCD Bildschirm
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
//Sensor Pins
#define PIR_SENSOR_PIN 1
#define BUZZER_PIN 10
#define LED_PIN 8
int pirState = LOW; // we start, assuming no motion detected
int status = 0; // variable for reading the pin status
//Tag für Log-Meldungen
//static const char *TAG = "touch_example";
// Counter
volatile int motion_counter = 0;
uint8_t state;
unsigned long clockTimer;
unsigned long weatherAPItimer;
unsigned long weatherDisplayTimer;
const char* password = "IoTFakW-ztrewq42";
const char* ssid = "HSMA-IoT";
uint8_t valIndex;
uint8_t cursorPos;
char entered_value_sender [2];
/*
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 1, 0, 3, 2 };
uint8_t rowPins[ROWS] = { 4, 5, 6, 7 };
*/
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
Clock rtc(&lcd);
//Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//structure for station list
typedef struct {
char * url; //stream url
char * name; //stations name
} Sender;
#define SENDER_ANZAHL 5 //number of stations in tzhe list
//station list can easily be modified to support other stations
Sender senderListe[SENDER_ANZAHL] PROGMEM = {
{"http://icecast.ndr.de/ndr/ndr2/niedersachsen/mp3/128/stream.mp3","NDR2 Niedersachsen"},
{"http://icecast.ndr.de/ndr/ndr1niedersachsen/hannover/mp3/128/stream.mp3","NDR1 Hannover"},
{"http://wdr-1live-live.icecast.wdr.de/wdr/1live/live/mp3/128/stream.mp3","WDR1"},
{"http://wdr-cosmo-live.icecast.wdr.de/wdr/cosmo/live/mp3/128/stream.mp3","WDR COSMO"},
{"http://radiohagen.cast.addradio.de/radiohagen/simulcast/high/stream.mp3","Radio Hagen"}};
/*
{"http://st01.sslstream.dlf.de/dlf/01/128/mp3/stream.mp3","Deutschlandfunk"},
{"http://dispatcher.rndfnk.com/br/br1/nbopf/mp3/low","Bayern1"},
{"http://dispatcher.rndfnk.com/br/br3/live/mp3/low","Bayern3"},
{"http://dispatcher.rndfnk.com/hr/hr3/live/mp3/48/stream.mp3","Hessen3"},
{"http://stream.antenne.de/antenne","Antenne Bayern"},
{"http://stream.1a-webradio.de/saw-deutsch/","Radio 1A Deutsche Hits"},
{"http://stream.1a-webradio.de/saw-rock/","Radio 1A Rock"},
{"http://streams.80s80s.de/ndw/mp3-192/streams.80s80s.de/","Neue Deutsche Welle"},
{"http://dispatcher.rndfnk.com/br/brklassik/live/mp3/low","Bayern Klassik"},
{"http://mdr-284280-1.cast.mdr.de/mdr/284280/1/mp3/low/stream.mp3","MDR"},
{"http://icecast.ndr.de/ndr/njoy/live/mp3/128/stream.mp3","N-JOY"},
{"http://dispatcher.rndfnk.com/rbb/rbb888/live/mp3/mid","RBB"},
{"http://dispatcher.rndfnk.com/rbb/antennebrandenburg/live/mp3/mid","Antenne Brandenburg"},
{"http://wdr-wdr3-live.icecastssl.wdr.de/wdr/wdr3/live/mp3/128/stream.mp3","WDR3"},
{"http://wdr-wdr2-aachenundregion.icecastssl.wdr.de/wdr/wdr2/aachenundregion/mp3/128/stream.mp3","WDR 2"},
{"http://rnrw.cast.addradio.de/rnrw-0182/deinschlager/low/stream.mp3","NRW Schlagerradio"},
{"http://rnrw.cast.addradio.de/rnrw-0182/deinrock/low/stream.mp3","NRW Rockradio"},
{"http://rnrw.cast.addradio.de/rnrw-0182/dein90er/low/stream.mp3","NRW 90er"},
{"http://mp3.hitradiort1.c.nmdn.net/rt1rockwl/livestream.mp3","RT1 Rock"},
{"http://sluchaj.radiorodzina.pl/RadioRodzinaWroclawLIVE.mp3","Polen"};
*/
// Audio-Ausgabe
//AudioOutputI2S audioOutput;
// HTTP-Server
AsyncWebServer server(80);
// Aktueller Senderindex
int aktuellerSenderindex = 0;
/* Funktion zur Initialisierung des Audiostreams
void setupAudioStream(const char* url) {
AudioGeneratorMP3* mp3 = new AudioGeneratorMP3();
mp3->begin(url, &audioOutput);
while (mp3->isRunning()) {
if (!mp3->loop()) break;
}
delete mp3;
}
*/
// Handler für die Auswahl des Senders
void auswahlSender(AsyncWebServerRequest *request) {
if (request->hasParam("index")) {
int index = request->getParam("index")->value().toInt();
if (index >= 0 && index < SENDER_ANZAHL) {
aktuellerSenderindex = index;
const char* url = senderListe[aktuellerSenderindex].url;
//setupAudioStream(url);
request->send(200, "text/plain", "OK");
} else {
request->send(400, "text/plain", "Invalid sender index");
}
} else {
request->send(400, "text/plain", "Missing sender index");
}
}
// Handler zum Umschalten der Sender
void umschaltenSender(AsyncWebServerRequest *request) {
aktuellerSenderindex++;
if (aktuellerSenderindex >= SENDER_ANZAHL) {
aktuellerSenderindex = 0;
}
const char* url = senderListe[aktuellerSenderindex].url;
//setupAudioStream(url);
request->send(200, "text/plain", "OK");
}
// Funktion zum Einrichten des HTTP-Servers
void setupServer() {
server.on("/select", HTTP_GET, auswahlSender);
server.on("/switch", HTTP_GET, umschaltenSender);
server.onNotFound([](AsyncWebServerRequest *request){
request->send(404, "text/plain", "Not found");
});
server.begin();
}
//show name of current station on LCD display
//show the speaker symbol in front if current station = active station
void anzeigeSender() {
lcd.clear();
lcd.setCursor(0,0);
String name = String(senderListe[aktuellerSenderindex].name);
if (name.length() < 15)
lcd.print("Läuft" + name);
else {
uint8_t p = name.lastIndexOf(" ",15); //if name does not fit, split line on space
lcd.print(name.substring(0,p));
lcd.setCursor(0,1);
lcd.print(name.substring(p+1,p+17));
}
}
void bewegungErkannt()
{
status = digitalRead(PIR_SENSOR_PIN); // read input value
if (status == HIGH) {
digitalWrite(LED_PIN, HIGH); // turn LED ON
//umschaltenSender(NULL);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bewegung erkannt");
delay(1000);
lcd.clear();
anzeigeSender();
delay(5200);
lcd.clear();
if (pirState == LOW) {
// we have just turned on
Serial.println("Neuer Sender: " + String(aktuellerSenderindex));
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(LED_PIN, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Umschalten beendet");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(PIR_SENSOR_PIN, INPUT);
Wire.begin(18, 19);
// Initialisierung der Audioausgabe
//ESP32Audio::i2s_init();
//audioOutput.SetPinout(26, 25, 22);
lcd.init();
lcd.backlight();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
lcd.print(".");
delay(1000);
}
lcd.clear();
if (!rtc.setTimeFromAPI())
{
char t [] = __TIME__;
char compileTime [] = { t[0], t[1], t[3], t[4], t[6], t[7] };
rtc.setTime(compileTime);
}
getWeather(lcd);
printWeather(lcd);
// Initialisierung des HTTP-Servers
setupServer();
}
void loop()
{
if (state == 0)
{
unsigned long millisNow = millis();
if (millisNow - clockTimer >= 1000)
{
clockTimer = millisNow;
rtc.updateClock();
}
if (millisNow - weatherDisplayTimer >= 10000)
{
weatherDisplayTimer = millisNow;
printWeather(lcd);
}
if (millisNow - weatherAPItimer >= 3600000)
{
weatherAPItimer = millisNow;
getWeather(lcd);
}
}
bewegungErkannt();
}Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1