-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (131 loc) · 4.19 KB
/
main.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
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
# importing libraries
import uasyncio as asyncio
import utime
import machine
import _thread
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# configuring LCD settings
I2C_ADDR = 0x27
I2C_NUM_ROWS = 4
I2C_NUM_COLS = 20
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# defining gpio variables
moisture = machine.ADC(26)
button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
moisturepower = machine.Pin(15,machine.Pin.OUT)
pump = machine.Pin(13,machine.Pin.OUT)
conversion_factor = 3.3 / (65535)
#setting up UART
tx= machine.Pin(4)
rx = machine.Pin(5)
id = 1
uart= machine.UART(id=id,baudrate=9600,tx=tx,rx=rx)
#turning off the LCD
lcd.backlight_off()
lcd.display_off()
#switching off relays
pump.value(0)
# creating and opening the log file
file = open("log.csv", "w")
hourr = 216000
global loglist
loglist= ["0"]#creating log list
global percentage
def measurement():
global percentage
moisturepower.value(1)
utime.sleep(2)
soil = moisture.read_u16()
percentage = round(((soil-63837.25)*100)/(27000-63837.25),2)
moisturepower.value(0)
return percentage
#def addtolist():
# global loglist
# if len(loglist) >= 12:
# global loglist
# loglist.pop(0)
# havana = str(percentage)
# loglist.append(havana)
#else:
# global loglist
# loglist.append(havana)
async def print_zero():
while True:
await asyncio.sleep(1)
if button.value() == 1:# code for displaying realtime mositure data on LCD when button is pushed
lcd.backlight_on()
lcd.display_on()
lcd.putstr("measuring...")
percentage = measurement()
out_string = "Soil Moisture: "
lcd.clear()
lcd.putstr(out_string)
lcd.move_to(0,1)
lcd.putstr(str(percentage) +"%")
utime.sleep(5)
lcd.clear()
# code for turning off LCD display when button is released
lcd.backlight_off()
lcd.display_off()
# bluetooth code
elif uart.any():
data = uart.read()
data = str(data)
print(data)
if ('send_history' in data):#code for sending moisture levels from the past twelve hours
print("Received order for historical data")
historical_data =",".join(loglist)
uart.write(historical_data)
elif ('SendMoisture' in data):# code for sending moisture data to phone
print("received order for moisture")
percentage = measurement()
uart.write(out_string + str(percentage))
moisturepower.value(0)
elif('PumpOn' in data):# code for turning pump on
print("received pump order")
pump.value(1)
elif('PumpOff' in data):# code for turning pump off
pump.value(0)
# code for logging data every hour
async def print_one():
while True:
await asyncio.sleep(30)
moisturepower.value(1)
percentage = measurement()
out_string = "Soil Moisture: "
file.write(str(percentage) +",")
file.flush
lekod()
if percentage < 65:
while True:
pump.value(1)
percentage = measurement()
if percentage > 65:
pump.value(0)
break
moisturepower.value(0)
print("recorded")
def lekod():
global loglist
global percentage
if len(loglist) >= 12:
global percentage
global loglist
loglist.pop(0)
havana = str(percentage)
loglist.append(havana)
else:
global percentage
global loglist
havana = str(percentage)
loglist.append(havana)
print(loglist)
async def main():
await asyncio.gather(print_zero(), print_one())
try:
asyncio.run(main())
except KeyboardInterrupt:
print("bye")