theholychilli
Posts: 18
Joined: Thu Apr 25, 2013 5:07 pm

if time = ?? play a song?

Mon Apr 29, 2013 7:07 pm

Hi,

I am making an alarm clock which goes off at 6am. I want the application to check if it is 6am, and if it is, play a song. And by each 2 minutes, it gets louder, and louder and so on.

I would like if you could point me in the right direction so I can work problems out by myself.

Thanks,
theholychilli 8-)

User avatar
patrikg
Posts: 168
Joined: Sun Mar 18, 2012 10:19 pm

Re: if time = ?? play a song?

Mon Apr 29, 2013 7:11 pm

Unix command "cron" and "at" can maybe the quickest way to do this.

theholychilli
Posts: 18
Joined: Thu Apr 25, 2013 5:07 pm

Re: if time = ?? play a song?

Mon Apr 29, 2013 7:22 pm

Could you explain to me what cron and at commands are?

scotty101
Posts: 4052
Joined: Fri Jun 08, 2012 6:03 pm

Re: if time = ?? play a song?

Mon Apr 29, 2013 8:44 pm

There are lots of good tutorials online to use cron.

Try this one to get you started http://www.linuxweblog.com/crotab-tutorial
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

IanH
Posts: 68
Joined: Mon Dec 19, 2011 5:53 pm

Re: if time = ?? play a song?

Mon Apr 29, 2013 9:28 pm

Presumably, though, you'll want some sort of friendly Python-based user interface which can set the alarm time. Editing and reinstalling a crontab is going to be awkward, especially if the crontab contains other stuff.

I'd take a look at the Python 'time' module - http://docs.python.org/2/library/time.html#module-time. The following snippet of code might be a helpful starting point:

Code: Select all

import time

alarm_HH = 22
alarm_MM = 23

while True:
    now = time.localtime()
    if now.tm_hour == alarm_HH and now.tm_min == alarm_MM:
        print "ALARM NOW"
    else:
        print "no alarm"
    time.sleep(60 - now.tm_sec)
When it's started, it gets the current time ("now", which has "tm_hour", "tm_min" and "tm_sec" fields). If they are the alarm time (in this case 22:23), a message is printed; otherwise a different one is printed. The code then waits (via time.sleep()) until the start of the next minute.
https://github.com/IanHarvey

theholychilli
Posts: 18
Joined: Thu Apr 25, 2013 5:07 pm

Re: if time = ?? play a song?

Tue Apr 30, 2013 3:59 pm

Thanks for this. :D

theholychilli
Posts: 18
Joined: Thu Apr 25, 2013 5:07 pm

Re: if time = ?? play a song?

Tue Apr 30, 2013 5:53 pm

Is there a way to make it raw input? I was thinking after the variables alarm_HH and alarm_MM you can put input(["enter time]) ?

scotty101
Posts: 4052
Joined: Fri Jun 08, 2012 6:03 pm

Re: if time = ?? play a song?

Thu May 02, 2013 3:14 pm

Of course.. give this a try.

Code: Select all

import time

alarm = raw_input("Enter Alarm Time (Format 08:15): ")
(alarm_HH,alarm_MM) = alarm.split(":")

while True:
    now = time.localtime()
    if now.tm_hour == int(alarm_HH) and now.tm_min == int(alarm_MM):
        print "ALARM NOW"
    else:
        print "no alarm"
    time.sleep(60 - now.tm_sec)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Maddo
Posts: 15
Joined: Mon May 06, 2013 4:39 am

Re: if time = ?? play a song?

Wed May 08, 2013 6:25 am

or this

Code: Select all

import time
import os

showhh = str(time.localtime().tm_hour)
showmm = str(time.localtime().tm_hour)

name = raw_input("Enter your name:")
 
print "Hello, " + name + "\n" + "Current Time:" + showhh + ":" + showmm
 
alarm_HH = int(raw_input("Enter the hour you want to wake up at:"))
alarm_MM = int(raw_input("Enter the minute you want to wake up at:"))

print "You want to wake up at " + alarm_HH + ":" + alarm_MM

def alarm():
    hh = int(time.localtime().tm_hour)
    mm = int(time.localtime().tm_hour)
    if hh == alarm_HH and mm == alarm_MM:
       print "ALARM NOW!"
       os.system("mpg321 /home/pi/voltage.mp3")
    else:
        pass

def main():
    while True:
        alarm()

if __name__ == '__main__': main()

EnzioBrK
Posts: 24
Joined: Wed Jan 23, 2013 9:26 pm
Location: Luton, Bedfordshire

Re: if time = ?? play a song?

Thu Aug 07, 2014 10:29 pm

Can anybody help me combine this alarm code with mine. If its like this the command "mpc play" is used every second. I want to be used just once, when the time is the same with the alarm. I'm using it with a C-berry tft display.
Thanks in advance and sorry for my bad english.

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

#Alarm Clock - ALPHA


from time import sleep
import pygame
import sys
import time
import RPi.GPIO as GPIO
import os
import socket
import datetime
import subprocess

station = ""


#Print IP
MyIp = "0.0.0.0"
IpAd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
IpAd.connect(('google.com', 0))
MyIp = IpAd.getsockname()[0]

#TEST LINE
print MyIp


alarm_HH = 00
alarm_MM = 23




os.system("sudo mpc clear")

os.system("sudo mpc load mine")

os.system("mpc play")

os.system("mpc repeat on")

time.sleep(1)


#Define NEXT, PREV, STOP buttons
NEXT = 2
#PREV = 3
#STOP = 4

#Get number of station loaded
cmd = subprocess.Popen("sudo mpc playlist",shell=True, stdout=subprocess.PIPE)
stations = cmd.stdout.readlines()
channelCount = len(stations)
currentChannel = channelCount

#Get the current station name
f=os.popen("mpc current")
for i in f.readlines():
    station += i
print station


pygame.init()

#Set display size
window = pygame.display.set_mode((320, 240))

#Set the window caption
pygame.display.set_caption('Alarm Clock by BrK')

###Set the color
##BLACK = (  0,   0,   0)
##WHITE = (255, 255, 255)
##RED   = (255,   0,   0)
##GREEN = (  0, 255,   0)
##BLUE  = (  0,   0, 255)
##YELLOW= (255, 255,   0)


#Set the fonts
font0 = pygame.font.SysFont("droidsans",21)
font1 = pygame.font.SysFont("droidsans",83)
font2 = pygame.font.SysFont("freemono",32)
font3 = pygame.font.SysFont("freemono",26)
font4 = pygame.font.SysFont("droidsans",16)

font2.set_bold(1)

#Set the label
label0 = font0.render("Internet Radio - BrK V1.0", 1, (255,0,0))
label1 = font3.render("IP:" + MyIp, 1, (255,0,0))
label4 = font4.render("Now playing :", 1, (0,0,255))
label5 = font4.render("Stations loaded: " + str(channelCount), 1, (0,0,255))

#Define  some rectangles
rect1 = pygame.Rect(4, 111, 312, 35)
rect2 = pygame.Rect(4, 152, 312, 50)


os.system("tft_init")   # Initialize the TFT display
os.system("tft_clear")  # Clear the TFT dispolay
os.system("tft_pwm 10") # Set the backlight contrast 0 - 100


#The main loop
while True:
    try:
        window.fill(pygame.Color(0, 0, 0))
        #Draw the one rectangle
        pygame.draw.rect(window, pygame.Color(255, 0, 0), rect2)
        #Draw tfew lines
        pygame.draw.line(window, pygame.Color(0, 0, 255), (0, 239), (319, 239))
        pygame.draw.line(window, pygame.Color(0, 0, 255), (0,   0), (0  , 239))
        pygame.draw.line(window, pygame.Color(0, 0, 255), (0,   0), (319,   0))
        pygame.draw.line(window, pygame.Color(0, 0, 255), (319, 0), (319, 239))

        #Get and display the time, that updates every second
        lt = time.localtime()
        label2 = font1.render(time.strftime("%H:%M:%S ", lt), 1, (255, 255, 0))
        window.blit(label2, (0, 21))

        #Get and display the date, that updates every secound
        label3 = font2.render(time.strftime("%a %d.%m.%Y ", lt),1, (255,255,255))
        window.blit(label3, (3, 110))

        #Display the other labels
        window.blit(label0, (  3,   2))  # Alarm clocb by BrK
        window.blit(label1, (  2, 210))  # IP
        window.blit(label4, (  4, 151))  # Now Playing:
        window.blit(label5, (  4, 185))  # Stations Loaded

        #Buttons and station name
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(13, GPIO.IN)
        GPIO.setup(19, GPIO.IN)
        GPIO.setup(26, GPIO.IN)
        
        if (GPIO.input(19) == False):
            os.system("mpc prev")
            time.sleep(0.5)
            os.system("mpc play")

        if (GPIO.input(26) == False):
            os.system("mpc stop")

        if (GPIO.input(13) == False):
            os.system("mpc next")

        #Get the station name
        f=os.popen("mpc current")
        station = ""
        for i in f.readlines():
            station +=i

        #Display the station name
        label6 = font4.render(station, 1, (0, 0, 255))
        window.blit(label6, (4, 169))

        #Alarm part goes here

        now = time.localtime()
        if now.tm_hour == alarm_HH and now.tm_min == alarm_MM:
            print "ALARM NOW"
            os.system("mpc play")
        else:
            print "no alarm"
        #time.sleep(60 - now.tm_sec)


        #update everything on the window
        pygame.display.update()

	pygame.image.save(window, "/ram/temp.bmp")
	os.system("tft_bmp /ram/temp.bmp")
	time.sleep(1)


    except:
        sys.exit()

        

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: if time = ?? play a song?

Thu Aug 07, 2014 10:42 pm

You could have a variable 'has_went_off' and initially set it to False, then when actual time > alarm time and actual time < alarm time plus eg. 10 mins, check whether it's went off already, and if not, set your song playing and also set 'has_went_off' to True. After the 10 mins is up, set 'has_went_off' back to False again ready for the next day.

Hope that makes sense...
note: I may or may not know what I'm talking about...

EnzioBrK
Posts: 24
Joined: Wed Jan 23, 2013 9:26 pm
Location: Luton, Bedfordshire

Re: if time = ?? play a song?

Thu Aug 07, 2014 10:49 pm

toxibunny wrote:You could have a variable 'has_went_off' and initially set it to False, then when actual time > alarm time and actual time < alarm time plus eg. 10 mins, check whether it's went off already, and if not, set your song playing and also set 'has_went_off' to True. After the 10 mins is up, set 'has_went_off' back to False again ready for the next day.

Hope that makes sense...
Thanks for your response. As a beginer it dosen't make to much sense, I get the ideea, but to convert it to code...
Is it possible to use this two times?
"while True:
try: "

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: if time = ?? play a song?

Thu Aug 07, 2014 11:00 pm

You just need another variable to tell the program if the alarm has already activated. That way you can avoid setting it off again and again and again. I'm on my phone so can't really say much more - hopefully someone will be along soon with fast fingers and better explaining skills :)
note: I may or may not know what I'm talking about...

Return to “Python”