Raspberry Pi as a Google Calendar Alarm Clock

This post isn’t really thumbnail conducive but I tried!

I have been sleeping in too much. I would like an alarm clock that is easy to set and will play a random song to wake me up.

For a while I was using this program, but as you may be able to tell it requires an awful lot of user input, which isn’t ideal for a sleepy person as they may mess up (as I have, thus this post) but the “fire()” command works really well. To use this program you will need AP scheduler and MPG321 (sudo apt-get install mpg321)

But it wasn’t working. I wanted a way to re-purpose a service I use everyday (that way I wouldn’t have to modify my workflow) as an Alarm Clock. I landed on Google Calendar because I can add events from pretty much every device I interact with on a daily basis, and upon searching found out that developing using the python API wasn’t that hard at all.

To kick things off, you’ll need to download and install the Google Data Library.

I’ll be using this version. Unzip the .tar.gz and from the top top level directory it creates, install the setup.py file. Then run the tests/run_data_tests.py to see if it all works. Mine does fine but it if yours doesn’t, go through this guide written by google to get yourself up and running.

The brunt of this program comes down to a single boolean statement, but first we have to set that up. The API produces an rfc3339 time, and that’s a lot of irrelevant information for this application.

To convert the time I’m using something I found on stackoverflow.

Here’s a video of the system in action, and a basic overview of the setup:

Here’s my program:

And there you have it! thanks for reading and leave me a comment if you have any questions/suggestions.

Hey! This post was written a long time ago, but I'm leaving it up on the off-chance it may help someone. Proceed with caution. It may not be a good idea to blindly integrate this code or work into your project, but instead use it as a starting point.

92 Comments

  1. Hi, i’m really a noob, but i want to try this 😀

    ..but where i have to paste you code? i have to create a .py file? where i have to put this file?

    1. Hi! You do have to create a file, and the code is the green block of the blog post. I suggest downloading and installing a program called geany for the raspberry pi

      1. Ok, i’ve created correctly the file, but it gives me an error at line 14, it doesn’t find xe module. It’s a google module?

  2. I think there is a little bug.

    If i set the wake event on calendar with the option ‘repeat every day’ it doesn’t work…

    It’s a common problem?

    1. hmm! I haven’t used this feature in google cal as I don’t really have a regimented wake up schedule. Please comment again if you come up with a solution!

        1. After using singleevents,still can not been prefect fixed .

          According to analyze the source code, the following can fixed:

          47 query.orderby = ‘starttime’
          48 query.singleevents = ‘true’
          49 query.sortorder = ‘a’
          50 query.futureevents = ‘true’

          More info, pls refter to src/gdata/calendar/service.py
          Sorry, my englist is poor

    2. Hi Devon or Cia91
      Did you find out how to fix problem with repeat every day.

      It’s almost perfect working with my lights turn of on.
      thanks,

      1. No I haven’t really had the need to use that feature as I wake up at a different time almost daily. If you figure it out please feel free to post here!

        1. See my reply that i posted a few months ago. It tells you how you can add it in. It also tells you about another bug.

          1. Eddie Bijnen Idon’t now how to change it to youre way.

            I add After line 28 fallowing:
            query.singleevents = ‘true’
            query.orderBy = ‘startTime’
            query.sortorder = ‘ascending’

            but i get back a error

  3. I just got a Pi, and happened across a Zen Alarm clock (130 bucks new, 4 bucks for me). I plan on wiring the striker into a GPIO channel, and use a 2 line LED display instead of the clock face. The only piece I needed, you’ve written.

    Thank you!

  4. I cannot thank you enough for making this little gem.
    Not only have I got something that integrates perfectly with my workflow to wake me up in an easy and effective manner, I have learnt more about my pi and python from following this code.
    My deepest thanks!
    Warmest regards,
    Stephen

  5. Hey there, I wanted to try this out but i kept running into problems ( iv never had to use python before).

    Iv installed python, pyfeed and even the xe just incase but I am getting an error for line 16 import error no module named apscheduler.scheduler.

    please help?

      1. Now its saying 2-13 import not found, cant read /var/mail/feed.date.rfc3339, /var/mail/datetime, /var/mail/apscheduler.scheduler. 17-19 import not found, 20 syntax error ( unexpected.

        really not liking python so far haha.

        Thank you very much for your help so far though!

          1. pi@raspberrypi ~ $ cd /share
            pi@raspberrypi /share $ sudo ./pi-alarm.py
            ./pi-alarm.py: 2: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 3: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 4: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 5: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 6: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 7: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 8: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 9: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 10: ./pi-alarm.py: import: not found
            ./pi-alarm.py: 11: ./pi-alarm.py: import: not found
            : not foundpy: 12: ./pi-alarm.py:
            ./pi-alarm.py: 13: ./pi-alarm.py: import: not found
            from: can’t read /var/mail/feed.date.rfc3339
            from: can’t read /var/mail/datetime
            from: can’t read /var/mail/apscheduler.scheduler
            ./pi-alarm.py: 17: ./pi-alarm.py: import: not found
            : not foundpy: 18: ./pi-alarm.py:
            ./pi-alarm.py: 20: ./pi-alarm.py: Syntax error: “(” unexpected
            pi@raspberrypi /share $

            Is there a setup I failed to run?

          2. I get the same errors stated by Josh. I’ve tried updating gData and raspbian but on line 2 the import: not found.

  6. Hello, your idea is amazing and just tha I need. But I have an error: ImportError: cannot import name tf_from_timestamp.

    Really, I don’t know what happen.

    Maybe the problem could be my OS, Raspbmc.

    Thank you very much!

        1. I am having the same problem. I’ve installed all of the listed libraries and still get “cannot import name tf_from_timestamp” Were you able to solve this somehow?

  7. Works very well!! Thanks for publishing!

    Is there anyone who knows how to alter the code for the SingeEvent=TRUE parameter. I tried interting it in several ways, but my understanding of this way of programming is not enough to get it working. Normally Google is my best friend, but after searching for several hours, i found no solution. TYVM!

    1. Hmm, this isn’t really relevant to my application, but if you figure out what’s going on, please report back!

  8. This code uses v2 of the Google Calendar API, I have done some work with v3 and used SingleEvents=True . v3 is a bit different, especially with authentication, but not that hard to use once you work out which code to steal from google’s dev site. Details on my blog.

      1. HI BruceR,

        i have made the follow code to control my lights,
        http://pastebin.com/KDwhzjNG

        i hope you can help me to change it to youre script

        when i run youre script i get the follow;
        error:
        File “herhaalcalendar.py”, line 3
        import gflags

        thanks,
        klaas

        1. Uehhh Klaas I think it’s time you change you’re MySQL password. And pray to god you didn’t just post you’re default password for everything 😛

        2. Have you installed the API library?

          You will need the python-setuptools package:
          sudo apt-get install python-setuptools 

          Then I installed the api package ..
          sudo easy_install –upgrade google-api-python-client

  9. Hi . Thank you for your post. I am getting the following error:

    Output:

    File “Alarm.py”, line 41
    os.system(command)
    ^
    IndentationError: unexpected indent

    Code:

    command =”mpg321″ + ” ” + “/home/pi/Alarm/” + “‘”+songfile+”‘”+ ” -g 100″

    Thank you

      1. is it possible that mpg321 is not installed on your system? I remember the error and I think installing mpg321 resolved it.

        1. check also that the /home/pi/Alarm/-folder is present and that upper/lowercase matches the command. It also has to have mp3s in it which can be played.

  10. Hi,
    So I have put the Google Data Library on my Pi and have unzipped it. I have also put on the xe module. When I go into python I copy the code from the website and fill in my email and password. I have also put a mp3 into the designated folder, But as soon as I try to run the program I get syntax errors over and over. Is there something that I am doing wrong or not installing

      1. There’s an error in your program:Invalid syntax. Every error I would get I would delete and run the program again. I got this at ln:12 ln:18 ln:27 and more.

        Thanks,
        Cameron

  11. Working on getting the script to accept recurring events but i just got to say.

    GOOGLE YOU’RE *$@#$^ Reference material sucks. Twice it has just been flat out WRONG

    1. RECURRING EVENTS FIX:

      Am not a programmer and I get frustrated really fast. But I did manage to get something done.

      After line 28 add the fallowing
      query.singleevents = ‘true’
      query.orderBy = ‘startTime’
      query.sortorder = ‘ascending’

      But there is a bug in the original program that screws up this fix. Only the first 24 results will be posted. So if you have more the 24 wake’s in your calender then it won’t launch your mp3!!! With this fix and without.

      This can be fixed by adding an combo of the fallowing commands.
      time.strftime(‘%Y-%m-%d %H:%M’)
      query.timemin = ‘2013-07-05T10:57:00-08:00’

      But I got to frustrated at this point.

  12. Hey, thanks for an awesome post!
    I’ve got it working, and everything seems to work, but I get the error:

    No handlers could be found for logger “apscheduler.scheduler”

    Any ideas???

    1. Hey, thanks for the comment try adding “import logging logging.basicConfig()” to your code and tell me how it goes!

  13. I’ve managed to make it response to recurring events. on line 28 add ‘singleevents=true’ in the query body: query = gdata.calendar.service.CalendarEventQuery(‘default’, ‘private’, ‘full’, text_query, ‘singleevents=true’) and it works well.

  14. Hello guys,

    Is it possible to have the code file wake.py because I have a error but I don’t find it. thanks in advance

  15. Very nice, thanks for posting this! Was testing this out and noticed a few things have changed since you’ve written it (like the way apscheduler works– doesn’t seem to have .scheduler available now) so I updated it and cleaned it up a bit. Using mpg123 instead of mpg321 and no real need to use xe or the feed/atom stuff. Running this locally on a Mac, you can install gdata and apscheduler with pip; I only had to install mpg123 locally from here: http://www.mpg123.de/download.shtml

    Here’s the python code:
    http://pastebin.com/CSyp6fMi

      1. I have tried your script but it’s not working
        I get a message to inform “gflags” will be soom unavailabe, and nothing more. Script is not closed but nothing happend

        An idea?

  16. Hi,
    Your code works for me, next to that i have an another script for control the relay with the GPIO 11, i would like blend the two script, so i writed that(just with the important parts where i changed somthing)

    import atom
    import getopt
    import sys
    import string
    import time
    import RPi.GPIO as GPIO

    […]

    calendar_service.source = ‘Google-Calendar_Python_Sample-1.0’
    calendar_service.ProgrammaticLogin()
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(11,GPIO.OUT)

    […]

    songfile = random.choice(os.listdir(“/home/pi/alarmclock/test_M$
    print “File Selected:”, songfile
    command =”mpg321″ + ” ” + “/home/pi/alarmclock/test_MP3s/” + “‘$

    GPIO.output(11,GPIO.HIGH)

    print command
    os.system(command) #runs the bash command
    else:
    $ the system’s current time

    The problem is there is error with GPIO.setwarnings(False) and i don’t realy know how to resolve this problem :S do you have an idea ?
    Thx !

  17. I have a new version of RPi.GPIO and the error disapear, i put the GPIO.output(11,GPIO.HIGH) before songfile = random.choice
    So now the light is activated in the same time : D, i stop the light simply with an android app wich control the GPIO.
    I have anothers ideas now so i will maybe comeback !

  18. I am getting “ImportError: No module named scheduler”
    I have APScheduler installed and everything else seems fine

    Anyone have any ideas

    1. you can do following

      sudo pip install –upgrade pip
      sudo pip uninstall apscheduler
      sudo pip install apscheduler==2.1.2

      but so sorry that the query script is not working on latest google calendar api

  19. Hi Devon!
    I have some problem.
    when I finished installing apscheduler.scheduler, it showed me that”
    error: Setup script exited with error in funcsigs setup command: Invalid environment marker: python_version<"2.7"
    "
    Could you tell me what happened?
    How can I fix this bug?

  20. Hi Devon!
    I have some problem.
    when I finished installing apscheduler.scheduler, it showed me that”
    error: Setup script exited with error in funcsigs setup command: Invalid environment marker: python_version<"2.7"
    "
    Could you tell me what happened?
    How can I fix this bug?

    1. Hi Devon
      OK, I solved this problem. But now I have a new problem.

      print ‘Full text query for events on Primary Calendar: \’%s\” % ( text_query,)

      SyntaxError: invalid syntax-bash: SyntaxError:: command not found

      What happened?

  21. My code isn’t going inside the feed.
    I tried printing comments above and below this to see til where the code works, it just wont go after this command

    feed = calendar_service.CalendarQuery(query)

    Can you please help me out.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.