×
Namespaces

Variants
Actions

Archived:SMS voting system using PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

Article Metadata
Article
Created: girishpadia (04 Mar 2011)
Last edited: hamishwillee (08 May 2013)

SMS voting system using python

Contents

Background

We can be silent outwardly but our mind can never remain silent. This is very much true with for all human beings. Our mind keeps thinking, whether we are idle or busy doing some work. We see dreams when asleep only because of our thinking process at the back of our mind. Our mind is curious. It often raises questions and tries to search answers. Sometimes we ask our questions to our friends and discuss. This is how the communication has grown more and more. Earlier, the communication was limited to a small group of people but now due to the advances in the mobile technology, communication has been spread throughout the world. One of the strongest media of this communication is the world of SMSes in mobile technology. SMS technology is more popular among the mobile users due to its simplicity of use and low cost. This is why no companies producing mobiles or providing this service to the users, has stopped their support. You can check any mobile instrument, open its menu and you will find the SMS will be on the first priority in the menu. This, itself shows how SMS is popular and important in mobiles communication.


Before a few days in the office I work, there was an election scheduled for employees’ union. A few of our colleagues who were least concerned about such elections or politics, in general, were having a casual discussion on this topic. All of sudden one of my friends raised a question that can’t we have an opinion poll before the election? The idea behind this was to know the mind of our employees. Everyone laughed at him saying that this was not the government election and we should not bother about it. I too laughed at that time, but my friend’s question triggered an idea of SMS voting system in my mind.

Introduction

This SMS voting system is developed in python because of its easiness to “messaging” module in mobile. I send an SMS to our employee as follow.

“Please vote your favorite employee from below.

A : Mr P

B : Mr Q

C : Mr R

D : Mr S


Send your vote by replying “VOTE <your option>” to my mobile number. Now I stared my python program in my mobile which is reading every incoming SMS. I created a blank text file in my mobile with just four lines as follow


0

0

0

0


The first zero indicates for option A, second for option B, third for option C and fourth for option D. All initial values are set to zero indicating that no votes are received yet. Now whenever an SMS come to the mobile, the program reads it contents and checks whether the contents are beginning with word “VOTE” ? If so, read the next option (which must be A or B or C or D). Based on the option the text file will be read and updated. For example if option “C” is received the text file will be in the following format after reading it.


0

0

1

0


Requirement

You need to install PySymbian in your mobile in order to run the program. There are several articles available about how to download and install it. Once you do this create a simple text file “vote.txt” on the path shown in the program. Create four lines in the program with initial values as 0.


Tested on device

This python program is tested on Nokia 5230.


Various options available

I have used simple text file reading or writing the votes. However one can use e32db or e32dbm to make it more structured.


Python Code

#import necessary libraries
import appuifw,inbox,e32
 
# setting the application title
appuifw.app.title = u"Voting System"
 
#declaring gloabal variables to use.
mytext = []
mylist = []
myvalues=[]
 
 
# def openFile():
# global f
# f=file(u"e:\\python\\vote.txt","w")
# f.seek(0)
# print "openFile"
 
# A function is defined to read the text file from mobile and store its values to an array variable "mylist"
 
def readFile():
#mylist = []
f=file(u"e:\\python\\vote.txt") #opens a file
f.seek(0)
global mylist
for i in range(0,4): # The voting system is right now only 4 options A,B,C,D.
mylist.append(int(f.readline()))
print "readFile" #It displays message "readFile" so that we can know which stage has been crossed.
f.close()
 
# A function message_received is called for every incoming sms
def message_received(msg_id):
global mytext,myvalues # retreives the values of global variables.
 
box=inbox.Inbox()
# app_lock.signal()
 
msg = box.content(msg_id)
myvalues = box.content(msg_id).split(" ") # contens are divided into an array
print "readSMS" # #It displays message "readSMS" so that we can know which stage has been crossed.
readFile() #calling the function readFile
updateData() #calling the function updateData
writeFile() #calling the function to update data back into file
box.delete(msg_id) #message will be deleted. This can be comment if you want to preserve the incoming SMS
 
#The function updateData will update the values of mylist variable which is originally retrieved from the text file
#into the values increamented by 1 to count the votes.
def updateData():
global myvalues,mylist
if myvalues[0] == "VOTE":
if myvalues[1] == "A":
mylist[0] = mylist[0] + 1
elif myvalues[1] == "B":
mylist[1] = mylist[1] + 1
elif myvalues[1] == "C":
mylist[2] = mylist[2] + 1
elif myvalues[1] == "D":
mylist[3] = mylist[3] + 1
print "updateData"
 
#Once the data is updated into the mylist variable, these data will be written back to the text file
 
def writeFile():
f=file(u"e:\\python\\vote.txt","w")
f.seek(0)
for i in range(0,4):
print >> f,mylist[i]
f.close()
 
 
box = inbox.Inbox()
box.bind(message_received)
appuifw.app.exit_key_handler = quit
print "Voting system is ON"
 
app_lock = e32.Ao_lock()
app_lock.wait()

Application Image

Please see following image which is watching every incoming sms


Sms voting system 1.PNG

This page was last modified on 8 May 2013, at 14:47.
50 page views in the last 30 days.