1

For an alarm clock I am trying to have the interface in HTML and CSS and I need the GPIO pins for PWM-output (3 channels and some digital, possibly also "analog" inputs). I want a neat web-interface, where I can set the alarm for some days and another alarm for other days of the week. I guess with CSS and HTML5 this is an easy/clear task from the web-perspective.

However, I wonder how I 1) establish the alarm feature: do I need a RTC clock or can I do this conveniently from the Raspberry system clock? And 2) how do i best interface the web-frontend with python (I know python, but not PHP) to access the GPIOs and other features of the Raspi? For this I found Flask and remi. It seems to me the former is more separated and uses the full potential of both worlds.

How do I best achieve 1) the alarm feature? And 2) is Flask a good option for me?

edit: I use an Raspi Zero W, so I have wLan and Internet (hopefully at all times). And I use a standing powersupply - in case the connection is dead it should resync, but otherwise I want the webserver for the interface to run 24/7.

I just wonder how I should implement the alarm itself. In the future I want to upgrade to a RTC. But for now I want to do it quick. But polling seems very bad - also I don't know how to poll in the flask context. It seems, that Flask just calls python, whenever a specific website-part is requested.

3

to your second question:

yes, Flask would be a very good framework for this kind of thing, because you could set it up very simply, something like like this:

import RPI.GPIO as gpio
    
#put your setup here
    
from flask import Flask, render_template, request

@app.route("/")
def main():
    return render_template('alarmClock.html', **templateData)

@app.route("/setAlarmClock/<time>")
def set():
    #set the alarm clock and run clock code

#run your alarm code here

then in alarmClock.html, you could have a link to set the time that went to /setAlarmClock/<time>.

I reccomend checking out this tutorial to learn the basics of Flask, and this tutorial to learn how to build a GPIO web interface with it.

hope this helps!

In response to your comment:

answering your first question, if you would not like to use an rtc, you can just get the time from your raspberry pi, which gets it from a ntp(network time protocol) server. to get the time in python you can use the datetime module:

import datetime
hour = datetime.datetime.now().time().hour
minute = datetime.datetime.now().time().minute

(please note the hour will be in 24-hour format, so 13 means 1 pm, 14 means 2 pm, etc.)

then in your code, you can check every minute to see if hour and minute match the hour and minute set for the alarm to go off, and if it does, you can activate your alarm. However, please note that if your raspberry pi is not getting the correct time every startup, you will need to troubleshoot that, which should be pretty easy. You can check by typing date on the command line, and if it returns the correct time, your good to go.

| improve this answer | |
  • 1
    Perfect thanks! This 100% answers 2)! Now I just wonder how I should implement the alarm itself. In the future I want to upgrade to a RTC. But for now I want to do it quick. But polling seems very bad - also I don't know how to poll in the flask context. It seems, that Flask just calls python, whenever a specific website-part is requested! Thanks also for the great links! – mike Mar 20 '18 at 23:01
  • @mike , I added a second part explaining how you can get the time in python. Please let me know any more help you need, and fell free to add your code you've written so far. – scitronboy Mar 21 '18 at 2:54
  • 1
    @mike Please mark this as the accepted answer. For your RTC, I recommend a DS3231 (they come in small packages for GPIO)... google ‘ds3231 for raspberry pi’. You don’t need a large breakout board. – user2497 Mar 21 '18 at 9:35
  • Thank you very much! I will start this project and will report back. As it happens I already have a DS3221 :D (I started the project with Arduino..)! I will update to the "proper" interrupt driven RTC-mode, when I have the setup working and running. Thanks for all the help! – mike Mar 21 '18 at 13:20
2

Flask is one of the many Python web frameworks you could use. Arguably, it would be better to pick a more "mainstream" framework like Django or web2py, since you'll be learning about technologies you'll likely encounter later on in other projects or at work.

To implement the alarm function itself you'll need to have a function which executes periodically and independently from the web server requests. For starters, I would take a look at the threading module in Python, which lets you create parallel processes, with optional timers to start them at the right moment.

| improve this answer | |
  • 1
    Thanks for the comprehensive list! I will look into that, even though I didn't really want to make the huge fuzz for the setup. I do write python only for sympy and otherwise mostly C++ and matlab. It's been years since I last touched a web markup :). I'll look into that though! – mike Mar 21 '18 at 13:18
1

About point 1:

You will need a RTC if your Rasperry can't rely on an internet connection to get synchronized.

I suppose you want your alarm-clock to be "disconnection-proof", unless you want to explain your boss you are late because of an ISP failure conjugated with your DIY-addiction :)

So yes, an alarm clock definitively needs some hardware RTC, some are GPIO compatible, in order to improve the native Raspberry system clock.

| improve this answer | |
  • Very good point! I definitely have to upgrade to an RTC implementation in the future! For now though I will use a phone-alarm backup and just want to reach proof of concept. – mike Mar 20 '18 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.