onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sun Nov 25, 2018 1:52 am

paddyg wrote:
Sat Nov 24, 2018 9:02 pm
Matt Richardson did a good Fask intro here http://mattrichardson.com/Raspberry-Pi-Flask/ and I've seen similar things with code red that you will be able to find with a bit of googling. I have to warn you that if you are new to programming then the asynchronous client/server concepts will be pretty tricky. I would have thought your application would require a human close to the action (holding the glass) and didn't lend itself to a web interface, why do you want to use it?
idea using flask or webserver is that i can get a better touch screen lets say put android tablet instead of touch screen for raspberry pi ( i haven't used any raspberry pi touch screen yet so have fear touch won't be good compare to phone / tablet ) so raspberry pi will be running behind machine and server url can be opened in browser on android tablet which will be front above taps ( little fancy stuff ) :lol:
however its not important if tkinter can give nice looking GUI i think i can use it.
updating / calibrating flow meter measure pour /size
going forward i have created SQlite3 database where i want to store my different measure strings /variables such as MEASURE_SMALL = 60 so instead grabbing measure data from python file i would able to get it from SQlite db. but i could not figure out how can fill MEASURE_SMALL ="get data from SQlite"
which i am able to with python script with print

Code: Select all

import sqlite3
conn = sqlite3.connect('wine.db')
c = conn.cursor()

def get_sensor():
    c.execute("SELECT value FROM data")
    print(c.fetchall())
    
get_sensor()
conn.commit()
results

Code: Select all

>>> %Run sql.py
[(60.0,)]
tkinter will have options to update db value

User avatar
paddyg
Posts: 2588
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Sun Nov 25, 2018 7:59 am

ah, that makes sense. I do actually have a project simmering where I use a RPI as a local WiFi router running Flask, getting input and output on gpiozero, storing and reading from sqlite ... so you might be able to use a big proportion of that. I will dig it out and have a look.

One thing that's missing is the html and js front end as my router communicates with a unity3d app on the phone (it's a VR exercise bike motivation game) Also needed is some security to stop other phones nearby logging in! On my app I intend to allow different users in the gym to be able to connect to a local hotpot monitoring a group of machines.

PS but I have to say that tk touch screen will be much easier to do!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sun Nov 25, 2018 3:03 pm

if i can figure out how to call these 3 buttons from external command like python3 test.py pour_small than iit would be easy to call it with anything and python will get measure data from sqlite

User avatar
paddyg
Posts: 2588
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Sun Nov 25, 2018 3:20 pm

just use sys.argv plenty of examples how to do that.. but I'm not sure that's the best approach as tk and gpiozero buttons already working!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sun Nov 25, 2018 3:31 pm

Ok than next only would be importing measure amount from sqlite3

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Tue Nov 27, 2018 9:44 pm

Hi Paddy ,
If you get chance could you please let me know how can i get MEASURE_SMALL value from sqlite ?
MESAURE_SMALL = ( get measure value information form sqlite db )
MESAURE_MEDIUM = ( get measure value information form sqlite db )

Code: Select all

import sqlite3
conn = sqlite3.connect('wine.db')
c = conn.cursor()

def get_sensor():
    c.execute("SELECT value FROM data")
    print(c.fetchall())
    
get_sensor()
conn.commit()

User avatar
paddyg
Posts: 2588
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Tue Nov 27, 2018 11:04 pm

well it's not entirely clear how you intended to use your db, i.e. are you hoping to write other info into it as you pour drinks to use it for stock control etc. I would expect you to have a table called `measures` say with fields `name` (text) and `pulses` (real) to start with like:
i.e.

Code: Select all

c.execute('''CREATE TABLE measures
             (name text, pulses real)''')
c.execute("INSERT INTO measures (name, pulses) VALUES ('small', 80), ('medium', 130), ('large', 200)")
then your pouring code might have something like

Code: Select all

#set up sqlite connection and cursor once at the start of program
...
c.execute("SELECT pulses FROM measures WHERE name = 'small'")
MEASURE_SMALL = c.fetchone()[0] #fetchone should return a tuple with only one value in it (I think)
alternatively you could change your three constants to a dict object, something like

Code: Select all

MEASURES = {}
c.execute("SELECT name, pulses FROM measures")
for m in c.fetchall():
    MEASURES[m[0]] = m[1]
# now measures should contain {'small': 80, 'medium': 130 etc..
# so where you previously used MEASURE_SMALL, use MEASURES['small']
lots of stuff for you to google here. Examples of all this on stackoverflow
PS just noticed a typo in insert statement (text should have been name) Also note that sqlite will store the numbers as floats but you should get away with that in this instance.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Wed Nov 28, 2018 5:46 pm

Paddy,
Its working i also added like following

Code: Select all

def pour_small():
    start_pour(MEASURES['small'])
Thank You

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Fri Nov 30, 2018 3:33 am

Hi paddy,
No rush i am trying make tkinter gui to update measures table value like small , medium and large to setup new pour size (pulses) but could not understand how to do it.

Code: Select all

# submit new pulse data to sqlite database.
        c.execute("update mesausers set pulses (small, medium, large) (?, ?, ?)")
        (small.get(), medium.get(), large.get()) #Insert record into database.

Code: Select all

from tkinter import * #GUI package
import sqlite3 as sq #For tables and database
window = Tk()
window.title("Dispenser Pour Size") 
window.geometry('800x600+0+0')
header = Label(window, text="Dispenser Pour Size", font=("arial",30,"bold"), fg="steelblue").pack()

con = sq.connect('/home/pi/Desktop/wine.db') #dB browser for sqlite needed
c = con.cursor() #SQLite command, to connect to db so 'execute' method can be called

L1 = Label(window, text = "Please enter pulse  for each pour", font=("arial", 10)).place(x=10,y=100)
L2 = Label(window, text = "Small", font=("arial",18)).place(x=10,y=150)
L3 = Label(window, text = "Medium", font=("arial",18)).place(x=10,y=200)
L4 = Label(window, text = "large", font=("arial",18)).place(x=10,y=250)


#Create variables for each list
comp = StringVar(window)#For 1st dd
comp.set('----') #Inital placeholder for field

# new pulse input value
small = StringVar(window)
medium = StringVar(window)
large = StringVar(window)

#Entry for 'input' in GUI
small = Entry(window, textvariable=small)
small.place(x=220,y=155)

medium = Entry(window, textvariable=medium)
medium.place(x=220,y=205)

large = Entry(window, textvariable=large)
large.place(x=220,y=255)



#get func to isolate the text entered in the entry boxes and submit to database
def get():
        print("You have submitted a record")
       # submit new pulse data to sqlite database.
        c.execute("update mesausers set pulses (small, medium, large) (?, ?, ?)")
        (small.get(), medium.get(), large.get()) #Insert record into database.
        con.commit()

#Reset fields after submit
        comp.set('----')
        small.set('')
        medium.set('')
        large.set('')
       

button_1 = Button(window, text="Submit",command=get)
button_1.place(x=300,y=300)

window.mainloop() #mainloop() -> make sure that window stays open

User avatar
paddyg
Posts: 2588
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Fri Nov 30, 2018 8:08 am

That looks odd to me. You are trying to set fields called small, medium, large for all records in your table to the values from your input screen. Maybe you changed the scheme from the one you said was working!?

Can you post the table creation statement. Also I might be able to be more constructive if you explained what you intended to use sqlite for subsequently.

Paddy
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Fri Nov 30, 2018 1:23 pm

table is like we created.
table name is measures
it has two fields : name and pulses
in name has 3 rows : small , medium , large
than pulses has like 100 , 200 and 300
i am trying to make this gui to update those 3 pulses filed so can calibrate or increase / decrease pour amount for each size

User avatar
paddyg
Posts: 2588
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Fri Nov 30, 2018 2:10 pm

OK (thought that was probably the case, though IMHO editing a python script is just as easy as entering stuff on command line or into tk GUI)

This is what you probably need to do. NB there should be an initialization where you read the values from the db to populate the tk Entry fields and also you should do some validation before writing them. Notice I have put the names and tk fields into a dict so they can be iterated over as you update them. Also I haven't kept a handle on your Label variables which you only need if you want to subsequently access them for some reason (i.e. reporting progress or errors etc)

Code: Select all

import tkinter as tk # alias for brevity. avoid importing * at all costs!
import sqlite3

window = tk.Tk()
window.title("Dispenser Pour Size") 
window.geometry('800x600+0+0')
header = tk.Label(window, text="Dispenser Pour Size", font=("arial",30,"bold"), fg="steelblue").pack()

con = sqlite3.connect('wine.db') #dB browser for sqlite needed
c = con.cursor() #SQLite command, to connect to db so 'execute' method can be called

tk.Label(window, text = "Please enter pulse  for each pour", font=("arial", 10)
        ).place(x=10, y=100)
field_list = {}
for (name, y) in [('small', 150), ('medium', 200), ('large', 250)]:
    tk.Label(window, text = name, font=("arial",18)
            ).place(x=10, y=y)
    field_list[name] = tk.Entry(window, textvariable='')
    field_list[name].place(x=220, y=(y+5))

def write_to_db():
    for f in field_list:
        # submit new pulse data to sqlite database.
        c.execute("UPDATE OR REPLACE measures SET pulses = ? WHERE name = ?",
        (field_list[f].get(), f)) #Insert record into database if doesn't exist else update
    con.commit()

button_1 = tk.Button(window, text="Submit",command=write_to_db
                    ).place(x=300,y=300)

window.mainloop() #mainloop() -> make sure that window stays open
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Fri Nov 30, 2018 4:09 pm

paddyg wrote:
Fri Nov 30, 2018 2:10 pm
OK (thought that was probably the case, though IMHO editing a python script is just as easy as entering stuff on command line or into tk GUI)

This is what you probably need to do. NB there should be an initialization where you read the values from the db to populate the tk Entry fields and also you should do some validation before writing them. Notice I have put the names and tk fields into a dict so they can be iterated over as you update them. Also I haven't kept a handle on your Label variables which you only need if you want to subsequently access them for some reason (i.e. reporting progress or errors etc)

Code: Select all

import tkinter as tk # alias for brevity. avoid importing * at all costs!
import sqlite3

window = tk.Tk()
window.title("Dispenser Pour Size") 
window.geometry('800x600+0+0')
header = tk.Label(window, text="Dispenser Pour Size", font=("arial",30,"bold"), fg="steelblue").pack()

con = sqlite3.connect('wine.db') #dB browser for sqlite needed
c = con.cursor() #SQLite command, to connect to db so 'execute' method can be called

tk.Label(window, text = "Please enter pulse  for each pour", font=("arial", 10)
        ).place(x=10, y=100)
field_list = {}
for (name, y) in [('small', 150), ('medium', 200), ('large', 250)]:
    tk.Label(window, text = name, font=("arial",18)
            ).place(x=10, y=y)
    field_list[name] = tk.Entry(window, textvariable='')
    field_list[name].place(x=220, y=(y+5))

def write_to_db():
    for f in field_list:
        # submit new pulse data to sqlite database.
        c.execute("UPDATE OR REPLACE measures SET pulses = ? WHERE name = ?",
        (field_list[f].get(), f)) #Insert record into database if doesn't exist else update
    con.commit()

button_1 = tk.Button(window, text="Submit",command=write_to_db
                    ).place(x=300,y=300)

window.mainloop() #mainloop() -> make sure that window stays open
working great thank you again !!! where can i send you couple beers ? :D

Return to “Python”