This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest6.py
82 lines (74 loc) · 2.21 KB
/
test6.py
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
import network ##baraye vasl kardan nodemcu be wifi va dastresi be internet
import ujson ##formot ersal va daryaft request va response az server
import utime ##baray ejad delay va jologire az hang karcan
import gc ##moderiat hafeze
import urequests as requests ##baraye ertebat ba internet
from machine import Pin,ADC ##tanzem halet pin ha
adc=machine.ADC(0) ##taref pin analog
gc.enable()
led=Pin(2,Pin.OUT)
button=Pin(0,Pin.IN)
motora1=Pin(12,Pin.OUT)
motorb1=Pin(13,Pin.OUT)
led.value(1)
def connect():
ssid = 'Anonymous'
password = '6339tktz6z' ##ssid va password wifi
s = network.WLAN(network.STA_IF) ##sazekar client kardan mothole
if s.isconnected() == True:
print("Already connected")
return
s.active(True)
s.connect(ssid, password)
while s.isconnected() == False:
print("connecting to wifi")
led.value(0)
utime.sleep(1)
led.value(1)
utime.sleep(1)
pass
print("Connection successful")
url='http://192.168.43.79:8000/delta/tele/led/'
value_list=[300,500,800,1024]
##list baraye tahlil rotobat
state_list=['waterlogged','dranch','damp','dry']
##list baraye tahlil rotobat
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
counter=0
while True:
if button.value() == 0:
while True:
led.value(1)
connect()
data=adc.read()##tabdel etelat az analog be digital
for m in value_list:
if data<=m:
state=state_list[counter]
break
else:
counter+=1
dade={'moisture':data,'state':state}##dade ke ghast ersal on be server ra darem
dade_dic=ujson.dumps(dade)
##response=requests.post(url,data=dade,headers={'Connection':'close'})
send=requests.request('POST', url, data=dade_dic,headers=headers, stream=False)
##print(send.status_code)
##response=requests.get(url)
x=send.json()
x=x['state']
if x=='on':
while data>300
motora1.value(1)
motorb1.value(0)
utime.sleep(0.2)
data=adc.read()
else :
motora1.value(0)
motorb1.value(0)
utime.sleep(1)
send.close()
counter=0
gc.collect()
else:
led.value(0)
## press flash button to run
pass