Hackster is hosting Impact Spotlights: Industrial Automation. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Industrial Automation. Stream on Thursday!
Khiem NguyenValey DelvaLudovic
Published

Bose Adaptive Sound

We make the Bose speaker smarter by making it adjust the volume automatic.

BeginnerShowcase (no instructions)6 hours1,122
Bose Adaptive Sound

Things used in this project

Story

Read more

Schematics

Board layout

Code

Arduino

C/C++
This is code for the arduino.
// Initialize LEDS and Sound and button
int ledPins[] = {7,6,5,4};
const int soundPin = 0;
const int buttonPin = 3;

int sound;
int scale;
int num_limit = 0;

//Setup 
void setup()
{
  Serial.begin(9600);
  int index;

  for(index = 0; index <= 3; index++)
  {
    pinMode(ledPins[index],OUTPUT);
  }
  pinMode(buttonPin, INPUT);
}
// Constantly looping 
void loop()
{
 int buttonState;
 buttonState = digitalRead(buttonPin);
 // When the button pressed then print out "button PRESSED"
 if (buttonState == LOW) {
    Serial.println("button PRESSED"); 
    delay(200);
 }

 // Main program
 sound = analogRead(soundPin);
 int flag = 0;
 
 if (sound < 30) {
    flag = 0;
    digitalWrite(ledPins[0], HIGH);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
 } else if (sound > 30 && sound < 170) {
    flag = 1;
    //Serial.println("green LED turned on");
    digitalWrite(ledPins[0], HIGH);
    digitalWrite(ledPins[1], HIGH);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
 } else if (sound > 170 && sound < 200) {
    flag = 2;
    Serial.println("yellow LED turned on");
    digitalWrite(ledPins[0], HIGH);
    digitalWrite(ledPins[1], HIGH);
    digitalWrite(ledPins[2], HIGH);
    digitalWrite(ledPins[3], LOW);
 } else if (sound > 250) {
    flag = 3;
    if (num_limit == 5) {
      Serial.println("red LED turned on five times");  
      num_limit = 0;
    }
    Serial.println("red LED turned on");
    
    digitalWrite(ledPins[0], HIGH);
    digitalWrite(ledPins[1], HIGH);
    digitalWrite(ledPins[2], HIGH);
    digitalWrite(ledPins[3], HIGH);
    ++num_limit;
 } else {
    flag = 4;
    
    digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW); 
 }


 Serial.print(sound);
 Serial.print("   ");
 Serial.println(flag);

}

Python part

Python
This parts talks with the Bose api.
# importing packages
import os
import time
import serial
from libsoundtouch import soundtouch_device

# Clear the terminal screen before launch
os.system('cls')

# Function that will display BOSE's soundtouch device in realtime
def display_realtime_info():
    info = '''
              {0}
              [+] Host: {1}
              [+] IP: {2}
              [+] MAC: {3}
              [+] Type: {4}
              [+] Country Code: {5}
              [+] Device ID: {6}
              [+] Track: {7}
              [+] Track Image: {8}
              [+] Artist: {9}
              [+] Album: {10}
              [+] Play State: {11}
              [+] Source: {12}
              [+] Volume: {13}
              {0}
            '''.format("=" * 50, deviceInfo.name, deviceInfo.device_ip, deviceInfo.mac_address, deviceInfo.type, deviceInfo.country_code, deviceInfo.device_id, device.status().track, device.status().image, device.status().artist, device.status().album, device.status().play_status, device.status().source, device.volume().actual)
            
    os.system('cls')
    print (info)


# Main Function
if __name__ == '__main__':
    # Connecting to arduino and it's port 
    ard = serial.Serial('com4',9600)
    
    # SoundTouch ip address
    SoundTouch_Ip = '192.168.1.251' # Testing 
    
    # Ping the device to tell whether it's up or off
    if (os.system('ping -n 1 ' + SoundTouch_Ip)) == 0:
      
        # if true then connect to the device and print it's information
        device = soundtouch_device(SoundTouch_Ip)
        deviceInfo = device.config
        device_is_off = False
        while not device_is_off:
            gol = False
            redOn = False
            yellowOn = False
            info = '''
              {0}
              [+] Host: {1}
              [+] IP: {2}
              [+] MAC: {3}
              [+] Type: {4}
              [+] Country Code: {5}
              [+] Device ID: {6}
              [+] Track: {7}
              [+] Track Image: {8}
              [+] Artist: {9}
              [+] Album: {10}
              [+] Play State: {11}
              [+] Source: {12}
              [+] Volume: {13}
              {0}
            '''.format("=" * 50, deviceInfo.name, deviceInfo.device_ip, deviceInfo.mac_address, deviceInfo.type, deviceInfo.country_code, deviceInfo.device_id, device.status().track, device.status().image, device.status().artist, device.status().album, device.status().play_status, device.status().source, device.volume().actual)
            print (info)
            
            # Looping the output from arduino
            while not gol:
                
                temp_mes = ard.readline()
                
                if temp_mes == b'button PRESSED\r\n':
                    display_realtime_info()
                
                # if the red LED turned on then decrease the volume by 10
                if temp_mes == b'red LED turned on\r\n': 
                    volume = device.volume()
                    device.set_volume( int(volume.actual) - 10)
                    redOn = True
                    gol = True
                
                # if the yellow LED turned on then increase the volume by 10
                elif temp_mes == b'yellow LED turned on\r\n':
                    volume = device.volume()
                    max_volume = 20
                    if volume.actual < max_volume:
                        time.sleep(2)
                        device.set_volume( int(volume.actual) + 10)
                        yellowOn = True
                        gol = True
                    
                # For demo, when the red LED turned on 5 times then mute the volume
                elif temp_mes == b'red LED turned on five times\r\n':
                    volume = device.volume()
                    time.sleep(1)
                    device.pause()
                    device.mute()
                    time.sleep(3)
                    device.play()
                    device.set_volume(int(volume.actual))
                    gol = True
                
            # wait for 2 seconds
            time.sleep(2)
            
            # Clear the terminal again
            os.system('cls')
            if redOn:
                print("Decreased volume by 10")
                redOn = False
            if yellowOn:
                print("Increased volume by 10")
                yellowOn = False
    else:
        device.power_on()

Credits

Khiem Nguyen
1 project • 0 followers
I'm an intermediate python programmer and intermediate web developer
Contact
Valey Delva
1 project • 0 followers
Contact
Ludovic
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.