2016-11-28 16:39:15 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
|
2016-11-29 18:37:10 +00:00
|
|
|
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
|
|
|
|
|
2016-11-28 16:39:15 +00:00
|
|
|
int buttonState;
|
|
|
|
bool previousState;
|
|
|
|
int previousMillis;
|
|
|
|
int currentMillis;
|
|
|
|
|
|
|
|
void setup() {
|
2016-11-29 18:37:10 +00:00
|
|
|
Serial.begin(115200); //Open the Serail Connection for Debugging
|
2016-11-28 16:39:15 +00:00
|
|
|
delay(10);
|
2016-11-29 18:37:10 +00:00
|
|
|
pinMode(buttonPin, INPUT); //Set the Pin to INPUT for checking the Status
|
2016-11-28 16:39:15 +00:00
|
|
|
|
2016-11-29 18:37:10 +00:00
|
|
|
Serial.print("Connecting to "); //Print the SSID
|
2016-11-28 16:39:15 +00:00
|
|
|
Serial.println(ssid);
|
|
|
|
|
2016-11-29 18:37:10 +00:00
|
|
|
WiFi.mode(WIFI_STA); //Open the Wifi connection
|
2016-11-28 16:39:15 +00:00
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
2016-11-29 18:37:10 +00:00
|
|
|
Serial.print("."); //Print Dots while Connecting
|
2016-11-28 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("");
|
2016-11-29 18:37:10 +00:00
|
|
|
Serial.println("WiFi connected"); //Selfexplaining
|
2016-11-28 16:39:15 +00:00
|
|
|
Serial.println("IP address: ");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2016-11-29 18:37:10 +00:00
|
|
|
currentMillis = millis(); //Save the current Time
|
|
|
|
buttonState = digitalRead(buttonPin); //Check the Pin Status
|
2016-11-28 16:39:15 +00:00
|
|
|
|
2016-11-29 18:37:10 +00:00
|
|
|
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
|
2016-11-28 16:39:15 +00:00
|
|
|
previousState = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mpdbass(bool start) {
|
2016-11-29 18:37:10 +00:00
|
|
|
WiFiClient client; //Open Connection
|
|
|
|
if (!client.connect(host, httpPort)) { //When not connected
|
|
|
|
Serial.println("connection failed"); //Error
|
2016-11-28 16:39:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long timeout = millis();
|
2016-11-29 18:37:10 +00:00
|
|
|
while (client.available() == 0) { //While not connected
|
|
|
|
if (millis() - timeout > 5000) { //If 5 seconds
|
|
|
|
Serial.println(">>> Client Timeout !"); // Timeout
|
2016-11-28 16:39:15 +00:00
|
|
|
client.stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:37:10 +00:00
|
|
|
while(client.available()){ //Print Client Foo
|
2016-11-28 16:39:15 +00:00
|
|
|
String line = client.readStringUntil('\r');
|
|
|
|
Serial.print(line);
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:37:10 +00:00
|
|
|
if (start == true){ //If Button pressed
|
2016-11-28 16:39:15 +00:00
|
|
|
Serial.println("Play");
|
2016-11-29 18:37:10 +00:00
|
|
|
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
|
2016-11-29 18:46:07 +00:00
|
|
|
client.println("setvol 100"); //set Max volume
|
2016-11-28 16:39:15 +00:00
|
|
|
} else if (start == false) {
|
|
|
|
Serial.println("Remove");
|
2016-11-29 18:37:10 +00:00
|
|
|
client.println("delete 0"); //Remove the first Song (The Bass Song)
|
|
|
|
client.println("setvol 60"); //Set back to 60
|
2016-11-28 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:37:10 +00:00
|
|
|
while(client.available()){ //Print Client Foo
|
2016-11-28 16:39:15 +00:00
|
|
|
String line = client.readStringUntil('\r');
|
|
|
|
Serial.print(line);
|
|
|
|
}
|
2016-11-29 18:46:07 +00:00
|
|
|
}
|