Main Picture

Nigel Li

Updated Code for AT Proposal - ALPHA 2.0

Nigel Li

The process to get to Alpha 2.0 of bathroom helper was challenging. At first I thought it would be a good idea to both tweet an send a message on Whatsapp. Through trial and error I realized that using Whatsapp as a way to send messages from the Raspberry Pi was too complicated and unfeasible. After several hours of research, I came across a simple messaging app similar to Whatsapp, Telegram. 

What I used in my code to reach my final product was Telegram Bot. Telegram Bot is essentially an automated messaging service when programmed to do so. I had to first request Telegram for an API access code, similar to the process I had to go through when working with Twitter. However, this time I had to create a Bot and I named it "BathroomHelperBot". After this I recieved the API code and added it to my code below. Finding out how to send a message directly to me was difficult and tedious. What I wanted to do was to send a message to my number to notify me that "The bathroom is flooded" (or any other functions attatched to the program). Followed by the ability to reply to the message and recieve a reply from the Bot, I failed at attempting to get this to work. 

One major issue I faced was not getting it to work at all. I realized that I placed the line of code which SENT the message below the line of code that restarted the program. A novice mistake, but one that I managed to overcome through patience. 

-----------------------------------------------------------------------------------------------------------------------------------

#Create GUI
from Tkinter import *
import sys
#gather twitter module
from twython import *
import datetime
import os
#import telegram module
import telepot

#Authentication variables for twitter
access_key = "767723777958420481-eLxPwGgY9NvJQa3MESxMBbQXYfLtFq8"
access_secret =     "2zfdXkjSIJyeyeo9ANMDtXJIg0uxieMvl5tD6JA7r3dhj"
consumer_key = "Yz2dZAC7KWq1NcMlPvaFIR8xa"
consumer_secret =     "oD2Wkp2toCGAfrXLHcG2Gb904LBwGOGCiZtOxbCHiXrEhzHAOZ"
#access Telegram's API
bot = telepot.Bot('181745128:AAGWPpBIKxI_V35oC09W5X7gxdTx9mcKuJg')

#designate api as twitter authetication
api = Twython(consumer_key, consumer_secret, access_key, access_secret)

tweet1 = "There is poop in the toilet %s"

tweet2 = "The toilet is flooded %s"

tweet3 = "The toilet is clogged %s"

i = datetime.datetime.now()#Assign variable for date and time

interface = Tk()#Establish interface 

#Tweets assigned
poop = tweet1 %i #the %i calls the string which will print the date and time

flood = tweet2 %i

clogged = tweet3 %i

########## FUNCTIONS ###################

def post1():
    api.update_status(status= tweet1 %i)
    bot.sendMessage(255454100, "There is poop in the toilet") #sends message to telegram
    python = sys.executable #exits the programme and restarts it         
    os.execl(python, python, * sys.argv)
    
def post2():
    api.update_status(status= tweet2 %i)
    bot.sendMessage(255454100, "The toilet is flooded")
    python = sys.executable
    os.execl(python, python, * sys.argv)

def post3():
    api.update_status(status= tweet3 %i)
    bot.sendMessage(255454100, "The toilet is clogged")
    python = sys.executable
    os.execl(python, python, * sys.argv)


   
########################################

#establish elements of GUI
interface.title("Bathroom Status")
interface.geometry("500x250")

app = Frame(interface)
app.grid()

instructions = Label(app, text = "What is wrong with this bathroom?")
instructions.grid()

#ASSIGNING BUTTONS#
button1 = Button(interface, text = "poop")
button1.grid()
button1["command"] = post1

button2 = Button(interface, text = "flooded")
button2.grid()
button2["command"] = post2

button3 = Button(interface, text = "clogged")
button3.grid()
button3["command"] = post3

#loop the gui
interface.mainloop()
datetime.mainloop()

 

 

 

Code for Unit 2 Project

Nigel Li

#Create GUI
from Tkinter import *
import sys
#gather twitter module
from twython import *
import datetime
import os

#Authentication variables
access_key = "767723777958420481-eLxPwGgY9NvJQa3MESxMBbQXYfLtFq8"
access_secret =     "2zfdXkjSIJyeyeo9ANMDtXJIg0uxieMvl5tD6JA7r3dhj"
consumer_key = "Yz2dZAC7KWq1NcMlPvaFIR8xa"
consumer_secret =     "oD2Wkp2toCGAfrXLHcG2Gb904LBwGOGCiZtOxbCHiXrEhzHAOZ"

#designate api as twitter authetication
api = Twython(consumer_key, consumer_secret, access_key, access_secret)

tweet1 = "There is poop in the toilet %s"

tweet2 = "The toilet is flooded %s"

tweet3 = "The toilet is clogged %s"

i = datetime.datetime.now()#Assign variable for date and time

interface = Tk()#Establish interface 

#Tweets assigned
poop = tweet1 %i #the %i calls the string which will print the date and time

flood = tweet2 %i

clogged = tweet3 %i

########## FUNCTIONS ###################

def post1():
    api.update_status(status= tweet1 %i) 
    python = sys.executable #exits the programme and restarts it         
    os.execl(python, python, * sys.argv)
    
def post2():
    api.update_status(status= tweet2 %i)
    python = sys.executable
    os.execl(python, python, * sys.argv)

def post3():
    api.update_status(status= tweet3 %i)
    python = sys.executable
    os.execl(python, python, * sys.argv)
   
########################################

#establish elements of GUI
interface.title("Bathroom Status")
interface.geometry("500x250")

app = Frame(interface)
app.grid()

instructions = Label(app, text = "What is wrong with this bathroom?")
instructions.grid()

#ASSIGNING BUTTONS#
button1 = Button(interface, text = "poop")
button1.grid()
button1["command"] = post1

button2 = Button(interface, text = "flooded")
button2.grid()
button2["command"] = post2

buttonReset = Button(interface, text = "clogged")
buttonReset.grid()
buttonReset["command"] = post3

#loop the gui
interface.mainloop()
datetime.mainloop()