From 925dc0f8ee105d2d96abc83b896c7bf1562adc44 Mon Sep 17 00:00:00 2001 From: SI37 Date: Sun, 21 Dec 2014 03:10:19 -0500 Subject: [PATCH 1/6] redid gui using pygtk redid gui using pygtk. still need to add stuff --- initialsetup.py | 313 ++++++++++++++++++++++++++---------------------- 1 file changed, 173 insertions(+), 140 deletions(-) diff --git a/initialsetup.py b/initialsetup.py index c72d167..76a5cd4 100644 --- a/initialsetup.py +++ b/initialsetup.py @@ -1,150 +1,183 @@ -from Tkinter import * -import httplib2 -from datetime import datetime -import simplejson - -class Application(Frame): - """gui for entering stuff """ - - def __init__(self,master): - """make frame""" - Frame.__init__(self,master) - self.grid() - self.create_widgets() - - def create_widgets(self): - Label(self, text = """Please enter all intial values as indicated.Your settings will be saved in -Aquarimeter Web, the web application which helps you view the status of your -aquarium.""").grid(row = 0,column = 0, columnspan = 5,sticky = W) - - #create labels to display information needed - - self.regFrame = LabelFrame(self,text="Login Details for Aquarimeter Web", bd = 5) - self.regFrame.grid(row=4, column = 0, columnspan=3, rowspan=5, \ - sticky='WENS', padx=5, pady=5) - Label(self.regFrame, text="E-mail").grid(row=6,sticky=E) - Label(self.regFrame, text="Password").grid(row=7,sticky=E) - Label(self.regFrame, text="First Name").grid(row=8,sticky=E) - Label(self.regFrame, text="Last Name").grid(row=9,sticky=E) - Label(self.regFrame, text=" ").grid(row=10,sticky=E) - - Label(self, text="Ideal Temperature").grid(row=11,sticky=E) - Label(self, text=" ").grid(row=12,sticky=E) - Label(self, text="Min temp").grid(row=13, sticky=E) - Label(self, text="Max temp").grid(row=14, sticky=E) - Label(self, text=" ").grid(row=15,sticky=E) - - #create places to enter text - self.email = Entry(self.regFrame) - self.email.grid(row=6, column=1) - self.passwrd = Entry(self.regFrame) - self.passwrd.grid(row=7, column=1) - self.firstNm = Entry(self.regFrame) - self.firstNm.grid(row=8, column=1) - self.lastNm = Entry(self.regFrame) - self.lastNm.grid(row=9, column=1) +#!/usr/bin/env python - - self.idlTemp = Entry(self) - self.idlTemp.grid(row=11, column=1) - DEGREES = ["C", "F"] - var = StringVar(self) - var.set(DEGREES[0]) - self.DegMenu = apply(OptionMenu, (self,var) + tuple(DEGREES)) - self.DegMenu.grid(row = 11, column = 2) - - self.minTemp = Entry(self) - self.minTemp.grid(row=13, column=1) - self.maxTemp = Entry(self) - self.maxTemp.grid(row=14, column=1) - - #create submit and cancel buttons - self.Submit = Button(self,text = "Register and save", command = self.connect).grid(row = 16,column = 3, sticky = E) - self.Cancel = Button(self,text = "Cancel", command = self.cancel).grid(row = 16, column = 2, sticky = E) - - - def connect(self): - """shows what was entered. needs to be adjusted to send these items""" - if(self.email.get() == "" or self.firstNm.get() == "" or - self.passwrd.get() == "" or self.lastNm.get() == "" or - self.idlTemp.get() == "" or self.minTemp.get() == "" or - self.maxTemp.get() == ""): - print "fill all required fields" - self.invalid - else: - #get info from those field - self.register - """emailEnt = self.email.get() - firstNmEnt = self.firstNm.get() - passwrdEnt = self.passwrd.get() - lastNmEnt = self.lastNm.get() - - idlTempEnt = self.idlTemp.get() - minTempEnt = self.minTemp.get() - maxTempEnt = self.maxTemp.get()""" - - - def cancel(self): - root.destroy() - - def register(self): - #register area json - REGISTER = {"user":{"email":self.email.get(), - "password":self.passwrd.get(), - "first_name":self.firstNmget(), - "last_name":self.lastNm.get()}} - #must change url - URL = 'http://localhost:8880/form' - - jsondata = simplejson.dumps(REGISTER) - h = httplib2.Http() - HTTPcode = h.request(URL, - 'POST', - jsondata, - headers={'Content-Type': 'application/json'}) - if(HTTPcode == 200): - print "everything is allright" - self.login +import pygtk +pygtk.require('2.0') +import gtk - else: - print "Validation errors have occured, check JSON for errors" +class Aquarium_Setup: - def login(): - #login and get username and auth_token - emailEnt = self.email.get() - passwrdEnt = self.passwrd.get() + #checks required fields is filled before registering + def validate(self): + if(self.valid_Email.get_text() == "" or + self.valid_Password.get_text() == "" or + self.first_Name.get_text() == "" or + self.last_Name.get_text() == ""): + return False + else: + return True + #checks password is long enough + def check_length(self,password): + if(len(password) < 10): + return False + else: + return True - if(emailEnt == "" or passwrdEnt == ""): - #pop up invalid message wait for it to enter again - print "no good" + #pop up message box asking to fill all fields + def error_fill(self): + info = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK) + info.set_property('title', 'Missing information') + info.set_property('text', 'Please fill in all fields') + info.run() + info.destroy() + + #pop up message for short password + def error_password(self): + info = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK) + info.set_property('title', 'Password too short') + info.set_property('text', 'Password must be at least 10 characters long') + info.run() + info.destroy() + + #registers with entered information from gui + def register(self,widget,data=None): + fill = self.validate() + long_pwd = self.check_length(self.valid_Password.get_text()) + if(not fill): + self.error_fill() + elif(not long_pwd): + self.error_password() else: - #place where the json payload happens - ######## - LOGIN = {"user":{"email":emailEnt, - "password":passwrdEnt}} - #url must be changed - URL = 'http://localhost:8880/form' - - jsondata = simplejson.dumps(LOGIN) - h = httplib2.Http() - User, auth_token, aquariums = h.request(URL, - 'POST', - jsondata, - headers={'Content-Type': 'application/json'}) - #must place in aquarimteter program - TOKENS = 'auth_token.txt' - token_file = open(TOKENS,'w') - token_file.write("%s" % (auth_token) ) - token_file.close() - - def invalid(self): + #json payload send should happen here. must be changed to do that + print self.valid_Email.get_text() + print self.valid_Password.get_text() + print self.first_Name.get_text() + print self.last_Name.get_text() + self.login() + + #login with password and email + def login(self): + #login must happen here and must save auth_token. must be changec + print "will now login" + + + #main gui window + def __init__(self): + self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) + self.window.set_title("Aquarimeter Intial Setup") + self.window.connect("destroy", lambda wid: gtk.main_quit()) + + main_Box = gtk.VBox(False, 5) #all items will be arranged vertically in here + self.window.add(main_Box) + self.window.set_border_width(10) + + label = gtk.Label("Please enter all intial values as indicated. Your" + "settings will be saved in\n Aquarimeter Web, the" + " web application which helps you view the status " + "of your\n aquarium. It can be at http://aquarium." + "oconnor.ninja/") + main_Box.pack_start(label) + #create fields for registeration information to be entered + reg_frame = gtk.Frame("Login Details for Aquarimeter Web") + + #labels for information needed + info_Box = gtk.HBox(False,5) + vbox1 = gtk.VBox(False,5) + info_Box.add(vbox1) + + label = gtk.Label("E-mail") + vbox1.add(label) + label = gtk.Label("Password") + vbox1.add(label) + label = gtk.Label("First Name") + vbox1.add(label) + label = gtk.Label("Last Name") + vbox1.add(label) + + #fields to enter information + vbox2 = gtk.VBox(False,5) + info_Box.add(vbox2) + + self.valid_Email = gtk.Entry() + vbox2.add(self.valid_Email) + + self.valid_Password = gtk.Entry() + vbox2.add(self.valid_Password) + + self.first_Name = gtk.Entry() + vbox2.add(self.first_Name) - print "invalid" + self.last_Name = gtk.Entry() + vbox2.add(self.last_Name) + + reg_frame.add(info_Box) + main_Box.pack_start(reg_frame, False, False, 0) + + #labels for varias temperature settings + temp_frame = gtk.Frame("Temperature set") + vbox3 = gtk.VBox(False,5) + hbox3 = gtk.HBox(False,5) + hbox3.add(vbox3) + + label = gtk.Label("Intial Temperature") + vbox3.add(label) + label = gtk.Label("Max Temperature") + vbox3.add(label) + label = gtk.Label("Min Temperature") + vbox3.add(label) + + #temperature selection setting + vbox4 = gtk.VBox(False,5) + hbox3.add(vbox4) + + adj = gtk.Adjustment(70.0, 55.0, 85.0, 1.0, 5.0, 0.0) + spinner = gtk.SpinButton(adj, 0, 0) + vbox4.add(spinner) + + adj = gtk.Adjustment(70.0, 55.0, 85.0, 1.0, 5.0, 0.0) + spinner2 = gtk.SpinButton(adj, 0, 0) + vbox4.add(spinner2) + + adj = gtk.Adjustment(70.0, 55.0, 85.0, 1.0, 5.0, 0.0) + spinner3 = gtk.SpinButton(adj, 0, 0) + vbox4.add(spinner3) + + #temperature units setting + vbox5 = gtk.VBox(False,5) + hbox3.add(vbox5) + + combo = gtk.Combo() + combo.entry.set_text("list") + slist = [ "F", "C" ] + combo.set_popdown_strings(slist) + vbox5.add(combo) + label = gtk.Label(" ") + vbox5.add(label) + label = gtk.Label(" ") + vbox5.add(label) + + temp_frame.add(hbox3) + main_Box.pack_start(temp_frame, False, False, 0) + + #cancel and submit button section + hbox10 = gtk.HBox(False,2) + main_Box.pack_start(hbox10, False,False,0) + + button = gtk.Button("Submit and Register") + button.connect("clicked", self.register) + hbox10.pack_end(button,False,False,2) + + button = gtk.Button("cancel") + button.connect_object("clicked",gtk.Widget.destroy, self.window) + hbox10.pack_end(button,False,False,2) + + self.window.show_all() + + def main(self): + gtk.main() -root = Tk() -root.title("Aquarimeter Intial Setup") -app = Application(root) +if __name__ == "__main__": + Aqua_Setup = Aquarium_Setup() + Aqua_Setup.main() -root.mainloop() From 9d542f0094af7ac27e64048a96ef29c3bdfdfeca Mon Sep 17 00:00:00 2001 From: SI37 Date: Sun, 21 Dec 2014 10:28:33 -0500 Subject: [PATCH 2/6] changed == to nots --- initialsetup.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/initialsetup.py b/initialsetup.py index 76a5cd4..d7fed03 100644 --- a/initialsetup.py +++ b/initialsetup.py @@ -8,10 +8,10 @@ class Aquarium_Setup: #checks required fields is filled before registering def validate(self): - if(self.valid_Email.get_text() == "" or - self.valid_Password.get_text() == "" or - self.first_Name.get_text() == "" or - self.last_Name.get_text() == ""): + if(not self.valid_Email.get_text() or + not self.valid_Password.get_text() or + not self.first_Name.get_text() or + not self.last_Name.get_text()): return False else: return True From 3e89cc7bb17ad1b756e2af95a47a9873648a1c4e Mon Sep 17 00:00:00 2001 From: SI37 Date: Sun, 21 Dec 2014 16:39:59 -0500 Subject: [PATCH 3/6] added some post request to login and register using requests python library for simplicity. --- initialsetup.py | 68 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/initialsetup.py b/initialsetup.py index d7fed03..7516175 100644 --- a/initialsetup.py +++ b/initialsetup.py @@ -3,11 +3,13 @@ import pygtk pygtk.require('2.0') import gtk +import json +import requests class Aquarium_Setup: #checks required fields is filled before registering - def validate(self): + def validate_fields(self): if(not self.valid_Email.get_text() or not self.valid_Password.get_text() or not self.first_Name.get_text() or @@ -39,27 +41,61 @@ def error_password(self): info.run() info.destroy() + #pop up message for registeration error code == 422 + def error_register(self): + info = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE) + info.set_property('title', 'Error in registeration') + info.set_property('text', 'Something went wrong. is email valid?') + info.run() + info.destroy() + #registers with entered information from gui - def register(self,widget,data=None): - fill = self.validate() + def register(self): + #json payload send happens here + #storing email and password so no need to get again + email = self.valid_Email.get_text() + password = self.valid_Password.get_text() + register_data = {"user":{"email":email, + "password":password, + "first_name":self.first_Name.get_text(), + "last_name":self.last_Name.get_text()}} + + reg_data = json.dumps(register_data) + url = "some url. need a specific one before sending" + reg = requests.post(url, reg_data) + + if reg.status_code == 200: + self.login(email,password) + else: + self.error_register() + + + #login with password and email + def login(self,email,password): + #login happens here + login_data = {"user":{"email":email, + "password":password}} + + log_data = json.dumps(login_data) + url = "some url. need a specific one before sending" + log = requests.post(url, log_data) + + if log.status_code == 200: + auth_token = log[auth_token] + #then run aquarimeter sending auth_token to it + else: + print "username/password is incorrect" + + + def check_register(self,widget): + fill = self.validate_fields() long_pwd = self.check_length(self.valid_Password.get_text()) if(not fill): self.error_fill() elif(not long_pwd): self.error_password() else: - #json payload send should happen here. must be changed to do that - print self.valid_Email.get_text() - print self.valid_Password.get_text() - print self.first_Name.get_text() - print self.last_Name.get_text() - self.login() - - #login with password and email - def login(self): - #login must happen here and must save auth_token. must be changec - print "will now login" - + self.register() #main gui window def __init__(self): @@ -164,7 +200,7 @@ def __init__(self): main_Box.pack_start(hbox10, False,False,0) button = gtk.Button("Submit and Register") - button.connect("clicked", self.register) + button.connect("clicked", self.check_register) hbox10.pack_end(button,False,False,2) button = gtk.Button("cancel") From 676c7f56fa9dc479e1200bc4756886f9d9169dd2 Mon Sep 17 00:00:00 2001 From: SI37 Date: Sun, 21 Dec 2014 16:57:39 -0500 Subject: [PATCH 4/6] added some post request to login and register using python requests module --- initialsetup.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/initialsetup.py b/initialsetup.py index 7516175..c063b34 100644 --- a/initialsetup.py +++ b/initialsetup.py @@ -82,9 +82,10 @@ def login(self,email,password): if log.status_code == 200: auth_token = log[auth_token] - #then run aquarimeter sending auth_token to it + #then run aquarimeter sending auth_token to it along with + #ideal, min and max temp else: - print "username/password is incorrect" + print "email/password is incorrect" def check_register(self,widget): From 81921b4d3595a1db644bcb2e41e6ddbb9425a44f Mon Sep 17 00:00:00 2001 From: SI37 Date: Sun, 21 Dec 2014 23:12:55 -0500 Subject: [PATCH 5/6] functions works correctly when called now functions now work correctly on raspberry pi --- aquarimeter2.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/aquarimeter2.py b/aquarimeter2.py index aa36f00..43b7cf6 100644 --- a/aquarimeter2.py +++ b/aquarimeter2.py @@ -5,6 +5,8 @@ import time import RPi.GPIO as GPIO import picamera +import json +import requests GPIO.setmode(GPIO.BCM) DEBUG = 1 @@ -27,7 +29,7 @@ def read_temp_raw(): # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) - def readadc(adcnum, clockpin, mosipin, misopin, cspin): + def readadc(self,adcnum, clockpin, mosipin, misopin, cspin): if ((adcnum > 7) or (adcnum < 0)): return -1 GPIO.output(cspin, True) @@ -62,24 +64,24 @@ def readadc(adcnum, clockpin, mosipin, misopin, cspin): return adcout #takes picture - def takePic(): + def takePic(self): with picamera.PiCamera() as camera: camera.capture('/home/pi/Desktop/image.jpg') #checks if anyhing is a good distance away to take a picture - def moveSens(): + def moveSens(self): pir_pot = readadc(pir_adc, SPICLK, SPIMOSI, SPIMISO, SPICS) if pir_pot >150 and pir_pot < 650: takePic() #reads current ph - def readPh(): + def readPh(self): ph_pot = readadc(ph_adc, SPICLK, SPIMOSI, SPIMISO, SPICS) ph_value = ph_pot*5/1024*3.5 return ph_value #read temperature - def read_Temp(): + def read_Temp(self): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) @@ -93,21 +95,21 @@ def read_Temp(): return temp_f # turn h bridge to heat - def heat_On(): + def heat_On(self): GPIO.output(A1, GPIO.HIGH) GPIO.output(A2, GPIO.LOW) GPIO.output(A3, GPIO.HIGH) GPIO.output(A4, GPIO.LOW) # turn h bridge to cool - def cool_On(): + def cool_On(self): GPIO.output(A1, GPIO.LOW) GPIO.output(A2, GPIO.HIGH) GPIO.output(A3, GPIO.LOW) GPIO.output(A4, GPIO.HIGH) #turn off any heating and cooling - def peltio_Off(): + def peltio_Off(self): GPIO.output(A1, GPIO.LOW) GPIO.output(A2, GPIO.LOW) GPIO.output(A3, GPIO.LOW) @@ -140,14 +142,14 @@ def peltio_Off(): #set up values -ph_adc = 0; +ph_adc = 2; pir_adc = 1; ideal_temp = 75; max_temp = 80; min_temp = 70; heat = False; cool = False; - +aqua = aquarium() while True: # read the analog pin for ph ph_value = aqua.readPh() From 0cf89a4f15f6ea17c18dbbebe4b3c5b0362fae38 Mon Sep 17 00:00:00 2001 From: SI37 Date: Mon, 22 Dec 2014 02:22:13 -0500 Subject: [PATCH 6/6] added send data --- aquarimeter2.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/aquarimeter2.py b/aquarimeter2.py index 43b7cf6..a8de9a6 100644 --- a/aquarimeter2.py +++ b/aquarimeter2.py @@ -18,10 +18,21 @@ device_folder = glob.glob(base_dir + '28*')[0] device_file = device_folder + '/w1_slave' +auth_token = "whatever auth_token taken intial setup" class aquarium(object): - + + def send_readings(self, ph, temp): + reading_data = {"auth_token":"HR6747C1iUM4XSRqKSsp", + "reading": {"ph":ph, + "temperature":temp}} + send_read = json.dumps(reading_data) + url = "url of site" + sent_data = requests.post(url, send_read) + + + #for reading from DS18B20 temperature connected to GPIO4 - def read_temp_raw(): + def read_temp_raw(self): f = open(device_file, 'r') lines = f.readlines() f.close() @@ -82,7 +93,7 @@ def readPh(self): #read temperature def read_Temp(self): - lines = read_temp_raw() + lines = self.read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() @@ -159,7 +170,7 @@ def peltio_Off(self): #read temp temp_f = aqua.read_temp() - + aqua.send_readings(ph_value, temp_f) if temp_f < min_temp: heat = True #turn h bridge to heat