IoT PiRadio using Cayenne

About This Project

Hi again!

This time I’m here to present you a very simple project, however a very funny one.

Two years ago when I have bought my first raspberry pi, I had done an Internet PiRadio where I could listen to some foreign radios.

Today I had done a little more! I had removed my LCD showing what radio station I was listening, and the navigation buttons, and added an IoT dashboard named Cayenne.

With Cayenne I’m able to toggle the radio On and Off, increase and decrease volume, and cycle trough my radio playlist.

With this configuration you can also add triggers to it, for example to wake you up at a certain time. (However I don’t recommend that you use it for work or school since I haven’t tested it deeply),

The following steps will describe how you should proceed to implement one like me.

What’s Connected

Raspberry Pi
Resistors
Wires
Audio Output (Speakers, Headphones…)
A Python Script
Breadboard

Triggers & Alerts

Yes I use triggers in order to “fool” Cayenne. Since we still don’t have a “Touch” button I set a trigger to set my buttons to Off every time I turn them On.

Scheduling

Yes the Schedule feature is awsome to turn the Radio On or Off at a specific time :slight_smile:

Dashboard Screenshots

They are at the bottom of the Project.

##Steps to complete the project

###Step 1: Have a fresh Raspbian imgage running on your Pi with SSH and VNC enabled.
This is the first thing that you need to have before continue further.

You will need to install Raspbian on your Raspberry Pi.

Don’t forget to:

sudo apt-get update
sudo apt-get dist-upgrade

I also recommend that you enable SSH and VNC, since the goal is to have an headless Pi.

###Step 2: Install MPC and MPD, and add radio stations to your playlist.
Install MPD (Music Player Daemon)and MPC (Client):

sudo apt-get install mpc mpd

Add some radio station to your playlist:

mpc add http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_p

Play it.

mpc play 1

If you don’t listen anything check if you have your audio output connected.

Remember that the “mpc add” command is the one that lets you add radios/musics to your playlist.

To clear your playlist just type “mpc clear” and add them again.

You can also tweek mpd but since I don’t have much experience I recommend that you google it.

###Step 3: Install Cayenne on your Raspberry Pi

I will not describe here the procedure of installing Cayenne on your Raspberry, since the Cayenne team has already done a great job describing it.

You can check everything you need at https://cayenne.mydevices.com

###Step 4: Wire the Resistors between GPIO pins.
As you can see, this project only use 10K resistors to connect some of your Raspberry GPIO pins.

The purpose of this setup is to set 4 pins of your Raspberry as Inputs and 4 as Outputs, that will be used by Cayenne and a Python Script.

The mapping of outputs and inputs are:

OUT IN

18 ----->> 21

23 ----->> 16

24 ----->> 20

25 ----->> 26

###Step 5: Python Script

This project uses a python script, that will check the status of your input pins.

There are 4 inputs that will do the following functions:

Cycle through your radio stations
On/Off button
Volume +
Volume -
The script is very simple. It uses an infinite While loop, and some If statements that will listen to when a change of your input state occurs.

#!/usr/bin/env python
#Some code was used from Blog My Wiki! | reading, writing, coding, making

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN)
GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)
GPIO.setup(26, GPIO.IN)

#sets initial station number to channel 1
station = 1

while True:

prev_input_on_off= 0
input_on_off = GPIO.input(26)
#If the last reading from On/Off Button was low and now is high:
if ((not prev_input_on_off) and input_on_off):

os.system("mpc play " + str(station))


#initialise previous input variables to 0
prev_input_cycle = 0
prev_input_plus = 0
prev_input_minus= 0

while True:
  #take a reading from pins 16 21 20 26
  input_cycle = GPIO.input(21)
  input_plus = GPIO.input(16)
  input_minus = GPIO.input(20)
    
  #If the last reading was low and this one high, do stuff
  if ((not prev_input_cycle) and input_cycle):
    # assumes you have 4 radio stations configured
    station += 1
    if station > 4:
      station = 1
    os.system("mpc play " + str(station))
    
  if ((not prev_input_plus) and input_plus):
    # Volume +5
    os.system("mpc volume +5")
  if ((not prev_input_minus) and input_minus):
    # Volume -5
    os.system("mpc volume -5")
  
  #update previous inputs
  prev_input_cycle = input_cycle
  prev_input_minus = input_minus
  prev_input_plus = input_plus
  prev_input_on_off = input_on_off
  
  #Check if the On/Off Button is still On.
  time.sleep(0.05)
  input_on_off = GPIO.input(26):
  if (input_on_off  == 0)
    break

else:
os.system(“mpc stop”)
time.sleep(2)

###Step 6: Setup your Cayenne Dashboard

Now it is time to setup your Cayenne.

Here you will need to do 3 major steps.

Set Outputs
Set Inputs
Set Triggers
All the inputs and outputs are the Generic Type.

If you have any questions or doubts regarding how to set them, please read the Cayenne Docs.

Check the Wire the Resistors between GPIO pins step and the picture to see which button does what.

Create the following Outputs:

Cycle Button

Volume +

Volume -

Start / Stop

And the Inputs:

Cycle Button State

Volume + State

Volume - State

Start / Stop

After you will need to use triggers in order to “fool” cayenne. The purpose is to set an Output to low every time you click to set it to High.

I need to used this for Cycle and Volumes since cayenne doesn’t have yet the option to set a like a Pulse Button.

Remeber that you can also add Events for example to turn the radio On or Off at a certain time, or any other action that you need.

Photos of the Project



Step 7: Listen to Radioooo!
So now that everything is done in order to listen to your radio, you just need to run your Python script and press your Start Button on Cayenne.

Here is a short video(another one :smile:) showing this version of PiRadio working!

Please let me know if you have any questions so I can help you doing this simple but very nice project =)

5 Likes

I love it!!

1 Like

This gets my vote. This is freaking awesome!

1 Like

Thanks a lot for you comment :smiley:

Hi could you give me information on the materials they use as connectors and how you got to make the connections of the connectors with the table