# Speaking alarm clock using Raspberry Pi #Connect 3.5" LCD and speaker though AUX and run the program with PyQt4 and espeak packages # Program by: B.Aswinth Raj # Website: circuitdigest.com # # GUI code was created using Qt Designer import sys import time #import PyQt4 from PyQt import QtCore, QtGui #PyQt4 is used for designing the GUI from espeak import espeak #text to speech sonversion from time import strftime # To get time from Raspberry pi #Code from Qt Designer try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_MainWindow(object): def setupUi(self, MainWindow): self.alarm_h = 0 self.alarm_m = 0 MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(676, 439) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.gridLayout = QtGui.QGridLayout(self.centralwidget) self.gridLayout.setObjectName(_fromUtf8("gridLayout")) self.label = QtGui.QLabel(self.centralwidget) font = QtGui.QFont() font.setPointSize(14) self.label.setFont(font) self.label.setObjectName(_fromUtf8("label")) self.gridLayout.addWidget(self.label, 3, 0, 1, 1) self.Time_LCD = QtGui.QLCDNumber(self.centralwidget) self.Time_LCD.setObjectName(_fromUtf8("Time_LCD")) self.Time_LCD.setDigitCount(8) self.Time_LCD.display(strftime("%H"+":"+"%M"+":"+"%S")) #get time from Pi and display it self.gridLayout.addWidget(self.Time_LCD, 1, 0, 1, 3) self.timer = QtCore.QTimer(MainWindow) self.timer.timeout.connect(self.Time) self.timer.start(1000) current_time = QtCore.QTime() self.Set_Time = QtGui.QTimeEdit(self.centralwidget) self.Set_Time.setObjectName(_fromUtf8("Set_Time")) self.Set_Time.setTime(current_time.currentTime()) self.gridLayout.addWidget(self.Set_Time, 2, 0, 1, 1) self.pushButton = QtGui.QPushButton(self.centralwidget) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.pushButton.clicked.connect(self.button_pressed) self.gridLayout.addWidget(self.pushButton, 2, 1, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 676, 21)) self.menubar.setObjectName(_fromUtf8("menubar")) MainWindow.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName(_fromUtf8("statusbar")) MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) #End of code from Qt Designer def retranslateUi(self, MainWindow): #update the GUI window print("Dispay Re-translated") MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) self.label.setText(_translate("MainWindow", "Alarm curretly Turned off", None)) self.pushButton.setText(_translate("MainWindow", "Set Alarm", None)) def Time(self): #Function to compare current time with set time self.Time_LCD.display(strftime("%H"+":"+"%M"+":"+"%S")) self.current_h = int (strftime("%H")) self.current_m = int (strftime("%M")) if (self.current_h == self.alarm_h) and (self.current_m == self.alarm_m) and ((int(strftime("%S"))%15) == 0): #if the both time match print("ALARM ON!!!!!") message1 = " The time is " + str(self.alarm_h) + " : " + str(self.alarm_m) + " on " + strftime("%A") message = "Sir, Good morning.. This is your wake up Alarm." + message1 self.label.setText(_translate("MainWindow",message1, None)) #display the message on GUI screen espeak.synth (message) #speak the message through audio jack time.sleep(1) def button_pressed(self): #when set alarm button is pressed print("Button Pressed") alarm_time = str(self.Set_Time.time()) self.alarm_h = int(alarm_time[19:21]) #value of hour is sotred in index value 19 and 20 self.alarm_m = int (alarm_time[23:25]) #value of minute is sotred in index value 23 and 24 message = "Alarm is set at " + str(self.alarm_h) + " hours " + str(self.alarm_m) + " minutes" self.label.setText(_translate("MainWindow", message, None)) #display the message on GUI screen espeak.synth (message) #speak the message through audio jack if __name__ == "__main__": #main function app = QtGui.QApplication(sys.argv) MainWindow = QtGui.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |