basser/basser.ino

88 lines
2.6 KiB
C++

#include <ESP8266WiFi.h>
const char* ssid = "CTDO-g"; //Netzwerk Name
const char* password = "ctdo2342"; //Wifi Passwort
const char* host = "rpi3.raum.ctdo.de"; //MPD Host
const int httpPort = 6600; //MPD Port
const int buttonPin = 5; //Pin vom Mikrocontroller
int buttonState;
bool previousState;
int previousMillis;
int currentMillis;
void setup() {
Serial.begin(115200); //Open the Serail Connection for Debugging
delay(10);
pinMode(buttonPin, INPUT); //Set the Pin to INPUT for checking the Status
Serial.print("Connecting to "); //Print the SSID
Serial.println(ssid);
WiFi.mode(WIFI_STA); //Open the Wifi connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("."); //Print Dots while Connecting
}
Serial.println("");
Serial.println("WiFi connected"); //Selfexplaining
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
currentMillis = millis(); //Save the current Time
buttonState = digitalRead(buttonPin); //Check the Pin Status
if (currentMillis - previousMillis > 500) { //Atleast all 500 Milliseconds
if (buttonState == HIGH && previousState == false) { //If Button pressed
mpdbass(true); //Make Bass
previousState = true;
} else if (buttonState == LOW && previousState == true) { //If Button released
mpdbass(false); //stop Bass
previousState = false;
}
}
}
void mpdbass(bool start) {
WiFiClient client; //Open Connection
if (!client.connect(host, httpPort)) { //When not connected
Serial.println("connection failed"); //Error
return;
}
unsigned long timeout = millis();
while (client.available() == 0) { //While not connected
if (millis() - timeout > 5000) { //If 5 seconds
Serial.println(">>> Client Timeout !"); // Timeout
client.stop();
return;
}
}
while(client.available()){ //Print Client Foo
String line = client.readStringUntil('\r');
Serial.print(line);
}
if (start == true){ //If Button pressed
Serial.println("Play");
client.println("addid \"users/lucas/Alben/Function/Incubation/01 Voiceprint.flac\" 0"); //Adds the Song as First Song
client.println("play 0"); //Play the First song
client.println("setvol 100"); //set Max volume
} else if (start == false) {
Serial.println("Remove");
client.println("delete 0"); //Remove the first Song (The Bass Song)
client.println("setvol 60"); //Set back to 60
}
while(client.available()){ //Print Client Foo
String line = client.readStringUntil('\r');
Serial.print(line);
}
}