Update sketch_oct11a.ino

This commit is contained in:
fionera 2016-11-29 19:37:10 +01:00
parent eaeafd35b9
commit aa03dd5dce
1 changed files with 35 additions and 33 deletions

View File

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