-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsending_with_button.py
More file actions
150 lines (123 loc) · 5.25 KB
/
sending_with_button.py
File metadata and controls
150 lines (123 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import threading
import tkinter as tk
from tkinter import BOTTOM, TOP, LEFT, RIGHT, BOTH, X, Y, SUNKEN, W, E
import time
import smtplib
import http.client
import urllib.request as urllib2
import json
def Remergency_lock(): #place valve close mechanism
print("valve closed")
class Prime(tk.Frame):
def __init__(self,hello):
tk.Frame.__init__(self,hello)
self.level=tk.IntVar()
self.us_level=tk.IntVar()
self.i=tk.IntVar()
self.a=tk.IntVar()
self.status=tk.StringVar()
self.a_status=tk.StringVar()
tk.Label(self, text="this is the current oil level").pack()
tk.Label(self,textvariable=self.level,bg="#b8b8bd",fg="black").pack(fill=X)#level status
self.E1=tk.Entry(self,text=self.us_level)#user entry box
self.E1.pack()
self.E1.focus()
btn3=tk.Button(self, text="Enter the limit", command=self.specify_level,bg="light blue",fg="black")
## self.tk.bind('<Return>',self.specify_level)
btn3.pack(fill=X)
btn=tk.Button(self, text="press to send msg to supplier",command=self.t2,bg="violet",fg="black").pack(side=LEFT, fill=Y)
btn2=tk.Button(self, text="press for emergency lock", command=self.emergency_lock,fg="black",bg="red")
btn2.pack(fill=BOTH, expand=1)
btn2=tk.Button(self, text="press to release", command=self.release,fg="black",bg="light green")
btn2.pack(fill=BOTH, expand=1)
btn1=tk.Button(self, text="press to close application",command=hello.destroy,bg="black",fg="white")
btn1.pack(fill=X)
l1=tk.Label(hello, textvariable=self.status,bd=1,relief=SUNKEN,anchor=W)#
l1.pack(side=BOTTOM, fill=X)
l2=tk.Label(hello, textvariable=self.a_status,bd=1,relief=SUNKEN,anchor=E)#
l2.pack(side=BOTTOM, fill=X)
self.TimerInterval=5000 ##delay before printing every value
self.v_level=0 ##variable to store the oil level
self.v_i=1 ##variable for sending every 5th value
self.us_level=0 ## variable that receives user defined value
self.v_a=0
self.c=0
self.Getv_level()
def emergency_lock(self):#valve close mechanism
self.a.set(self.v_a)
self.v_a=1
self.c=1
self.status.set("closed")
print("Valve closed")
def release(self):#valve release mechanism
self.a.set(self.v_a)
self.v_a=0
self.c=0
self.status.set("released")
print("valve released")
def specify_level(self):
self.status.set("user limit set")
self.us_level=self.E1.get()
def thingspeak(self):
self.i.set(self.v_i)
self.v_i+=1
if self.v_i%10==0:
data=urllib2.urlopen("https://api.thingspeak.com/update?api_key=K13USAWQYIS3ZLPP&field1="+str(round(self.v_level,2)))
data.close()
print("data has been sent")
else:
return
def t2(self): #sending mail is an io task that can halt main execution, thread created for stability
t3=threading.Thread(target=self.send_mail)
t3.start()
def send_mail(self):
print("sending mail")
self.status.set("sending mail")
s=smtplib.SMTP('smtp.gmail.com',587)
s.starttls()
s.login("finalproject.2024@gmail.com","project@2024")
s.sendmail("finalproject.2024@gmail.com","gouravdeb@gmail.com","\n Oil level low and the current level is: - "+str(self.v_level))
s.quit()
self.status.set("mail sent")
print("sent mail")
def close_read(self):
## TS=urllib2.urlopen("https://dweet.io/get/latest/dweet/for/control")
TS=urllib2.urlopen("https://api.thingspeak.com/channels/920146/fields/2.json?results=2")
response = TS.read()
data=json.loads(response)
print(json.dumps(data,indent=4,sort_keys=True))
time.sleep(20)
b=data['channel']['feeds'][0]['field2']
b=json.dumps(b)
b=int(b)
if b==1:
self.emergency_lock()
print("msg from client")
else:
self.release()
def Getv_level(self):
self.level.set(self.v_level)
self.v_level+=1.01111
self.v_level=round(self.v_level,2)
t1=threading.Thread(target=self.thingspeak)#placing thingspeak data in thread to improve stability
t1.start()
closing_thread=threading.Thread(target=self.close_read)
closing_thread.start()
print(self.v_level)
if int(self.us_level)<=int(self.v_level) and self.us_level!=0:
Remergency_lock()
elif self.c==1:
self.emergency_lock()
else:
self.release()
print("all safe")
self.a_status.set("reading sensor data now...")
self.after(self.TimerInterval,self.Getv_level)
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("oil level checker")
self.geometry("300x250")
Prime(self).pack()
self.mainloop()
Application()