Joao Duarte
Published © GPL3+

IoT PiRadio/Alarm Using Cayenne

This is a very simple project that uses a Python script and an IoT application named Cayenne, that will allow you to listen to Radio.

BeginnerShowcase (no instructions)4 hours1,541
IoT PiRadio/Alarm Using Cayenne

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Resistor 10k ohm
Resistor 10k ohm
×4
Speaker
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Cayenne
myDevices Cayenne

Story

Read more

Schematics

Schematic and Cayenne

Code

Python Code

Python
#!/usr/bin/env python
#Some code was used from www.suppertime.co.uk/blogmywiki

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) 

Credits

Joao Duarte

Joao Duarte

2 projects • 3 followers

Comments