S2 Spa Soother - Install Code and Test
If you followed along with the Adafruit tutorial on getting Pi Zero PWM audio, you should already have sound tested and working out from your circuit by this point.
Again, we are using SSH to our Pi Zero.
MPD (the Music Player Daemon) is a good choice to play sound files - it handles a lot for you. Another choice would have been pygame.
To make a playlist:
Then add your sound files in the order you expect them to be associated with the buttons:
sound1.wav
sound2.wav
sound3.wav
sound4.wav
Note: these will be the actual names used by mpc to play the files, so they have to match the code in the spa_soother.py script!
Save the file with CTRL-O, CTRL-X.
and download the python script spa_soother.py there.
What's Going On in the Code:
How the songs are played with a button press:
You can set the volume globally for all the sound files with the potentiometer on the LM386 output. To fine tune the volume for each sound, you can use the command:
Once you are satisfied the software is working, you can set the python script to start at boot time.
Note: Raspbian Jessie uses "systemd". If you are used to using "systemv", the start-up procedure has changed.
How To Autorun A Python Script On Boot Using systemd
Edit a new file:
sudo nano /lib/systemd/system/spa_soother.service
Inside the file add the following contents:
Description=S2 Spa Soother
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/scripts/python/s2_spa_soother.py
[Install]
WantedBy=multi-user.target
Change permissions:
sudo chmod644 /lib/systemd/system/spa_soother.service
Reload and enable:
sudo systemctl daemon-reload
sudo systemctl enable spa_soother.service
Check the status of the service:
sudo systemctl status spa_soother.service
Here's a pinout guide to the Pi Zero:
Shutdown Process
The TX0 (Board Pin 8) from Pi Zero goes to the RXD pin on the RainbowDuino, on the "5V ONLY" Area shown below. The Pi Zero will use this to tell the RainbowDuino it is shutting down, and that the RainbowDuino should wait 30 seconds, then turn Power OFF at the Pololu switch. The RainbowDuino will do this by writing HIGH to its own A2 pin. A2 is connected to "OFF" on the Pololu switch. A voltage > 1V to OFF on the switch kills the power to both the Pi Zero and the RainbowDuino.
Tip: Add a right-angle header to the pin connections at location J8 (pins A0 to D3) for easy connection with female jumper wires.
RainbowDuino Connections:
From the 5V ONLY Area, to power the Pi Zero:
To upload the S2_Spa_Soother_Spin.ino file to the RainbowDuino:
When you're satisfied everything is working properly, move on to the Final Assembly...
Again, we are using SSH to our Pi Zero.
Now, on the Pi Zero install mpd, the music player daemon and its client, mpc:
sudo apt-get install mpd mpc
MPD (the Music Player Daemon) is a good choice to play sound files - it handles a lot for you. Another choice would have been pygame.
To add the sound files to the mpd system, I copied them over like so, from the linux computer I had created them on, where Music is on another computer where I stored the sound files in my source home directory:
sudo scp username@192.168.0.nnn:Music/sound1.wav /var/lib/mpd/music/sound1.wav
After you have copied over your sound files, be sure to do:
mpc update
This will add the sound files to the mpd database.
To make a playlist:
sudo nano /var/lib/mpd/playlists/mylist.m3u
sound1.wav
sound2.wav
sound3.wav
sound4.wav
Note: these will be the actual names used by mpc to play the files, so they have to match the code in the spa_soother.py script!
Save the file with CTRL-O, CTRL-X.
To test out your rig, wire up the PWM/LM386 to the Pi Zero, attach a speaker and do:
mpc clear
mpc load sound1.wav
mpc play 1
The volume may be too high/too low. Adjust with:
mpc volume 75
Where the number after volume is the new volume setting.
If that works, you can move on to installing the python script for sound from the github repository.
From the pi user home directory:
From the pi user home directory:
mkdir scripts
cd scripts
mkdir python
and download the python script spa_soother.py there.
What's Going On in the Code:
How the songs are played with a button press:
You can set the volume globally for all the sound files with the potentiometer on the LM386 output. To fine tune the volume for each sound, you can use the command:
exec_command("mpc play 1")
exec_command("mpc volume nn")
Once you are satisfied the software is working, you can set the python script to start at boot time.
Note: Raspbian Jessie uses "systemd". If you are used to using "systemv", the start-up procedure has changed.
How To Autorun A Python Script On Boot Using systemd
Edit a new file:
sudo nano /lib/systemd/system/spa_soother.service
Inside the file add the following contents:
Description=S2 Spa Soother
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/scripts/python/s2_spa_soother.py
[Install]
WantedBy=multi-user.target
Change permissions:
sudo chmod644 /lib/systemd/system/spa_soother.service
Reload and enable:
sudo systemctl daemon-reload
sudo systemctl enable spa_soother.service
Check the status of the service:
sudo systemctl status spa_soother.service
For wiring this up, I used a +5V from the Button Power Rail (explained previously) and GND (any of 4 possible) from the Pi Zero to the PWM/LM386 board rail. The Speaker input from the Pi Zero comes from Board Pin 33. Instead, you could use Board Pin 12. It won't matter, our sound file is mono only.
Here's a pinout guide to the Pi Zero:
Shutdown Process
- Timer expires on Pi Zero; you can change this to any interval you like but it defaults to 20 minutes
- Pi Zero sends Serial message to RainbowDuino, "Shutdown"
- Pi Zero issues a "sudo shutdown -h now" for itself
- RainbowDuino listens for "Shutdown" message
- When received, RainbowDuino checks for 30 seconds of elapsed time, enough for the Pi Zero to safely shutdown
- RainbowDuino issues a HIGH via pin A2 connected to the OFF section of the Pololu switch
- Pololu switch kills power for the RainbowDuino (and the Pi Zero attached to it) and goes into very low quiescent mode
The TX0 (Board Pin 8) from Pi Zero goes to the RXD pin on the RainbowDuino, on the "5V ONLY" Area shown below. The Pi Zero will use this to tell the RainbowDuino it is shutting down, and that the RainbowDuino should wait 30 seconds, then turn Power OFF at the Pololu switch. The RainbowDuino will do this by writing HIGH to its own A2 pin. A2 is connected to "OFF" on the Pololu switch. A voltage > 1V to OFF on the switch kills the power to both the Pi Zero and the RainbowDuino.
Tip: Add a right-angle header to the pin connections at location J8 (pins A0 to D3) for easy connection with female jumper wires.
RainbowDuino Pin Connections
RainbowDuino Connections:
- 5V RainbowDuino to pin 2 of Pi Zero
- GND RainbowDuino to pin 6/9/14/25 of Pi Zero
To upload the S2_Spa_Soother_Spin.ino file to the RainbowDuino:
- Make sure the USB/HOST switch is set to USB
- In the Arduino Board description, use "Arduino Duemilanova or Diecemila"
- Make sure the TX0 from Pi Zero is not connected during upload, then reconnect to run the sketch
- Tip: Insert the Geeetech LED matrix with writing on the side toward the RED/BLUE silk screen, same side as green screw terminal power blocks
Power Connections
I used a 9V/1A power supply from Adafruit wired directly to the Pololu VIN through a 2.5mm power jack. The VOUT connection is wired to the 6.5V-9V power terminal block on the RainbowDuino.
5V power and GND is daisy-chained from the 5V ONLY Area on the RainbowDuino to the Pi Zero.
Thus, when the RainbowDuino issues an OFF to the Pololu switch, both devices are powered down.
DO NOT attach ANY 5V connected devices to 6.5V-9V power. You will emit the Magic Blue Smoke!
Thus, when the RainbowDuino issues an OFF to the Pololu switch, both devices are powered down.
DO NOT attach ANY 5V connected devices to 6.5V-9V power. You will emit the Magic Blue Smoke!
When you're satisfied everything is working properly, move on to the Final Assembly...
Comments
Post a Comment